Spaces:
Running
on
Zero
Running
on
Zero
Upload 3 files
Browse files- .gitattributes +1 -0
- app.py +216 -0
- images/Chest_Xray_PA_3-8-2010.png +3 -0
- requirements.txt +3 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
images/Chest_Xray_PA_3-8-2010.png filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,216 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from PIL import Image
|
4 |
+
import torch
|
5 |
+
import os
|
6 |
+
import spaces
|
7 |
+
|
8 |
+
# Initialize the model pipeline
|
9 |
+
print("Loading MedGemma model...")
|
10 |
+
pipe = pipeline(
|
11 |
+
"image-text-to-text",
|
12 |
+
model="google/medgemma-4b-it",
|
13 |
+
torch_dtype=torch.bfloat16,
|
14 |
+
device="cuda" if torch.cuda.is_available() else "cpu",
|
15 |
+
)
|
16 |
+
print("Model loaded successfully!")
|
17 |
+
|
18 |
+
@spaces.GPU()
|
19 |
+
def analyze_xray(image, custom_prompt=None):
|
20 |
+
"""
|
21 |
+
Analyze X-ray image using MedGemma model
|
22 |
+
"""
|
23 |
+
if image is None:
|
24 |
+
return "Please upload an X-ray image first."
|
25 |
+
|
26 |
+
try:
|
27 |
+
# Use custom prompt if provided, otherwise use default
|
28 |
+
if custom_prompt and custom_prompt.strip():
|
29 |
+
prompt_text = custom_prompt.strip()
|
30 |
+
else:
|
31 |
+
prompt_text = "Describe this X-ray in detail, including any abnormalities or notable findings."
|
32 |
+
|
33 |
+
messages = [
|
34 |
+
{
|
35 |
+
"role": "system",
|
36 |
+
"content": [{"type": "text", "text": "You are an expert radiologist with years of experience in interpreting medical images."}]
|
37 |
+
},
|
38 |
+
{
|
39 |
+
"role": "user",
|
40 |
+
"content": [
|
41 |
+
{"type": "text", "text": prompt_text},
|
42 |
+
{"type": "image", "image": image},
|
43 |
+
]
|
44 |
+
}
|
45 |
+
]
|
46 |
+
|
47 |
+
# Generate analysis
|
48 |
+
output = pipe(text=messages, max_new_tokens=300)
|
49 |
+
result = output[0]["generated_text"][-1]["content"]
|
50 |
+
|
51 |
+
return result
|
52 |
+
|
53 |
+
except Exception as e:
|
54 |
+
return f"Error analyzing image: {str(e)}"
|
55 |
+
|
56 |
+
def load_sample_image():
|
57 |
+
"""Load the sample X-ray image if it exists"""
|
58 |
+
sample_path = "./images/Chest_Xray_PA_3-8-2010.png"
|
59 |
+
if os.path.exists(sample_path):
|
60 |
+
return Image.open(sample_path)
|
61 |
+
return None
|
62 |
+
|
63 |
+
# Create Gradio interface
|
64 |
+
with gr.Blocks(
|
65 |
+
theme=gr.themes.Soft(),
|
66 |
+
title="AI X-ray Analysis System",
|
67 |
+
css="""
|
68 |
+
.header {
|
69 |
+
text-align: center;
|
70 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
71 |
+
color: white;
|
72 |
+
padding: 2rem;
|
73 |
+
border-radius: 10px;
|
74 |
+
margin-bottom: 2rem;
|
75 |
+
}
|
76 |
+
.warning {
|
77 |
+
background-color: #fff3cd;
|
78 |
+
border: 1px solid #ffeaa7;
|
79 |
+
border-radius: 8px;
|
80 |
+
padding: 1rem;
|
81 |
+
margin: 1rem 0;
|
82 |
+
color: #856404;
|
83 |
+
}
|
84 |
+
.gradio-container {
|
85 |
+
max-width: 1200px;
|
86 |
+
margin: auto;
|
87 |
+
}
|
88 |
+
"""
|
89 |
+
) as demo:
|
90 |
+
|
91 |
+
# Header
|
92 |
+
gr.HTML("""
|
93 |
+
<div class="header">
|
94 |
+
<h1>π©» AI X-ray Analysis System</h1>
|
95 |
+
<p>Advanced medical image analysis powered by Google's MedGemma AI</p>
|
96 |
+
</div>
|
97 |
+
""")
|
98 |
+
|
99 |
+
# Warning disclaimer
|
100 |
+
gr.HTML("""
|
101 |
+
<div class="warning">
|
102 |
+
<strong>β οΈ Medical Disclaimer:</strong> This AI tool is for educational and research purposes only.
|
103 |
+
It should not be used as a substitute for professional medical diagnosis or treatment.
|
104 |
+
Always consult qualified healthcare professionals for medical advice.
|
105 |
+
</div>
|
106 |
+
""")
|
107 |
+
|
108 |
+
with gr.Row():
|
109 |
+
with gr.Column(scale=1):
|
110 |
+
gr.Markdown("### π€ Upload X-ray Image")
|
111 |
+
|
112 |
+
# Image input
|
113 |
+
image_input = gr.Image(
|
114 |
+
label="X-ray Image",
|
115 |
+
type="pil",
|
116 |
+
height=400,
|
117 |
+
sources=["upload", "clipboard"]
|
118 |
+
)
|
119 |
+
|
120 |
+
# Sample image button
|
121 |
+
sample_btn = gr.Button(
|
122 |
+
"π Load Sample Image",
|
123 |
+
variant="secondary",
|
124 |
+
size="sm"
|
125 |
+
)
|
126 |
+
|
127 |
+
# Custom prompt input
|
128 |
+
gr.Markdown("### π¬ Custom Analysis Prompt (Optional)")
|
129 |
+
custom_prompt = gr.Textbox(
|
130 |
+
label="Custom Prompt",
|
131 |
+
placeholder="Enter specific questions about the X-ray (e.g., 'Focus on the heart area' or 'Look for signs of pneumonia')",
|
132 |
+
lines=3,
|
133 |
+
max_lines=5
|
134 |
+
)
|
135 |
+
|
136 |
+
# Analyze button
|
137 |
+
analyze_btn = gr.Button(
|
138 |
+
"π Analyze X-ray",
|
139 |
+
variant="primary",
|
140 |
+
size="lg"
|
141 |
+
)
|
142 |
+
|
143 |
+
with gr.Column(scale=1):
|
144 |
+
gr.Markdown("### π Analysis Results")
|
145 |
+
|
146 |
+
# Output text
|
147 |
+
output_text = gr.Textbox(
|
148 |
+
label="AI Analysis Report",
|
149 |
+
lines=15,
|
150 |
+
max_lines=20,
|
151 |
+
show_copy_button=True,
|
152 |
+
placeholder="Upload an X-ray image and click 'Analyze X-ray' to see the AI analysis results here..."
|
153 |
+
)
|
154 |
+
|
155 |
+
# Quick action buttons
|
156 |
+
with gr.Row():
|
157 |
+
clear_btn = gr.Button("ποΈ Clear", variant="secondary", size="sm")
|
158 |
+
copy_btn = gr.Button("π Copy Results", variant="secondary", size="sm")
|
159 |
+
|
160 |
+
# Example prompts section
|
161 |
+
gr.Markdown("### π‘ Example Prompts")
|
162 |
+
with gr.Row():
|
163 |
+
example_prompts = [
|
164 |
+
"Describe this X-ray in detail, including any abnormalities or notable findings.",
|
165 |
+
"Focus on the lung fields and identify any signs of infection or disease.",
|
166 |
+
"Examine the heart size and shape. Is the cardiac silhouette normal?",
|
167 |
+
"Look for any signs of fractures or bone abnormalities.",
|
168 |
+
"Analyze the overall image quality and positioning."
|
169 |
+
]
|
170 |
+
|
171 |
+
for i, prompt in enumerate(example_prompts):
|
172 |
+
gr.Button(
|
173 |
+
f"Example {i+1}",
|
174 |
+
size="sm"
|
175 |
+
).click(
|
176 |
+
lambda p=prompt: p,
|
177 |
+
outputs=custom_prompt
|
178 |
+
)
|
179 |
+
|
180 |
+
# Event handlers
|
181 |
+
def clear_all():
|
182 |
+
return None, "", ""
|
183 |
+
|
184 |
+
sample_btn.click(
|
185 |
+
fn=load_sample_image,
|
186 |
+
outputs=image_input
|
187 |
+
)
|
188 |
+
|
189 |
+
analyze_btn.click(
|
190 |
+
fn=analyze_xray,
|
191 |
+
inputs=[image_input, custom_prompt],
|
192 |
+
outputs=output_text
|
193 |
+
)
|
194 |
+
|
195 |
+
clear_btn.click(
|
196 |
+
fn=clear_all,
|
197 |
+
outputs=[image_input, custom_prompt, output_text]
|
198 |
+
)
|
199 |
+
|
200 |
+
# Auto-analyze when image is uploaded (optional)
|
201 |
+
image_input.change(
|
202 |
+
fn=lambda img: analyze_xray(img) if img is not None else "",
|
203 |
+
inputs=image_input,
|
204 |
+
outputs=output_text
|
205 |
+
)
|
206 |
+
|
207 |
+
# Launch the app
|
208 |
+
if __name__ == "__main__":
|
209 |
+
print("Starting Gradio interface...")
|
210 |
+
demo.launch(
|
211 |
+
server_name="0.0.0.0",
|
212 |
+
server_port=7860,
|
213 |
+
share=False, # Set to True if you want to create a public link
|
214 |
+
show_error=True,
|
215 |
+
favicon_path=None
|
216 |
+
)
|
images/Chest_Xray_PA_3-8-2010.png
ADDED
![]() |
Git LFS Details
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
spaces
|
3 |
+
pillow
|