Download pages/03_Explore_Documents.py from Array3022/PubMedTM: direct link, hf CLI and curl.
- Browser
- Download file 3.25 kB
-
https://huggingface.co/spaces/Array3022/PubMedTM/resolve/main/pages/03_Explore_Documents.py
- Command line
-
hf download hf://spaces/Array3022/PubMedTM/pages/03_Explore_Documents.py
-
curl -L -o 03_Explore_Documents.py https://huggingface.co/spaces/Array3022/PubMedTM/resolve/main/pages/03_Explore_Documents.py
3.25 kB
| import streamlit as st | |
| import pandas as pd | |
| from top2vec import Top2Vec | |
| # def append_dict(data): | |
| # # Create df with author info | |
| # author_df = pd.DataFrame(columns=['Name', 'Affiliation']) | |
| # for i, item in enumerate(data): | |
| # author_df.at[i, 'Name'] = item['name'] | |
| # if item['affiliation'] is not None: | |
| # author_df.at[i, 'Affiliation'] = item['affiliation'] | |
| # else: | |
| # author_df.at[i, 'Affiliation'] = '' | |
| # # Find longest affiliation string | |
| # max_affiliation = 0 | |
| # for item in author_df['Affiliation']: | |
| # if len(item) > max_affiliation: | |
| # max_affiliation = len(item) | |
| # # Add space to affiliation strings to match longest string | |
| # for i, item in enumerate(author_df['Affiliation']): | |
| # author_df.at[i, 'Affiliation'] = item + ' ' * (max_affiliation - len(item)) | |
| # # Change index to start at 1 | |
| # author_df.index = author_df.index + 1 | |
| # return author_df | |
| def load_model(): | |
| name = '2024-02-14_22_19_09_reduced__model_medline0[test,mincount_250,topicmergedelta_0.15,ngram_True,ngramvocab_args_250_.4_80M_npmi_connector].json' | |
| with open(name, 'rb') as f: | |
| return Top2Vec.load(f) | |
| def load_data(): | |
| df = pd.read_pickle('medline_0_df_nlp.pkl.bz2', compression='bz2') | |
| return df | |
| # Notification of loading | |
| start_container = st.empty() | |
| if 'first' not in st.session_state: | |
| st.session_state['first'] = True | |
| if st.session_state['first'] == True: | |
| start_container.text('Loading model and data... This may take two to three minutes on the initial run.') | |
| st.session_state['first'] = False | |
| # Load the model and data | |
| model = load_model() | |
| df = load_data() | |
| # Remove loading notification | |
| start_container.empty() | |
| # User interface | |
| with st.sidebar.form('Explore Documents', clear_on_submit=False): | |
| st.write('Input a search term(s) separated by a space and receive the documents that are most semantical similar.') | |
| txt_ed = st.text_area('Keyword(s) or Phrase(s)') | |
| num_ed = st.number_input('Number of documents to return', | |
| min_value=1, | |
| max_value=50, | |
| value=10) # Default value is 10 | |
| submitted_ed = st.form_submit_button("Submit") | |
| if submitted_ed: | |
| txt_ed_list = txt_ed.split() | |
| doc_scores, doc_ids = model.search_documents_by_keywords(txt_ed_list, num_docs=num_ed) | |
| for i, doc in enumerate(doc_ids): | |
| st.header(f'{df.at[doc, "TI"]}') | |
| with st.expander('See more'): | |
| # st.write('Authors:', f'{df.at[doc, "AU"]}') | |
| st.write('Authors:', ','.join(df.at[doc, "AU"]).replace(',', ', ')) | |
| # st.dataframe(append_dict(df.iat[doc, 5])) | |
| st.write('Year:', f'{df.at[doc, "DP"]}') | |
| st.markdown(f'PMUID: [{df.at[doc, "PMID"]}](https://pubmed.ncbi.nlm.nih.gov/{df.at[doc, "PMID"]})') | |
| st.write('Journal:', f'*{df.at[doc, "JT"]}*') | |
| st.write('Journal Location:', f'{df.at[doc, "PL"]}') | |
| st.write('Cosine Similarity:', str(doc_scores[i])) | |
| st.write('Abstract:', f'{df.at[doc,"AB"]}') | |
| st.write('Keywords:', '|'.join(df.at[doc, "MH"]).replace('|', ' | ')) |