DeepLearning101 commited on
Commit
7bab834
1 Parent(s): 2023ef5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -16
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import torch
2
-
3
  import gradio as gr
4
  import yt_dlp as youtube_dl
5
  from transformers import pipeline
@@ -7,6 +6,7 @@ from transformers.pipelines.audio_utils import ffmpeg_read
7
 
8
  import tempfile
9
  import os
 
10
 
11
  MODEL_NAME = "openai/whisper-large-v3"
12
  BATCH_SIZE = 8
@@ -22,14 +22,12 @@ pipe = pipeline(
22
  device=device,
23
  )
24
 
25
-
26
  def transcribe(inputs, task):
27
  if inputs is None:
28
  raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
29
 
30
  text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
31
- return text
32
-
33
 
34
  def _return_yt_html_embed(yt_url):
35
  video_id = yt_url.split("?v=")[-1]
@@ -70,7 +68,6 @@ def download_yt_audio(yt_url, filename):
70
  except youtube_dl.utils.ExtractorError as err:
71
  raise gr.Error(str(err))
72
 
73
-
74
  def yt_transcribe(yt_url, task, max_filesize=75.0):
75
  html_embed_str = _return_yt_html_embed(yt_url)
76
 
@@ -87,14 +84,11 @@ def yt_transcribe(yt_url, task, max_filesize=75.0):
87
 
88
  return html_embed_str, text
89
 
90
-
91
- demo = gr.Blocks()
92
-
93
  mf_transcribe = gr.Interface(
94
  fn=transcribe,
95
  inputs=[
96
- gr.inputs.Audio(source="microphone", type="filepath", optional=True),
97
- gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
98
  ],
99
  outputs="text",
100
  layout="horizontal",
@@ -111,8 +105,8 @@ mf_transcribe = gr.Interface(
111
  file_transcribe = gr.Interface(
112
  fn=transcribe,
113
  inputs=[
114
- gr.inputs.Audio(source="upload", type="filepath", optional=True, label="Audio file"),
115
- gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
116
  ],
117
  outputs="text",
118
  layout="horizontal",
@@ -129,8 +123,8 @@ file_transcribe = gr.Interface(
129
  yt_transcribe = gr.Interface(
130
  fn=yt_transcribe,
131
  inputs=[
132
- gr.inputs.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
133
- gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe")
134
  ],
135
  outputs=["html", "text"],
136
  layout="horizontal",
@@ -144,8 +138,7 @@ yt_transcribe = gr.Interface(
144
  allow_flagging="never",
145
  )
146
 
147
- with demo:
148
  gr.TabbedInterface([mf_transcribe, file_transcribe, yt_transcribe], ["Microphone", "Audio file", "YouTube"])
149
 
150
  demo.launch(enable_queue=True)
151
-
 
1
  import torch
 
2
  import gradio as gr
3
  import yt_dlp as youtube_dl
4
  from transformers import pipeline
 
6
 
7
  import tempfile
8
  import os
9
+ import time
10
 
11
  MODEL_NAME = "openai/whisper-large-v3"
12
  BATCH_SIZE = 8
 
22
  device=device,
23
  )
24
 
 
25
  def transcribe(inputs, task):
26
  if inputs is None:
27
  raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
28
 
29
  text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
30
+ return text
 
31
 
32
  def _return_yt_html_embed(yt_url):
33
  video_id = yt_url.split("?v=")[-1]
 
68
  except youtube_dl.utils.ExtractorError as err:
69
  raise gr.Error(str(err))
70
 
 
71
  def yt_transcribe(yt_url, task, max_filesize=75.0):
72
  html_embed_str = _return_yt_html_embed(yt_url)
73
 
 
84
 
85
  return html_embed_str, text
86
 
 
 
 
87
  mf_transcribe = gr.Interface(
88
  fn=transcribe,
89
  inputs=[
90
+ gr.Audio(source="microphone", type="filepath", optional=True),
91
+ gr.Radio(["transcribe", "translate"], label="Task", value="transcribe"),
92
  ],
93
  outputs="text",
94
  layout="horizontal",
 
105
  file_transcribe = gr.Interface(
106
  fn=transcribe,
107
  inputs=[
108
+ gr.Audio(source="upload", type="filepath", optional=True, label="Audio file"),
109
+ gr.Radio(["transcribe", "translate"], label="Task", value="transcribe"),
110
  ],
111
  outputs="text",
112
  layout="horizontal",
 
123
  yt_transcribe = gr.Interface(
124
  fn=yt_transcribe,
125
  inputs=[
126
+ gr.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
127
+ gr.Radio(["transcribe", "translate"], label="Task", value="transcribe")
128
  ],
129
  outputs=["html", "text"],
130
  layout="horizontal",
 
138
  allow_flagging="never",
139
  )
140
 
141
+ with gr.Blocks() as demo:
142
  gr.TabbedInterface([mf_transcribe, file_transcribe, yt_transcribe], ["Microphone", "Audio file", "YouTube"])
143
 
144
  demo.launch(enable_queue=True)