sanchit-gandhi HF staff commited on
Commit
3c0cd8e
1 Parent(s): b97a3c2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -24
app.py CHANGED
@@ -23,28 +23,12 @@ pipe = pipeline(
23
  )
24
 
25
 
26
- def transcribe(microphone, file_upload, task):
27
- warn_output = ""
28
- if (microphone is not None) and (file_upload is not None):
29
- warn_output = (
30
- "WARNING: You've uploaded an audio file and used the microphone. "
31
- "The recorded file from the microphone will be used and the uploaded audio will be discarded.\n"
32
- )
33
 
34
- elif (microphone is None) and (file_upload is None):
35
- raise gr.Error("You have to either use the microphone or upload an audio file")
36
-
37
- file_size_mb = os.stat(inputs).st_size / (1024 * 1024)
38
- if file_size_mb > FILE_LIMIT_MB:
39
- raise gr.Error(
40
- f"File size exceeds file size limit. Got file of size {file_size_mb:.2f}MB for a limit of {FILE_LIMIT_MB}MB."
41
- )
42
-
43
- file = microphone if microphone is not None else file_upload
44
-
45
- text = pipe(file, batch_size=BATCH_SIZE, generate_kwargs={"task": task})["text"]
46
-
47
- return warn_output + text
48
 
49
 
50
  def _return_yt_html_embed(yt_url):
@@ -57,23 +41,29 @@ def _return_yt_html_embed(yt_url):
57
 
58
  def download_yt_audio(yt_url, filename):
59
  info_loader = youtube_dl.YoutubeDL()
 
60
  try:
61
  info = info_loader.extract_info(yt_url, download=False)
62
  except youtube_dl.utils.DownloadError as err:
63
  raise gr.Error(str(err))
 
64
  file_length = info["duration_string"]
65
  file_h_m_s = file_length.split(":")
66
  file_h_m_s = [int(sub_length) for sub_length in file_h_m_s]
 
67
  if len(file_h_m_s) == 1:
68
  file_h_m_s.insert(0, 0)
69
  if len(file_h_m_s) == 2:
70
  file_h_m_s.insert(0, 0)
71
  file_length_s = file_h_m_s[0] * 3600 + file_h_m_s[1] * 60 + file_h_m_s[2]
 
72
  if file_length_s > YT_LENGTH_LIMIT_S:
73
  yt_length_limit_hms = time.strftime("%HH:%MM:%SS", time.gmtime(YT_LENGTH_LIMIT_S))
74
  file_length_hms = time.strftime("%HH:%MM:%SS", time.gmtime(file_length_s))
75
  raise gr.Error(f"Maximum YouTube length is {yt_length_limit_hms}, got {file_length_hms} YouTube video.")
 
76
  ydl_opts = {"outtmpl": filename, "format": "worstvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}
 
77
  with youtube_dl.YoutubeDL(ydl_opts) as ydl:
78
  try:
79
  ydl.download([yt_url])
@@ -93,7 +83,7 @@ def yt_transcribe(yt_url, task, max_filesize=75.0):
93
  inputs = ffmpeg_read(inputs, pipe.feature_extractor.sampling_rate)
94
  inputs = {"array": inputs, "sampling_rate": pipe.feature_extractor.sampling_rate}
95
 
96
- text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task})["text"]
97
 
98
  return html_embed_str, text
99
 
@@ -104,7 +94,24 @@ mf_transcribe = gr.Interface(
104
  fn=transcribe,
105
  inputs=[
106
  gr.inputs.Audio(source="microphone", type="filepath", optional=True),
107
- gr.inputs.Audio(source="upload", type="filepath", optional=True),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
109
  ],
110
  outputs="text",
@@ -138,7 +145,7 @@ yt_transcribe = gr.Interface(
138
  )
139
 
140
  with demo:
141
- gr.TabbedInterface([mf_transcribe, yt_transcribe], ["Transcribe Audio", "Transcribe YouTube"])
142
 
143
  demo.launch(enable_queue=True)
144
 
 
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):
 
41
 
42
  def download_yt_audio(yt_url, filename):
43
  info_loader = youtube_dl.YoutubeDL()
44
+
45
  try:
46
  info = info_loader.extract_info(yt_url, download=False)
47
  except youtube_dl.utils.DownloadError as err:
48
  raise gr.Error(str(err))
49
+
50
  file_length = info["duration_string"]
51
  file_h_m_s = file_length.split(":")
52
  file_h_m_s = [int(sub_length) for sub_length in file_h_m_s]
53
+
54
  if len(file_h_m_s) == 1:
55
  file_h_m_s.insert(0, 0)
56
  if len(file_h_m_s) == 2:
57
  file_h_m_s.insert(0, 0)
58
  file_length_s = file_h_m_s[0] * 3600 + file_h_m_s[1] * 60 + file_h_m_s[2]
59
+
60
  if file_length_s > YT_LENGTH_LIMIT_S:
61
  yt_length_limit_hms = time.strftime("%HH:%MM:%SS", time.gmtime(YT_LENGTH_LIMIT_S))
62
  file_length_hms = time.strftime("%HH:%MM:%SS", time.gmtime(file_length_s))
63
  raise gr.Error(f"Maximum YouTube length is {yt_length_limit_hms}, got {file_length_hms} YouTube video.")
64
+
65
  ydl_opts = {"outtmpl": filename, "format": "worstvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}
66
+
67
  with youtube_dl.YoutubeDL(ydl_opts) as ydl:
68
  try:
69
  ydl.download([yt_url])
 
83
  inputs = ffmpeg_read(inputs, pipe.feature_extractor.sampling_rate)
84
  inputs = {"array": inputs, "sampling_rate": pipe.feature_extractor.sampling_rate}
85
 
86
+ text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
87
 
88
  return html_embed_str, text
89
 
 
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",
101
+ theme="huggingface",
102
+ title="Whisper Large V2: Transcribe Audio",
103
+ description=(
104
+ "Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the"
105
+ f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
106
+ " of arbitrary length."
107
+ ),
108
+ allow_flagging="never",
109
+ )
110
+
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",
 
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