Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,39 @@
|
|
1 |
-
import gradio as gr
|
2 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
3 |
-
|
4 |
-
|
5 |
|
6 |
model_id = "dblasko/blip-dalle3-img2prompt"
|
7 |
model = BlipForConditionalGeneration.from_pretrained(model_id)
|
8 |
processor = BlipProcessor.from_pretrained(model_id)
|
9 |
|
10 |
def generate_caption(image):
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
# Create a Gradio interface with an image input and a textbox output
|
22 |
-
demo = gr.Interface(fn=generate_caption, inputs=gr.Image(), outputs=gr.Textbox(label="Generated caption"))
|
|
|
1 |
+
: A commit has happened since. Please refresh and try again.import gradio as gr
|
2 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
3 |
+
from gtts import gTTS
|
4 |
+
import IPython.display as ipd
|
5 |
|
6 |
model_id = "dblasko/blip-dalle3-img2prompt"
|
7 |
model = BlipForConditionalGeneration.from_pretrained(model_id)
|
8 |
processor = BlipProcessor.from_pretrained(model_id)
|
9 |
|
10 |
def generate_caption(image):
|
11 |
+
inputs = processor(images=image, return_tensors="pt")
|
12 |
+
pixel_values = inputs.pixel_values
|
13 |
+
|
14 |
+
generated_ids = model.generate(pixel_values=pixel_values, max_length=50)
|
15 |
+
generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True, temperature=0.8, top_k=40, top_p=0.9)[0]
|
16 |
+
|
17 |
+
# Convert text to speech and save as audio file
|
18 |
+
tts = gTTS(text=generated_caption, lang='en')
|
19 |
+
audio_path = "generated_audio.mp3"
|
20 |
+
tts.save(audio_path)
|
21 |
+
|
22 |
+
return generated_caption, audio_path
|
23 |
+
|
24 |
+
def play_audio(audio_path):
|
25 |
+
# Display an audio player
|
26 |
+
return ipd.Audio(audio_path)
|
27 |
+
|
28 |
+
# Create a Gradio interface with an image input, a textbox output, a button, and an audio player
|
29 |
+
demo = gr.Interface(
|
30 |
+
fn=generate_caption,
|
31 |
+
inputs=gr.Image(),
|
32 |
+
outputs=[
|
33 |
+
gr.Textbox(label="Generated caption"),
|
34 |
+
gr.Button("Convert to Audio", play_audio, type="button"),
|
35 |
+
gr.Audio("audio", type="player")
|
36 |
+
]
|
37 |
+
)
|
38 |
+
demo.launch()
|
39 |
|
|
|
|