Surrendra commited on
Commit
d356a41
·
verified ·
1 Parent(s): b5dc88b

Added a sample code from hugging face

Browse files
Files changed (1) hide show
  1. app.py +152 -0
app.py CHANGED
@@ -148,3 +148,155 @@ def summarize_text(text, lang='en'):
148
 
149
  summary_text = response.to_dict()['choices'][0]['message']['content']
150
  return summary_text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
 
149
  summary_text = response.to_dict()['choices'][0]['message']['content']
150
  return summary_text
151
+
152
+
153
+ import torch
154
+
155
+ import gradio as gr
156
+ import yt_dlp as youtube_dl
157
+ from transformers import pipeline
158
+ from transformers.pipelines.audio_utils import ffmpeg_read
159
+
160
+ import tempfile
161
+ import os
162
+
163
+ MODEL_NAME = "openai/whisper-large-v3"
164
+ BATCH_SIZE = 8
165
+ FILE_LIMIT_MB = 1000
166
+ YT_LENGTH_LIMIT_S = 3600 # limit to 1 hour YouTube files
167
+
168
+ device = 0 if torch.cuda.is_available() else "cpu"
169
+
170
+ pipe = pipeline(
171
+ task="automatic-speech-recognition",
172
+ model=MODEL_NAME,
173
+ chunk_length_s=30,
174
+ device=device,
175
+ )
176
+
177
+
178
+ def transcribe(inputs, task):
179
+ if inputs is None:
180
+ raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
181
+
182
+ text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
183
+ return text
184
+
185
+
186
+ def _return_yt_html_embed(yt_url):
187
+ video_id = yt_url.split("?v=")[-1]
188
+ HTML_str = (
189
+ f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}"> </iframe>'
190
+ " </center>"
191
+ )
192
+ return HTML_str
193
+
194
+ def download_yt_audio(yt_url, filename):
195
+ info_loader = youtube_dl.YoutubeDL()
196
+
197
+ try:
198
+ info = info_loader.extract_info(yt_url, download=False)
199
+ except youtube_dl.utils.DownloadError as err:
200
+ raise gr.Error(str(err))
201
+
202
+ file_length = info["duration_string"]
203
+ file_h_m_s = file_length.split(":")
204
+ file_h_m_s = [int(sub_length) for sub_length in file_h_m_s]
205
+
206
+ if len(file_h_m_s) == 1:
207
+ file_h_m_s.insert(0, 0)
208
+ if len(file_h_m_s) == 2:
209
+ file_h_m_s.insert(0, 0)
210
+ file_length_s = file_h_m_s[0] * 3600 + file_h_m_s[1] * 60 + file_h_m_s[2]
211
+
212
+ if file_length_s > YT_LENGTH_LIMIT_S:
213
+ yt_length_limit_hms = time.strftime("%HH:%MM:%SS", time.gmtime(YT_LENGTH_LIMIT_S))
214
+ file_length_hms = time.strftime("%HH:%MM:%SS", time.gmtime(file_length_s))
215
+ raise gr.Error(f"Maximum YouTube length is {yt_length_limit_hms}, got {file_length_hms} YouTube video.")
216
+
217
+ ydl_opts = {"outtmpl": filename, "format": "worstvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}
218
+
219
+ with youtube_dl.YoutubeDL(ydl_opts) as ydl:
220
+ try:
221
+ ydl.download([yt_url])
222
+ except youtube_dl.utils.ExtractorError as err:
223
+ raise gr.Error(str(err))
224
+
225
+
226
+ def yt_transcribe(yt_url, task, max_filesize=75.0):
227
+ html_embed_str = _return_yt_html_embed(yt_url)
228
+
229
+ with tempfile.TemporaryDirectory() as tmpdirname:
230
+ filepath = os.path.join(tmpdirname, "video.mp4")
231
+ download_yt_audio(yt_url, filepath)
232
+ with open(filepath, "rb") as f:
233
+ inputs = f.read()
234
+
235
+ inputs = ffmpeg_read(inputs, pipe.feature_extractor.sampling_rate)
236
+ inputs = {"array": inputs, "sampling_rate": pipe.feature_extractor.sampling_rate}
237
+
238
+ text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
239
+
240
+ return html_embed_str, text
241
+
242
+
243
+ demo = gr.Blocks()
244
+
245
+ mf_transcribe = gr.Interface(
246
+ fn=transcribe,
247
+ inputs=[
248
+ gr.inputs.Audio(source="microphone", type="filepath", optional=True),
249
+ gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
250
+ ],
251
+ outputs="text",
252
+ layout="horizontal",
253
+ theme="huggingface",
254
+ title="Whisper Large V3: Transcribe Audio",
255
+ description=(
256
+ "Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the OpenAI Whisper"
257
+ f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
258
+ " of arbitrary length."
259
+ ),
260
+ allow_flagging="never",
261
+ )
262
+
263
+ file_transcribe = gr.Interface(
264
+ fn=transcribe,
265
+ inputs=[
266
+ gr.inputs.Audio(source="upload", type="filepath", optional=True, label="Audio file"),
267
+ gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
268
+ ],
269
+ outputs="text",
270
+ layout="horizontal",
271
+ theme="huggingface",
272
+ title="Whisper Large V3: Transcribe Audio",
273
+ description=(
274
+ "Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the OpenAI Whisper"
275
+ f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
276
+ " of arbitrary length."
277
+ ),
278
+ allow_flagging="never",
279
+ )
280
+
281
+ yt_transcribe = gr.Interface(
282
+ fn=yt_transcribe,
283
+ inputs=[
284
+ gr.inputs.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
285
+ gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe")
286
+ ],
287
+ outputs=["html", "text"],
288
+ layout="horizontal",
289
+ theme="huggingface",
290
+ title="Whisper Large V3: Transcribe YouTube",
291
+ description=(
292
+ "Transcribe long-form YouTube videos with the click of a button! Demo uses the OpenAI Whisper checkpoint"
293
+ f" [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe video files of"
294
+ " arbitrary length."
295
+ ),
296
+ allow_flagging="never",
297
+ )
298
+
299
+ with demo:
300
+ gr.TabbedInterface([mf_transcribe, file_transcribe, yt_transcribe], ["Microphone", "Audio file", "YouTube"])
301
+
302
+ demo.launch(enable_queue=True)