File size: 901 Bytes
2f5c91e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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()