Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import logging
|
2 |
+
import pandas as pd
|
3 |
+
import gradio as gr
|
4 |
+
import ipywidgets as widgets
|
5 |
+
from pymed import PubMed
|
6 |
+
|
7 |
+
def search_pubmed(search_term, keywords, max_results):
|
8 |
+
# Validate the input
|
9 |
+
if max_results < 1:
|
10 |
+
raise ValueError("Max Results must be a positive integer")
|
11 |
+
|
12 |
+
# Connect to PubMed database
|
13 |
+
pubmed = PubMed(tool="MyTool", email="aalamel@clemson.edu")
|
14 |
+
results = pubmed.query(search_term, max_results=max_results)
|
15 |
+
|
16 |
+
# Prepare the lists to store article information
|
17 |
+
articleList = []
|
18 |
+
articleInfo = []
|
19 |
+
|
20 |
+
# Try to retrieve the articles and process them
|
21 |
+
try:
|
22 |
+
for article in results:
|
23 |
+
articleDict = article.toDict()
|
24 |
+
articleList.append(articleDict)
|
25 |
+
except Exception as e:
|
26 |
+
# Log the error if it occurs
|
27 |
+
logging.error("Error while processing articles: {}".format(e))
|
28 |
+
raise
|
29 |
+
|
30 |
+
# Store the information of each article in articleInfo
|
31 |
+
for article in articleList:
|
32 |
+
pubmedId = article['pubmed_id'].partition('\n')[0]
|
33 |
+
articleInfo.append({u'pubmed_id': pubmedId,
|
34 |
+
u'title': article['title'],
|
35 |
+
u'abstract': article['abstract']
|
36 |
+
})
|
37 |
+
|
38 |
+
# Convert the article information to a Pandas dataframe
|
39 |
+
cardio_abstract = pd.DataFrame.from_dict(articleInfo)
|
40 |
+
|
41 |
+
# Filter the dataframe based on the selected keywords
|
42 |
+
cardio_abstract = cardio_abstract[keywords]
|
43 |
+
|
44 |
+
# Return the filtered dataframe
|
45 |
+
return cardio_abstract
|
46 |
+
|
47 |
+
def download_csv(b):
|
48 |
+
download_button.description = "Downloading..."
|
49 |
+
download_button.disabled = True
|
50 |
+
dataframe.to_csv("pubmed_results.csv", index=False)
|
51 |
+
download_button.description = "Download CSV"
|
52 |
+
download_button.disabled = False
|
53 |
+
|
54 |
+
inputs = [gr.inputs.Textbox(label="Search Term"),
|
55 |
+
gr.inputs.Checkbox(["pubmed_id", "title", "abstract"], label="Keywords"),
|
56 |
+
gr.inputs.Slider(minimum=1, maximum=10000, default=100, label="Max Results")]
|
57 |
+
|
58 |
+
outputs = [gr.outputs.Dataframe(type="pandas")]
|
59 |
+
|
60 |
+
interface = gr.Interface(search_pubmed, inputs, outputs, title="PubMed Search")
|
61 |
+
|
62 |
+
result = interface.launch(share=True)
|
63 |
+
dataframe = result[0]
|
64 |
+
|
65 |
+
download_button = widgets.Button(description="Download CSV")
|
66 |
+
download_button.on_click(download_csv)
|
67 |
+
display(download_button)
|