blackshadow1 commited on
Commit
fd2ed4d
·
verified ·
1 Parent(s): c3c8396

updated code ✅✅

Browse files
Files changed (1) hide show
  1. app.py +31 -18
app.py CHANGED
@@ -1,27 +1,40 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # Load a pre-configured medical analysis pipeline
5
  analyzer = pipeline(
6
  "image-to-text",
7
- model="Salesforce/blip-image-captioning-base",
8
- device="cpu"
9
  )
10
 
11
- def analyze(image, question):
12
- prompt = f"As a radiologist, {question}" if question else "Describe this medical scan professionally"
13
- result = analyzer(image, prompt=prompt)
14
- return result[0]["generated_text"]
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- with gr.Blocks() as app:
17
- gr.Markdown("# 🩺 Medical Image Analyzer")
18
- with gr.Row():
19
- with gr.Column():
20
- img_input = gr.Image(type="pil", label="Upload Scan")
21
- question = gr.Textbox(label="Question (optional)")
22
- btn = gr.Button("Analyze")
23
- with gr.Column():
24
- output = gr.Textbox(label="Diagnosis")
25
- btn.click(analyze, [img_input, question], output)
 
 
26
 
27
- app.launch()
 
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)