Spaces:
Build error
Build error
Commit
·
9758654
1
Parent(s):
1d50b5e
split into 3 tabs and tweak q params
Browse files
app.py
CHANGED
@@ -13,7 +13,10 @@ from transformers.pipelines.audio_utils import ffmpeg_read
|
|
13 |
|
14 |
title = "Whisper JAX: The Fastest Whisper API ⚡️"
|
15 |
|
16 |
-
description = "Whisper JAX is an optimised implementation of the [Whisper model](https://huggingface.co/openai/whisper-large-v2) by OpenAI. It runs on JAX with a TPU v4-8 in the backend. Compared to PyTorch on an A100 GPU, it is over [**70x faster**](https://github.com/sanchit-gandhi/whisper-jax#benchmarks), making it the fastest Whisper API available.
|
|
|
|
|
|
|
17 |
|
18 |
API_URL = os.getenv("API_URL")
|
19 |
API_URL_FROM_FEATURES = os.getenv("API_URL_FROM_FEATURES")
|
@@ -23,7 +26,7 @@ article = "Whisper large-v2 model by OpenAI. Backend running JAX on a TPU v4-8 t
|
|
23 |
language_names = sorted(TO_LANGUAGE_CODE.keys())
|
24 |
CHUNK_LENGTH_S = 30
|
25 |
BATCH_SIZE = 16
|
26 |
-
NUM_PROC =
|
27 |
FILE_LIMIT_MB = 1000
|
28 |
|
29 |
|
@@ -73,19 +76,7 @@ if __name__ == "__main__":
|
|
73 |
processor = WhisperPrePostProcessor.from_pretrained("openai/whisper-large-v2")
|
74 |
pool = Pool(NUM_PROC)
|
75 |
|
76 |
-
def transcribe_chunked_audio(
|
77 |
-
warn_output = ""
|
78 |
-
if (microphone is not None) and (file_upload is not None):
|
79 |
-
warn_output = (
|
80 |
-
"WARNING: You've uploaded an audio file and used the microphone. "
|
81 |
-
"The recorded file from the microphone will be used and the uploaded audio will be discarded.\n"
|
82 |
-
)
|
83 |
-
|
84 |
-
elif (microphone is None) and (file_upload is None):
|
85 |
-
return "ERROR: You have to either use the microphone or upload an audio file", None
|
86 |
-
|
87 |
-
inputs = microphone if microphone is not None else file_upload
|
88 |
-
|
89 |
file_size_mb = os.stat(inputs).st_size / (1024 * 1024)
|
90 |
if file_size_mb > FILE_LIMIT_MB:
|
91 |
return f"ERROR: File size exceeds file size limit. Got file of size {file_size_mb:.2f}MB for a limit of {FILE_LIMIT_MB}MB.", None
|
@@ -106,7 +97,7 @@ if __name__ == "__main__":
|
|
106 |
|
107 |
post_processed = processor.postprocess(model_outputs, return_timestamps=return_timestamps)
|
108 |
timestamps = post_processed.get("chunks")
|
109 |
-
return
|
110 |
|
111 |
def _return_yt_html_embed(yt_url):
|
112 |
video_id = yt_url.split("?v=")[-1]
|
@@ -123,11 +114,27 @@ if __name__ == "__main__":
|
|
123 |
|
124 |
return html_embed_str, text, timestamps
|
125 |
|
126 |
-
|
127 |
fn=transcribe_chunked_audio,
|
128 |
inputs=[
|
129 |
gr.inputs.Audio(source="microphone", optional=True, type="filepath"),
|
130 |
-
gr.inputs.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
|
132 |
gr.inputs.Checkbox(default=False, label="Return timestamps"),
|
133 |
],
|
@@ -164,7 +171,7 @@ if __name__ == "__main__":
|
|
164 |
demo = gr.Blocks()
|
165 |
|
166 |
with demo:
|
167 |
-
gr.TabbedInterface([audio_chunked, youtube], ["Transcribe Audio", "Transcribe YouTube"])
|
168 |
|
169 |
-
demo.queue(
|
170 |
-
demo.launch()
|
|
|
13 |
|
14 |
title = "Whisper JAX: The Fastest Whisper API ⚡️"
|
15 |
|
16 |
+
description = """Whisper JAX is an optimised implementation of the [Whisper model](https://huggingface.co/openai/whisper-large-v2) by OpenAI. It runs on JAX with a TPU v4-8 in the backend. Compared to PyTorch on an A100 GPU, it is over [**70x faster**](https://github.com/sanchit-gandhi/whisper-jax#benchmarks), making it the fastest Whisper API available.
|
17 |
+
|
18 |
+
Note that using microphone or audio file requires the audio input to be transferred from the Gradio demo to the TPU, which for large audio files can be slow. We recommend using YouTube where possible, since this directly downloads the audio file to the TPU, skipping the file transfer step.
|
19 |
+
"""
|
20 |
|
21 |
API_URL = os.getenv("API_URL")
|
22 |
API_URL_FROM_FEATURES = os.getenv("API_URL_FROM_FEATURES")
|
|
|
26 |
language_names = sorted(TO_LANGUAGE_CODE.keys())
|
27 |
CHUNK_LENGTH_S = 30
|
28 |
BATCH_SIZE = 16
|
29 |
+
NUM_PROC = 16
|
30 |
FILE_LIMIT_MB = 1000
|
31 |
|
32 |
|
|
|
76 |
processor = WhisperPrePostProcessor.from_pretrained("openai/whisper-large-v2")
|
77 |
pool = Pool(NUM_PROC)
|
78 |
|
79 |
+
def transcribe_chunked_audio(inputs, task, return_timestamps):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
file_size_mb = os.stat(inputs).st_size / (1024 * 1024)
|
81 |
if file_size_mb > FILE_LIMIT_MB:
|
82 |
return f"ERROR: File size exceeds file size limit. Got file of size {file_size_mb:.2f}MB for a limit of {FILE_LIMIT_MB}MB.", None
|
|
|
97 |
|
98 |
post_processed = processor.postprocess(model_outputs, return_timestamps=return_timestamps)
|
99 |
timestamps = post_processed.get("chunks")
|
100 |
+
return post_processed["text"], timestamps
|
101 |
|
102 |
def _return_yt_html_embed(yt_url):
|
103 |
video_id = yt_url.split("?v=")[-1]
|
|
|
114 |
|
115 |
return html_embed_str, text, timestamps
|
116 |
|
117 |
+
microphone_chunked = gr.Interface(
|
118 |
fn=transcribe_chunked_audio,
|
119 |
inputs=[
|
120 |
gr.inputs.Audio(source="microphone", optional=True, type="filepath"),
|
121 |
+
gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
|
122 |
+
gr.inputs.Checkbox(default=False, label="Return timestamps"),
|
123 |
+
],
|
124 |
+
outputs=[
|
125 |
+
gr.outputs.Textbox(label="Transcription"),
|
126 |
+
gr.outputs.Textbox(label="Timestamps"),
|
127 |
+
],
|
128 |
+
allow_flagging="never",
|
129 |
+
title=title,
|
130 |
+
description=description,
|
131 |
+
article=article,
|
132 |
+
)
|
133 |
+
|
134 |
+
audio_chunked = gr.Interface(
|
135 |
+
fn=transcribe_chunked_audio,
|
136 |
+
inputs=[
|
137 |
+
gr.inputs.Audio(source="upload", optional=True, label="Audio file", type="filepath"),
|
138 |
gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
|
139 |
gr.inputs.Checkbox(default=False, label="Return timestamps"),
|
140 |
],
|
|
|
171 |
demo = gr.Blocks()
|
172 |
|
173 |
with demo:
|
174 |
+
gr.TabbedInterface([microphone_chunked, audio_chunked, youtube], ["Transcribe Microphone", "Transcribe Audio File", "Transcribe YouTube"])
|
175 |
|
176 |
+
demo.queue(max_size=3)
|
177 |
+
demo.launch(show_api=False)
|