Spaces:
Sleeping
Sleeping
Update app.py
Browse files1 unmodified line234567893 unmodified lines1314151617181920212223242526272829303132333435363738391 unmodified lineimport gradio as grfrom huggingface_hub import InferenceClient# 1. Initialize the Client with your token# Make sure HF_TOKEN is in your Space's 'Secrets'client = InferenceClient(model="google/medgemma-4b-it",token=os.environ.get("HF_TOKEN")3 unmodified linesif img is None:return "Please upload an image."# Hidden expert promptprompt = "Identify the eye disease in this retinal scan and provide suggested solutions."try:# The new client handles the API call and image encoding automaticallyresponse = client.visual_question_answering(image=img,question=prompt)# Note: MedGemma might return different formats;# usually it's a direct string or a list of answers.if isinstance(response, list):return response[0]['answer']return responseexcept Exception as e:return f"Error connecting to MedGemma: {str(e)}"# Minimalist Interfacedemo = gr.Interface(fn=diagnose,inputs=gr.Image(type="pil", label="Upload Eye Scan"),outputs=gr.Textbox(label="Diagnosis & Solutions"),title="MedGemma Eye Expert",description="Automated analysis via1 unmodified line23456783 unmodified lines1213141516171819202122232425262728293031323334353637383940414243441 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")3 unmodified linesif 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)
app.py+18-13