Johnyquest7 commited on
Commit
1c9f0c5
·
verified ·
1 Parent(s): be0292f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -23
app.py CHANGED
@@ -43,9 +43,18 @@ def pubmed_search(mesh_terms, email, start_date, end_date):
43
  return "Please provide MeSH terms and email.", None, None
44
 
45
  # Convert date objects to string format
46
- start_date_str = start_date.strftime("%Y-%m-%d")
47
- end_date_str = end_date.strftime("%Y-%m-%d")
48
-
 
 
 
 
 
 
 
 
 
49
  # Join MeSH terms for query
50
  query = " AND ".join([f'"{term}"[MeSH Terms]' for term in mesh_terms.split(",")])
51
 
@@ -53,39 +62,84 @@ def pubmed_search(mesh_terms, email, start_date, end_date):
53
  pubmed_ids = fetch_pubmed(query, email, start_date_str, end_date_str)
54
  if not pubmed_ids:
55
  return "No articles found for the given search terms and date range.", None, None
56
-
57
- # (Keep the rest of the function unchanged)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  # Gradio interface components
60
  with gr.Blocks() as app:
61
  gr.Markdown("# PubMed Search Tool for Thyroid AI Research")
62
 
63
  with gr.Row():
64
- mesh_term_input = gr.Textbox(label="Enter a MeSH Term", placeholder="e.g., thyroid", interactive=True)
 
 
 
 
65
  add_button = gr.Button("Add MeSH Term")
66
- mesh_terms_box = gr.Textbox(label="Added MeSH Terms", interactive=False, lines=2)
 
 
 
 
67
 
68
- start_date = gr.DateTime(label="Start Date", value="2025-01-01")
69
- end_date = gr.DateTime(label="End Date", value="2025-01-07")
70
- email_input = gr.Textbox(label="Email", placeholder="Your email (required by PubMed API)", interactive=True)
 
 
 
 
 
 
71
 
72
  search_button = gr.Button("Search PubMed")
73
  status_output = gr.Textbox(label="Status")
74
-
75
  markdown_file = gr.File(label="Markdown File", interactive=False)
76
  text_file = gr.File(label="Text File", interactive=False)
77
 
78
- # Logic for adding MeSH terms
79
- def add_mesh_term(term, terms):
80
- if term.strip():
81
- terms = terms.split(",") if terms else []
82
- terms.append(term.strip())
83
- return ", ".join(terms)
84
- return terms
85
-
86
  # Bind functions to interface
87
- add_button.click(fn=add_mesh_term, inputs=[mesh_term_input, mesh_terms_box], outputs=mesh_terms_box)
88
- search_button.click(fn=pubmed_search, inputs=[mesh_terms_box, email_input, start_date, end_date], outputs=[status_output, markdown_file, text_file])
 
 
 
 
 
 
 
 
 
 
89
 
90
- # Launch app
91
- app.launch()
 
43
  return "Please provide MeSH terms and email.", None, None
44
 
45
  # Convert date objects to string format
46
+ try:
47
+ # If start_date and end_date are strings, parse them first
48
+ if isinstance(start_date, str):
49
+ start_date = datetime.strptime(start_date, "%Y-%m-%d")
50
+ if isinstance(end_date, str):
51
+ end_date = datetime.strptime(end_date, "%Y-%m-%d")
52
+
53
+ start_date_str = start_date.strftime("%Y-%m-%d")
54
+ end_date_str = end_date.strftime("%Y-%m-%d")
55
+ except ValueError as e:
56
+ return f"Error processing dates: {str(e)}", None, None
57
+
58
  # Join MeSH terms for query
59
  query = " AND ".join([f'"{term}"[MeSH Terms]' for term in mesh_terms.split(",")])
60
 
 
62
  pubmed_ids = fetch_pubmed(query, email, start_date_str, end_date_str)
63
  if not pubmed_ids:
64
  return "No articles found for the given search terms and date range.", None, None
65
+
66
+ # Fetch article details
67
+ try:
68
+ records = fetch_details(pubmed_ids)
69
+ except Exception as e:
70
+ return f"Error fetching article details: {str(e)}", None, None
71
+
72
+ # Compile markdown content
73
+ markdown_content = compile_summaries(records)
74
+
75
+ # Create temporary files
76
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
77
+ markdown_filename = f"pubmed_results_{timestamp}.md"
78
+ text_filename = f"pubmed_results_{timestamp}.txt"
79
+
80
+ # Save files
81
+ try:
82
+ with open(markdown_filename, "w", encoding="utf-8") as f:
83
+ f.write(markdown_content)
84
+ with open(text_filename, "w", encoding="utf-8") as f:
85
+ f.write(markdown_content)
86
+ except Exception as e:
87
+ return f"Error saving files: {str(e)}", None, None
88
+
89
+ return "Search completed successfully!", markdown_filename, text_filename
90
+
91
+ # Logic for adding MeSH terms
92
+ def add_mesh_term(term, terms):
93
+ if term.strip():
94
+ terms = terms.split(",") if terms else []
95
+ terms.append(term.strip())
96
+ return ", ".join(terms)
97
+ return terms
98
 
99
  # Gradio interface components
100
  with gr.Blocks() as app:
101
  gr.Markdown("# PubMed Search Tool for Thyroid AI Research")
102
 
103
  with gr.Row():
104
+ mesh_term_input = gr.Textbox(
105
+ label="Enter a MeSH Term",
106
+ placeholder="e.g., thyroid",
107
+ interactive=True
108
+ )
109
  add_button = gr.Button("Add MeSH Term")
110
+ mesh_terms_box = gr.Textbox(
111
+ label="Added MeSH Terms",
112
+ interactive=False,
113
+ lines=2
114
+ )
115
 
116
+ with gr.Row():
117
+ start_date = gr.Date(label="Start Date", value="2025-01-01") # Changed from DateTime to Date
118
+ end_date = gr.Date(label="End Date", value="2025-01-07") # Changed from DateTime to Date
119
+
120
+ email_input = gr.Textbox(
121
+ label="Email",
122
+ placeholder="Your email (required by PubMed API)",
123
+ interactive=True
124
+ )
125
 
126
  search_button = gr.Button("Search PubMed")
127
  status_output = gr.Textbox(label="Status")
 
128
  markdown_file = gr.File(label="Markdown File", interactive=False)
129
  text_file = gr.File(label="Text File", interactive=False)
130
 
 
 
 
 
 
 
 
 
131
  # Bind functions to interface
132
+ add_button.click(
133
+ fn=add_mesh_term,
134
+ inputs=[mesh_term_input, mesh_terms_box],
135
+ outputs=mesh_terms_box
136
+ )
137
+
138
+ search_button.click(
139
+ fn=pubmed_search,
140
+ inputs=[mesh_terms_box, email_input, start_date, end_date],
141
+ outputs=[status_output, markdown_file, text_file]
142
+ )
143
+
144
 
145
+ app.launch()