Gaborandi commited on
Commit
fd23452
1 Parent(s): 3f2a988

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -72
app.py CHANGED
@@ -1,73 +1,34 @@
1
- import logging
2
- import pandas as pd
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
- # Prepare the lists to store article information
19
- articleList = []
20
- articleInfo = []
21
-
22
- # Try to retrieve the articles and process them
23
- try:
24
- for article in results:
25
- articleDict = article.toDict()
26
- articleList.append(articleDict)
27
- except Exception as e:
28
- # Log the error if it occurs
29
- logging.error("Error while processing articles: {}".format(e))
30
- raise
31
-
32
- # Store the information of each article in articleInfo
33
- for article in articleList:
34
- pubmedId = article['pubmed_id'].partition('\n')[0]
35
- articleInfo.append({u'pubmed_id': pubmedId,
36
- u'title': article['title'],
37
- u'abstract': article['abstract']
38
- })
39
-
40
- # Convert the article information to a Pandas dataframe
41
- cardio_abstract = pd.DataFrame.from_dict(articleInfo)
42
-
43
- # Filter the dataframe based on the selected keywords
44
- cardio_abstract = cardio_abstract[keywords]
45
-
46
- # Return the filtered dataframe
47
- return cardio_abstract
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()