File size: 1,293 Bytes
becd4ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
from PIL import Image
import gradio as gr
import torch
from transformers import pipeline
from huggingface_hub import login

MODEL_ID = "google/medgemma-4b-pt"

# If model is gated, add token as secret in HF Space settings
hf_token = os.environ.get("HUGGING_FACE_HUB_TOKEN")
if hf_token:
    login(token=hf_token)

def load_model():
    device = 0 if torch.cuda.is_available() else -1
    try:
        return pipeline(
            "image-text-to-text",
            model=MODEL_ID,
            device=device,
            torch_dtype=torch.bfloat16
        )
    except:
        return pipeline("image-text-to-text", model=MODEL_ID, device=device)

pipe = load_model()

def analyze_image(image: Image.Image):
    if pipe is None:
        return "Model failed to load."
    try:
        result = pipe(images=image, text="<start_of_image> findings:", max_new_tokens=200)
        return result[0].get("generated_text", str(result))
    except Exception as e:
        return f"Error: {e}"

demo = gr.Interface(
    fn=analyze_image,
    inputs=gr.Image(type="pil", label="Upload image"),
    outputs=gr.Textbox(label="AI Findings"),
    title="MedGemma Image Analyzer",
    description="Upload a medical image and get AI-generated findings."
)

if __name__ == "__main__":
    demo.launch()