Spaces:
Sleeping
Sleeping
Update app.py
Browse files12345678791011101213141516171819162021221923242526272829303132333435import osimport torchimport gradio as grfrom transformers import pipeline, BitsAndBytesConfig
# 1. Get Tokentoken = os.environ.get("HF_TOKEN")
# This config shrinks the model so it fits on free hardware# 2. Configure 4-bit loading (Essential for 16GB RAM)quant_config = BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_compute_dtype=torch.bfloat16 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}, # <--- Use 4-bit quantization 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, inputs=gr.Image(type="pil"), outputs="text")
if __name__ == "__main__": # 0.0.0.0 and 7860 are REQUIRED for Hugging Face Spaces demo.launch(server_name="0.0.0.0", server_port=7860)app.py+20-4