Spaces:
Sleeping
Sleeping
Update app.py
Browse files1 unmodified line234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162631 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"))def diagnose_eye(img):if 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)1 unmodified line23456789101112131415161718192021222324252627282930313233341 unmodified lineimport gradio as grfrom huggingface_hub import InferenceClient# Use a Classic Read Token for best API compatibilityclient = InferenceClient(model="google/medgemma-4b-it",token=os.environ.get("HF_TOKEN"))def diagnose(img):if img is None: return "Please upload an image."try:# Multimodal VQA callresponse = client.visual_question_answering(image=img,question="Identify the eye disease and list solutions.")if isinstance(response, list): return response[0]['answer']return str(response)except Exception as e:return f"Error: {str(e)}"with gr.Blocks() as demo:gr.Markdown("# Eye Disease Detector")input_img = gr.Image(type="pil")output_text = gr.Textbox()btn = gr.Button("Diagnose")btn.click(diagnose, input_img, output_text)if __name__ == "__main__":# ssr_mode=False fixes 'Method not implemented'# show_error=True helps you see errors in the UI box instead of just the consoledemo.launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False, show_error=True)
app.py+17-46