Spaces:
Sleeping
Sleeping
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546123456789101112131415import gradio as gr
from transformers import pipeline
import torch
from PIL import Image
# Load the medical vision-language model
# We use bfloat16 to save memory (requires a GPU-enabled Space for best performance)
pipe = pipeline(
"image-text-to-text",
model="google/medgemma-4b-it",
torch_dtype=torch.bfloat16,
device_map="auto"
)
def diagnose_eye(img):
if img is None:
return "Please upload an image.", ""
# Hidden expert prompt
prompt = "Identify the eye condition in this fundus image. Provide a Diagnosis Label and a bulleted list of suggested Medical Solutions."
# Run inference
results = pipe(img, prompt=prompt, generate_kwargs={"max_new_tokens": 250})
full_text = results[0]['generated_text']
# Split text into Label and Solutions for the UI
if "Solutions:" in full_text:
parts = full_text.split("Solutions:")
return parts[0].strip(), parts[1].strip()
return full_text, "Consult a specialist for detailed solutions."
# Build the Minimalist Interface
demo = gr.Interface(
fn=diagnose_eye,
inputs=gr.Image(type="pil", label="Upload Eye Scan"),
outputs=[
gr.Textbox(label="Diagnosis Label"),
gr.Textbox(label="Suggested Solutions")
],
title="MedGemma Eye Expert",
description="Upload a retinal/fundus image for automated medical analysis. Research Use Only.",
allow_flagging="never"
)
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860)import osfrom transformers import pipelineimport torch
# This automatically pulls the secret you added in Step 2token = os.environ.get("HF_TOKEN")
# Load the model using the token for authenticationpipe = pipeline( "image-text-to-text", model="google/medgemma-4b-it", torch_dtype=torch.bfloat16, token=token, # <--- THIS IS THE FIX device_map="auto")app.py+15-46