Spaces:
Sleeping
Sleeping
Resolve merge conflicts
Browse files- Dockerfile +14 -0
- app.py +37 -96
- requirements.txt +9 -1
Dockerfile
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
FROM python:3.10
|
| 2 |
|
| 3 |
WORKDIR /app
|
|
@@ -9,4 +10,17 @@ RUN pip install -r requirements.txt
|
|
| 9 |
|
| 10 |
RUN python -m spacy download en_core_web_sm
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
CMD ["python", "app.py"]
|
|
|
|
| 1 |
+
<<<<<<< HEAD
|
| 2 |
FROM python:3.10
|
| 3 |
|
| 4 |
WORKDIR /app
|
|
|
|
| 10 |
|
| 11 |
RUN python -m spacy download en_core_web_sm
|
| 12 |
|
| 13 |
+
=======
|
| 14 |
+
FROM python:3.10
|
| 15 |
+
|
| 16 |
+
WORKDIR /app
|
| 17 |
+
|
| 18 |
+
COPY . /app
|
| 19 |
+
|
| 20 |
+
RUN apt-get update && apt-get install -y curl
|
| 21 |
+
RUN pip install -r requirements.txt
|
| 22 |
+
|
| 23 |
+
RUN python -m spacy download en_core_web_sm
|
| 24 |
+
|
| 25 |
+
>>>>>>> 3c8dbea734b3938b3c2fd1f974ec877199a17aea
|
| 26 |
CMD ["python", "app.py"]
|
app.py
CHANGED
|
@@ -1,110 +1,51 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
from
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
# ---------------------------- Configuration ----------------------------
|
| 7 |
-
ENTREZ_EMAIL =
|
| 8 |
-
HUGGINGFACE_API_TOKEN = "HUGGINGFACE_API_TOKEN"
|
| 9 |
-
SUMMARIZATION_MODEL = "facebook/bart-large-cnn" # Or try "google/pegasus-large"
|
| 10 |
-
SPACY_MODEL = "en_core_web_sm" # Small model for faster processing
|
| 11 |
-
# ---------------------------- Global Variables ----------------------------
|
| 12 |
-
|
| 13 |
-
summarizer = None # Initialized in the setup function
|
| 14 |
-
nlp = None # Initialized in the setup function
|
| 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 |
-
handle = Entrez.esearch(db="pubmed", term=query, retmax="5") # Limit to 5 for demonstration
|
| 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 # Access the global summarizer
|
| 43 |
-
if summarizer is None:
|
| 44 |
-
return "Summarizer not initialized. Please reload the interface."
|
| 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."""
|
| 67 |
-
article_ids = search_pubmed(query)
|
| 68 |
-
|
| 69 |
-
if isinstance(article_ids, list) and article_ids:
|
| 70 |
-
results = []
|
| 71 |
-
for article_id in article_ids:
|
| 72 |
-
abstract = fetch_abstract(article_id)
|
| 73 |
-
if "Error" not in abstract:
|
| 74 |
-
summary = summarize_abstract(abstract)
|
| 75 |
-
entities = extract_entities(abstract) #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")
|
| 79 |
-
return "\n".join(results)
|
| 80 |
-
else:
|
| 81 |
-
return f"No articles found or error occurred: {article_ids}"
|
| 82 |
|
| 83 |
# ---------------------------- Gradio Interface ----------------------------
|
| 84 |
|
| 85 |
-
def setup():
|
| 86 |
-
"""Initializes the summarization model and Spacy model."""
|
| 87 |
-
global summarizer, nlp
|
| 88 |
-
try:
|
| 89 |
-
summarizer = pipeline("summarization", model=SUMMARIZATION_MODEL, token=HUGGINGFACE_API_TOKEN)
|
| 90 |
-
nlp = spacy.load(SPACY_MODEL)
|
| 91 |
-
return "MedAI Agent initialized successfully!"
|
| 92 |
-
except Exception as e:
|
| 93 |
-
return f"Initialization error: {e}"
|
| 94 |
-
|
| 95 |
def launch_gradio():
|
| 96 |
"""Launches the Gradio interface."""
|
| 97 |
-
|
| 98 |
-
|
| 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.
|
| 103 |
-
|
| 104 |
-
submit_button.click(medai_agent, inputs=query_input, outputs=output_results)
|
| 105 |
|
| 106 |
-
#
|
| 107 |
-
|
| 108 |
|
| 109 |
iface.launch()
|
| 110 |
|
|
|
|
| 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;
|
| 20 |
+
margin-bottom: 10px;
|
| 21 |
+
padding: 10px;
|
| 22 |
+
border-radius: 5px;
|
| 23 |
+
}
|
| 24 |
+
.article.error {
|
| 25 |
+
border-color: #f00;
|
| 26 |
+
}
|
| 27 |
+
.article-id {
|
| 28 |
+
font-size: 1.2em;
|
| 29 |
+
margin-bottom: 5px;
|
| 30 |
+
}
|
| 31 |
+
.abstract {
|
| 32 |
+
font-style: italic;
|
| 33 |
+
}
|
| 34 |
+
.error-message {
|
| 35 |
+
color: #f00;
|
| 36 |
+
}
|
| 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 |
|
requirements.txt
CHANGED
|
@@ -1,4 +1,12 @@
|
|
|
|
|
| 1 |
gradio
|
| 2 |
transformers
|
| 3 |
biopython
|
| 4 |
-
spacy
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<<<<<<< HEAD
|
| 2 |
gradio
|
| 3 |
transformers
|
| 4 |
biopython
|
| 5 |
+
spacy
|
| 6 |
+
=======
|
| 7 |
+
gradio
|
| 8 |
+
transformers
|
| 9 |
+
biopython
|
| 10 |
+
spacy
|
| 11 |
+
torch
|
| 12 |
+
>>>>>>> 3c8dbea734b3938b3c2fd1f974ec877199a17aea
|