Spaces:
Runtime error
Runtime error
Commit
·
becd4ee
1
Parent(s):
850dc06
Initial MedGemma Space setup
Browse files- README.md +9 -0
- app.py +47 -0
- requirements.txt +0 -0
README.md
CHANGED
|
@@ -12,3 +12,12 @@ short_description: used for Medical image analysis
|
|
| 12 |
---
|
| 13 |
|
| 14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
---
|
| 13 |
|
| 14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 15 |
+
|
| 16 |
+
# MedGemma Space
|
| 17 |
+
|
| 18 |
+
A Hugging Face Space running the Google MedGemma 4B model for medical image analysis.
|
| 19 |
+
|
| 20 |
+
## Usage
|
| 21 |
+
|
| 22 |
+
Upload a medical image in the UI to receive AI-generated findings.
|
| 23 |
+
For API access, use the Gradio Python client or call the `/predict` endpoint.
|
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
from huggingface_hub import login
|
| 7 |
+
|
| 8 |
+
MODEL_ID = "google/medgemma-4b-pt"
|
| 9 |
+
|
| 10 |
+
# If model is gated, add token as secret in HF Space settings
|
| 11 |
+
hf_token = os.environ.get("HUGGING_FACE_HUB_TOKEN")
|
| 12 |
+
if hf_token:
|
| 13 |
+
login(token=hf_token)
|
| 14 |
+
|
| 15 |
+
def load_model():
|
| 16 |
+
device = 0 if torch.cuda.is_available() else -1
|
| 17 |
+
try:
|
| 18 |
+
return pipeline(
|
| 19 |
+
"image-text-to-text",
|
| 20 |
+
model=MODEL_ID,
|
| 21 |
+
device=device,
|
| 22 |
+
torch_dtype=torch.bfloat16
|
| 23 |
+
)
|
| 24 |
+
except:
|
| 25 |
+
return pipeline("image-text-to-text", model=MODEL_ID, device=device)
|
| 26 |
+
|
| 27 |
+
pipe = load_model()
|
| 28 |
+
|
| 29 |
+
def analyze_image(image: Image.Image):
|
| 30 |
+
if pipe is None:
|
| 31 |
+
return "Model failed to load."
|
| 32 |
+
try:
|
| 33 |
+
result = pipe(images=image, text="<start_of_image> findings:", max_new_tokens=200)
|
| 34 |
+
return result[0].get("generated_text", str(result))
|
| 35 |
+
except Exception as e:
|
| 36 |
+
return f"Error: {e}"
|
| 37 |
+
|
| 38 |
+
demo = gr.Interface(
|
| 39 |
+
fn=analyze_image,
|
| 40 |
+
inputs=gr.Image(type="pil", label="Upload image"),
|
| 41 |
+
outputs=gr.Textbox(label="AI Findings"),
|
| 42 |
+
title="MedGemma Image Analyzer",
|
| 43 |
+
description="Upload a medical image and get AI-generated findings."
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
demo.launch()
|
requirements.txt
ADDED
|
File without changes
|