Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,11 @@
|
|
| 1 |
import torch
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
import yt_dlp as youtube_dl
|
| 5 |
from transformers import pipeline
|
| 6 |
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 +21,12 @@ pipe = pipeline(
|
|
| 22 |
device=device,
|
| 23 |
)
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 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(
|
| 31 |
-
return
|
| 32 |
-
|
| 33 |
|
| 34 |
def _return_yt_html_embed(yt_url):
|
| 35 |
video_id = yt_url.split("?v=")[-1]
|
|
@@ -70,7 +67,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,65 +83,40 @@ def yt_transcribe(yt_url, task, max_filesize=75.0):
|
|
| 87 |
|
| 88 |
return html_embed_str, text
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 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 V3: Transcribe Audio",
|
| 103 |
-
description=(
|
| 104 |
-
"Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the OpenAI Whisper"
|
| 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",
|
| 118 |
-
layout="horizontal",
|
| 119 |
-
theme="huggingface",
|
| 120 |
-
title="Whisper Large V3: Transcribe Audio",
|
| 121 |
-
description=(
|
| 122 |
-
"Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the OpenAI Whisper"
|
| 123 |
-
f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
|
| 124 |
-
" of arbitrary length."
|
| 125 |
-
),
|
| 126 |
-
allow_flagging="never",
|
| 127 |
-
)
|
| 128 |
-
|
| 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",
|
| 137 |
-
theme="huggingface",
|
| 138 |
-
title="Whisper Large V3: Transcribe YouTube",
|
| 139 |
-
description=(
|
| 140 |
-
"Transcribe long-form YouTube videos with the click of a button! Demo uses the OpenAI Whisper checkpoint"
|
| 141 |
-
f" [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe video files of"
|
| 142 |
-
" arbitrary length."
|
| 143 |
-
),
|
| 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
|
| 5 |
from transformers.pipelines.audio_utils import ffmpeg_read
|
|
|
|
| 6 |
import tempfile
|
| 7 |
import os
|
| 8 |
+
import time
|
| 9 |
|
| 10 |
MODEL_NAME = "openai/whisper-large-v3"
|
| 11 |
BATCH_SIZE = 8
|
|
|
|
| 21 |
device=device,
|
| 22 |
)
|
| 23 |
|
| 24 |
+
def transcribe(audio, task):
|
| 25 |
+
if audio is None:
|
|
|
|
| 26 |
raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
|
| 27 |
|
| 28 |
+
text = pipe(audio, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
|
| 29 |
+
return text
|
|
|
|
| 30 |
|
| 31 |
def _return_yt_html_embed(yt_url):
|
| 32 |
video_id = yt_url.split("?v=")[-1]
|
|
|
|
| 67 |
except youtube_dl.utils.ExtractorError as err:
|
| 68 |
raise gr.Error(str(err))
|
| 69 |
|
|
|
|
| 70 |
def yt_transcribe(yt_url, task, max_filesize=75.0):
|
| 71 |
html_embed_str = _return_yt_html_embed(yt_url)
|
| 72 |
|
|
|
|
| 83 |
|
| 84 |
return html_embed_str, text
|
| 85 |
|
| 86 |
+
with gr.Blocks(theme="huggingface") as demo:
|
| 87 |
+
gr.Markdown("# Whisper Large V3: Transcribe Audio")
|
| 88 |
+
gr.Markdown(
|
| 89 |
+
"Transcribe long-form audio inputs with the click of a button! Demo uses the OpenAI Whisper"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
|
| 91 |
" of arbitrary length."
|
| 92 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
with gr.Tabs():
|
| 95 |
+
with gr.TabItem("Microphone"):
|
| 96 |
+
with gr.Row():
|
| 97 |
+
mic_input = gr.Audio(source="microphone", type="filepath", label="Microphone Input")
|
| 98 |
+
mic_task = gr.Radio(["transcribe", "translate"], label="Task", value="transcribe")
|
| 99 |
+
mic_output = gr.Textbox(label="Transcription")
|
| 100 |
+
mic_button = gr.Button("Transcribe")
|
| 101 |
+
|
| 102 |
+
with gr.TabItem("Audio file"):
|
| 103 |
+
with gr.Row():
|
| 104 |
+
file_input = gr.Audio(source="upload", type="filepath", label="Audio file")
|
| 105 |
+
file_task = gr.Radio(["transcribe", "translate"], label="Task", value="transcribe")
|
| 106 |
+
file_output = gr.Textbox(label="Transcription")
|
| 107 |
+
file_button = gr.Button("Transcribe")
|
| 108 |
+
|
| 109 |
+
with gr.TabItem("YouTube"):
|
| 110 |
+
with gr.Row():
|
| 111 |
+
yt_input = gr.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL")
|
| 112 |
+
yt_task = gr.Radio(["transcribe", "translate"], label="Task", value="transcribe")
|
| 113 |
+
yt_embed = gr.HTML(label="Video")
|
| 114 |
+
yt_output = gr.Textbox(label="Transcription")
|
| 115 |
+
yt_button = gr.Button("Transcribe")
|
| 116 |
+
|
| 117 |
+
mic_button.click(transcribe, inputs=[mic_input, mic_task], outputs=mic_output)
|
| 118 |
+
file_button.click(transcribe, inputs=[file_input, file_task], outputs=file_output)
|
| 119 |
+
yt_button.click(yt_transcribe, inputs=[yt_input, yt_task], outputs=[yt_embed, yt_output])
|
| 120 |
+
|
| 121 |
+
if __name__ == "__main__":
|
| 122 |
+
demo.launch(enable_queue=True)
|