mayankchugh-learning commited on
Commit
4c13691
1 Parent(s): 99b3be8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # from phonemizer.backend.espeak.wrapper import EspeakWrapper
10
+
11
+ # _ESPEAK_LIBRARY = '/opt/homebrew/Cellar/espeak/1.48.04_1/lib/libespeak.1.1.48.dylib' #use the Path to the library.
12
+ # EspeakWrapper.set_library(_ESPEAK_LIBRARY)
13
+
14
+ device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
15
+
16
+ narrator = pipeline("text-to-speech", model="kakao-enterprise/vits-ljs")
17
+
18
+ # tts_model_path = "./Model/models--kakao-enterprise--vits-ljs/snapshots/3bcb8321394f671bd948ebf0d086d694dda95464"
19
+
20
+ # narrator = pipeline("text-to-speech", model=tts_model_path)
21
+
22
+ # Load the pretrained weights
23
+ caption_image = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large", device=device)
24
+
25
+ # model_path = "./Model/models--Salesforce--blip-image-captioning-large/snapshots/2227ac38c9f16105cb0412e7cab4759978a8fd90"
26
+
27
+ # Load the pretrained weights
28
+ # caption_image = pipeline("image-to-text", model=model_path, device=device)
29
+
30
+ # define the function to generate audio from text
31
+ def generate_audio(text):
32
+
33
+ # generate the narrated text
34
+ narrated_text = narrator(text)
35
+
36
+ # save the audio to WAV file
37
+ wavfile.write("output.wav", rate=narrated_text["sampling_rate"],
38
+ data=narrated_text["audio"][0])
39
+
40
+ # Return the path to the saved output WAV file
41
+ return "output.wav"
42
+
43
+
44
+ def caption_my_image(pil_image):
45
+
46
+ semantics = caption_image(pil_image)[0]["generated_text"]
47
+ audio = generate_audio(semantics)
48
+
49
+ return audio
50
+
51
+
52
+ gr.close_all()
53
+
54
+ demo = gr.Interface(fn=caption_my_image,
55
+ inputs=[gr.Image(label="Select Image", type="pil")],
56
+ outputs=[gr.Audio(label="Generated Audio")],
57
+ title="@IT AI Enthusiast (https://www.youtube.com/@itaienthusiast/) - Project 8: Image Captioning with AI",
58
+ description="THIS APPLICATION WILL BE USED TO CAPTION IMAGES WITH THE HELP OF AI")
59
+
60
+ demo.launch()