Spaces:
Runtime error
Runtime error
import gradio as gr | |
from transformers import BlipProcessor, BlipForConditionalGeneration | |
from gtts import gTTS | |
import pygame | |
model_id = "dblasko/blip-dalle3-img2prompt" | |
model = BlipForConditionalGeneration.from_pretrained(model_id) | |
processor = BlipProcessor.from_pretrained(model_id) | |
def generate_caption(image): | |
inputs = processor(images=image, return_tensors="pt") | |
pixel_values = inputs.pixel_values | |
generated_ids = model.generate(pixel_values=pixel_values, max_length=50) | |
generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True, temperature=0.8, top_k=40, top_p=0.9)[0] | |
# Convert text to speech and save as audio file | |
tts = gTTS(text=generated_caption, lang='en') | |
audio_path = "generated_audio.mp3" | |
tts.save(audio_path) | |
return generated_caption, audio_path | |
def play_audio(audio_path): | |
pygame.mixer.init() | |
pygame.mixer.music.load(audio_path) | |
pygame.mixer.music.play() | |
pygame.event.wait() | |
# Create a Gradio interface with an image input, a textbox output, a button, and an audio player | |
demo = gr.Interface( | |
fn=generate_caption, | |
inputs=gr.Image(), | |
outputs=[ | |
gr.Textbox(label="Generated caption"), | |
gr.Button("Convert to Audio", play_audio, type="button"), | |
gr.Audio("audio", type="player") | |
] | |
) | |
demo.launch() | |