gokulan006's picture
Update app.py
bb19b7d verified
import torch
import gradio as gr
from transformers import pipeline
# ✅ Force CPU if GPU is unavailable
device = 0 if torch.cuda.is_available() else -1
# ✅ Load summarization model safely
try:
summarizer = pipeline(
task="summarization",
model="facebook/bart-large-cnn",
tokenizer="facebook/bart-large-cnn",
device=device
)
except Exception as e:
summarizer = None # Prevent crashing if model fails to load
print("Error loading model:", str(e))
# ✅ Summarization function
def summarize_medical_text(text: str, summary_type: str) -> str:
if summarizer is None:
return "⚠️ Model failed to load. Try restarting the Space."
if not text.strip():
return "⚠️ Please enter valid medical text."
length_settings = {
"Abstract": (30, 100),
"Notes": (50, 150),
"Full Paper": (100, 300)
}
min_length, max_length = length_settings.get(summary_type, (50, 150))
try:
summary = summarizer(text, max_length=max_length, min_length=min_length, do_sample=False)
return summary[0]['summary_text']
except Exception as e:
return f"⚠️ Error during summarization: {str(e)}"
# ✅ Gradio App
demo = gr.Interface(
fn=summarize_medical_text,
inputs=[
gr.Textbox(lines=10, placeholder="Paste medical research paper, notes, or abstract...", label="Enter Medical Text"),
gr.Radio(["Abstract", "Notes", "Full Paper"], label="Select Summary Type", value="Abstract")
],
outputs=gr.Textbox(label="Summarized Medical Text"),
title="🔬📜 Medical Research Summarizer",
description="Summarize **medical research papers, notes, and abstracts** using AI-powered BART model.",
examples=[
["Recent advancements in telemedicine have significantly improved access to healthcare in rural areas. Telemedicine provides remote consultations, digital prescriptions, and continuous monitoring for chronic conditions like diabetes and hypertension. This study evaluates the effectiveness of telemedicine in improving patient outcomes. A randomized controlled trial was conducted with 500 patients over a period of 12 months. The results indicate a 30% reduction in hospital readmissions and a 40% increase in patient adherence to prescribed medications. Additionally, patient satisfaction scores improved significantly, with 85% of participants preferring telemedicine over in-person visits. These findings support the integration of telemedicine into primary healthcare systems to improve accessibility and efficiency in medical care.", "Abstract"],
["CRISPR technology has transformed genetic research, enabling precise gene editing by targeting specific DNA sequences. This revolutionary approach has applications in disease treatment, agriculture, and synthetic biology. CRISPR-Cas9, the most widely used variant, functions by cutting DNA at a designated site, allowing researchers to modify genetic information. Clinical trials are underway to explore CRISPR’s potential in treating genetic disorders like sickle cell anemia and cystic fibrosis. While promising, ethical concerns regarding human genome editing and unintended mutations remain topics of debate. Future research focuses on enhancing CRISPR’s precision, reducing off-target effects, and ensuring regulatory compliance for medical applications.", "Notes"],
["This study examines the effectiveness of AI-driven diagnostic tools in medical imaging. Traditional diagnostic methods often rely on radiologists to interpret X-rays, CT scans, and MRIs, which can be time-consuming and prone to human error. AI algorithms, particularly deep learning models, have demonstrated the ability to analyze medical images with high accuracy, sometimes exceeding human performance. In a clinical trial involving 1,000 patients, AI-assisted diagnosis improved detection rates of lung cancer by 15% compared to traditional methods. Additionally, AI models significantly reduced the time required for diagnosis, allowing radiologists to focus on complex cases. Despite these benefits, challenges remain, including data privacy concerns, model biases, and regulatory hurdles. As AI continues to evolve, integrating human expertise with AI systems will be crucial to achieving the best patient outcomes.", "Full Paper"]
],
theme="soft"
)
if __name__ == "__main__":
demo.launch()