File size: 3,250 Bytes
d62ad66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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


@st.cache_resource
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)

@st.cache_data
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('|', ' | '))