Mei000 commited on
Commit
d4abdb4
1 Parent(s): 3f289a4

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +93 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import whisper
3
+ from pytube import YouTube
4
+
5
+
6
+ class GradioInference():
7
+ def __init__(self):
8
+ self.sizes = list(whisper._MODELS.keys())
9
+ self.langs = ["none"] + sorted(list(whisper.tokenizer.LANGUAGES.values()))
10
+ self.current_size = "base"
11
+ self.loaded_model = whisper.load_model(self.current_size)
12
+ self.yt = None
13
+
14
+ def __call__(self, link, lang, size, subs):
15
+ if self.yt is None:
16
+ self.yt = YouTube(link)
17
+ path = self.yt.streams.filter(only_audio=True)[0].download(filename="tmp.mp4")
18
+
19
+ if lang == "none":
20
+ lang = None
21
+
22
+ if size != self.current_size:
23
+ self.loaded_model = whisper.load_model(size)
24
+ self.current_size = size
25
+ results = self.loaded_model.transcribe(path, language=lang)
26
+
27
+ if subs == "None":
28
+ return results["text"]
29
+ elif subs == ".srt":
30
+ return self.srt(results["segments"])
31
+ elif ".csv" == ".csv":
32
+ return self.csv(results["segments"])
33
+
34
+ def srt(self, segments):
35
+ output = ""
36
+ for i, segment in enumerate(segments):
37
+ output += f"{i+1}\n"
38
+ output += f"{self.format_time(segment['start'])} --> {self.format_time(segment['end'])}\n"
39
+ output += f"{segment['text']}\n\n"
40
+ return output
41
+
42
+ def csv(self, segments):
43
+ output = ""
44
+ for segment in segments:
45
+ output += f"{segment['start']},{segment['end']},{segment['text']}\n"
46
+ return output
47
+
48
+ def format_time(self, time):
49
+ hours = time//3600
50
+ minutes = (time - hours*3600)//60
51
+ seconds = time - hours*3600 - minutes*60
52
+ milliseconds = (time - int(time))*1000
53
+ return f"{int(hours):02d}:{int(minutes):02d}:{int(seconds):02d},{int(milliseconds):03d}"
54
+
55
+ def populate_metadata(self, link):
56
+ self.yt = YouTube(link)
57
+ return self.yt.thumbnail_url, self.yt.title
58
+
59
+ gio = GradioInference()
60
+ title="Youtube Whisperer"
61
+ description="Speech to text transcription of Youtube videos using OpenAI's Whisper"
62
+
63
+ block = gr.Blocks()
64
+ with block:
65
+ gr.HTML(
66
+ """
67
+ <div style="text-align: center; max-width: 500px; margin: 0 auto;">
68
+ <div>
69
+ <h1>Youtube Whisperer</h1>
70
+ </div>
71
+ <p style="margin-bottom: 10px; font-size: 94%">
72
+ Speech to text transcription of Youtube videos using OpenAI's Whisper
73
+ </p>
74
+ </div>
75
+ """
76
+ )
77
+ with gr.Group():
78
+ with gr.Box():
79
+ with gr.Row().style(equal_height=True):
80
+ sz = gr.Dropdown(label="Model Size", choices=gio.sizes, value='base')
81
+ lang = gr.Dropdown(label="Language (Optional)", choices=gio.langs, value="none")
82
+ with gr.Row().style(equal_height=True):
83
+ wt = gr.Radio(["None", ".srt", ".csv"], label="With Timestamps?")
84
+ link = gr.Textbox(label="YouTube Link")
85
+ title = gr.Label(label="Video Title")
86
+ with gr.Row().style(equal_height=True):
87
+ img = gr.Image(label="Thumbnail")
88
+ text = gr.Textbox(label="Transcription", placeholder="Transcription Output", lines=10)
89
+ with gr.Row().style(equal_height=True):
90
+ btn = gr.Button("Transcribe")
91
+ btn.click(gio, inputs=[link, lang, sz, wt], outputs=[text])
92
+ link.change(gio.populate_metadata, inputs=[link], outputs=[img, title])
93
+ block.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ transformers
2
+ git+https://github.com/openai/whisper.git
3
+ pytube