File size: 1,828 Bytes
034b2f2
2f5c91e
034b2f2
 
 
2f5c91e
 
034b2f2
 
 
 
 
2f5c91e
034b2f2
 
 
fee2e0a
 
 
 
 
2f5c91e
034b2f2
 
 
fee2e0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
034b2f2
 
 
 
 
fa5cf58
fee2e0a
fa5cf58
 
fee2e0a
034b2f2
 
2f5c91e
034b2f2
c11c555
 
 
2f5c91e
 
c11c555
034b2f2
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# app.py
import gradio as gr
from transformers import BlipProcessor, BlipForConditionalGeneration
from gtts import gTTS
import io
from PIL import Image

# -------------------------------
# Load BLIP-base model (lighter version)
# -------------------------------
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")

# -------------------------------
# Generate caption function
# -------------------------------
# def generate_caption_tts(image):
#     caption = generate_caption(model, processor, image)
#     audio_file = text_to_audio_file(caption)
#     return caption, audio_file  # return file path, not BytesIO


# -------------------------------
# Convert text to speech using gTTS
# -------------------------------
import tempfile
import pyttsx3

def text_to_audio_file(text):
    # Create a temporary file
    tmp_file = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
    tmp_path = tmp_file.name
    tmp_file.close()

    engine = pyttsx3.init()
    engine.save_to_file(text, tmp_path)
    engine.runAndWait()

    return tmp_path


# -------------------------------
# Gradio interface: Caption + Audio
# -------------------------------
def generate_caption_tts(image):
    caption = generate_caption_from_image(model, processor, image)  # uses global model/processor
    audio_file = text_to_audio_file(caption)
    return caption, audio_file



interface = gr.Interface(
    fn=generate_caption_tts,
    inputs=gr.Image(type="numpy"),
    outputs=[gr.Textbox(label="Generated Caption"), gr.Audio(type="filepath", label="TTS Audio")],
    title="Image Captioning for Visually Impaired",
    description="Upload an image, get a caption and audio description."
)


interface.launch()