Spaces:
Sleeping
Sleeping
| 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() | |