Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ from docx import Document
|
|
5 |
from datetime import datetime
|
6 |
from xml.etree import ElementTree as ET
|
7 |
|
|
|
8 |
def fetch_pubmed_articles(query, max_results, page, sort_by, journal_filter, min_year, max_year):
|
9 |
if not query or query.strip() == "":
|
10 |
return "β Please enter a search query.", "", pd.DataFrame()
|
@@ -55,6 +56,7 @@ def fetch_pubmed_articles(query, max_results, page, sort_by, journal_filter, min
|
|
55 |
df = pd.DataFrame(articles)
|
56 |
if df.empty:
|
57 |
return "β No results could be parsed.", "", pd.DataFrame()
|
|
|
58 |
if journal_filter:
|
59 |
df = df[df["Journal"].str.contains(journal_filter, case=False, na=False)]
|
60 |
if min_year and str(min_year).isdigit():
|
@@ -63,26 +65,26 @@ def fetch_pubmed_articles(query, max_results, page, sort_by, journal_filter, min
|
|
63 |
df = df[df["Year"] <= int(max_year)]
|
64 |
if df.empty:
|
65 |
return "β No results matched your filters.", "", pd.DataFrame()
|
|
|
66 |
if sort_by == "Year":
|
67 |
df = df.sort_values(by="Year", ascending=False, na_position='last')
|
68 |
elif sort_by == "Title":
|
69 |
df = df.sort_values(by="Title", ascending=True, na_position='last')
|
70 |
elif sort_by == "Journal":
|
71 |
df = df.sort_values(by="Journal", ascending=True, na_position='last')
|
72 |
-
|
|
|
73 |
for _, row in df.iterrows():
|
74 |
pmid_url = f"https://pubmed.ncbi.nlm.nih.gov/{row['PMID']}/"
|
75 |
result_text += f"#### [{row['Title']}]({pmid_url})\n"
|
76 |
result_text += f"**Journal:** {row['Journal']} \n"
|
77 |
result_text += f"**Year:** {row['Year']} \n"
|
78 |
result_text += f"**Abstract:** {row['Abstract']}\n\n---\n"
|
79 |
-
return "β
Search complete!", result_text, df
|
80 |
-
except Exception as e:
|
81 |
-
return f"β Error: {str(e)}", "", pd.DataFrame()
|
82 |
|
83 |
-
|
84 |
-
|
85 |
-
if df.empty:
|
86 |
return None
|
87 |
now = datetime.now().strftime("%Y%m%d_%H%M%S")
|
88 |
if file_type == "CSV":
|
@@ -104,40 +106,32 @@ def export_results(df_dict, file_type):
|
|
104 |
return path
|
105 |
return None
|
106 |
|
|
|
107 |
with gr.Blocks(theme="soft") as app:
|
108 |
gr.Markdown("""
|
109 |
-
# π¬
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
---
|
116 |
-
|
117 |
-
### π How to use:
|
118 |
-
1. Enter your search keywords (e.g., `brain inflammation`).
|
119 |
-
2. Adjust filters (journal name, min/max year, etc.).
|
120 |
-
3. Click **π Search PubMed** to view articles.
|
121 |
-
4. Use buttons to **download results**.
|
122 |
-
|
123 |
-
---
|
124 |
""")
|
125 |
|
126 |
with gr.Row():
|
127 |
with gr.Column():
|
128 |
query_input = gr.Textbox(label="Search Query", placeholder="e.g., brain inflammation", lines=2)
|
129 |
-
max_results_input = gr.Number(label="Max Results", value=20, minimum=1, maximum=100)
|
130 |
page_input = gr.Number(label="Page Number", value=1, minimum=1)
|
131 |
sort_input = gr.Dropdown(["Year", "Title", "Journal"], value="Year", label="Sort By")
|
132 |
journal_filter_input = gr.Textbox(label="Journal Filter (optional)")
|
133 |
min_year_input = gr.Number(label="Min Year", value=2000, minimum=1800, maximum=2100)
|
134 |
max_year_input = gr.Number(label="Max Year", value=2025, minimum=1800, maximum=2100)
|
135 |
search_button = gr.Button("π Search PubMed")
|
136 |
-
status_output = gr.Markdown()
|
137 |
with gr.Column():
|
138 |
-
markdown_output = gr.Markdown("
|
139 |
-
table_output = gr.DataFrame(label="Results Table", interactive=False)
|
140 |
-
|
141 |
|
142 |
with gr.Row():
|
143 |
export_csv_button = gr.Button("β¬οΈ Export CSV")
|
@@ -145,19 +139,21 @@ with gr.Blocks(theme="soft") as app:
|
|
145 |
export_csv_output = gr.File(label="Download CSV")
|
146 |
export_docx_output = gr.File(label="Download DOCX")
|
147 |
|
|
|
148 |
def run_search(query, max_results, page, sort_by, journal_filter, min_year, max_year):
|
149 |
status, md, df = fetch_pubmed_articles(query, max_results, page, sort_by, journal_filter, min_year, max_year)
|
150 |
-
return status, md, df, df
|
151 |
|
152 |
search_button.click(
|
153 |
fn=run_search,
|
154 |
inputs=[query_input, max_results_input, page_input, sort_input, journal_filter_input, min_year_input, max_year_input],
|
155 |
-
outputs=[status_output, markdown_output, table_output,
|
156 |
)
|
157 |
|
158 |
-
export_csv_button.click(
|
159 |
-
export_docx_button.click(
|
160 |
|
161 |
app.launch()
|
162 |
|
163 |
|
|
|
|
5 |
from datetime import datetime
|
6 |
from xml.etree import ElementTree as ET
|
7 |
|
8 |
+
# Fetch articles from PubMed
|
9 |
def fetch_pubmed_articles(query, max_results, page, sort_by, journal_filter, min_year, max_year):
|
10 |
if not query or query.strip() == "":
|
11 |
return "β Please enter a search query.", "", pd.DataFrame()
|
|
|
56 |
df = pd.DataFrame(articles)
|
57 |
if df.empty:
|
58 |
return "β No results could be parsed.", "", pd.DataFrame()
|
59 |
+
# Filter
|
60 |
if journal_filter:
|
61 |
df = df[df["Journal"].str.contains(journal_filter, case=False, na=False)]
|
62 |
if min_year and str(min_year).isdigit():
|
|
|
65 |
df = df[df["Year"] <= int(max_year)]
|
66 |
if df.empty:
|
67 |
return "β No results matched your filters.", "", pd.DataFrame()
|
68 |
+
# Sort
|
69 |
if sort_by == "Year":
|
70 |
df = df.sort_values(by="Year", ascending=False, na_position='last')
|
71 |
elif sort_by == "Title":
|
72 |
df = df.sort_values(by="Title", ascending=True, na_position='last')
|
73 |
elif sort_by == "Journal":
|
74 |
df = df.sort_values(by="Journal", ascending=True, na_position='last')
|
75 |
+
# Markdown rendering
|
76 |
+
result_text = f"**Showing {len(df)} results:**\n\n"
|
77 |
for _, row in df.iterrows():
|
78 |
pmid_url = f"https://pubmed.ncbi.nlm.nih.gov/{row['PMID']}/"
|
79 |
result_text += f"#### [{row['Title']}]({pmid_url})\n"
|
80 |
result_text += f"**Journal:** {row['Journal']} \n"
|
81 |
result_text += f"**Year:** {row['Year']} \n"
|
82 |
result_text += f"**Abstract:** {row['Abstract']}\n\n---\n"
|
83 |
+
return "β
Search complete!", result_text, df
|
|
|
|
|
84 |
|
85 |
+
# Export results
|
86 |
+
def export_results(df, file_type):
|
87 |
+
if df is None or df.empty:
|
88 |
return None
|
89 |
now = datetime.now().strftime("%Y%m%d_%H%M%S")
|
90 |
if file_type == "CSV":
|
|
|
106 |
return path
|
107 |
return None
|
108 |
|
109 |
+
# Gradio Interface
|
110 |
with gr.Blocks(theme="soft") as app:
|
111 |
gr.Markdown("""
|
112 |
+
# π¬ PubMed Article Search Tool
|
113 |
+
**Instructions:**
|
114 |
+
- Enter a biomedical search term (e.g., `autism risk factors`)
|
115 |
+
- Adjust optional filters below
|
116 |
+
- Click **Search PubMed** to retrieve articles
|
117 |
+
- Use export buttons to download results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
""")
|
119 |
|
120 |
with gr.Row():
|
121 |
with gr.Column():
|
122 |
query_input = gr.Textbox(label="Search Query", placeholder="e.g., brain inflammation", lines=2)
|
123 |
+
max_results_input = gr.Number(label="Max Results (1β100)", value=20, minimum=1, maximum=100)
|
124 |
page_input = gr.Number(label="Page Number", value=1, minimum=1)
|
125 |
sort_input = gr.Dropdown(["Year", "Title", "Journal"], value="Year", label="Sort By")
|
126 |
journal_filter_input = gr.Textbox(label="Journal Filter (optional)")
|
127 |
min_year_input = gr.Number(label="Min Year", value=2000, minimum=1800, maximum=2100)
|
128 |
max_year_input = gr.Number(label="Max Year", value=2025, minimum=1800, maximum=2100)
|
129 |
search_button = gr.Button("π Search PubMed")
|
130 |
+
status_output = gr.Markdown(value="")
|
131 |
with gr.Column():
|
132 |
+
markdown_output = gr.Markdown(value="Results will appear here.")
|
133 |
+
table_output = gr.DataFrame(label="Results Table", visible=True, interactive=False)
|
134 |
+
export_df = gr.Dataframe(visible=False) # hidden for internal export
|
135 |
|
136 |
with gr.Row():
|
137 |
export_csv_button = gr.Button("β¬οΈ Export CSV")
|
|
|
139 |
export_csv_output = gr.File(label="Download CSV")
|
140 |
export_docx_output = gr.File(label="Download DOCX")
|
141 |
|
142 |
+
# Logic
|
143 |
def run_search(query, max_results, page, sort_by, journal_filter, min_year, max_year):
|
144 |
status, md, df = fetch_pubmed_articles(query, max_results, page, sort_by, journal_filter, min_year, max_year)
|
145 |
+
return status, md, df, df
|
146 |
|
147 |
search_button.click(
|
148 |
fn=run_search,
|
149 |
inputs=[query_input, max_results_input, page_input, sort_input, journal_filter_input, min_year_input, max_year_input],
|
150 |
+
outputs=[status_output, markdown_output, table_output, export_df]
|
151 |
)
|
152 |
|
153 |
+
export_csv_button.click(lambda df: export_results(df, "CSV"), inputs=[export_df], outputs=[export_csv_output])
|
154 |
+
export_docx_button.click(lambda df: export_results(df, "DOCX"), inputs=[export_df], outputs=[export_docx_output])
|
155 |
|
156 |
app.launch()
|
157 |
|
158 |
|
159 |
+
|