Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,8 @@ from transformers import pipeline
|
|
4 |
import os # For environment variables and file paths
|
5 |
|
6 |
# ---------------------------- Configuration ----------------------------
|
7 |
-
ENTREZ_EMAIL = os.environ.get("ENTREZ_EMAIL", "
|
|
|
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 |
|
@@ -49,24 +50,6 @@ def fetch_abstract(article_id: str) -> str:
|
|
49 |
log_error(f"Error fetching abstract for {article_id}: {e}")
|
50 |
return f"Error fetching abstract for {article_id}: {e}"
|
51 |
|
52 |
-
def summarize_abstract(abstract: str) -> str:
|
53 |
-
"""Summarizes an abstract using a transformer model."""
|
54 |
-
global summarizer
|
55 |
-
if summarizer is None:
|
56 |
-
log_error("Summarizer not initialized.")
|
57 |
-
return "Summarizer not initialized. Check initialization status."
|
58 |
-
|
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 |
|
72 |
def medai_agent(query: str) -> str:
|
@@ -78,10 +61,9 @@ def medai_agent(query: str) -> str:
|
|
78 |
for article_id in article_ids:
|
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='
|
85 |
f"</div>\n")
|
86 |
else:
|
87 |
results.append(f"<div class='article error'>\n"
|
@@ -99,11 +81,7 @@ def setup():
|
|
99 |
global summarizer, initialization_status
|
100 |
initialization_status = "Initializing..."
|
101 |
try:
|
102 |
-
|
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}"
|
@@ -131,7 +109,7 @@ def launch_gradio():
|
|
131 |
font-size: 1.2em;
|
132 |
margin-bottom: 5px;
|
133 |
}
|
134 |
-
.
|
135 |
font-style: italic;
|
136 |
}
|
137 |
.error-message {
|
@@ -140,7 +118,7 @@ def launch_gradio():
|
|
140 |
"""
|
141 |
|
142 |
with gr.Blocks(css=css) as iface:
|
143 |
-
gr.Markdown("# MedAI: Medical Literature Review and
|
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")
|
|
|
4 |
import os # For environment variables and file paths
|
5 |
|
6 |
# ---------------------------- Configuration ----------------------------
|
7 |
+
ENTREZ_EMAIL = os.environ.get("ENTREZ_EMAIL", "oluwafemidiakhioa@gmail.com") # 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 |
|
|
|
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:
|
|
|
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"
|
|
|
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}"
|
|
|
109 |
font-size: 1.2em;
|
110 |
margin-bottom: 5px;
|
111 |
}
|
112 |
+
.abstract {
|
113 |
font-style: italic;
|
114 |
}
|
115 |
.error-message {
|
|
|
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")
|