mgbam commited on
Commit
3c8dbea
·
verified ·
1 Parent(s): e7d54ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -91
app.py CHANGED
@@ -1,100 +1,19 @@
1
  import gradio as gr
2
- from Bio import Entrez
3
- from transformers import pipeline
4
- import os # For environment variables and file paths
5
-
6
  # ---------------------------- Configuration ----------------------------
7
- ENTREZ_EMAIL = os.environ.get("ENTREZ_EMAIL", "[email protected]") # Use environment variable, default fallback
8
-
9
- HUGGINGFACE_API_TOKEN = os.environ.get("HUGGINGFACE_API_TOKEN", "HUGGINGFACE_API_TOKEN") # Use environment variable, default fallback
10
- SUMMARIZATION_MODEL = "facebook/bart-large-cnn"
11
 
12
  # ---------------------------- Global Variables ----------------------------
13
- summarizer = None
14
- initialization_status = "Initializing..." # Track initialization state
15
-
16
- # ---------------------------- Helper Functions ----------------------------
17
-
18
- def log_error(message: str):
19
- """Logs an error message to the console and a file (if possible)."""
20
- print(f"ERROR: {message}")
21
- try:
22
- with open("error_log.txt", "a") as f:
23
- f.write(f"{message}\n")
24
- except:
25
- print("Couldn't write to error log file.") #If logging fails, still print to console
26
-
27
- # ---------------------------- Tool Functions ----------------------------
28
-
29
- def search_pubmed(query: str) -> list:
30
- """Searches PubMed and returns a list of article IDs."""
31
- try:
32
- Entrez.email = ENTREZ_EMAIL
33
- handle = Entrez.esearch(db="pubmed", term=query, retmax="5")
34
- record = Entrez.read(handle)
35
- handle.close()
36
- return record["IdList"]
37
- except Exception as e:
38
- log_error(f"PubMed search error: {e}")
39
- return [f"Error during PubMed search: {e}"]
40
-
41
- def fetch_abstract(article_id: str) -> str:
42
- """Fetches the abstract for a given PubMed article ID."""
43
- try:
44
- Entrez.email = ENTREZ_EMAIL
45
- handle = Entrez.efetch(db="pubmed", id=article_id, rettype="abstract", retmode="text")
46
- abstract = handle.read()
47
- handle.close()
48
- return abstract
49
- except Exception as e:
50
- log_error(f"Error fetching abstract for {article_id}: {e}")
51
- return f"Error fetching abstract for {article_id}: {e}"
52
-
53
- # ---------------------------- Agent Function ----------------------------
54
-
55
- def medai_agent(query: str) -> str:
56
- """Orchestrates the medical literature review and summarization."""
57
- article_ids = search_pubmed(query)
58
-
59
- if isinstance(article_ids, list) and article_ids:
60
- results = []
61
- for article_id in article_ids:
62
- abstract = fetch_abstract(article_id)
63
- if "Error" not in abstract:
64
- results.append(f"<div class='article'>\n"
65
- f" <h3 class='article-id'>Article ID: {article_id}</h3>\n"
66
- f" <p class='abstract'><strong>Abstract:</strong> {abstract}</p>\n"
67
- f"</div>\n")
68
- else:
69
- results.append(f"<div class='article error'>\n"
70
- f" <h3 class='article-id'>Article ID: {article_id}</h3>\n"
71
- f" <p class='error-message'>Error processing article: {abstract}</p>\n"
72
- f"</div>\n")
73
- return "\n".join(results)
74
- else:
75
- return f"No articles found or error occurred: {article_ids}"
76
-
77
- # ---------------------------- Initialization and Setup ----------------------------
78
-
79
- def setup():
80
- """Initializes the summarization model."""
81
- global summarizer, initialization_status
82
- initialization_status = "Initializing..."
83
- try:
84
- initialization_status = "Model is running. The user is now set to search and obtain abstract articles."
85
- return initialization_status
86
- except Exception as e:
87
- initialization_status = f"Initialization error: {e}"
88
- log_error(initialization_status)
89
- return initialization_status
90
 
91
  # ---------------------------- Gradio Interface ----------------------------
92
 
93
  def launch_gradio():
94
  """Launches the Gradio interface."""
95
- global initialization_status
96
 
97
- # CSS to style the article output
98
  css = """
99
  .article {
100
  border: 1px solid #ddd;
@@ -118,14 +37,15 @@ def launch_gradio():
118
  """
119
 
120
  with gr.Blocks(css=css) as iface:
121
- gr.Markdown("# MedAI: Medical Literature Review and Abstract Finder")
122
- status_display = gr.Textbox(value=initialization_status, interactive=False)
 
123
  query_input = gr.Textbox(lines=3, placeholder="Enter your medical query (e.g., 'new treatments for diabetes')...")
124
  submit_button = gr.Button("Submit")
125
  output_results = gr.HTML() # Use HTML for formatted output
126
 
127
- submit_button.click(medai_agent, inputs=query_input, outputs=output_results)
128
- status_display.value = setup()
129
 
130
  iface.launch()
131
 
 
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;
 
37
  """
38
 
39
  with gr.Blocks(css=css) as iface:
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