Spaces:
Sleeping
Sleeping
Update app.py
Browse files1 unmodified line2345678910111213141516171819202122232425262728293031323334353637383940414243441 unmodified lineimport gradio as grfrom huggingface_hub import InferenceClient# 1. Initialize the Client using your HF_TOKEN secretclient = InferenceClient(model="google/medgemma-4b-it",token=os.environ.get("HF_TOKEN"))def diagnose(img):if img is None:return "Please upload an image."# Detailed instruction for the medical modelprompt = "Identify the eye disease in this retinal scan and provide suggested solutions."try:# Calls the model via Hugging Face's serverless infrastructureresponse = client.visual_question_answering(image=img,question=prompt)# Format the response clearlyif isinstance(response, list) and len(response) > 0:return response[0].get('answer', "No clear diagnosis found.")return str(response)except Exception as e:return f"Error: {str(e)}. Ensure you have accepted the MedGemma license on Hugging Face."# 2. Build the Interfacedemo = gr.Interface(fn=diagnose,inputs=gr.Image(type="pil", label="Upload Eye Scan"),outputs=gr.Textbox(label="Diagnosis & Solutions"),title="👁️ Eye Disease Detector",description="Upload a fundus/retinal image for automated analysis powered by MedGemma 4B.")if __name__ == "__main__":# ssr_mode=False avoids the 'Invalid file descriptor' errordemo.launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False)1 unmodified line2345678910111213141516171819202122232425262728293031323334353637383940414243444546471 unmodified lineimport gradio as grfrom huggingface_hub import InferenceClient# Initialize clientclient = InferenceClient(model="google/medgemma-4b-it",token=os.environ.get("HF_TOKEN"))def diagnose_eye(img):if img is None:return "Please upload an image."# Hidden medical prompt for MedGemmaprompt = "Identify the eye disease in this retinal scan and provide suggested solutions."try:# Call the multimodal modelresponse = client.visual_question_answering(image=img, question=prompt)# Clean up the outputif isinstance(response, list) and len(response) > 0:return response[0].get('answer', "No diagnosis found.")return str(response)except Exception as e:err = str(e)if "403" in err or "gated" in err.lower():return "❌ Error: The API is still blocking access. Try 'Factory Rebooting' your Space settings."return f"⚠️ API Error: {err}"# Build the UIwith gr.Blocks(title="MedGemma Eye Expert") as demo:gr.Markdown("# 👁️ MedGemma Eye Expert")gr.Markdown("Upload a fundus image for automated medical analysis and solutions.")with gr.Row():input_img = gr.Image(type="pil", label="Retinal Scan")output_text = gr.Textbox(label="Diagnosis & Solutions")submit_btn = gr.Button("Analyze Scan")submit_btn.click(fn=diagnose_eye, inputs=input_img, outputs=output_text)if __name__ == "__main__":# ssr_mode=False is critical for Gradio 5 stabilitydemo.launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False)
app.py+24-21