basharat8763 commited on
Commit
8b0e56f
·
verified ·
1 Parent(s): 1e32b43

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ caption_image = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large",device=device)
10
+
11
+ narrator = pipeline("text-to-speech", model="kakao-enterprise/vits-ljs")
12
+
13
+
14
+
15
+
16
+ def generate_audio(text):
17
+ # Generate the narrated text
18
+ narrated_text = narrator(text)
19
+
20
+ # Save the audio to a WAV file
21
+ wavfile.write("output.wav", rate=narrated_text["sampling_rate"],
22
+ data=narrated_text["audio"][0])
23
+ # Return the path to the saved audio file
24
+ return "output.wav"
25
+
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
+
32
+ demo = gr.Interface(
33
+ fn=caption_my_image,
34
+ inputs=[gr.Image(label="Select Image", type="pil")],
35
+ outputs=[gr.Audio(label="Image Caption")],
36
+ title="Project 07: Image Captioning",
37
+ description="As understood from the title, if not already, this application will caption your image"
38
+ )
39
+
40
+ demo.launch()