Spaces:
Running
Running
TogetherAI
commited on
Commit
•
ef45b2f
1
Parent(s):
80d9bfc
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,3 @@
|
|
1 |
-
import torch
|
2 |
-
|
3 |
import gradio as gr
|
4 |
import yt_dlp as youtube_dl
|
5 |
from transformers import pipeline
|
@@ -7,6 +5,7 @@ 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,7 +21,6 @@ pipe = pipeline(
|
|
22 |
device=device,
|
23 |
)
|
24 |
|
25 |
-
|
26 |
def transcribe(inputs, task):
|
27 |
if inputs is None:
|
28 |
raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
|
@@ -30,66 +28,16 @@ def transcribe(inputs, task):
|
|
30 |
text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
|
31 |
return text
|
32 |
|
|
|
33 |
|
34 |
-
|
35 |
-
video_id = yt_url.split("?v=")[-1]
|
36 |
-
HTML_str = (
|
37 |
-
f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}"> </iframe>'
|
38 |
-
" </center>"
|
39 |
-
)
|
40 |
-
return HTML_str
|
41 |
-
|
42 |
-
def download_yt_audio(yt_url, filename):
|
43 |
-
info_loader = youtube_dl.YoutubeDL()
|
44 |
-
|
45 |
-
try:
|
46 |
-
info = info_loader.extract_info(yt_url, download=False)
|
47 |
-
except youtube_dl.utils.DownloadError as err:
|
48 |
-
raise gr.Error(str(err))
|
49 |
-
|
50 |
-
file_length = info["duration_string"]
|
51 |
-
file_h_m_s = file_length.split(":")
|
52 |
-
file_h_m_s = [int(sub_length) for sub_length in file_h_m_s]
|
53 |
-
|
54 |
-
if len(file_h_m_s) == 1:
|
55 |
-
file_h_m_s.insert(0, 0)
|
56 |
-
if len(file_h_m_s) == 2:
|
57 |
-
file_h_m_s.insert(0, 0)
|
58 |
-
file_length_s = file_h_m_s[0] * 3600 + file_h_m_s[1] * 60 + file_h_m_s[2]
|
59 |
-
|
60 |
-
if file_length_s > YT_LENGTH_LIMIT_S:
|
61 |
-
yt_length_limit_hms = time.strftime("%HH:%MM:%SS", time.gmtime(YT_LENGTH_LIMIT_S))
|
62 |
-
file_length_hms = time.strftime("%HH:%MM:%SS", time.gmtime(file_length_s))
|
63 |
-
raise gr.Error(f"Maximum YouTube length is {yt_length_limit_hms}, got {file_length_hms} YouTube video.")
|
64 |
-
|
65 |
-
ydl_opts = {"outtmpl": filename, "format": "worstvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}
|
66 |
-
|
67 |
-
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
68 |
-
try:
|
69 |
-
ydl.download([yt_url])
|
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 |
-
|
77 |
-
with tempfile.TemporaryDirectory() as tmpdirname:
|
78 |
-
filepath = os.path.join(tmpdirname, "video.mp4")
|
79 |
-
download_yt_audio(yt_url, filepath)
|
80 |
-
with open(filepath, "rb") as f:
|
81 |
-
inputs = f.read()
|
82 |
-
|
83 |
-
inputs = ffmpeg_read(inputs, pipe.feature_extractor.sampling_rate)
|
84 |
-
inputs = {"array": inputs, "sampling_rate": pipe.feature_extractor.sampling_rate}
|
85 |
-
|
86 |
-
text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
|
87 |
-
|
88 |
-
return html_embed_str, text
|
89 |
-
|
90 |
-
|
91 |
demo = gr.Blocks(theme="TogetherAi/Alex2")
|
92 |
|
|
|
|
|
|
|
|
|
|
|
93 |
mf_transcribe = gr.Interface(
|
94 |
fn=transcribe,
|
95 |
inputs=[
|
@@ -97,17 +45,15 @@ mf_transcribe = gr.Interface(
|
|
97 |
gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
|
98 |
],
|
99 |
outputs="text",
|
100 |
-
layout="horizontal",
|
101 |
-
theme="TogetherAi/Alex2",
|
102 |
-
title="Whisper Large V3: Audio transkribieren",
|
103 |
-
description=(
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
),
|
108 |
-
allow_flagging="never",
|
109 |
-
|
110 |
-
|
111 |
)
|
112 |
|
113 |
file_transcribe = gr.Interface(
|
@@ -121,9 +67,9 @@ file_transcribe = gr.Interface(
|
|
121 |
theme="TogetherAi/Alex2",
|
122 |
title="Whisper Large V3: Transcribe Audio",
|
123 |
description=(
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
),
|
128 |
allow_flagging="never",
|
129 |
)
|
@@ -132,7 +78,7 @@ yt_transcribe = gr.Interface(
|
|
132 |
fn=yt_transcribe,
|
133 |
inputs=[
|
134 |
gr.inputs.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
|
135 |
-
gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe")
|
136 |
],
|
137 |
outputs=["html", "text"],
|
138 |
layout="horizontal",
|
@@ -146,7 +92,10 @@ yt_transcribe = gr.Interface(
|
|
146 |
allow_flagging="never",
|
147 |
)
|
148 |
|
|
|
149 |
with demo:
|
150 |
gr.TabbedInterface([mf_transcribe, file_transcribe, yt_transcribe], ["Microphone", "Audio file", "YouTube"])
|
151 |
|
|
|
152 |
demo.launch(enable_queue=True)
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import yt_dlp as youtube_dl
|
3 |
from transformers import pipeline
|
|
|
5 |
|
6 |
import tempfile
|
7 |
import os
|
8 |
+
import time # Hinzugefügtes Modul für die Zeitberechnung
|
9 |
|
10 |
MODEL_NAME = "openai/whisper-large-v3"
|
11 |
BATCH_SIZE = 8
|
|
|
21 |
device=device,
|
22 |
)
|
23 |
|
|
|
24 |
def transcribe(inputs, task):
|
25 |
if inputs is None:
|
26 |
raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
|
|
|
28 |
text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
|
29 |
return text
|
30 |
|
31 |
+
# ... (Fortsetzung des Codes für die Funktionen _return_yt_html_embed, download_yt_audio, yt_transcribe, etc.)
|
32 |
|
33 |
+
# Schritt 1: Definiere das gr.Blocks-Element für das Layout der Demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
demo = gr.Blocks(theme="TogetherAi/Alex2")
|
35 |
|
36 |
+
# Schritt 2: Ändere das Layout für das Blockelement auf "centered" und setze die Breite auf 500 Pixel
|
37 |
+
demo.layout = "centered" # Layout auf "centered" ändern
|
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=[
|
|
|
45 |
gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
|
46 |
],
|
47 |
outputs="text",
|
48 |
+
layout="horizontal",
|
49 |
+
theme="TogetherAi/Alex2",
|
50 |
+
title="Whisper Large V3: Audio transkribieren",
|
51 |
+
description=(
|
52 |
+
"Transkribiere lange Mikrofon- oder Audioeingaben mit einem Klick! Die Demo verwendet den"
|
53 |
+
f" Checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) und 🤗 Transformers, um Audiodateien"
|
54 |
+
" beliebiger Länge zu transkribieren."
|
55 |
+
),
|
56 |
+
allow_flagging="never",
|
|
|
|
|
57 |
)
|
58 |
|
59 |
file_transcribe = gr.Interface(
|
|
|
67 |
theme="TogetherAi/Alex2",
|
68 |
title="Whisper Large V3: Transcribe Audio",
|
69 |
description=(
|
70 |
+
"Transkribiere lange Mikrofon- oder Audioeingaben mit einem Klick! Die Demo verwendet den"
|
71 |
+
f" Checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) und 🤗 Transformers, um Audiodateien"
|
72 |
+
" beliebiger Länge zu transkribieren."
|
73 |
),
|
74 |
allow_flagging="never",
|
75 |
)
|
|
|
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 |
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 |
+
|