Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,65 +2,86 @@ import gradio as gr
|
|
| 2 |
from Bio import Entrez
|
| 3 |
from transformers import pipeline
|
| 4 |
import spacy
|
|
|
|
| 5 |
|
| 6 |
# ---------------------------- Configuration ----------------------------
|
| 7 |
-
ENTREZ_EMAIL = "
|
| 8 |
-
HUGGINGFACE_API_TOKEN = "HUGGINGFACE_API_TOKEN"
|
| 9 |
-
SUMMARIZATION_MODEL = "facebook/bart-large-cnn"
|
| 10 |
-
SPACY_MODEL = "en_core_web_sm"
|
|
|
|
| 11 |
# ---------------------------- Global Variables ----------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# ---------------------------- Tool Functions ----------------------------
|
| 17 |
|
| 18 |
def search_pubmed(query: str) -> list:
|
| 19 |
"""Searches PubMed and returns a list of article IDs."""
|
| 20 |
-
Entrez.email = ENTREZ_EMAIL
|
| 21 |
try:
|
| 22 |
-
|
|
|
|
| 23 |
record = Entrez.read(handle)
|
| 24 |
handle.close()
|
| 25 |
return record["IdList"]
|
| 26 |
except Exception as e:
|
|
|
|
| 27 |
return [f"Error during PubMed search: {e}"]
|
| 28 |
|
| 29 |
def fetch_abstract(article_id: str) -> str:
|
| 30 |
"""Fetches the abstract for a given PubMed article ID."""
|
| 31 |
-
Entrez.email = ENTREZ_EMAIL
|
| 32 |
try:
|
|
|
|
| 33 |
handle = Entrez.efetch(db="pubmed", id=article_id, rettype="abstract", retmode="text")
|
| 34 |
abstract = handle.read()
|
| 35 |
handle.close()
|
| 36 |
return abstract
|
| 37 |
except Exception as e:
|
|
|
|
| 38 |
return f"Error fetching abstract for {article_id}: {e}"
|
| 39 |
|
| 40 |
def summarize_abstract(abstract: str) -> str:
|
| 41 |
"""Summarizes an abstract using a transformer model."""
|
| 42 |
-
global summarizer
|
| 43 |
if summarizer is None:
|
| 44 |
-
|
|
|
|
| 45 |
|
| 46 |
try:
|
| 47 |
summary = summarizer(abstract, max_length=130, min_length=30, do_sample=False)[0]['summary_text']
|
| 48 |
return summary
|
| 49 |
except Exception as e:
|
|
|
|
| 50 |
return f"Error during summarization: {e}"
|
| 51 |
|
| 52 |
def extract_entities(text: str) -> list:
|
| 53 |
"""Extracts entities (simplified) using SpaCy."""
|
| 54 |
global nlp
|
|
|
|
|
|
|
|
|
|
| 55 |
try:
|
| 56 |
doc = nlp(text)
|
| 57 |
entities = [(ent.text, ent.label_) for ent in doc.ents]
|
| 58 |
return entities
|
| 59 |
except Exception as e:
|
|
|
|
| 60 |
return [f"Error during entity extraction: {e}"]
|
| 61 |
|
| 62 |
-
|
| 63 |
-
# ---------------------------- Agent Function (Simplified) ----------------------------
|
| 64 |
|
| 65 |
def medai_agent(query: str) -> str:
|
| 66 |
"""Orchestrates the medical literature review and summarization."""
|
|
@@ -72,7 +93,7 @@ def medai_agent(query: str) -> str:
|
|
| 72 |
abstract = fetch_abstract(article_id)
|
| 73 |
if "Error" not in abstract:
|
| 74 |
summary = summarize_abstract(abstract)
|
| 75 |
-
entities = extract_entities(abstract)
|
| 76 |
results.append(f"**Article ID:** {article_id}\n\n**Summary:** {summary}\n\n**Entities:** {entities}\n\n---\n")
|
| 77 |
else:
|
| 78 |
results.append(f"Error processing article {article_id}: {abstract}\n\n---\n")
|
|
@@ -80,31 +101,44 @@ def medai_agent(query: str) -> str:
|
|
| 80 |
else:
|
| 81 |
return f"No articles found or error occurred: {article_ids}"
|
| 82 |
|
| 83 |
-
# ----------------------------
|
| 84 |
|
| 85 |
def setup():
|
| 86 |
-
"""Initializes the summarization model and
|
| 87 |
-
global summarizer, nlp
|
|
|
|
| 88 |
try:
|
|
|
|
| 89 |
summarizer = pipeline("summarization", model=SUMMARIZATION_MODEL, token=HUGGINGFACE_API_TOKEN)
|
|
|
|
|
|
|
| 90 |
nlp = spacy.load(SPACY_MODEL)
|
| 91 |
-
|
|
|
|
|
|
|
| 92 |
except Exception as e:
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
def launch_gradio():
|
| 96 |
"""Launches the Gradio interface."""
|
|
|
|
| 97 |
with gr.Blocks() as iface:
|
| 98 |
gr.Markdown("# MedAI: Medical Literature Review and Summarization")
|
| 99 |
-
|
| 100 |
query_input = gr.Textbox(lines=3, placeholder="Enter your medical query (e.g., 'new treatments for diabetes')...")
|
| 101 |
submit_button = gr.Button("Submit")
|
| 102 |
output_results = gr.Markdown()
|
| 103 |
|
| 104 |
submit_button.click(medai_agent, inputs=query_input, outputs=output_results)
|
|
|
|
| 105 |
|
| 106 |
-
#
|
| 107 |
-
|
|
|
|
| 108 |
|
| 109 |
iface.launch()
|
| 110 |
|
|
|
|
| 2 |
from Bio import Entrez
|
| 3 |
from transformers import pipeline
|
| 4 |
import spacy
|
| 5 |
+
import os # For environment variables and file paths
|
| 6 |
|
| 7 |
# ---------------------------- Configuration ----------------------------
|
| 8 |
+
ENTREZ_EMAIL = os.environ.get("ENTREZ_EMAIL", "your_email@example.com") # Use environment variable, default fallback
|
| 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 |
+
SPACY_MODEL = "en_core_web_sm"
|
| 12 |
+
|
| 13 |
# ---------------------------- Global Variables ----------------------------
|
| 14 |
+
summarizer = None
|
| 15 |
+
nlp = None
|
| 16 |
+
initialization_status = "Initializing..." # Track initialization state
|
| 17 |
+
|
| 18 |
|
| 19 |
+
# ---------------------------- Helper Functions ----------------------------
|
| 20 |
+
|
| 21 |
+
def log_error(message: str):
|
| 22 |
+
"""Logs an error message to the console and a file (if possible)."""
|
| 23 |
+
print(f"ERROR: {message}")
|
| 24 |
+
try:
|
| 25 |
+
with open("error_log.txt", "a") as f:
|
| 26 |
+
f.write(f"{message}\n")
|
| 27 |
+
except:
|
| 28 |
+
print("Couldn't write to error log file.") #If logging fails, still print to console
|
| 29 |
|
| 30 |
# ---------------------------- Tool Functions ----------------------------
|
| 31 |
|
| 32 |
def search_pubmed(query: str) -> list:
|
| 33 |
"""Searches PubMed and returns a list of article IDs."""
|
|
|
|
| 34 |
try:
|
| 35 |
+
Entrez.email = ENTREZ_EMAIL
|
| 36 |
+
handle = Entrez.esearch(db="pubmed", term=query, retmax="5")
|
| 37 |
record = Entrez.read(handle)
|
| 38 |
handle.close()
|
| 39 |
return record["IdList"]
|
| 40 |
except Exception as e:
|
| 41 |
+
log_error(f"PubMed search error: {e}")
|
| 42 |
return [f"Error during PubMed search: {e}"]
|
| 43 |
|
| 44 |
def fetch_abstract(article_id: str) -> str:
|
| 45 |
"""Fetches the abstract for a given PubMed article ID."""
|
|
|
|
| 46 |
try:
|
| 47 |
+
Entrez.email = ENTREZ_EMAIL
|
| 48 |
handle = Entrez.efetch(db="pubmed", id=article_id, rettype="abstract", retmode="text")
|
| 49 |
abstract = handle.read()
|
| 50 |
handle.close()
|
| 51 |
return abstract
|
| 52 |
except Exception as e:
|
| 53 |
+
log_error(f"Error fetching abstract for {article_id}: {e}")
|
| 54 |
return f"Error fetching abstract for {article_id}: {e}"
|
| 55 |
|
| 56 |
def summarize_abstract(abstract: str) -> str:
|
| 57 |
"""Summarizes an abstract using a transformer model."""
|
| 58 |
+
global summarizer
|
| 59 |
if summarizer is None:
|
| 60 |
+
log_error("Summarizer not initialized.")
|
| 61 |
+
return "Summarizer not initialized. Check initialization status."
|
| 62 |
|
| 63 |
try:
|
| 64 |
summary = summarizer(abstract, max_length=130, min_length=30, do_sample=False)[0]['summary_text']
|
| 65 |
return summary
|
| 66 |
except Exception as e:
|
| 67 |
+
log_error(f"Summarization error: {e}")
|
| 68 |
return f"Error during summarization: {e}"
|
| 69 |
|
| 70 |
def extract_entities(text: str) -> list:
|
| 71 |
"""Extracts entities (simplified) using SpaCy."""
|
| 72 |
global nlp
|
| 73 |
+
if nlp is None:
|
| 74 |
+
log_error("SpaCy model not initialized.")
|
| 75 |
+
return "SpaCy model not initialized. Check initialization status."
|
| 76 |
try:
|
| 77 |
doc = nlp(text)
|
| 78 |
entities = [(ent.text, ent.label_) for ent in doc.ents]
|
| 79 |
return entities
|
| 80 |
except Exception as e:
|
| 81 |
+
log_error(f"Entity extraction error: {e}")
|
| 82 |
return [f"Error during entity extraction: {e}"]
|
| 83 |
|
| 84 |
+
# ---------------------------- Agent Function ----------------------------
|
|
|
|
| 85 |
|
| 86 |
def medai_agent(query: str) -> str:
|
| 87 |
"""Orchestrates the medical literature review and summarization."""
|
|
|
|
| 93 |
abstract = fetch_abstract(article_id)
|
| 94 |
if "Error" not in abstract:
|
| 95 |
summary = summarize_abstract(abstract)
|
| 96 |
+
entities = extract_entities(abstract)
|
| 97 |
results.append(f"**Article ID:** {article_id}\n\n**Summary:** {summary}\n\n**Entities:** {entities}\n\n---\n")
|
| 98 |
else:
|
| 99 |
results.append(f"Error processing article {article_id}: {abstract}\n\n---\n")
|
|
|
|
| 101 |
else:
|
| 102 |
return f"No articles found or error occurred: {article_ids}"
|
| 103 |
|
| 104 |
+
# ---------------------------- Initialization and Setup ----------------------------
|
| 105 |
|
| 106 |
def setup():
|
| 107 |
+
"""Initializes the summarization model and SpaCy model."""
|
| 108 |
+
global summarizer, nlp, initialization_status
|
| 109 |
+
initialization_status = "Initializing..."
|
| 110 |
try:
|
| 111 |
+
print("Initializing summarization pipeline...")
|
| 112 |
summarizer = pipeline("summarization", model=SUMMARIZATION_MODEL, token=HUGGINGFACE_API_TOKEN)
|
| 113 |
+
print("Summarization pipeline initialized.")
|
| 114 |
+
print("Loading SpaCy model...")
|
| 115 |
nlp = spacy.load(SPACY_MODEL)
|
| 116 |
+
print("SpaCy model loaded.")
|
| 117 |
+
initialization_status = "MedAI Agent initialized successfully!"
|
| 118 |
+
return initialization_status # Return the status message
|
| 119 |
except Exception as e:
|
| 120 |
+
initialization_status = f"Initialization error: {e}"
|
| 121 |
+
log_error(initialization_status)
|
| 122 |
+
return initialization_status # Return the error message
|
| 123 |
+
|
| 124 |
+
# ---------------------------- Gradio Interface ----------------------------
|
| 125 |
|
| 126 |
def launch_gradio():
|
| 127 |
"""Launches the Gradio interface."""
|
| 128 |
+
global initialization_status #Allows the function to modify global variable
|
| 129 |
with gr.Blocks() as iface:
|
| 130 |
gr.Markdown("# MedAI: Medical Literature Review and Summarization")
|
| 131 |
+
status_display = gr.Textbox(value=initialization_status, interactive=False) # Displays initialization status
|
| 132 |
query_input = gr.Textbox(lines=3, placeholder="Enter your medical query (e.g., 'new treatments for diabetes')...")
|
| 133 |
submit_button = gr.Button("Submit")
|
| 134 |
output_results = gr.Markdown()
|
| 135 |
|
| 136 |
submit_button.click(medai_agent, inputs=query_input, outputs=output_results)
|
| 137 |
+
#The run of the agent will not change.
|
| 138 |
|
| 139 |
+
#The setup is running. The value of the text display will update based on this step.
|
| 140 |
+
setup_result = setup()
|
| 141 |
+
status_display.value = setup_result #update the status display.
|
| 142 |
|
| 143 |
iface.launch()
|
| 144 |
|