mgbam commited on
Commit
7765a07
·
verified ·
1 Parent(s): d23fedd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -9
app.py CHANGED
@@ -1,19 +1,79 @@
1
  import gradio as gr
2
- import os
3
- from components import pubmed_search
4
- from components import model_utils
5
- import time
6
  # ---------------------------- Configuration ----------------------------
7
- ENTREZ_EMAIL = os.environ.get("ENTREZ_EMAIL", "ENTREZ_EMAIL")
8
- HUGGINGFACE_API_TOKEN = os.environ.get("HUGGINGFACE_API_TOKEN", "HUGGINGFACE_API_TOKEN")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- # ---------------------------- Global Variables ----------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  # ---------------------------- Gradio Interface ----------------------------
13
 
14
  def launch_gradio():
15
  """Launches the Gradio interface."""
16
 
 
17
  css = """
18
  .article {
19
  border: 1px solid #ddd;
@@ -40,12 +100,15 @@ def launch_gradio():
40
  gr.Markdown("# MedAI: Medical Literature Review")
41
  gr.Markdown("Enter a medical query to retrieve abstracts from PubMed.")
42
 
43
- query_input = gr.Textbox(lines=3, placeholder="Enter your medical query (e.g., 'new treatments for diabetes')...")
44
  submit_button = gr.Button("Submit")
45
  output_results = gr.HTML() # Use HTML for formatted output
 
46
 
47
  # Get data
48
- submit_button.click(pubmed_search.medai_agent, inputs=query_input, outputs=output_results)
 
 
49
 
50
  iface.launch()
51
 
 
1
  import gradio as gr
2
+ from Bio import Entrez
3
+ import os # For environment variables and file paths
4
+ from components import federated_learning
5
+
6
  # ---------------------------- Configuration ----------------------------
7
+ ENTREZ_EMAIL = os.environ.get("ENTREZ_EMAIL", "ENTREZ_EMAIL") # Use environment variable, default fallback
8
+ HUGGINGFACE_API_TOKEN = os.environ.get("HUGGINGFACE_API_TOKEN", "HUGGINGFACE_API_TOKEN") # Use environment variable, default fallback
9
+
10
+ # ---------------------------- Helper Functions ----------------------------
11
+
12
+ def log_error(message: str):
13
+ """Logs an error message to the console and a file (if possible)."""
14
+ print(f"ERROR: {message}")
15
+ try:
16
+ with open("error_log.txt", "a") as f:
17
+ f.write(f"{message}\n")
18
+ except:
19
+ print("Couldn't write to error log file.") #If logging fails, still print to console
20
+
21
+ # ---------------------------- Tool Functions ----------------------------
22
+
23
+ def search_pubmed(query: str) -> list:
24
+ """Searches PubMed and returns a list of article IDs."""
25
+ try:
26
+ Entrez.email = ENTREZ_EMAIL
27
+ handle = Entrez.esearch(db="pubmed", term=query, retmax="5")
28
+ record = Entrez.read(handle)
29
+ handle.close()
30
+ return record["IdList"]
31
+ except Exception as e:
32
+ log_error(f"PubMed search error: {e}")
33
+ return [f"Error during PubMed search: {e}"]
34
+
35
+ def fetch_abstract(article_id: str) -> str:
36
+ """Fetches the abstract for a given PubMed article ID."""
37
+ try:
38
+ Entrez.email = ENTREZ_EMAIL
39
+ handle = Entrez.efetch(db="pubmed", id=article_id, rettype="abstract", retmode="text")
40
+ abstract = handle.read()
41
+ handle.close()
42
+ return abstract
43
+ except Exception as e:
44
+ log_error(f"Error fetching abstract for {article_id}: {e}")
45
+ return f"Error fetching abstract for {article_id}: {e}"
46
+
47
+ # ---------------------------- Agent Function ----------------------------
48
+
49
+ def medai_agent(query: str) -> str:
50
+ """Orchestrates the medical literature review and presents abstract."""
51
+ article_ids = search_pubmed(query)
52
 
53
+ if isinstance(article_ids, list) and article_ids:
54
+ results = []
55
+ for article_id in article_ids:
56
+ abstract = fetch_abstract(article_id)
57
+ if "Error" not in abstract:
58
+ results.append(f"<div class='article'>\n"
59
+ f" <h3 class='article-id'>Article ID: {article_id}</h3>\n"
60
+ f" <p class='abstract'><strong>Abstract:</strong> {abstract}</p>\n"
61
+ f"</div>\n")
62
+ else:
63
+ results.append(f"<div class='article error'>\n"
64
+ f" <h3 class='article-id'>Article ID: {article_id}</h3>\n"
65
+ f" <p class='error-message'>Error processing article: {abstract}</p>\n"
66
+ f"</div>\n")
67
+ return "\n".join(results)
68
+ else:
69
+ return f"No articles found or error occurred: {article_ids}"
70
 
71
  # ---------------------------- Gradio Interface ----------------------------
72
 
73
  def launch_gradio():
74
  """Launches the Gradio interface."""
75
 
76
+ # CSS to style the article output
77
  css = """
78
  .article {
79
  border: 1px solid #ddd;
 
100
  gr.Markdown("# MedAI: Medical Literature Review")
101
  gr.Markdown("Enter a medical query to retrieve abstracts from PubMed.")
102
 
103
+ query_input = gr.Textbox(lines=3, placeholder="Enter your medical query to get abstract from PubMed.")
104
  submit_button = gr.Button("Submit")
105
  output_results = gr.HTML() # Use HTML for formatted output
106
+ federated_learning_output = gr.HTML()
107
 
108
  # Get data
109
+ submit_button.click(medai_agent, inputs=query_input, outputs=output_results)
110
+ run_fl_button = gr.Button("Run Federated Learning (Conceptual)")
111
+ run_fl_button.click(federated_learning.run_federated_learning, outputs = federated_learning_output)
112
 
113
  iface.launch()
114