Techymom commited on
Commit
2519293
·
verified ·
1 Parent(s): caa5faa

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -0
app.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import xml.etree.ElementTree as ET
4
+ import pandas as pd
5
+ import csv
6
+ from io import StringIO
7
+ #from google.colab import userdata
8
+ #userdata.get('secretName')
9
+
10
+ # Correctly defined functions for fetching articles and converting them to CSV format
11
+ def fetch_articles(keyword, max_results=10):
12
+ search_url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi"
13
+ search_params = {"db": "pubmed", "term": keyword, "retmax": max_results, "usehistory": "y"}
14
+ search_response = requests.get(search_url, params=search_params)
15
+ search_tree = ET.fromstring(search_response.content)
16
+ id_list = [id_.text for id_ in search_tree.findall('.//Id')]
17
+
18
+ fetch_url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi"
19
+ fetch_params = {"db": "pubmed", "id": ",".join(id_list), "retmode": "xml"}
20
+ fetch_response = requests.get(fetch_url, params=fetch_params)
21
+ fetch_tree = ET.fromstring(fetch_response.content)
22
+
23
+ articles = []
24
+ for article in fetch_tree.findall('.//PubmedArticle'):
25
+ title = article.find('.//ArticleTitle').text
26
+ authors_list = [author.find('.//LastName').text + " " + author.find('.//ForeName').text for author in article.findall('.//Author') if author.find('.//LastName') is not None and author.find('.//ForeName') is not None]
27
+ authors_formatted = ", ".join(authors_list) if len(authors_list) <= 2 else authors_list[0] + " et al."
28
+
29
+ pub_date_element = article.find('.//PubDate')
30
+ year = "Unknown"
31
+ if pub_date_element is not None:
32
+ year_element = pub_date_element.find('Year')
33
+ if year_element is not None:
34
+ year = year_element.text
35
+
36
+ journal = article.find('.//Journal/Title').text if article.find('.//Journal/Title') is not None else 'No Journal'
37
+ abstract = article.find('.//Abstract/AbstractText').text if article.find('.//Abstract/AbstractText') is not None else 'No Abstract'
38
+ # mesh_terms = [mesh.text for mesh in article.findall('.//MeshHeading/DescriptorName')]
39
+ article_doi = article.find(".//ArticleId[@IdType='doi']")
40
+ doi = article_doi.text if article_doi is not None else "No DOI available"
41
+ doi_link = f"https://doi.org/{doi}" if doi != "No DOI available" else ""
42
+
43
+ articles.append({
44
+ "Title": title,
45
+ "Authors": authors_formatted,
46
+ "Year": year,
47
+ "DOI": doi_link,
48
+ "Abstract": abstract,
49
+ "Journal": journal,
50
+ # "MeSH Terms": "; ".join(mesh_terms)
51
+ })
52
+
53
+ return articles
54
+
55
+ def write_articles_to_csv(articles, filename="articles.csv"):
56
+ with open(filename, mode='w', newline='', encoding='utf-8') as file:
57
+ writer = csv.writer(file)
58
+ writer.writerow(['Authors', 'Year', 'Title', 'DOI', 'Abstract', 'Journal'])
59
+ for article in articles:
60
+ writer.writerow([article['Authors'], article['Year'], article['Title'], article['DOI'], article['Abstract'], article['Journal']])
61
+ print(f"Articles written to {filename}: {len(articles)}")
62
+
63
+ def articles_to_csv_string(articles):
64
+ output = StringIO()
65
+ writer = csv.writer(output)
66
+ writer.writerow(['Authors', 'Year', 'Title', 'DOI', 'Abstract', 'Journal'])
67
+ for article in articles:
68
+ writer.writerow([article['Authors'], article['Year'], article['Title'], article['DOI'], article['Abstract'], article['Journal']])
69
+ output.seek(0)
70
+ return output.getvalue()
71
+
72
+ def process_inputs(first_name, help_message, keyword1, keyword2, keyword3):
73
+ if len(help_message) > 50:
74
+ return pd.DataFrame([{"Error": "Message exceeds 50 characters. Please shorten your message."}])
75
+
76
+ # Combine keywords with "AND" for a broader search, not strictly as MeSH terms
77
+ keywords = f"{keyword1} AND {keyword2} AND {keyword3}"
78
+ articles = fetch_articles(keywords, max_results=10)
79
+ if articles:
80
+ csv_string = articles_to_csv_string(articles) # Convert articles to CSV string
81
+ df = pd.read_csv(StringIO(csv_string)) # Convert CSV string to DataFrame
82
+ return df
83
+ else:
84
+ return pd.DataFrame([{"Error": "No articles found for the given keywords."}])
85
+ iface = gr.Interface(
86
+ fn=process_inputs,
87
+ inputs=[
88
+ # gr.Textbox(label="First Name", placeholder="Enter your first name here..."),
89
+ # gr.Textbox(label="How can I help you today?", placeholder="Your answer (under 50 characters)"),
90
+ gr.Textbox(label="Word 1", placeholder="First emotional state or concern..."),
91
+ gr.Textbox(label="Word 2", placeholder="Second emotional state or concern..."),
92
+ gr.Textbox(label="Word 3", placeholder="Third emotional state or concern...")
93
+ ],
94
+ outputs=gr.Dataframe(label="Related Research Articles"),
95
+ title="Mental Health Research Assistant",
96
+ description="This tool helps you find research articles related to your mental health concerns. Enter your emotional states or concerns as keywords."
97
+ )
98
+
99
+ iface.launch(debug=True)
100
+