prograk commited on
Commit
c7004b8
·
verified ·
1 Parent(s): 760a2c3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # model_path = "../Models/models--Salesforce--blip-image-captioning-base/snapshots/82a37760796d32b1411fe092ab5d4e227313294b"
10
+ # model_path2 = "../Models/models--kakao-enterprise--vits-ljs/snapshots/3bcb8321394f671bd948ebf0d086d694dda95464"
11
+
12
+ device = "cuda" if torch.cuda.is_available() else "cpu"
13
+
14
+ # caption_image = pipeline("image-to-text", model=model_path, device=device)
15
+ caption_image = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base", device=device)
16
+
17
+ # narrator = pipeline("text-to-speech", model=model_path2)
18
+ narrator = pipeline("text-to-speech", model="kakao-enterprise/vits-ljs")
19
+
20
+ def generate_audio(text):
21
+ # Generate the narrated text
22
+ narrated_text = narrator(text)
23
+
24
+ # Save the audio to a WAV file
25
+ wavfile.write("output.wav", rate=narrated_text["sampling_rate"],
26
+ data=narrated_text["audio"][0])
27
+
28
+ # Return the path to the saved audio file
29
+ return "output.wav"
30
+
31
+ def caption_my_image(pil_image):
32
+ semantics = caption_image(images=pil_image)[0]['generated_text']
33
+ return generate_audio(semantics)
34
+
35
+ gr.close_all()
36
+
37
+ demo = gr.Interface(fn=caption_my_image,
38
+ inputs=[gr.Image(label="Select Image",type="pil")],
39
+ outputs=[gr.Audio(label="Generated Caption")],
40
+ title="@GenAILearniverse Project 8: Image Captioning",
41
+ description="THIS APPLICATION WILL BE USED TO GET THE AUDIO CAPTION OF IMAGE.")
42
+
43
+ demo.launch()