Spaces:
Sleeping
Sleeping
File size: 2,149 Bytes
5657a0a 5e60d7c 5657a0a 5e60d7c 5657a0a 5e60d7c 5657a0a | 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 | import torch
import gradio as gr
from PIL import Image
import scipy.io.wavfile as wavfile
# Use a pipeline as a high-level helper
from transformers import pipeline
device = "cuda" if torch.cuda.is_available() else "cpu"
# Initialize pipelines
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 the narrated text without max_new_tokens
narrated_text = narrator(text)
# Save the audio to a WAV file
audio_path = "output.wav"
wavfile.write(audio_path, rate=narrated_text["sampling_rate"], data=narrated_text["audio"][0])
return audio_path
def truncate_text(text, max_length=200):
return text[:max_length] + '...' if len(text) > max_length else text
def caption_my_image(pil_image):
# Generate the caption from the image
semantics = caption_image(images=pil_image)[0]['generated_text']
# Optionally truncate the text
truncated_semantics = truncate_text(semantics, max_length=200)
# Generate the corresponding audio
audio_path = generate_audio(truncated_semantics)
return semantics, audio_path
# Gradio interface
demo = gr.Interface(
fn=caption_my_image,
inputs=[gr.Image(label="πΈ Select Image", type="pil")],
outputs=[gr.Textbox(label="π Generated Caption"), gr.Audio(label="π Audio Caption")],
title="πΌοΈ SM Project: Image Captioning",
description=(
"β¨ **Welcome to the Image Captioning App!** β¨\n\n"
"This application will allow you to:\n"
"1. **Upload an Image** π·\n"
"2. **Generate a Caption** π\n"
"3. **Listen to the Audio Caption** π\n\n"
"π **Instructions**:\n"
"1. Click on the 'Select Image' button to upload an image.\n"
"2. Wait for the application to generate a caption for your image.\n"
"3. Enjoy the narrated audio of the caption!\n\n"
"π **Let's get started!** π\n\n"
"ποΈ **Created by SHASHWAT MISHRA**"
),
)
# Launch the demo
demo.launch()
|