Spaces:
Runtime error
Runtime error
File size: 1,342 Bytes
8fe5718 fe24d04 ff7ab28 6262d5a 6b4a9a6 fe24d04 6d97bc1 6262d5a fe24d04 97599f2 5fe2fff 97599f2 6262d5a 5fe2fff 6262d5a 5fe2fff 195e4ea 5fe2fff 6262d5a ff7ab28 5fe2fff 97599f2 f757ea8 |
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 |
import gradio as gr
from transformers import BlipProcessor, BlipForConditionalGeneration
from concurrent.futures import ThreadPoolExecutor
import pyttsx3
model_id = "dblasko/blip-dalle3-img2prompt"
model = BlipForConditionalGeneration.from_pretrained(model_id)
processor = BlipProcessor.from_pretrained(model_id)
# Initialize Text-to-Speech engine
tts_engine = pyttsx3.init()
def generate_caption(image):
# Generate caption from 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 the generated caption to speech
tts_engine.save_to_file(generated_caption, "generated_audio.mp3")
tts_engine.runAndWait()
return generated_caption, "generated_audio.mp3"
# 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", None),
],
live=True # ทำให้ Gradio ทำงานแบบไม่บล็อก
)
demo.launch(share=True)
|