Download pages/02_Explore_Topics.py from Array3022/PubMedTM: direct link, hf CLI and curl.
- Browser
- Download file 2.59 kB
-
https://huggingface.co/spaces/Array3022/PubMedTM/resolve/main/pages/02_Explore_Topics.py
- Command line
-
hf download hf://spaces/Array3022/PubMedTM/pages/02_Explore_Topics.py
-
curl -L -o 02_Explore_Topics.py https://huggingface.co/spaces/Array3022/PubMedTM/resolve/main/pages/02_Explore_Topics.py
2.59 kB
| import streamlit as st | |
| import pandas as pd | |
| from top2vec import Top2Vec | |
| from wordcloud import WordCloud | |
| 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 Topics', clear_on_submit=False): | |
| st.write('Input a search term(s) separated by a space and receive the topics that are most semantically similar.') | |
| txt_et = st.text_area('Keyword(s) or Phrase(s)') | |
| num_et = st.number_input('Number of topics to return', | |
| min_value=1, | |
| max_value=model.get_num_topics(reduced=True), # Number of topics in the reduced model | |
| value=10) # Default value is 10 | |
| submitted_et = st.form_submit_button("Submit") | |
| if submitted_et: | |
| txt_et_list = txt_et.split() | |
| topic_words, word_scores, topic_scores, topic_nums = model.search_topics(txt_et_list, num_topics=num_et, reduced=True) | |
| for i, topic in enumerate(topic_nums): | |
| st.subheader('Topic Number: ' + str(topic)) | |
| st.write('Search Cosine Similarity: ' + str(topic_scores[i])) | |
| st.write('Topic Words:', ', '.join(topic_words[i][:10])) | |
| # Top documents for each topic | |
| with st.expander('Top Documents in Topic'): | |
| _, document_ids = model.search_documents_by_topic(topic_num=topic, reduced=True, num_docs=10) | |
| for id in document_ids: | |
| st.markdown(f'[{df.at[id, "TI"]}](https://pubmed.ncbi.nlm.nih.gov/{df.at[id, "PMID"]})') | |
| # Wordcloud | |
| new_dict = {} | |
| for ii, words in enumerate(topic_words[i]): | |
| new_dict[words] = word_scores[i][ii] | |
| st.image(WordCloud(width=850, height=250, | |
| background_color='white', | |
| prefer_horizontal=0.7).generate_from_frequencies(new_dict).to_image()) | |