Spaces:
Sleeping
Sleeping
updated code ✅✅
Browse files
app.py
CHANGED
@@ -1,27 +1,40 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
4 |
-
#
|
5 |
analyzer = pipeline(
|
6 |
"image-to-text",
|
7 |
-
model="Salesforce/blip-image-captioning-base"
|
8 |
-
device="cpu"
|
9 |
)
|
10 |
|
11 |
-
def
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
26 |
|
27 |
-
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
4 |
+
# Initialize the model at startup
|
5 |
analyzer = pipeline(
|
6 |
"image-to-text",
|
7 |
+
model="Salesforce/blip-image-captioning-base"
|
|
|
8 |
)
|
9 |
|
10 |
+
def analyze_medical_image(image, question=""):
|
11 |
+
"""Analyze medical images with optional question"""
|
12 |
+
try:
|
13 |
+
if image is None:
|
14 |
+
return "⚠️ Please upload a medical image"
|
15 |
+
|
16 |
+
prompt = (
|
17 |
+
f"Question: As a radiologist, {question if question else 'describe any abnormalities in this medical scan'}. "
|
18 |
+
"Answer professionally:"
|
19 |
+
)
|
20 |
+
|
21 |
+
results = analyzer(image, prompt=prompt)
|
22 |
+
return results[0]["generated_text"].replace(prompt, "").strip()
|
23 |
+
|
24 |
+
except Exception as e:
|
25 |
+
return f"❌ Error: {str(e)}"
|
26 |
|
27 |
+
# Simple Gradio interface
|
28 |
+
demo = gr.Interface(
|
29 |
+
fn=analyze_medical_image,
|
30 |
+
inputs=[
|
31 |
+
gr.Image(type="pil", label="Upload Medical Scan"),
|
32 |
+
gr.Textbox(label="Clinical Question (optional)", placeholder="Describe symptoms...")
|
33 |
+
],
|
34 |
+
outputs=gr.Textbox(label="Analysis Report"),
|
35 |
+
title="🩺 Medical Image Analyzer",
|
36 |
+
description="Upload medical scans (X-rays, CT, MRI) for AI analysis",
|
37 |
+
allow_flagging="never"
|
38 |
+
)
|
39 |
|
40 |
+
demo.launch(show_error=True)
|