Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,166 +1,169 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import requests
|
3 |
-
import pandas as pd
|
4 |
-
import io
|
5 |
-
from docx import Document
|
6 |
-
import tempfile
|
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 |
-
f"
|
60 |
-
f"
|
61 |
-
f"
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
return
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
"""
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
doc.
|
93 |
-
doc.add_paragraph(f"
|
94 |
-
doc.add_paragraph("
|
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 |
-
with gr.Row():
|
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 |
-
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import pandas as pd
|
4 |
+
import io
|
5 |
+
from docx import Document
|
6 |
+
import tempfile
|
7 |
+
import os
|
8 |
+
os.system("pip install python-docx")
|
9 |
+
|
10 |
+
|
11 |
+
API_BASE_URL = "https://pubmed-api-jwfq.onrender.com/search_pubmed"
|
12 |
+
|
13 |
+
global_df = None # Global variable to store search results for export
|
14 |
+
|
15 |
+
def fetch_pubmed_articles(query, max_results=10, page=1, sort_by="Year", filter_journal="All", min_year=None, max_year=None):
|
16 |
+
"""
|
17 |
+
Fetches PubMed articles and applies sorting and filtering.
|
18 |
+
"""
|
19 |
+
try:
|
20 |
+
url = f"{API_BASE_URL}?query={query}&max_results={max_results}&page={page}"
|
21 |
+
response = requests.get(url)
|
22 |
+
|
23 |
+
if response.status_code != 200:
|
24 |
+
return f"β οΈ API Error: {response.status_code} - {response.text}", None
|
25 |
+
|
26 |
+
articles = response.json()
|
27 |
+
|
28 |
+
if not articles:
|
29 |
+
return "No articles found for this query.", None
|
30 |
+
|
31 |
+
for article in articles:
|
32 |
+
try:
|
33 |
+
article["Year"] = int(article["Year"])
|
34 |
+
except:
|
35 |
+
article["Year"] = 0
|
36 |
+
|
37 |
+
# Apply journal filtering
|
38 |
+
if filter_journal and filter_journal != "All":
|
39 |
+
articles = [a for a in articles if filter_journal.lower() in a['Journal'].lower()]
|
40 |
+
|
41 |
+
# Apply year filtering
|
42 |
+
if min_year:
|
43 |
+
articles = [a for a in articles if a["Year"] >= int(min_year)]
|
44 |
+
if max_year:
|
45 |
+
articles = [a for a in articles if a["Year"] <= int(max_year)]
|
46 |
+
|
47 |
+
# Apply sorting
|
48 |
+
if sort_by == "Year":
|
49 |
+
articles.sort(key=lambda x: x["Year"], reverse=True)
|
50 |
+
elif sort_by == "Title":
|
51 |
+
articles.sort(key=lambda x: x["Title"])
|
52 |
+
elif sort_by == "Journal":
|
53 |
+
articles.sort(key=lambda x: x["Journal"])
|
54 |
+
|
55 |
+
# Format results
|
56 |
+
formatted_results = []
|
57 |
+
for article in articles:
|
58 |
+
formatted_results.append(
|
59 |
+
f"## π° {article['Title']}\n"
|
60 |
+
f"π **<span style='color:blue'>{article['Journal']}</span>** ({article['Year']})\n"
|
61 |
+
f"π¨βπ¬ **<span style='color:gray'>{article['Authors']}</span>**\n"
|
62 |
+
f"π [Read on PubMed]({article['PubMed_URL']})\n\n"
|
63 |
+
f"<details><summary>π **Show Abstract**</summary>\n{article['Abstract']}\n</details>"
|
64 |
+
f"\n---\n"
|
65 |
+
)
|
66 |
+
|
67 |
+
df = pd.DataFrame(articles)
|
68 |
+
return "\n\n".join(formatted_results), df
|
69 |
+
|
70 |
+
except Exception as e:
|
71 |
+
return f"β οΈ Error fetching data: {str(e)}", None
|
72 |
+
|
73 |
+
|
74 |
+
|
75 |
+
def export_results(df, format_type):
|
76 |
+
"""
|
77 |
+
Exports search results as a CSV or DOCX file.
|
78 |
+
- Returns the file path instead of BytesIO to avoid TypeError in Gradio.
|
79 |
+
"""
|
80 |
+
if df is None or df.empty:
|
81 |
+
return None
|
82 |
+
|
83 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=f".{format_type.lower()}")
|
84 |
+
temp_file_path = temp_file.name # Store the temporary file path
|
85 |
+
|
86 |
+
if format_type == "CSV":
|
87 |
+
df.to_csv(temp_file_path, index=False)
|
88 |
+
elif format_type == "DOCX":
|
89 |
+
doc = Document()
|
90 |
+
doc.add_heading("PubMed Search Results", level=1)
|
91 |
+
for _, row in df.iterrows():
|
92 |
+
doc.add_heading(row["Title"], level=2)
|
93 |
+
doc.add_paragraph(f"π Journal: {row['Journal']} ({row['Year']})")
|
94 |
+
doc.add_paragraph(f"π¨βπ¬ Authors: {row['Authors']}")
|
95 |
+
doc.add_paragraph(f"π Link: {row['PubMed_URL']}")
|
96 |
+
doc.add_paragraph(f"π Abstract: {row['Abstract']}")
|
97 |
+
doc.add_paragraph("---")
|
98 |
+
doc.save(temp_file_path)
|
99 |
+
|
100 |
+
temp_file.close() # Close the file before returning the path
|
101 |
+
return temp_file_path # Return file path instead of BytesIO
|
102 |
+
|
103 |
+
|
104 |
+
with gr.Blocks() as app:
|
105 |
+
gr.Markdown("""
|
106 |
+
# π **PubMed Search Tool with Advanced Features**
|
107 |
+
|
108 |
+
## π **How to Use This App**
|
109 |
+
1οΈβ£ **Enter a Search Query** *(e.g., "Deep Learning in Psychiatry")*
|
110 |
+
2οΈβ£ **Set the Number of Results & Page Number** *(Default: 10 results per page)*
|
111 |
+
3οΈβ£ **Choose Sorting Option** *(Year, Title, or Journal - Default: Year)*
|
112 |
+
4οΈβ£ **(Optional) Filter by Journal Name** *(e.g., "Nature", "JAMA")*
|
113 |
+
5οΈβ£ **(Optional) Filter by Year Range** *(Set min & max year, e.g., 2015 - 2023)*
|
114 |
+
6οΈβ£ **Click "π Search" to fetch results**
|
115 |
+
7οΈβ£ **Click "π Export as CSV" or "π Export as Word DOCX" to save articles**
|
116 |
+
8οΈβ£ **Click "π Show Abstract" under each result to expand full abstract**
|
117 |
+
|
118 |
+
## β οΈ **Important Notes**
|
119 |
+
- **Sorting & Filtering can be combined** *(e.g., show only "Nature" articles from 2020-2024, sorted by Title)*
|
120 |
+
|
121 |
+
""")
|
122 |
+
|
123 |
+
with gr.Row():
|
124 |
+
query_input = gr.Textbox(label="π Search Query", placeholder="Enter topic (e.g., 'Neural Networks in Psychiatry')", lines=1)
|
125 |
+
|
126 |
+
with gr.Row():
|
127 |
+
max_results_input = gr.Slider(1, 50, value=10, step=1, label="π Number of Results per Page")
|
128 |
+
page_input = gr.Slider(1, 200, value=1, step=1, label="π Page Number")
|
129 |
+
|
130 |
+
with gr.Row():
|
131 |
+
sort_input = gr.Dropdown(choices=["Year", "Title", "Journal"], value="Year", label="π Sort By")
|
132 |
+
journal_filter_input = gr.Textbox(label="π― Filter by Journal (Optional)", placeholder="Enter journal name or leave blank")
|
133 |
+
|
134 |
+
with gr.Row():
|
135 |
+
min_year_input = gr.Number(label="π
Min Year", value=None)
|
136 |
+
max_year_input = gr.Number(label="π
Max Year", value=None)
|
137 |
+
|
138 |
+
with gr.Row():
|
139 |
+
search_button = gr.Button("π Search")
|
140 |
+
export_csv_button = gr.Button("π Export as CSV")
|
141 |
+
export_docx_button = gr.Button("π Export as Word DOCX")
|
142 |
+
|
143 |
+
results_output = gr.HTML()
|
144 |
+
export_csv_output = gr.File(label="Download CSV")
|
145 |
+
export_docx_output = gr.File(label="Download Word DOCX")
|
146 |
+
|
147 |
+
def search_and_display(query, max_results, page, sort_by, journal_filter, min_year, max_year):
|
148 |
+
global global_df
|
149 |
+
result_text, df = fetch_pubmed_articles(query, max_results, page, sort_by, journal_filter, min_year, max_year)
|
150 |
+
global_df = df
|
151 |
+
return result_text
|
152 |
+
|
153 |
+
def export_csv():
|
154 |
+
if global_df is not None:
|
155 |
+
return export_results(global_df, "CSV")
|
156 |
+
|
157 |
+
def export_docx():
|
158 |
+
if global_df is not None:
|
159 |
+
return export_results(global_df, "DOCX")
|
160 |
+
|
161 |
+
search_button.click(search_and_display,
|
162 |
+
inputs=[query_input, max_results_input, page_input, sort_input, journal_filter_input, min_year_input, max_year_input],
|
163 |
+
outputs=results_output)
|
164 |
+
|
165 |
+
export_csv_button.click(export_csv, outputs=export_csv_output)
|
166 |
+
export_docx_button.click(export_docx, outputs=export_docx_output)
|
167 |
+
|
168 |
+
if __name__ == "__main__":
|
169 |
+
app.launch(inbrowser=True)
|