Spaces:
Sleeping
Sleeping
Update app.py
Browse files1 unmodified line2345567893 unmodified lines13141515161617181919202021222324252622272824293031322829303133343536373839403341424336444546394047484950515253544255565758464759606162631 unmodified lineimport gradio as grfrom huggingface_hub import InferenceClient
# Initialize client# 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 lines if img is None: return "Please upload an image." # Hidden medical prompt for MedGemma prompt = "Identify the eye disease in this retinal scan and provide suggested solutions." # Instruction-tuned prompt for medical accuracy prompt = "Identify the eye disease in this retinal scan and list 3 recommended medical solutions." try: # Call the multimodal model response = client.visual_question_answering(image=img, question=prompt) # MedGemma 4B IT is a multimodal model # The client handles image encoding and gated authentication automatically response = client.visual_question_answering( image=img, question=prompt ) # Clean up the output # Format the result based on the API's returned list if isinstance(response, list) and len(response) > 0: return response[0].get('answer', "No diagnosis found.") return response[0].get('answer', "Analysis complete, but no specific label 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}" error_msg = str(e) # Detailed user-friendly error messages if "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}"
# Build the UI# 3. Create a clean, minimalist 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.") gr.Markdown("Drop a fundus/retinal image below for an AI-powered medical diagnosis and solution set.") with gr.Row(): input_img = gr.Image(type="pil", label="Retinal Scan") output_text = gr.Textbox(label="Diagnosis & Solutions") 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 = gr.Button("Analyze Scan") 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__": # ssr_mode=False is critical for Gradio 5 stability demo.launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False) demo.launch( server_name="0.0.0.0", server_port=7860, ssr_mode=False )app.py+34-18