Spaces:
Sleeping
Sleeping
Download app.py from gopalagra/blind-image-captioning: direct link, hf CLI and curl.
- Browser
- Download file 901 Bytes
-
https://huggingface.co/spaces/gopalagra/blind-image-captioning/resolve/c3798267d9279b2390611898a91969d24c491726/app.py
- Command line
-
hf download hf://spaces/gopalagra/blind-image-captioning@c3798267d9279b2390611898a91969d24c491726/app.py
-
curl -L -o app.py https://huggingface.co/spaces/gopalagra/blind-image-captioning/resolve/c3798267d9279b2390611898a91969d24c491726/app.py
901 Bytes
| import gradio as gr | |
| from transformers import pipeline | |
| import requests | |
| from io import BytesIO | |
| from PIL import Image | |
| import pyttsx3 # Text-to-speech (optional) | |
| # --- Load hosted model --- | |
| captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base") | |
| # --- Caption & TTS function --- | |
| def generate_caption_tts(image): | |
| # If user uploads a URL | |
| if isinstance(image, str): | |
| image = Image.open(BytesIO(requests.get(image).content)) | |
| caption = captioner(image)[0]['generated_text'] | |
| # TTS (optional) | |
| tts = pyttsx3.init() | |
| tts.say(caption) | |
| tts.runAndWait() | |
| return caption | |
| # --- Gradio interface --- | |
| iface = gr.Interface( | |
| fn=generate_caption_tts, | |
| inputs=gr.Image(type="pil"), | |
| outputs="text", | |
| title="Image Captioning for Visually Impaired", | |
| description="Upload any image and get a descriptive caption." | |
| ) | |
| iface.launch() | |