-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.py
More file actions
314 lines (258 loc) · 11.3 KB
/
Copy pathapp.py
File metadata and controls
314 lines (258 loc) · 11.3 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import streamlit as st
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from datetime import datetime, timedelta
from dotenv import load_dotenv
import os
from typing import List, Dict
from src.clients.pubmed_client import PubMedClient
from src.clients.biorxiv_client import BioRxivClient
from src.embedder import PaperEmbedder
from src.storage import PaperStorage
load_dotenv()
st.set_page_config(
page_title="Literature Intelligence Tool",
page_icon="\U0001f52c",
layout="wide"
)
@st.cache_resource
def initialize_components():
"""Initialize clients, embedder, and storage."""
email = os.getenv("PUBMED_EMAIL", "user@example.com")
api_key = os.getenv("PUBMED_API_KEY")
persist_dir = os.getenv("CHROMA_PERSIST_DIR", "./chroma_db")
model_name = os.getenv("EMBEDDING_MODEL", "pritamdeka/BioBERT-mnli-snli-scinli-scitail-mednli-stsb")
verify_ssl = os.getenv("VERIFY_SSL", "false").lower() == "true"
pubmed_client = PubMedClient(email=email, api_key=api_key, verify_ssl=verify_ssl)
biorxiv_client = BioRxivClient()
embedder = PaperEmbedder(model_name=model_name)
storage = PaperStorage(persist_directory=persist_dir)
return pubmed_client, biorxiv_client, embedder, storage
def fetch_and_index_papers(
query: str,
sources: List[str],
max_results: int,
days_back: int,
pubmed_client: PubMedClient,
biorxiv_client: BioRxivClient,
embedder: PaperEmbedder,
storage: PaperStorage
) -> Dict[str, int]:
"""Fetch papers from selected sources and index them."""
all_papers = []
if "PubMed" in sources:
with st.spinner("Fetching from PubMed..."):
pubmed_papers = pubmed_client.search(
query=query,
max_results=max_results,
days_back=days_back
)
all_papers.extend(pubmed_papers)
if "bioRxiv" in sources:
with st.spinner("Fetching from bioRxiv..."):
biorxiv_papers = biorxiv_client.search(
query=query,
max_results=max_results,
days_back=days_back
)
all_papers.extend(biorxiv_papers)
if all_papers:
with st.spinner(f"Generating embeddings for {len(all_papers)} papers..."):
abstracts = [f"{p.get('title', '')}. {p.get('abstract', '')}" for p in all_papers]
embeddings = embedder.embed_batch(abstracts)
with st.spinner("Indexing papers..."):
added_count = storage.add_papers(all_papers, embeddings)
return {
'total_fetched': len(all_papers),
'newly_added': added_count,
'already_existed': len(all_papers) - added_count
}
return {'total_fetched': 0, 'newly_added': 0, 'already_existed': 0}
def search_papers(
query: str,
n_results: int,
source_filter: str,
embedder: PaperEmbedder,
storage: PaperStorage
) -> List[Dict]:
"""Semantic search for papers."""
query_embedding = embedder.embed_text(query)
source = None if source_filter == "All" else source_filter.lower()
results = storage.search_papers(
query_embedding=query_embedding,
n_results=n_results,
source_filter=source
)
return results
def main():
st.title("\U0001f52c Literature Intelligence Tool")
st.markdown("Track, index, and analyze scientific papers from PubMed and bioRxiv")
pubmed_client, biorxiv_client, embedder, storage = initialize_components()
stats = storage.get_database_stats()
st.sidebar.header("Database Statistics")
st.sidebar.metric("Total Papers", stats['total_papers'])
if stats['by_source']:
st.sidebar.markdown("**By Source:**")
for source, count in stats['by_source'].items():
st.sidebar.text(f"{source}: {count}")
tab1, tab2, tab3 = st.tabs(["Fetch & Index", "Semantic Search", "Trends Analysis"])
with tab1:
st.header("Fetch and Index Papers")
col1, col2 = st.columns(2)
with col1:
query = st.text_input(
"Search Query",
placeholder="e.g., EGFR inhibitor, CAR-T cell therapy, Alzheimer's disease",
help="Enter gene names, drug names, diseases, or any search terms"
)
sources = st.multiselect(
"Data Sources",
options=["PubMed", "bioRxiv"],
default=["PubMed"]
)
with col2:
max_results = st.slider(
"Maximum Results per Source",
min_value=10,
max_value=500,
value=100,
step=10
)
days_back = st.slider(
"Days to Look Back",
min_value=7,
max_value=365,
value=90,
step=7
)
if st.button("Fetch and Index Papers", type="primary"):
if not query:
st.error("Please enter a search query")
elif not sources:
st.error("Please select at least one data source")
else:
result = fetch_and_index_papers(
query=query,
sources=sources,
max_results=max_results,
days_back=days_back,
pubmed_client=pubmed_client,
biorxiv_client=biorxiv_client,
embedder=embedder,
storage=storage
)
st.success(f"Fetching complete!")
st.info(f"Total fetched: {result['total_fetched']} | Newly added: {result['newly_added']} | Already existed: {result['already_existed']}")
st.rerun()
with tab2:
st.header("Semantic Search")
search_query = st.text_input(
"Search Query",
placeholder="e.g., novel therapeutic approaches for cancer",
help="Describe what you're looking for in natural language"
)
col1, col2 = st.columns(2)
with col1:
n_results = st.slider("Number of Results", min_value=5, max_value=50, value=10)
with col2:
source_filter = st.selectbox("Filter by Source", options=["All", "PubMed", "bioRxiv"])
if st.button("Search", type="primary"):
if not search_query:
st.error("Please enter a search query")
else:
with st.spinner("Searching..."):
results = search_papers(
query=search_query,
n_results=n_results,
source_filter=source_filter,
embedder=embedder,
storage=storage
)
if results:
st.success(f"Found {len(results)} relevant papers")
for i, paper in enumerate(results):
with st.expander(
f"{i+1}. [{paper['source'].upper()}] {paper['title']} (Similarity: {paper['similarity_score']:.3f})",
expanded=(i == 0)
):
st.markdown(f"**Paper ID:** {paper['paper_id']}")
st.markdown(f"**Publication Date:** {paper['publication_date']}")
st.markdown(f"**Authors:** {', '.join(paper['authors'][:5])}" + (
f" + {len(paper['authors']) - 5} more" if len(paper['authors']) > 5 else ""
))
st.markdown(f"**Abstract:**\n{paper['abstract']}")
if paper['keywords']:
st.markdown(f"**Keywords:** {', '.join(paper['keywords'][:10])}")
else:
st.warning("No papers found. Try fetching and indexing papers first.")
with tab3:
st.header("Trends Analysis")
if stats['total_papers'] > 0:
start_date = st.date_input(
"Start Date",
value=datetime.now() - timedelta(days=90)
)
end_date = st.date_input(
"End Date",
value=datetime.now()
)
if st.button("Analyze Trends", type="primary"):
with st.spinner("Analyzing trends..."):
papers = storage.get_papers_by_date_range(
start_date=start_date.strftime("%Y-%m-%d"),
end_date=end_date.strftime("%Y-%m-%d")
)
if papers:
df = pd.DataFrame(papers)
df['publication_date'] = pd.to_datetime(df['publication_date'])
st.subheader("Publications Over Time")
daily_counts = df.groupby(df['publication_date'].dt.date).size().reset_index()
daily_counts.columns = ['Date', 'Count']
fig = px.line(
daily_counts,
x='Date',
y='Count',
title='Publications per Day'
)
st.plotly_chart(fig, use_container_width=True)
col1, col2 = st.columns(2)
with col1:
st.subheader("By Source")
source_counts = df['source'].value_counts()
fig = px.pie(
values=source_counts.values,
names=source_counts.index,
title='Distribution by Source'
)
st.plotly_chart(fig, use_container_width=True)
with col2:
st.subheader("By Query Tag")
tag_counts = df['query_tag'].value_counts().head(10)
fig = px.bar(
x=tag_counts.index,
y=tag_counts.values,
title='Top 10 Query Tags',
labels={'x': 'Query Tag', 'y': 'Count'}
)
st.plotly_chart(fig, use_container_width=True)
st.subheader("Top Keywords")
all_keywords = []
for keywords in df['keywords']:
all_keywords.extend(keywords)
if all_keywords:
keyword_counts = pd.Series(all_keywords).value_counts().head(20)
fig = px.bar(
x=keyword_counts.values,
y=keyword_counts.index,
orientation='h',
title='Top 20 Keywords',
labels={'x': 'Count', 'y': 'Keyword'}
)
st.plotly_chart(fig, use_container_width=True)
else:
st.warning("No papers found in the selected date range")
else:
st.info("No papers in database yet. Use the 'Fetch & Index' tab to add papers.")
if __name__ == "__main__":
main()