Chenzhou commited on
Commit
d1baf01
·
1 Parent(s): 2965889

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -14
app.py CHANGED
@@ -1,18 +1,78 @@
1
- from transformers import pipeline
2
- import gradio as gr
 
 
 
3
 
4
- pipe = pipeline(model="tilos/whisper-small-zh-HK") # change to "your-username/the-name-you-picked"
 
5
 
6
- def transcribe(audio):
7
- text = pipe(audio)["text"]
8
- return text
9
 
10
- iface = gr.Interface(
11
- fn=transcribe,
12
- inputs=gr.Audio(source="microphone", type="filepath"),
13
- outputs="text",
14
- title="Whisper Small Cantonese",
15
- description="Realtime demo for Cantonese speech recognition using a fine-tuned Whisper small model.",
16
- )
17
 
18
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import sys
4
+ import subprocess
5
+ #from moviepy.editor import VideoFileClip
6
 
7
+ import whisper
8
+ from whisper.utils import write_vtt
9
 
10
+ model = whisper.load_model("medium")
 
 
11
 
12
+ def video2mp3(video_file, output_ext="mp3"):
13
+ filename, ext = os.path.splitext(video_file)
14
+ subprocess.call(["ffmpeg", "-y", "-i", video_file, f"{filename}.{output_ext}"],
15
+ stdout=subprocess.DEVNULL,
16
+ stderr=subprocess.STDOUT)
17
+ return f"{filename}.{output_ext}"
 
18
 
19
+
20
+ def translate(input_video):
21
+
22
+ audio_file = video2mp3(input_video)
23
+
24
+ options = dict(beam_size=5, best_of=5)
25
+ translate_options = dict(task="translate", **options)
26
+ result = model.transcribe(audio_file,**translate_options)
27
+
28
+ output_dir = '/content/'
29
+ audio_path = audio_file.split(".")[0]
30
+
31
+ with open(os.path.join(output_dir, audio_path + ".vtt"), "w") as vtt:
32
+ write_vtt(result["segments"], file=vtt)
33
+
34
+ subtitle = audio_path + ".vtt"
35
+ output_video = audio_path + "_subtitled.mp4"
36
+
37
+ os.system(f"ffmpeg -i {input_video} -vf subtitles={subtitle} {output_video}")
38
+
39
+ return output_video
40
+
41
+
42
+
43
+ title = "Add Text/Caption to your YouTube Shorts - MultiLingual"
44
+
45
+ block = gr.Blocks()
46
+
47
+ with block:
48
+
49
+ with gr.Group():
50
+ with gr.Box():
51
+
52
+
53
+
54
+ with gr.Row().style():
55
+
56
+ inp_video = gr.Video(
57
+ label="Input Video",
58
+ type="filepath",
59
+ mirror_webcam = False
60
+ )
61
+ op_video = gr.Video()
62
+ btn = gr.Button("Generate Subtitle Video")
63
+
64
+
65
+
66
+
67
+
68
+
69
+ btn.click(translate, inputs=[inp_video], outputs=[op_video])
70
+
71
+ gr.HTML('''
72
+ <div class="footer">
73
+ <p>Model by <a href="https://github.com/openai/whisper" style="text-decoration: underline;" target="_blank">OpenAI</a> - Gradio App by <a href="https://twitter.com/1littlecoder" style="text-decoration: underline;" target="_blank">1littlecoder</a>
74
+ </p>
75
+ </div>
76
+ ''')
77
+
78
+ block.launch(debug = True)