Spaces:
Sleeping
Sleeping
Update app.py
Browse files1 unmodified line23456783 unmodified lines1213141516171819202122232425262728293031323334353637383940414243444546471 unmodified lineimport gradio as grfrom huggingface_hub import InferenceClient# Initialize clientclient = InferenceClient(model="google/medgemma-4b-it",token=os.environ.get("HF_TOKEN")3 unmodified linesif 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)1 unmodified line234567893 unmodified lines1314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162631 unmodified lineimport gradio as grfrom huggingface_hub import InferenceClient# 1. Initialize Client with MedGemma 4B# Ensure your HF_TOKEN is a 'Classic Read' token in Space Secretsclient = InferenceClient(model="google/medgemma-4b-it",token=os.environ.get("HF_TOKEN")3 unmodified linesif img is None:return "Please upload an image."# Instruction-tuned prompt for medical accuracyprompt = "Identify the eye disease in this retinal scan and list 3 recommended medical solutions."try:# MedGemma 4B IT is a multimodal model# The client handles image encoding and gated authentication automaticallyresponse = client.visual_question_answering(image=img,question=prompt)# Format the result based on the API's returned listif isinstance(response, list) and len(response) > 0:return response[0].get('answer', "Analysis complete, but no specific label found.")return str(response)except Exception as e:error_msg = str(e)# Detailed user-friendly error messagesif "403" in error_msg or "gated" in error_msg.lower():return "❌ Access Denied: Please check if your HF_TOKEN is correct and you have accepted the MedGemma license."if "loading" in error_msg.lower() or "503" in error_msg:return "⏳ The model is currently loading on Hugging Face servers. Please retry in 60 seconds."return f"⚠️ Error: {error_msg}"# 3. Create a clean, minimalist UIwith gr.Blocks(title="MedGemma Eye Expert") as demo:gr.Markdown("# 👁️ MedGemma Eye Expert")gr.Markdown("Drop a fundus/retinal image below for an AI-powered medical diagnosis and solution set.")with gr.Row():with gr.Column():input_img = gr.Image(type="pil", label="Upload Retinal Image")submit_btn = gr.Button("Diagnose Problem", variant="primary")with gr.Column():output_text = gr.Textbox(label="Diagnosis & Solutions", lines=10)gr.HTML("<p style='color: gray; font-size: 0.8em;'>Note: This tool is for research purposes. Always consult a licensed ophthalmologist.</p>")submit_btn.click(fn=diagnose_eye, inputs=input_img, outputs=output_text)# 4. Launch with SSR disabled to prevent 'Invalid File Descriptor' errorsif __name__ == "__main__":demo.launch(server_name="0.0.0.0",server_port=7860,ssr_mode=False)
app.py+34-18