ajibs75 commited on
Commit
3fc93cc
·
verified ·
1 Parent(s): cd72a9f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -0
app.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use a pipeline as a high-level helper
2
+ import torch
3
+ from transformers import pipeline
4
+ from scipy.io import wavfile
5
+ from PIL import Image
6
+ import gradio as gr
7
+
8
+
9
+ device = "cuda" if torch.cuda.is_available() else "cpu"
10
+ image_pipe = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large",device=device)
11
+ narator = pipeline("text-to-speech", model="kakao-enterprise/vits-ljs",device=device)
12
+
13
+ def generate_audio(text):
14
+ # generate the audio from the text
15
+ audio_text = narator(text)
16
+ # save the audio to a WAV file
17
+ wavfile.write(filename="audio.wav",
18
+ rate=audio_text['sampling_rate'],
19
+ data=audio_text['audio'][0])
20
+ return "audio.wav"
21
+
22
+
23
+ def caption_my_image(image_path):
24
+ image = image_pipe(image_path)
25
+ caption_text = image[0]['generated_text']
26
+ return generate_audio(caption_text)
27
+
28
+
29
+ demo = gr.Interface(fn=caption_my_image,
30
+ inputs=[gr.Image(label="Image",type="pil")],
31
+ outputs=[gr.Audio(label="Image Caption")],
32
+ title="@SmartChoiceLearningHubs HF project 1 :Image to Text to Speech",
33
+ description="This app generates a caption for an image and converts the caption to speech.")
34
+
35
+ demo.launch()