Spaces:
Runtime error
Runtime error
Commit
•
f3ee165
1
Parent(s):
ef45b2f
Update app.py
Browse files
app.py
CHANGED
@@ -2,15 +2,16 @@ import gradio as gr
|
|
2 |
import yt_dlp as youtube_dl
|
3 |
from transformers import pipeline
|
4 |
from transformers.pipelines.audio_utils import ffmpeg_read
|
|
|
5 |
|
6 |
import tempfile
|
7 |
import os
|
8 |
-
import time
|
9 |
|
10 |
MODEL_NAME = "openai/whisper-large-v3"
|
11 |
BATCH_SIZE = 8
|
12 |
FILE_LIMIT_MB = 1000
|
13 |
-
YT_LENGTH_LIMIT_S = 3600
|
14 |
|
15 |
device = 0 if torch.cuda.is_available() else "cpu"
|
16 |
|
@@ -26,18 +27,68 @@ def transcribe(inputs, task):
|
|
26 |
raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
|
27 |
|
28 |
text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
|
29 |
-
return
|
30 |
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
-
# Schritt 1: Definiere das gr.Blocks-Element für das Layout der Demo
|
34 |
demo = gr.Blocks(theme="TogetherAi/Alex2")
|
35 |
|
36 |
-
|
37 |
-
demo.
|
38 |
-
demo.width = 500 # Breite auf 500 setzen
|
39 |
|
40 |
-
# Schritt 3: Erstelle die Schnittstellen wie zuvor für Audioaufnahmen, das Hochladen von Audiodateien und das Transkribieren von YouTube-Videos
|
41 |
mf_transcribe = gr.Interface(
|
42 |
fn=transcribe,
|
43 |
inputs=[
|
@@ -78,7 +129,7 @@ yt_transcribe = gr.Interface(
|
|
78 |
fn=yt_transcribe,
|
79 |
inputs=[
|
80 |
gr.inputs.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
|
81 |
-
gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe")
|
82 |
],
|
83 |
outputs=["html", "text"],
|
84 |
layout="horizontal",
|
@@ -92,10 +143,7 @@ yt_transcribe = gr.Interface(
|
|
92 |
allow_flagging="never",
|
93 |
)
|
94 |
|
95 |
-
# Schritt 4: Erstelle eine TabbedInterface, um die verschiedenen Schnittstellen für Mikrofon, Hochladen von Audiodateien und YouTube-Transkription anzuzeigen
|
96 |
with demo:
|
97 |
gr.TabbedInterface([mf_transcribe, file_transcribe, yt_transcribe], ["Microphone", "Audio file", "YouTube"])
|
98 |
|
99 |
-
# Schritt 5: Starte die Demo
|
100 |
demo.launch(enable_queue=True)
|
101 |
-
|
|
|
2 |
import yt_dlp as youtube_dl
|
3 |
from transformers import pipeline
|
4 |
from transformers.pipelines.audio_utils import ffmpeg_read
|
5 |
+
import torch
|
6 |
|
7 |
import tempfile
|
8 |
import os
|
9 |
+
import time
|
10 |
|
11 |
MODEL_NAME = "openai/whisper-large-v3"
|
12 |
BATCH_SIZE = 8
|
13 |
FILE_LIMIT_MB = 1000
|
14 |
+
YT_LENGTH_LIMIT_S = 3600
|
15 |
|
16 |
device = 0 if torch.cuda.is_available() else "cpu"
|
17 |
|
|
|
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]
|
34 |
+
HTML_str = (
|
35 |
+
f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}"> </iframe>'
|
36 |
+
" </center>"
|
37 |
+
)
|
38 |
+
return HTML_str
|
39 |
+
|
40 |
+
def download_yt_audio(yt_url, filename):
|
41 |
+
info_loader = youtube_dl.YoutubeDL()
|
42 |
+
|
43 |
+
try:
|
44 |
+
info = info_loader.extract_info(yt_url, download=False)
|
45 |
+
except youtube_dl.utils.DownloadError as err:
|
46 |
+
raise gr.Error(str(err))
|
47 |
+
|
48 |
+
file_length = info["duration_string"]
|
49 |
+
file_h_m_s = file_length.split(":")
|
50 |
+
file_h_m_s = [int(sub_length) for sub_length in file_h_m_s]
|
51 |
+
|
52 |
+
if len(file_h_m_s) == 1:
|
53 |
+
file_h_m_s.insert(0, 0)
|
54 |
+
if len(file_h_m_s) == 2:
|
55 |
+
file_h_m_s.insert(0, 0)
|
56 |
+
file_length_s = file_h_m_s[0] * 3600 + file_h_m_s[1] * 60 + file_h_m_s[2]
|
57 |
+
|
58 |
+
if file_length_s > YT_LENGTH_LIMIT_S:
|
59 |
+
yt_length_limit_hms = time.strftime("%HH:%MM:%SS", time.gmtime(YT_LENGTH_LIMIT_S))
|
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 = {"outtmpl": filename, "format": "worstvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}
|
64 |
+
|
65 |
+
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
66 |
+
try:
|
67 |
+
ydl.download([yt_url])
|
68 |
+
except youtube_dl.utils.ExtractorError as err:
|
69 |
+
raise gr.Error(str(err))
|
70 |
+
|
71 |
+
def yt_transcribe(yt_url, task, max_filesize=75.0):
|
72 |
+
html_embed_str = _return_yt_html_embed(yt_url)
|
73 |
+
|
74 |
+
with tempfile.TemporaryDirectory() as tmpdirname:
|
75 |
+
filepath = os.path.join(tmpdirname, "video.mp4")
|
76 |
+
download_yt_audio(yt_url, filepath)
|
77 |
+
with open(filepath, "rb") as f:
|
78 |
+
inputs = f.read()
|
79 |
+
|
80 |
+
inputs = ffmpeg_read(inputs, pipe.feature_extractor.sampling_rate)
|
81 |
+
inputs = {"array": inputs, "sampling_rate": pipe.feature_extractor.sampling_rate}
|
82 |
+
|
83 |
+
text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
|
84 |
+
|
85 |
+
return html_embed_str, text
|
86 |
|
|
|
87 |
demo = gr.Blocks(theme="TogetherAi/Alex2")
|
88 |
|
89 |
+
demo.layout = "centered"
|
90 |
+
demo.width = 500
|
|
|
91 |
|
|
|
92 |
mf_transcribe = gr.Interface(
|
93 |
fn=transcribe,
|
94 |
inputs=[
|
|
|
129 |
fn=yt_transcribe,
|
130 |
inputs=[
|
131 |
gr.inputs.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
|
132 |
+
gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe")
|
133 |
],
|
134 |
outputs=["html", "text"],
|
135 |
layout="horizontal",
|
|
|
143 |
allow_flagging="never",
|
144 |
)
|
145 |
|
|
|
146 |
with demo:
|
147 |
gr.TabbedInterface([mf_transcribe, file_transcribe, yt_transcribe], ["Microphone", "Audio file", "YouTube"])
|
148 |
|
|
|
149 |
demo.launch(enable_queue=True)
|
|