Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,73 +1,34 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
import gradio as gr
|
4 |
-
from pymed import PubMed
|
5 |
-
import urllib.parse
|
6 |
-
import urllib.request
|
7 |
-
import ipywidgets as widgets
|
8 |
-
|
9 |
-
def search_pubmed(search_term, keywords, max_results, tool, email):
|
10 |
-
# Validate the input
|
11 |
-
if max_results is None or max_results < 1:
|
12 |
-
raise ValueError("Max Results must be a positive integer")
|
13 |
-
|
14 |
-
# Connect to PubMed database
|
15 |
-
pubmed = PubMed(tool=tool, email=email)
|
16 |
results = pubmed.query(search_term, max_results=max_results)
|
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 |
-
def download_csv(b):
|
50 |
-
download_button.description = "Downloading..."
|
51 |
-
download_button.disabled = True
|
52 |
-
input_dict = interface.process(raw=False)
|
53 |
-
search_term = input_dict["Search Term"]
|
54 |
-
keywords = input_dict["Keywords"]
|
55 |
-
max_results = input_dict["Max Results"]
|
56 |
-
dataframe = search_pubmed(search_term, keywords, max_results)
|
57 |
-
dataframe.to_csv("pubmed_results.csv", index=False)
|
58 |
-
download_button.description = "Download CSV"
|
59 |
-
download_button.disabled = False
|
60 |
-
|
61 |
-
inputs = [gr.inputs.Textbox(label="Search Term"),
|
62 |
-
gr.inputs.Checkbox(["pubmed_id", "title", "abstract"], label="Keywords"),
|
63 |
-
gr.inputs.Slider(minimum=1, maximum=10000, default=100, label="Max Results")]
|
64 |
-
|
65 |
-
outputs = [gr.outputs.Dataframe(type="pandas")]
|
66 |
-
|
67 |
-
interface = gr.Interface(search_pubmed, inputs, outputs, title="PubMed Search")
|
68 |
-
|
69 |
-
result = interface.launch(share=True)
|
70 |
-
|
71 |
-
download_button = widgets.Button(description="Download CSV")
|
72 |
-
download_button.on_click(download_csv)
|
73 |
-
display(download_button)
|
|
|
1 |
+
def search_pubmed_with_gradio(search_term, max_results, include_pubmed_id, include_title, include_abstract):
|
2 |
+
pubmed = PubMed(tool="MyTool", email="aalamel@clemson.edu")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
results = pubmed.query(search_term, max_results=max_results)
|
4 |
+
article_list = []
|
5 |
+
for article in results:
|
6 |
+
article_dict = article.toDict()
|
7 |
+
if include_pubmed_id:
|
8 |
+
pubmed_id = article_dict['pubmed_id'].partition('\n')[0]
|
9 |
+
else:
|
10 |
+
pubmed_id = ""
|
11 |
+
if include_title:
|
12 |
+
title = article_dict['title']
|
13 |
+
else:
|
14 |
+
title = ""
|
15 |
+
if include_abstract:
|
16 |
+
abstract = article_dict['abstract']
|
17 |
+
else:
|
18 |
+
abstract = ""
|
19 |
+
article_list.append({'pubmed_id': pubmed_id, 'title': title, 'abstract': abstract})
|
20 |
+
df = pd.DataFrame(article_list)
|
21 |
+
return df
|
22 |
+
|
23 |
+
interface = gr.Interface(search_pubmed_with_gradio,
|
24 |
+
[gr.inputs.Textbox(label="Search Term"),
|
25 |
+
gr.inputs.Slider(minimum=1, maximum=10000, default=100, label="Max Results"),
|
26 |
+
gr.inputs.Checkbox("pubmed_id", label="Pubmed ID"),
|
27 |
+
gr.inputs.Checkbox("title", label="Title"),
|
28 |
+
gr.inputs.Checkbox("abstract", label="Abstract")],
|
29 |
+
"dataframe",
|
30 |
+
title="PubMed Search",
|
31 |
+
description="Enter a keyword or more than a keyword to search in PubMed database")
|
32 |
+
|
33 |
+
if __name__ == "__main__":
|
34 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|