Spaces:
Sleeping
Sleeping
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243import osimport torchimport gradio as grfrom transformers import pipeline, BitsAndBytesConfig# 1. Get Tokentoken = os.environ.get("HF_TOKEN")# 2. Configure 4-bit loading (Essential for 16GB RAM)quant_config = BitsAndBytesConfig(load_in_4bit=True,bnb_4bit_compute_dtype=torch.float16)# 3. Load Model with Quantization# Note: Even on CPU, bitsandbytes helps manage the weight loadingpipe = pipeline("image-text-to-text",model="google/medgemma-4b-it",model_kwargs={"quantization_config": quant_config},token=token,device_map="auto")def diagnose(img):if img is None: return "Upload image", ""result = pipe(img, prompt="Diagnose this eye image and give solutions.")return result[0]['generated_text']# 4. Launch with explicit Port/Hostdemo = gr.Interface(fn=diagnose, inp`uts=gr.Image(type="pil"), outputs="text")if __name__ == "__main__":# 0.0.0.0 and 7860 are REQUIRED for Hugging Face Spaces# Change your launch line from:# demo.launch()# To this:demo.launch(server_name="0.0.0.0",server_port=7860,ssr_mode=False)12345678910111213141516171819202122232425262728293031323334353637import osimport gradio as grimport requests# Use the token from your Space SecretsHF_TOKEN = os.environ.get("HF_TOKEN")API_URL = "https://api-inference.huggingface.co/models/google/medgemma-4b-it"headers = {"Authorization": f"Bearer {HF_TOKEN}"}def diagnose(image_path):if image_path is None:return "Please upload an image."with open(image_path, "rb") as f:data = f.read()# Use the serverless API to avoid RAM crashes on Free Tierresponse = requests.post(API_URL, headers=headers, data=data)if response.status_code == 200:result = response.json()return result[0].get("generated_text", "Analysis complete.")else:return f"Error: {response.status_code}. Make sure you have access to MedGemma."# Corrected Interface Syntaxdemo = gr.Interface(fn=diagnose,inputs=gr.Image(type="filepath", label="Upload Eye Scan"),outputs=gr.Textbox(label="Diagnosis & Solutions"),title="Eye Disease Detector",description="Powered by MedGemma 4B")if __name__ == "__main__":# Disable SSR to fix the file descriptor / asyncio errordemo.launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False)
app.py+29-35