Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,14 +26,39 @@ pipe = pipeline(
|
|
| 26 |
|
| 27 |
@spaces.GPU
|
| 28 |
def transcribe(inputs, task):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
if inputs is None:
|
| 30 |
raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
|
| 31 |
|
| 32 |
text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
|
| 33 |
-
return
|
| 34 |
|
| 35 |
|
| 36 |
def _return_yt_html_embed(yt_url):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
video_id = yt_url.split("?v=")[-1]
|
| 38 |
HTML_str = (
|
| 39 |
f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}"> </iframe>'
|
|
@@ -41,7 +66,23 @@ def _return_yt_html_embed(yt_url):
|
|
| 41 |
)
|
| 42 |
return HTML_str
|
| 43 |
|
|
|
|
| 44 |
def download_yt_audio(yt_url, filename):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
info_loader = youtube_dl.YoutubeDL()
|
| 46 |
|
| 47 |
try:
|
|
@@ -72,8 +113,26 @@ def download_yt_audio(yt_url, filename):
|
|
| 72 |
except youtube_dl.utils.ExtractorError as err:
|
| 73 |
raise gr.Error(str(err))
|
| 74 |
|
|
|
|
| 75 |
@spaces.GPU
|
| 76 |
def yt_transcribe(yt_url, task, max_filesize=75.0):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
html_embed_str = _return_yt_html_embed(yt_url)
|
| 78 |
|
| 79 |
with tempfile.TemporaryDirectory() as tmpdirname:
|
|
@@ -105,7 +164,6 @@ mf_transcribe = gr.Interface(
|
|
| 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(
|
|
@@ -121,7 +179,6 @@ file_transcribe = gr.Interface(
|
|
| 121 |
f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
|
| 122 |
" of arbitrary length."
|
| 123 |
),
|
| 124 |
-
allow_flagging="never",
|
| 125 |
)
|
| 126 |
|
| 127 |
yt_transcribe = gr.Interface(
|
|
@@ -137,11 +194,9 @@ yt_transcribe = gr.Interface(
|
|
| 137 |
f" [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe video files of"
|
| 138 |
" arbitrary length."
|
| 139 |
),
|
| 140 |
-
allow_flagging="never",
|
| 141 |
)
|
| 142 |
|
| 143 |
with demo:
|
| 144 |
gr.TabbedInterface([mf_transcribe, file_transcribe, yt_transcribe], ["Microphone", "Audio file", "YouTube"])
|
| 145 |
|
| 146 |
demo.launch(mcp_server=True)
|
| 147 |
-
|
|
|
|
| 26 |
|
| 27 |
@spaces.GPU
|
| 28 |
def transcribe(inputs, task):
|
| 29 |
+
"""
|
| 30 |
+
Transcribe or translate an audio input using the Whisper Large V3 model.
|
| 31 |
+
This function runs automatic speech recognition on an uploaded or recorded
|
| 32 |
+
audio file using a Hugging Face Transformers pipeline. It supports both
|
| 33 |
+
transcription (same-language) and translation (to English).
|
| 34 |
+
|
| 35 |
+
Args:
|
| 36 |
+
inputs (str or dict): The audio input, typically a filepath to an audio file
|
| 37 |
+
provided by Gradio, or a dictionary containing audio array and sampling rate.
|
| 38 |
+
task (str): The task to perform, either "transcribe" or "translate".
|
| 39 |
+
|
| 40 |
+
Returns:
|
| 41 |
+
str: The transcribed or translated text output from the Whisper model.
|
| 42 |
+
"""
|
| 43 |
if inputs is None:
|
| 44 |
raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
|
| 45 |
|
| 46 |
text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
|
| 47 |
+
return text
|
| 48 |
|
| 49 |
|
| 50 |
def _return_yt_html_embed(yt_url):
|
| 51 |
+
"""
|
| 52 |
+
Generate an HTML iframe embed for a YouTube video.
|
| 53 |
+
This helper function extracts the video ID from a YouTube URL and
|
| 54 |
+
returns a simple HTML string for embedding the video in the UI.
|
| 55 |
+
|
| 56 |
+
Args:
|
| 57 |
+
yt_url (str): The full URL of the YouTube video.
|
| 58 |
+
|
| 59 |
+
Returns:
|
| 60 |
+
str: An HTML string containing an iframe embedding the YouTube video.
|
| 61 |
+
"""
|
| 62 |
video_id = yt_url.split("?v=")[-1]
|
| 63 |
HTML_str = (
|
| 64 |
f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}"> </iframe>'
|
|
|
|
| 66 |
)
|
| 67 |
return HTML_str
|
| 68 |
|
| 69 |
+
|
| 70 |
def download_yt_audio(yt_url, filename):
|
| 71 |
+
"""
|
| 72 |
+
Download a YouTube video's audio to a local file.
|
| 73 |
+
This function validates the video duration against a maximum length,
|
| 74 |
+
then downloads the video (with audio) to a specified local filepath
|
| 75 |
+
using yt-dlp.
|
| 76 |
+
|
| 77 |
+
Args:
|
| 78 |
+
yt_url (str): The URL of the YouTube video to download.
|
| 79 |
+
filename (str): The destination filepath where the video/audio
|
| 80 |
+
will be saved.
|
| 81 |
+
|
| 82 |
+
Raises:
|
| 83 |
+
gr.Error: If the video cannot be downloaded or exceeds the
|
| 84 |
+
maximum allowed duration.
|
| 85 |
+
"""
|
| 86 |
info_loader = youtube_dl.YoutubeDL()
|
| 87 |
|
| 88 |
try:
|
|
|
|
| 113 |
except youtube_dl.utils.ExtractorError as err:
|
| 114 |
raise gr.Error(str(err))
|
| 115 |
|
| 116 |
+
|
| 117 |
@spaces.GPU
|
| 118 |
def yt_transcribe(yt_url, task, max_filesize=75.0):
|
| 119 |
+
"""
|
| 120 |
+
Transcribe or translate audio from a YouTube video.
|
| 121 |
+
This function downloads a YouTube video, extracts its audio,
|
| 122 |
+
converts it to the appropriate format, and runs Whisper-based
|
| 123 |
+
automatic speech recognition on the content.
|
| 124 |
+
|
| 125 |
+
Args:
|
| 126 |
+
yt_url (str): The URL of the YouTube video to transcribe.
|
| 127 |
+
task (str): The task to perform, either "transcribe" or "translate".
|
| 128 |
+
max_filesize (float, optional): Maximum allowed file size in MB.
|
| 129 |
+
Currently unused but kept for interface compatibility.
|
| 130 |
+
|
| 131 |
+
Returns:
|
| 132 |
+
tuple:
|
| 133 |
+
- html_embed_str (str): An HTML iframe embedding the YouTube video.
|
| 134 |
+
- text (str): The transcribed or translated text output.
|
| 135 |
+
"""
|
| 136 |
html_embed_str = _return_yt_html_embed(yt_url)
|
| 137 |
|
| 138 |
with tempfile.TemporaryDirectory() as tmpdirname:
|
|
|
|
| 164 |
f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
|
| 165 |
" of arbitrary length."
|
| 166 |
),
|
|
|
|
| 167 |
)
|
| 168 |
|
| 169 |
file_transcribe = gr.Interface(
|
|
|
|
| 179 |
f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
|
| 180 |
" of arbitrary length."
|
| 181 |
),
|
|
|
|
| 182 |
)
|
| 183 |
|
| 184 |
yt_transcribe = gr.Interface(
|
|
|
|
| 194 |
f" [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe video files of"
|
| 195 |
" arbitrary length."
|
| 196 |
),
|
|
|
|
| 197 |
)
|
| 198 |
|
| 199 |
with demo:
|
| 200 |
gr.TabbedInterface([mf_transcribe, file_transcribe, yt_transcribe], ["Microphone", "Audio file", "YouTube"])
|
| 201 |
|
| 202 |
demo.launch(mcp_server=True)
|
|
|