Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,16 @@
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
print(gr.__version__)
|
3 |
import requests
|
4 |
import xml.etree.ElementTree as ET
|
5 |
import pandas as pd
|
6 |
import csv
|
7 |
from io import StringIO
|
|
|
|
|
|
|
8 |
|
9 |
# Correctly defined functions for fetching articles and converting them to CSV format
|
10 |
def fetch_articles(keyword, max_results=10):
|
@@ -34,7 +40,6 @@ def fetch_articles(keyword, max_results=10):
|
|
34 |
|
35 |
journal = article.find('.//Journal/Title').text if article.find('.//Journal/Title') is not None else 'No Journal'
|
36 |
abstract = article.find('.//Abstract/AbstractText').text if article.find('.//Abstract/AbstractText') is not None else 'No Abstract'
|
37 |
-
# mesh_terms = [mesh.text for mesh in article.findall('.//MeshHeading/DescriptorName')]
|
38 |
article_doi = article.find(".//ArticleId[@IdType='doi']")
|
39 |
doi = article_doi.text if article_doi is not None else "No DOI available"
|
40 |
doi_link = f"https://doi.org/{doi}" if doi != "No DOI available" else ""
|
@@ -46,7 +51,6 @@ def fetch_articles(keyword, max_results=10):
|
|
46 |
"DOI": doi_link,
|
47 |
"Abstract": abstract,
|
48 |
"Journal": journal,
|
49 |
-
# "MeSH Terms": "; ".join(mesh_terms)
|
50 |
})
|
51 |
|
52 |
return articles
|
@@ -68,19 +72,44 @@ def articles_to_csv_string(articles):
|
|
68 |
output.seek(0)
|
69 |
return output.getvalue()
|
70 |
|
71 |
-
def
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
#
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
iface = gr.Interface(
|
85 |
fn=process_inputs,
|
86 |
inputs=[
|
@@ -90,7 +119,11 @@ iface = gr.Interface(
|
|
90 |
gr.Textbox(label="What is this session's presenting concern?", placeholder="anxiety..."),
|
91 |
gr.Textbox(label="What type of activity are you interested in?", placeholder="Art therapy, dbt, narrative...")
|
92 |
],
|
93 |
-
outputs=
|
|
|
|
|
|
|
|
|
94 |
title="Workshop Session Planner",
|
95 |
description="This tool helps you find research articles related to your professional practice. Enter your parameters as keywords."
|
96 |
)
|
|
|
1 |
+
# !pip install --upgrade gradio
|
2 |
+
|
3 |
+
import tempfile
|
4 |
+
import os
|
5 |
import gradio as gr
|
|
|
6 |
import requests
|
7 |
import xml.etree.ElementTree as ET
|
8 |
import pandas as pd
|
9 |
import csv
|
10 |
from io import StringIO
|
11 |
+
from datetime import datetime
|
12 |
+
import os
|
13 |
+
import tempfile
|
14 |
|
15 |
# Correctly defined functions for fetching articles and converting them to CSV format
|
16 |
def fetch_articles(keyword, max_results=10):
|
|
|
40 |
|
41 |
journal = article.find('.//Journal/Title').text if article.find('.//Journal/Title') is not None else 'No Journal'
|
42 |
abstract = article.find('.//Abstract/AbstractText').text if article.find('.//Abstract/AbstractText') is not None else 'No Abstract'
|
|
|
43 |
article_doi = article.find(".//ArticleId[@IdType='doi']")
|
44 |
doi = article_doi.text if article_doi is not None else "No DOI available"
|
45 |
doi_link = f"https://doi.org/{doi}" if doi != "No DOI available" else ""
|
|
|
51 |
"DOI": doi_link,
|
52 |
"Abstract": abstract,
|
53 |
"Journal": journal,
|
|
|
54 |
})
|
55 |
|
56 |
return articles
|
|
|
72 |
output.seek(0)
|
73 |
return output.getvalue()
|
74 |
|
75 |
+
def generate_filename(keyword1, keyword2, keyword3):
|
76 |
+
# Format the current timestamp
|
77 |
+
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
78 |
+
# Create a filename that includes the keywords and timestamp
|
79 |
+
# Note: Filenames need to be safe for the filesystem, so replace or remove characters as necessary
|
80 |
+
filename = f"articles_{keyword1}_{keyword2}_{keyword3}_{timestamp}.csv".replace(' ', '_').replace('/', '_')
|
81 |
+
# Ensure the filename length does not exceed filesystem limits
|
82 |
+
return filename[:255]
|
83 |
+
|
84 |
+
def process_inputs(keyword1, keyword2, keyword3):
|
85 |
+
try:
|
86 |
+
keywords = f"{keyword1} AND {keyword2} AND {keyword3}"
|
87 |
+
articles = fetch_articles(keywords, max_results=10)
|
88 |
+
if not articles: # If no articles were found
|
89 |
+
df_empty = pd.DataFrame({"Error": ["No articles found or an error occurred."]})
|
90 |
+
# Generate a nicer filename
|
91 |
+
filename = generate_filename(keyword1, keyword2, keyword3)
|
92 |
+
# Create a temporary file with the specified filename
|
93 |
+
temp_file_path = os.path.join(tempfile.gettempdir(), filename)
|
94 |
+
df_empty.to_csv(temp_file_path, index=False)
|
95 |
+
return df_empty, temp_file_path
|
96 |
+
|
97 |
+
# If articles were found
|
98 |
+
csv_string = articles_to_csv_string(articles)
|
99 |
+
df = pd.read_csv(StringIO(csv_string))
|
100 |
+
filename = generate_filename(keyword1, keyword2, keyword3)
|
101 |
+
temp_file_path = os.path.join(tempfile.gettempdir(), filename)
|
102 |
+
df.to_csv(temp_file_path, index=False)
|
103 |
+
|
104 |
+
return df, temp_file_path
|
105 |
+
except Exception as e:
|
106 |
+
print(f"An error occurred: {e}")
|
107 |
+
df_empty = pd.DataFrame({"Error": ["An error occurred during processing."]})
|
108 |
+
filename = generate_filename(keyword1, keyword2, keyword3)
|
109 |
+
temp_file_path = os.path.join(tempfile.gettempdir(), filename)
|
110 |
+
df_empty.to_csv(temp_file_path, index=False)
|
111 |
+
return df_empty, temp_file_path
|
112 |
+
|
113 |
iface = gr.Interface(
|
114 |
fn=process_inputs,
|
115 |
inputs=[
|
|
|
119 |
gr.Textbox(label="What is this session's presenting concern?", placeholder="anxiety..."),
|
120 |
gr.Textbox(label="What type of activity are you interested in?", placeholder="Art therapy, dbt, narrative...")
|
121 |
],
|
122 |
+
outputs=[
|
123 |
+
gr.Dataframe(label="Related Research Articles"),
|
124 |
+
gr.File(label="Download Articles as CSV")
|
125 |
+
],
|
126 |
+
|
127 |
title="Workshop Session Planner",
|
128 |
description="This tool helps you find research articles related to your professional practice. Enter your parameters as keywords."
|
129 |
)
|