Spaces:
Paused
Paused
Upload 3 files
Browse files- .gitattributes +1 -0
- app.py +235 -0
- images/Infection.jpg +3 -0
- requirements.txt +6 -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/Infection.jpg filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
import time
|
| 8 |
+
import os
|
| 9 |
+
from huggingface_hub import login
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# Access the secret token
|
| 13 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 14 |
+
|
| 15 |
+
# Login to Hugging Face Hub
|
| 16 |
+
login(token=hf_token)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Initialize the model pipeline
|
| 20 |
+
print("Loading MedGemma model...")
|
| 21 |
+
pipe = pipeline(
|
| 22 |
+
"image-text-to-text",
|
| 23 |
+
model="google/medgemma-4b-it",
|
| 24 |
+
torch_dtype=torch.bfloat16,
|
| 25 |
+
device="cuda" if torch.cuda.is_available() else "cpu",
|
| 26 |
+
# device_map="auto",
|
| 27 |
+
)
|
| 28 |
+
print("Model loaded successfully!")
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
@spaces.GPU(duration=300)
|
| 32 |
+
def analyze_img(image, custom_prompt=None):
|
| 33 |
+
"""
|
| 34 |
+
Analyze image using MedGemma model
|
| 35 |
+
"""
|
| 36 |
+
if image is None:
|
| 37 |
+
return "Please upload an image first."
|
| 38 |
+
|
| 39 |
+
try:
|
| 40 |
+
# System prompt for the model
|
| 41 |
+
system_prompt_text = """You are a expert medical AI assistant with years of experience in interpreting medical images. Your purpose is to assist qualified clinicians by providing an detailed analysis of the provided medical image."""
|
| 42 |
+
# Use custom prompt if provided, otherwise use default
|
| 43 |
+
if custom_prompt and custom_prompt.strip():
|
| 44 |
+
prompt_text = custom_prompt.strip()
|
| 45 |
+
else:
|
| 46 |
+
prompt_text = "Describe this image in detail, including any abnormalities or notable findings."
|
| 47 |
+
|
| 48 |
+
messages = [
|
| 49 |
+
{
|
| 50 |
+
"role": "system",
|
| 51 |
+
"content": [
|
| 52 |
+
{
|
| 53 |
+
"type": "text",
|
| 54 |
+
"text": system_prompt_text,
|
| 55 |
+
}
|
| 56 |
+
],
|
| 57 |
+
},
|
| 58 |
+
{
|
| 59 |
+
"role": "user",
|
| 60 |
+
"content": [
|
| 61 |
+
{"type": "text", "text": prompt_text},
|
| 62 |
+
{"type": "image", "image": image},
|
| 63 |
+
],
|
| 64 |
+
},
|
| 65 |
+
]
|
| 66 |
+
|
| 67 |
+
# Generate analysis
|
| 68 |
+
output = pipe(text=messages, max_new_tokens=1024)
|
| 69 |
+
full_response = output[0]["generated_text"][-1]["content"]
|
| 70 |
+
|
| 71 |
+
partial_message = ""
|
| 72 |
+
for char in full_response:
|
| 73 |
+
partial_message += char
|
| 74 |
+
time.sleep(0.01) # Add a small delay to make the typing visible
|
| 75 |
+
yield partial_message
|
| 76 |
+
|
| 77 |
+
except Exception as e:
|
| 78 |
+
return f"Error analyzing image: {str(e)}"
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def load_sample_image():
|
| 82 |
+
"""Load the sample image if it exists"""
|
| 83 |
+
sample_path = "./images/Infection.jpg"
|
| 84 |
+
if os.path.exists(sample_path):
|
| 85 |
+
return Image.open(sample_path)
|
| 86 |
+
return None
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
# Create Gradio interface
|
| 90 |
+
with gr.Blocks(
|
| 91 |
+
theme=gr.themes.Citrus(),
|
| 92 |
+
title="MedGemma",
|
| 93 |
+
css="""
|
| 94 |
+
.header {
|
| 95 |
+
text-align: center;
|
| 96 |
+
background: linear-gradient(135deg, #f5af19 0%, #f12711 100%);
|
| 97 |
+
color: white;
|
| 98 |
+
padding: 2rem;
|
| 99 |
+
border-radius: 10px;
|
| 100 |
+
margin-bottom: 2rem;
|
| 101 |
+
}
|
| 102 |
+
.warning {
|
| 103 |
+
background-color: #fff0e6;
|
| 104 |
+
border: 3px solid #ffab73;
|
| 105 |
+
border-radius: 8px;
|
| 106 |
+
padding: 1rem;
|
| 107 |
+
margin: 1rem 0;
|
| 108 |
+
color: #8c2b00;
|
| 109 |
+
}
|
| 110 |
+
.gradio-container {
|
| 111 |
+
max-width: 1200px;
|
| 112 |
+
margin: auto;
|
| 113 |
+
}
|
| 114 |
+
.warning strong{
|
| 115 |
+
color: inherit;
|
| 116 |
+
}
|
| 117 |
+
""",
|
| 118 |
+
) as demo:
|
| 119 |
+
|
| 120 |
+
# Header
|
| 121 |
+
gr.HTML(
|
| 122 |
+
"""
|
| 123 |
+
<div class="header">
|
| 124 |
+
<h1> MedGemma Medical Image Analysis and QnA</h1>
|
| 125 |
+
<p>Advanced medical image analysis powered by Google's MedGemma</p>
|
| 126 |
+
</div>
|
| 127 |
+
"""
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
# Warning disclaimer
|
| 131 |
+
gr.HTML(
|
| 132 |
+
"""
|
| 133 |
+
<div class="warning">
|
| 134 |
+
<strong> Medical Disclaimer:</strong> This model is for educational and research purposes only.
|
| 135 |
+
It should not be used as a substitute for professional medical diagnosis or treatment.
|
| 136 |
+
Always consult qualified healthcare professionals for medical advice.
|
| 137 |
+
</div>
|
| 138 |
+
"""
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
with gr.Row():
|
| 142 |
+
with gr.Column(scale=1):
|
| 143 |
+
gr.Markdown("### π€ Upload Medical Image (Radiology, Pathology, Dermatology, CT, X-Ray)")
|
| 144 |
+
|
| 145 |
+
# Image input
|
| 146 |
+
image_input = gr.Image(label="Input Image", type="pil", height=400, sources=["upload", "clipboard"])
|
| 147 |
+
|
| 148 |
+
# Sample image button
|
| 149 |
+
sample_btn = gr.Button("π Load Sample Image", variant="secondary", size="sm")
|
| 150 |
+
|
| 151 |
+
# Custom prompt input
|
| 152 |
+
gr.Markdown("### π¬ Custom Analysis Prompt (Optional)")
|
| 153 |
+
custom_prompt = gr.Textbox(
|
| 154 |
+
label="Custom Prompt",
|
| 155 |
+
placeholder="Enter specific questions about the Image (e.g., 'Focus on the heart area' or 'Look for signs of pneumonia')",
|
| 156 |
+
value="Describe this Image and Generate a compact Clinical report",
|
| 157 |
+
lines=3,
|
| 158 |
+
max_lines=5,
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
# Analyze button
|
| 162 |
+
analyze_btn = gr.Button("π Analyze Image", variant="primary", size="lg")
|
| 163 |
+
|
| 164 |
+
with gr.Column(scale=1):
|
| 165 |
+
gr.Markdown("### π Analysis Report")
|
| 166 |
+
|
| 167 |
+
# Output text
|
| 168 |
+
output_text = gr.Textbox(
|
| 169 |
+
label="Generated Report",
|
| 170 |
+
lines=28,
|
| 171 |
+
max_lines=1024,
|
| 172 |
+
show_label=False,
|
| 173 |
+
show_copy_button=False,
|
| 174 |
+
placeholder="Upload an X-ray image or CT scan or any othe medical image and click 'Analyze Image' to see the AI analysis results here...",
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
# Quick action buttons
|
| 178 |
+
with gr.Row():
|
| 179 |
+
clear_btn = gr.Button("ποΈ Clear", variant="secondary", size="sm")
|
| 180 |
+
copy_btn = gr.Button("π Copy Results", variant="secondary", size="sm")
|
| 181 |
+
|
| 182 |
+
# Example prompts section
|
| 183 |
+
gr.Markdown("### π‘ Example Prompts")
|
| 184 |
+
with gr.Row():
|
| 185 |
+
example_prompts = [
|
| 186 |
+
"Describe this X-ray in detail, including any abnormalities or notable findings.",
|
| 187 |
+
"Describe the morphology of this skin lesion, focusing on color, border, and texture.",
|
| 188 |
+
"What are the key histological features visible in this tissue sample?",
|
| 189 |
+
"Look for any signs of fractures or bone abnormalities.",
|
| 190 |
+
"Analyze this fundus image and describe the condition of the optic disc and vasculature.",
|
| 191 |
+
]
|
| 192 |
+
|
| 193 |
+
for i, prompt in enumerate(example_prompts):
|
| 194 |
+
gr.Button(f"Example {i+1}", size="sm").click(lambda p=prompt: p, outputs=custom_prompt)
|
| 195 |
+
|
| 196 |
+
# Event handlers
|
| 197 |
+
def clear_all():
|
| 198 |
+
return None, "", ""
|
| 199 |
+
|
| 200 |
+
sample_btn.click(fn=load_sample_image, outputs=image_input)
|
| 201 |
+
|
| 202 |
+
analyze_btn.click(fn=analyze_img, inputs=[image_input, custom_prompt], outputs=output_text)
|
| 203 |
+
|
| 204 |
+
clear_btn.click(fn=clear_all, outputs=[image_input, custom_prompt, output_text])
|
| 205 |
+
|
| 206 |
+
copy_btn.click(
|
| 207 |
+
fn=None, # No Python function needed for this client-side action
|
| 208 |
+
inputs=[output_text],
|
| 209 |
+
js="""
|
| 210 |
+
(text_to_copy) => {
|
| 211 |
+
if (text_to_copy) {
|
| 212 |
+
navigator.clipboard.writeText(text_to_copy);
|
| 213 |
+
alert("Results copied to clipboard!");
|
| 214 |
+
} else {
|
| 215 |
+
alert("Nothing to copy!");
|
| 216 |
+
}
|
| 217 |
+
}
|
| 218 |
+
""",
|
| 219 |
+
)
|
| 220 |
+
|
| 221 |
+
# Auto-analyze when image is uploaded (optional)
|
| 222 |
+
image_input.change(
|
| 223 |
+
fn=lambda img: analyze_img(img) if img is not None else "", inputs=image_input, outputs=output_text
|
| 224 |
+
)
|
| 225 |
+
|
| 226 |
+
# Launch the app
|
| 227 |
+
if __name__ == "__main__":
|
| 228 |
+
print("Starting Gradio interface...")
|
| 229 |
+
demo.launch(
|
| 230 |
+
server_name="0.0.0.0",
|
| 231 |
+
server_port=7860,
|
| 232 |
+
share=False, # Set to True if you want to create a public link
|
| 233 |
+
show_error=True,
|
| 234 |
+
favicon_path=None,
|
| 235 |
+
)
|
images/Infection.jpg
ADDED
|
Git LFS Details
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
Pillow
|
| 3 |
+
spaces
|
| 4 |
+
torch
|
| 5 |
+
accelerate
|
| 6 |
+
bitsandbytes
|