Skip to content

Commit

Permalink
update session_state
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasrenault committed Jul 3, 2024
1 parent 4b62b00 commit 75d39ee
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions cprex/ui/streamlit.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@
run_pipeline,
)

# Define session state and callbacks for file submission
if "url_submited" not in st.session_state:
st.session_state.url_submited = False
if "file_uploaded" not in st.session_state:
st.session_state.file_uploaded = False


def on_submit_local_file():
st.session_state.file_uploaded = True
st.session_state.url_submited = False
st.session_state.selected_entity = None


def on_submit_remote_file():
st.session_state.url_submited = True
st.session_state.file_uploaded = False
st.session_state.selected_entity = None


# Set page title
st.set_page_config(
page_title="CPREx - Chemical Properties Relation Extraction",
Expand Down Expand Up @@ -87,24 +106,15 @@


uploaded_file = st.file_uploader(
"Choose a file to upload",
type="pdf",
"Choose a file to upload", type="pdf", on_change=on_submit_local_file
)

pdf_url = st.text_input(
"Or enter the URL of a PDF file and press Submit",
"https://www.ncbi.nlm.nih.gov/pmc/articles/PMC10343900/pdf/molecules-28-05029.pdf",
)

if "submit" not in st.session_state:
st.session_state.submit = False


def submit_remote_file():
st.session_state.submit = True


st.button("Submit !", on_click=submit_remote_file)
st.button("Submit !", on_click=on_submit_remote_file)

###############################
### Make sure models are downloaded and grobid is running.
Expand Down Expand Up @@ -144,6 +154,10 @@ def display_filters(
chems, props, qtys = count_entities(docs)
with st.sidebar:
st.markdown("**Entities**")
st.caption(
"Linked entities are displayed in blue. "
"Click on one to display information extracted from PubChem."
)
display_entity_values(chems, "pink", "Chemicals", properties=properties)
if (
"selected_entity" in st.session_state
Expand Down Expand Up @@ -178,9 +192,9 @@ def parse_pdf_and_display_results(pdf: bytes, segment_sentences: bool = False):
raise e


if uploaded_file is not None:
if st.session_state.file_uploaded and uploaded_file is not None:
parse_pdf_and_display_results(uploaded_file.getvalue())
elif st.session_state.submit and pdf_url:
elif st.session_state.url_submited and pdf_url:
parse_pdf_and_display_results(get_pdf_content_from_url(pdf_url))
else:
st.stop()

0 comments on commit 75d39ee

Please sign in to comment.