File size: 1,339 Bytes
aa5fa68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
360f58b
 
aa5fa68
 
 
 
 
 
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
import torch
import gradio as gr
from PIL import Image
import scipy.io.wavfile as wavfile
from transformers import pipeline

# Set device for processing
device = "cuda" if torch.cuda.is_available() else "cpu"

# Load models
caption_image = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large", device=device)
narrator = pipeline("text-to-speech", model="kakao-enterprise/vits-ljs")

def generate_audio(text):
    """Generate audio narration from text."""
    narrated_text = narrator(text)
    wavfile.write("output.wav", rate=narrated_text["sampling_rate"], data=narrated_text["audio"][0])
    return "output.wav"

def caption_my_image(pil_image):
    """Generate caption for the image and convert it to audio."""
    semantics = caption_image(images=pil_image)[0]['generated_text']
    return generate_audio(semantics)

# Define the Gradio interface
demo = gr.Interface(
    fn=caption_my_image,
    inputs=[gr.Image(label="Upload Your Image", type="pil")],
    outputs=[gr.Audio(label="Generated Audio Caption")],
    title="Image Captioning and Narration",
    description=(
        "Upload an image to generate a descriptive caption and listen to its narration.\n"
        "This app is brought to you by **Taizun**."
    ),
    theme="compact"  # Use a minimalistic theme
)

# Launch the application
demo.launch()