Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,9 @@
|
|
| 1 |
import torch
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
import spaces
|
| 5 |
import yt_dlp as youtube_dl
|
| 6 |
from transformers import pipeline
|
| 7 |
from transformers.pipelines.audio_utils import ffmpeg_read
|
| 8 |
-
|
| 9 |
import tempfile
|
| 10 |
import os
|
| 11 |
|
|
@@ -29,8 +27,7 @@ def transcribe(inputs, task):
|
|
| 29 |
raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
|
| 30 |
|
| 31 |
text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
|
| 32 |
-
return
|
| 33 |
-
|
| 34 |
|
| 35 |
def _return_yt_html_embed(yt_url):
|
| 36 |
video_id = yt_url.split("?v=")[-1]
|
|
@@ -63,7 +60,14 @@ def download_yt_audio(yt_url, filename):
|
|
| 63 |
file_length_hms = time.strftime("%HH:%MM:%SS", time.gmtime(file_length_s))
|
| 64 |
raise gr.Error(f"Maximum YouTube length is {yt_length_limit_hms}, got {file_length_hms} YouTube video.")
|
| 65 |
|
| 66 |
-
ydl_opts = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
| 69 |
try:
|
|
@@ -71,12 +75,11 @@ def download_yt_audio(yt_url, filename):
|
|
| 71 |
except youtube_dl.utils.ExtractorError as err:
|
| 72 |
raise gr.Error(str(err))
|
| 73 |
|
| 74 |
-
|
| 75 |
def yt_transcribe(yt_url, task, max_filesize=75.0):
|
| 76 |
html_embed_str = _return_yt_html_embed(yt_url)
|
| 77 |
|
| 78 |
with tempfile.TemporaryDirectory() as tmpdirname:
|
| 79 |
-
filepath = os.path.join(tmpdirname, "
|
| 80 |
download_yt_audio(yt_url, filepath)
|
| 81 |
with open(filepath, "rb") as f:
|
| 82 |
inputs = f.read()
|
|
@@ -86,8 +89,7 @@ def yt_transcribe(yt_url, task, max_filesize=75.0):
|
|
| 86 |
|
| 87 |
text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
|
| 88 |
|
| 89 |
-
return html_embed_str, text
|
| 90 |
-
|
| 91 |
|
| 92 |
demo = gr.Blocks()
|
| 93 |
|
|
@@ -131,7 +133,11 @@ yt_transcribe = gr.Interface(
|
|
| 131 |
gr.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
|
| 132 |
gr.Radio(["transcribe", "translate"], label="Task", value="transcribe")
|
| 133 |
],
|
| 134 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
theme="huggingface",
|
| 136 |
title="Whisper Large V3: Transcribe YouTube",
|
| 137 |
description=(
|
|
@@ -145,5 +151,4 @@ yt_transcribe = gr.Interface(
|
|
| 145 |
with demo:
|
| 146 |
gr.TabbedInterface([mf_transcribe, file_transcribe, yt_transcribe], ["Microphone", "Audio file", "YouTube"])
|
| 147 |
|
| 148 |
-
demo.launch()
|
| 149 |
-
|
|
|
|
| 1 |
import torch
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import spaces
|
| 4 |
import yt_dlp as youtube_dl
|
| 5 |
from transformers import pipeline
|
| 6 |
from transformers.pipelines.audio_utils import ffmpeg_read
|
|
|
|
| 7 |
import tempfile
|
| 8 |
import os
|
| 9 |
|
|
|
|
| 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]
|
|
|
|
| 60 |
file_length_hms = time.strftime("%HH:%MM:%SS", time.gmtime(file_length_s))
|
| 61 |
raise gr.Error(f"Maximum YouTube length is {yt_length_limit_hms}, got {file_length_hms} YouTube video.")
|
| 62 |
|
| 63 |
+
ydl_opts = {
|
| 64 |
+
"outtmpl": filename,
|
| 65 |
+
"format": "bestaudio/best",
|
| 66 |
+
"postprocessors": [{
|
| 67 |
+
"key": "FFmpegExtractAudio",
|
| 68 |
+
"preferredcodec": "mp3",
|
| 69 |
+
}]
|
| 70 |
+
}
|
| 71 |
|
| 72 |
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
| 73 |
try:
|
|
|
|
| 75 |
except youtube_dl.utils.ExtractorError as err:
|
| 76 |
raise gr.Error(str(err))
|
| 77 |
|
|
|
|
| 78 |
def yt_transcribe(yt_url, task, max_filesize=75.0):
|
| 79 |
html_embed_str = _return_yt_html_embed(yt_url)
|
| 80 |
|
| 81 |
with tempfile.TemporaryDirectory() as tmpdirname:
|
| 82 |
+
filepath = os.path.join(tmpdirname, "audio.mp3")
|
| 83 |
download_yt_audio(yt_url, filepath)
|
| 84 |
with open(filepath, "rb") as f:
|
| 85 |
inputs = f.read()
|
|
|
|
| 89 |
|
| 90 |
text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
|
| 91 |
|
| 92 |
+
return html_embed_str, text, filepath
|
|
|
|
| 93 |
|
| 94 |
demo = gr.Blocks()
|
| 95 |
|
|
|
|
| 133 |
gr.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
|
| 134 |
gr.Radio(["transcribe", "translate"], label="Task", value="transcribe")
|
| 135 |
],
|
| 136 |
+
outputs=[
|
| 137 |
+
"html",
|
| 138 |
+
"text",
|
| 139 |
+
gr.Audio(label="Extracted Audio")
|
| 140 |
+
],
|
| 141 |
theme="huggingface",
|
| 142 |
title="Whisper Large V3: Transcribe YouTube",
|
| 143 |
description=(
|
|
|
|
| 151 |
with demo:
|
| 152 |
gr.TabbedInterface([mf_transcribe, file_transcribe, yt_transcribe], ["Microphone", "Audio file", "YouTube"])
|
| 153 |
|
| 154 |
+
demo.launch()
|
|
|