Spaces:
Sleeping
Sleeping
kasper-boy
commited on
Commit
•
78f98da
1
Parent(s):
d118a19
Create PicTalker.py
Browse files- PicTalker.py +37 -0
PicTalker.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gr
|
3 |
+
from PIL import Image
|
4 |
+
import scipy.io.wavfile as wavfile
|
5 |
+
|
6 |
+
# Use a pipeline as a high-level helper
|
7 |
+
from transformers import pipeline
|
8 |
+
|
9 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
+
|
11 |
+
caption_image = pipeline("image-to-text",
|
12 |
+
model="Salesforce/blip-image-captioning-large", device=device)
|
13 |
+
|
14 |
+
narrator = pipeline("text-to-speech",
|
15 |
+
model="kakao-enterprise/vits-ljs")
|
16 |
+
|
17 |
+
def generate_audio(text):
|
18 |
+
# Generate the narrated text
|
19 |
+
narrated_text = narrator(text)
|
20 |
+
|
21 |
+
# Save the audio to a WAV file
|
22 |
+
wavfile.write("output.wav", rate=narrated_text["sampling_rate"],
|
23 |
+
data=narrated_text["audio"][0])
|
24 |
+
# Return the path to the saved audio file
|
25 |
+
return "output.wav"
|
26 |
+
|
27 |
+
|
28 |
+
def caption_my_image(pil_image):
|
29 |
+
semantics = caption_image(images=pil_image)[0]['generated_text']
|
30 |
+
return generate_audio(semantics)
|
31 |
+
|
32 |
+
demo = gr.Interface(fn=caption_my_image,
|
33 |
+
inputs=[gr.Image(label="Select Image",type="pil")],
|
34 |
+
outputs=[gr.Audio(label="Image Caption")],
|
35 |
+
title="@GenAILearniverse Project 8: Image Captioning",
|
36 |
+
description="THIS APPLICATION WILL BE USED TO CAPTION THE IMAGE.")
|
37 |
+
demo.launch()
|