Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,15 @@
|
|
1 |
import gradio as gr
|
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", "[email protected]") # 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 |
# ---------------------------- Helper Functions ----------------------------
|
@@ -26,39 +23,6 @@ def log_error(message: str):
|
|
26 |
except:
|
27 |
print("Couldn't write to error log file.") #If logging fails, still print to console
|
28 |
|
29 |
-
# ---------------------------- Language Model Loading ----------------------------
|
30 |
-
|
31 |
-
def load_spacy_model(model_name="en_core_web_sm"):
|
32 |
-
"""Loads the SpaCy language model, downloading it if necessary."""
|
33 |
-
global initialization_status # To update the initialization status
|
34 |
-
|
35 |
-
try:
|
36 |
-
print(f"Attempting to load SpaCy model '{model_name}'...")
|
37 |
-
nlp_model = spacy.load(model_name)
|
38 |
-
print(f"Successfully loaded SpaCy model '{model_name}'.")
|
39 |
-
initialization_status += f"\nSpaCy model '{model_name}' loaded."
|
40 |
-
return nlp_model
|
41 |
-
except OSError:
|
42 |
-
print(f"SpaCy model '{model_name}' not found. Downloading...")
|
43 |
-
initialization_status += f"\nSpaCy model '{model_name}' not found. Downloading..."
|
44 |
-
try:
|
45 |
-
import subprocess
|
46 |
-
subprocess.check_call(["python", "-m", "spacy", "download", model_name])
|
47 |
-
nlp_model = spacy.load(model_name)
|
48 |
-
print(f"Successfully loaded SpaCy model '{model_name}' after downloading.")
|
49 |
-
initialization_status += f"\nSuccessfully loaded SpaCy model '{model_name}' after downloading."
|
50 |
-
return nlp_model
|
51 |
-
|
52 |
-
except Exception as e:
|
53 |
-
log_error(f"Failed to download or load SpaCy model '{model_name}': {e}")
|
54 |
-
initialization_status += f"\nFailed to download or load SpaCy model '{model_name}': {e}"
|
55 |
-
return None # Indicate failure
|
56 |
-
|
57 |
-
except Exception as e:
|
58 |
-
log_error(f"Error loading SpaCy model '{model_name}': {e}")
|
59 |
-
initialization_status += f"\nError loading SpaCy model '{model_name}': {e}"
|
60 |
-
return None
|
61 |
-
|
62 |
# ---------------------------- Tool Functions ----------------------------
|
63 |
|
64 |
def search_pubmed(query: str) -> list:
|
@@ -95,27 +59,13 @@ def summarize_abstract(abstract: str) -> str:
|
|
95 |
try:
|
96 |
# Check if the abstract is empty or too short
|
97 |
if not abstract or len(abstract.strip()) < 50:
|
98 |
-
return "Abstract too short to summarize."
|
99 |
|
100 |
summary = summarizer(abstract, max_length=130, min_length=30, do_sample=False)[0]['summary_text']
|
101 |
return summary
|
102 |
except Exception as e:
|
103 |
log_error(f"Summarization error: {e}")
|
104 |
-
return f"Error during summarization:
|
105 |
-
|
106 |
-
def extract_entities(text: str) -> list:
|
107 |
-
"""Extracts entities (simplified) using SpaCy."""
|
108 |
-
global nlp
|
109 |
-
if nlp is None:
|
110 |
-
log_error("SpaCy model not initialized.")
|
111 |
-
return "SpaCy model not initialized. Check initialization status."
|
112 |
-
try:
|
113 |
-
doc = nlp(text)
|
114 |
-
entities = [(ent.text, ent.label_) for ent in doc.ents]
|
115 |
-
return entities
|
116 |
-
except Exception as e:
|
117 |
-
log_error(f"Entity extraction error: {e}")
|
118 |
-
return [f"Error during entity extraction: {e}"]
|
119 |
|
120 |
# ---------------------------- Agent Function ----------------------------
|
121 |
|
@@ -129,10 +79,15 @@ def medai_agent(query: str) -> str:
|
|
129 |
abstract = fetch_abstract(article_id)
|
130 |
if "Error" not in abstract:
|
131 |
summary = summarize_abstract(abstract)
|
132 |
-
|
133 |
-
|
|
|
|
|
134 |
else:
|
135 |
-
results.append(f"
|
|
|
|
|
|
|
136 |
return "\n".join(results)
|
137 |
else:
|
138 |
return f"No articles found or error occurred: {article_ids}"
|
@@ -140,48 +95,59 @@ def medai_agent(query: str) -> str:
|
|
140 |
# ---------------------------- Initialization and Setup ----------------------------
|
141 |
|
142 |
def setup():
|
143 |
-
"""Initializes the summarization model
|
144 |
-
global summarizer,
|
145 |
initialization_status = "Initializing..."
|
146 |
try:
|
147 |
print("Initializing summarization pipeline...")
|
148 |
initialization_status += "\nInitializing summarization pipeline..."
|
149 |
summarizer = pipeline("summarization", model=SUMMARIZATION_MODEL, token=HUGGINGFACE_API_TOKEN)
|
150 |
print("Summarization pipeline initialized.")
|
151 |
-
initialization_status += "\nSummarization pipeline initialized."
|
152 |
-
|
153 |
-
print("Loading SpaCy model...")
|
154 |
-
initialization_status += "\nLoading SpaCy model..."
|
155 |
-
global nlp
|
156 |
-
nlp = load_spacy_model() # Call the SpaCy loading function.
|
157 |
-
if nlp is None:
|
158 |
-
initialization_status += "\nSpaCy model failed to load. Check the error log."
|
159 |
-
return initialization_status
|
160 |
-
|
161 |
-
print("SpaCy model loaded.")
|
162 |
-
initialization_status += "\nSpaCy model loaded."
|
163 |
-
|
164 |
-
initialization_status = "MedAI Agent initialized successfully!"
|
165 |
-
return initialization_status # Return the status message
|
166 |
except Exception as e:
|
167 |
initialization_status = f"Initialization error: {e}"
|
168 |
log_error(initialization_status)
|
169 |
-
return initialization_status
|
170 |
|
171 |
# ---------------------------- Gradio Interface ----------------------------
|
172 |
|
173 |
def launch_gradio():
|
174 |
"""Launches the Gradio interface."""
|
175 |
-
global initialization_status
|
176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
gr.Markdown("# MedAI: Medical Literature Review and Summarization")
|
178 |
-
status_display = gr.Textbox(value=initialization_status, interactive=False)
|
179 |
query_input = gr.Textbox(lines=3, placeholder="Enter your medical query (e.g., 'new treatments for diabetes')...")
|
180 |
submit_button = gr.Button("Submit")
|
181 |
-
output_results = gr.
|
182 |
|
183 |
submit_button.click(medai_agent, inputs=query_input, outputs=output_results)
|
184 |
-
status_display.value = setup()
|
185 |
|
186 |
iface.launch()
|
187 |
|
|
|
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 |
HUGGINGFACE_API_TOKEN = os.environ.get("HUGGINGFACE_API_TOKEN", "HUGGINGFACE_API_TOKEN") # Use environment variable, default fallback
|
9 |
SUMMARIZATION_MODEL = "facebook/bart-large-cnn"
|
|
|
10 |
|
11 |
# ---------------------------- Global Variables ----------------------------
|
12 |
summarizer = None
|
|
|
13 |
initialization_status = "Initializing..." # Track initialization state
|
14 |
|
15 |
# ---------------------------- Helper Functions ----------------------------
|
|
|
23 |
except:
|
24 |
print("Couldn't write to error log file.") #If logging fails, still print to console
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
# ---------------------------- Tool Functions ----------------------------
|
27 |
|
28 |
def search_pubmed(query: str) -> list:
|
|
|
59 |
try:
|
60 |
# Check if the abstract is empty or too short
|
61 |
if not abstract or len(abstract.strip()) < 50:
|
62 |
+
return "Abstract too short to summarize. A more detailed abstract was not found."
|
63 |
|
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: Failed to generate concise summary with the current model."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
# ---------------------------- Agent Function ----------------------------
|
71 |
|
|
|
79 |
abstract = fetch_abstract(article_id)
|
80 |
if "Error" not in abstract:
|
81 |
summary = summarize_abstract(abstract)
|
82 |
+
results.append(f"<div class='article'>\n"
|
83 |
+
f" <h3 class='article-id'>Article ID: {article_id}</h3>\n"
|
84 |
+
f" <p class='summary'><strong>Summary:</strong> {summary}</p>\n"
|
85 |
+
f"</div>\n")
|
86 |
else:
|
87 |
+
results.append(f"<div class='article error'>\n"
|
88 |
+
f" <h3 class='article-id'>Article ID: {article_id}</h3>\n"
|
89 |
+
f" <p class='error-message'>Error processing article: {abstract}</p>\n"
|
90 |
+
f"</div>\n")
|
91 |
return "\n".join(results)
|
92 |
else:
|
93 |
return f"No articles found or error occurred: {article_ids}"
|
|
|
95 |
# ---------------------------- Initialization and Setup ----------------------------
|
96 |
|
97 |
def setup():
|
98 |
+
"""Initializes the summarization model."""
|
99 |
+
global summarizer, initialization_status
|
100 |
initialization_status = "Initializing..."
|
101 |
try:
|
102 |
print("Initializing summarization pipeline...")
|
103 |
initialization_status += "\nInitializing summarization pipeline..."
|
104 |
summarizer = pipeline("summarization", model=SUMMARIZATION_MODEL, token=HUGGINGFACE_API_TOKEN)
|
105 |
print("Summarization pipeline initialized.")
|
106 |
+
initialization_status += f"\nSummarization pipeline initialized. Model {SUMMARIZATION_MODEL} loaded and ready."
|
107 |
+
return initialization_status
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
except Exception as e:
|
109 |
initialization_status = f"Initialization error: {e}"
|
110 |
log_error(initialization_status)
|
111 |
+
return initialization_status
|
112 |
|
113 |
# ---------------------------- Gradio Interface ----------------------------
|
114 |
|
115 |
def launch_gradio():
|
116 |
"""Launches the Gradio interface."""
|
117 |
+
global initialization_status
|
118 |
+
|
119 |
+
# CSS to style the article output
|
120 |
+
css = """
|
121 |
+
.article {
|
122 |
+
border: 1px solid #ddd;
|
123 |
+
margin-bottom: 10px;
|
124 |
+
padding: 10px;
|
125 |
+
border-radius: 5px;
|
126 |
+
}
|
127 |
+
.article.error {
|
128 |
+
border-color: #f00;
|
129 |
+
}
|
130 |
+
.article-id {
|
131 |
+
font-size: 1.2em;
|
132 |
+
margin-bottom: 5px;
|
133 |
+
}
|
134 |
+
.summary {
|
135 |
+
font-style: italic;
|
136 |
+
}
|
137 |
+
.error-message {
|
138 |
+
color: #f00;
|
139 |
+
}
|
140 |
+
"""
|
141 |
+
|
142 |
+
with gr.Blocks(css=css) as iface:
|
143 |
gr.Markdown("# MedAI: Medical Literature Review and Summarization")
|
144 |
+
status_display = gr.Textbox(value=initialization_status, interactive=False)
|
145 |
query_input = gr.Textbox(lines=3, placeholder="Enter your medical query (e.g., 'new treatments for diabetes')...")
|
146 |
submit_button = gr.Button("Submit")
|
147 |
+
output_results = gr.HTML() # Use HTML for formatted output
|
148 |
|
149 |
submit_button.click(medai_agent, inputs=query_input, outputs=output_results)
|
150 |
+
status_display.value = setup()
|
151 |
|
152 |
iface.launch()
|
153 |
|