kasper-boy commited on
Commit
2046ce1
1 Parent(s): 6701e91

Create PicTalker.py

Browse files
Files changed (1) hide show
  1. PicTalker.py +36 -0
PicTalker.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ def caption_my_image(pil_image):
28
+ semantics = caption_image(images=pil_image)[0]['generated_text']
29
+ return generate_audio(semantics)
30
+
31
+ demo = gr.Interface(fn=caption_my_image,
32
+ inputs=[gr.Image(label="Select Image", type="pil")],
33
+ outputs=[gr.Audio(label="Image Caption")],
34
+ title="PicTalker | ImageNarrator | SnapSpeech | SpeakScene",
35
+ description="Turn photos into phonetic wonders with audio captions.")
36
+ demo.launch()