Update app.py
Browse files
app.py
CHANGED
@@ -1,79 +1,87 @@
|
|
1 |
-
import
|
2 |
-
import torch
|
3 |
-
|
4 |
-
import gradio as gr
|
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 |
|
12 |
-
|
13 |
-
BATCH_SIZE = 8
|
14 |
FILE_LIMIT_MB = 1000
|
15 |
YT_LENGTH_LIMIT_S = 3600 # limit to 1 hour YouTube files
|
16 |
|
17 |
-
device = 0 if torch.cuda.is_available() else "cpu"
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
)
|
26 |
|
|
|
|
|
|
|
27 |
|
28 |
@spaces.GPU
|
29 |
def transcribe(inputs, task):
|
30 |
if inputs is None:
|
31 |
-
raise gr.Error(
|
|
|
32 |
|
33 |
-
text = pipe(inputs,
|
34 |
-
|
|
|
35 |
|
36 |
|
37 |
def _return_yt_html_embed(yt_url):
|
38 |
video_id = yt_url.split("?v=")[-1]
|
39 |
HTML_str = (
|
40 |
-
f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{
|
|
|
41 |
" </center>"
|
42 |
)
|
43 |
return HTML_str
|
44 |
|
|
|
45 |
def download_yt_audio(yt_url, filename):
|
46 |
info_loader = youtube_dl.YoutubeDL()
|
47 |
-
|
48 |
try:
|
49 |
info = info_loader.extract_info(yt_url, download=False)
|
50 |
except youtube_dl.utils.DownloadError as err:
|
51 |
raise gr.Error(str(err))
|
52 |
-
|
53 |
file_length = info["duration_string"]
|
54 |
file_h_m_s = file_length.split(":")
|
55 |
file_h_m_s = [int(sub_length) for sub_length in file_h_m_s]
|
56 |
-
|
57 |
if len(file_h_m_s) == 1:
|
58 |
file_h_m_s.insert(0, 0)
|
59 |
if len(file_h_m_s) == 2:
|
60 |
file_h_m_s.insert(0, 0)
|
61 |
file_length_s = file_h_m_s[0] * 3600 + file_h_m_s[1] * 60 + file_h_m_s[2]
|
62 |
-
|
63 |
if file_length_s > YT_LENGTH_LIMIT_S:
|
64 |
-
yt_length_limit_hms = time.strftime(
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
70 |
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
71 |
try:
|
72 |
ydl.download([yt_url])
|
73 |
except youtube_dl.utils.ExtractorError as err:
|
74 |
raise gr.Error(str(err))
|
75 |
|
76 |
-
|
77 |
def yt_transcribe(yt_url, task, max_filesize=75.0):
|
78 |
html_embed_str = _return_yt_html_embed(yt_url)
|
79 |
|
@@ -84,65 +92,70 @@ def yt_transcribe(yt_url, task, max_filesize=75.0):
|
|
84 |
inputs = f.read()
|
85 |
|
86 |
inputs = ffmpeg_read(inputs, pipe.feature_extractor.sampling_rate)
|
87 |
-
inputs = {"array": inputs,
|
|
|
88 |
|
89 |
-
text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={
|
|
|
90 |
|
91 |
return html_embed_str, text
|
92 |
|
93 |
|
94 |
-
demo = gr.Blocks(
|
95 |
|
96 |
-
|
97 |
fn=transcribe,
|
98 |
inputs=[
|
99 |
-
gr.Audio(sources="
|
100 |
-
gr.Radio(["transcribe"
|
101 |
],
|
102 |
outputs="text",
|
103 |
title="Whisper Large V3 Turbo Shqip: Transcribe Audio",
|
104 |
description=(
|
105 |
-
"
|
106 |
-
f"
|
107 |
-
" of
|
108 |
-
|
|
|
109 |
allow_flagging="never",
|
110 |
)
|
111 |
|
112 |
-
|
113 |
fn=transcribe,
|
114 |
inputs=[
|
115 |
-
gr.Audio(sources="
|
116 |
-
gr.Radio(["transcribe"
|
117 |
],
|
118 |
outputs="text",
|
119 |
title="Whisper Large V3 Turbo Shqip: Transcribe Audio",
|
120 |
description=(
|
121 |
-
"
|
122 |
-
f"
|
123 |
-
" of
|
124 |
-
|
|
|
125 |
allow_flagging="never",
|
126 |
)
|
127 |
|
128 |
yt_transcribe = gr.Interface(
|
129 |
fn=yt_transcribe,
|
130 |
inputs=[
|
131 |
-
gr.Textbox(
|
132 |
-
|
|
|
133 |
],
|
134 |
outputs=["html", "text"],
|
135 |
-
title="Whisper Large V3 Turbo Shqip: Transcribe
|
136 |
description=(
|
137 |
-
"
|
138 |
-
f" [{
|
139 |
-
"
|
140 |
-
|
|
|
141 |
allow_flagging="never",
|
142 |
)
|
143 |
|
144 |
with demo:
|
145 |
-
gr.TabbedInterface([mf_transcribe, file_transcribe], ["Microphone", "Audio file"])
|
146 |
-
|
147 |
-
demo.queue().launch(ssr_mode=False)
|
148 |
|
|
|
|
1 |
+
from transformers import pipeline, AutoModelForSpeechSeq2Seq, AutoProcessor
|
|
|
|
|
|
|
|
|
|
|
2 |
from transformers.pipelines.audio_utils import ffmpeg_read
|
3 |
+
import yt_dlp as youtube_dl
|
4 |
+
import gradio as gr
|
5 |
import tempfile
|
6 |
+
import torch
|
7 |
+
import time
|
8 |
import os
|
9 |
|
10 |
+
BATCH_SIZE = 16
|
|
|
11 |
FILE_LIMIT_MB = 1000
|
12 |
YT_LENGTH_LIMIT_S = 3600 # limit to 1 hour YouTube files
|
13 |
|
14 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
15 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
16 |
+
model_id = "Kushtrim/whisper-large-v3-turbo-shqip"
|
17 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
18 |
+
model_id, torch_dtype=torch_dtype, use_safetensors=True, token=True).to(device)
|
19 |
+
processor = AutoProcessor.from_pretrained(model_id, token=True)
|
20 |
+
pipe = pipeline("automatic-speech-recognition", model=model, tokenizer=processor.tokenizer, feature_extractor=processor.feature_extractor,
|
21 |
+
chunk_length_s=30, torch_dtype=torch_dtype, device=device,
|
22 |
+
token=os.environ["HF"])
|
23 |
|
24 |
+
# pipe = pipeline("automatic-speech-recognition", model=model, tokenizer=processor.tokenizer, feature_extractor=processor.feature_extractor,
|
25 |
+
# max_new_tokens=128, chunk_length_s=15, batch_size=16, torch_dtype=torch_dtype, device=device,
|
26 |
+
# token=os.environ["HF"])
|
27 |
|
28 |
@spaces.GPU
|
29 |
def transcribe(inputs, task):
|
30 |
if inputs is None:
|
31 |
+
raise gr.Error(
|
32 |
+
"No audio file submitted! Please upload or record an audio file before submitting your request.")
|
33 |
|
34 |
+
text = pipe(inputs, generate_kwargs={
|
35 |
+
"task": task, 'language': 'sq'}, return_timestamps=True)["text"]
|
36 |
+
return text
|
37 |
|
38 |
|
39 |
def _return_yt_html_embed(yt_url):
|
40 |
video_id = yt_url.split("?v=")[-1]
|
41 |
HTML_str = (
|
42 |
+
f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{
|
43 |
+
video_id}"> </iframe>'
|
44 |
" </center>"
|
45 |
)
|
46 |
return HTML_str
|
47 |
|
48 |
+
|
49 |
def download_yt_audio(yt_url, filename):
|
50 |
info_loader = youtube_dl.YoutubeDL()
|
51 |
+
|
52 |
try:
|
53 |
info = info_loader.extract_info(yt_url, download=False)
|
54 |
except youtube_dl.utils.DownloadError as err:
|
55 |
raise gr.Error(str(err))
|
56 |
+
|
57 |
file_length = info["duration_string"]
|
58 |
file_h_m_s = file_length.split(":")
|
59 |
file_h_m_s = [int(sub_length) for sub_length in file_h_m_s]
|
60 |
+
|
61 |
if len(file_h_m_s) == 1:
|
62 |
file_h_m_s.insert(0, 0)
|
63 |
if len(file_h_m_s) == 2:
|
64 |
file_h_m_s.insert(0, 0)
|
65 |
file_length_s = file_h_m_s[0] * 3600 + file_h_m_s[1] * 60 + file_h_m_s[2]
|
66 |
+
|
67 |
if file_length_s > YT_LENGTH_LIMIT_S:
|
68 |
+
yt_length_limit_hms = time.strftime(
|
69 |
+
"%HH:%MM:%SS", time.gmtime(YT_LENGTH_LIMIT_S))
|
70 |
+
file_length_hms = time.strftime(
|
71 |
+
"%HH:%MM:%SS", time.gmtime(file_length_s))
|
72 |
+
raise gr.Error(f"Maximum YouTube length is {yt_length_limit_hms}, got {
|
73 |
+
file_length_hms} YouTube video.")
|
74 |
+
|
75 |
+
ydl_opts = {"outtmpl": filename,
|
76 |
+
"format": "worstvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}
|
77 |
+
|
78 |
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
79 |
try:
|
80 |
ydl.download([yt_url])
|
81 |
except youtube_dl.utils.ExtractorError as err:
|
82 |
raise gr.Error(str(err))
|
83 |
|
84 |
+
|
85 |
def yt_transcribe(yt_url, task, max_filesize=75.0):
|
86 |
html_embed_str = _return_yt_html_embed(yt_url)
|
87 |
|
|
|
92 |
inputs = f.read()
|
93 |
|
94 |
inputs = ffmpeg_read(inputs, pipe.feature_extractor.sampling_rate)
|
95 |
+
inputs = {"array": inputs,
|
96 |
+
"sampling_rate": pipe.feature_extractor.sampling_rate}
|
97 |
|
98 |
+
text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={
|
99 |
+
"task": task}, return_timestamps=True)["text"]
|
100 |
|
101 |
return html_embed_str, text
|
102 |
|
103 |
|
104 |
+
demo = gr.Blocks()
|
105 |
|
106 |
+
file_transcribe = gr.Interface(
|
107 |
fn=transcribe,
|
108 |
inputs=[
|
109 |
+
gr.Audio(sources=["upload"], type="filepath", label="Audio file"),
|
110 |
+
gr.Radio(choices=["transcribe", "translate"], label="Task"),
|
111 |
],
|
112 |
outputs="text",
|
113 |
title="Whisper Large V3 Turbo Shqip: Transcribe Audio",
|
114 |
description=(
|
115 |
+
"Easily transcribe long-form audio inputs in Albanian with high accuracy! This demo utilizes the fine-tuned "
|
116 |
+
f"Whisper model [{model_id}](https://huggingface.co/{model_id}), specially adapted for the Albanian language, "
|
117 |
+
"powered by 🤗 Transformers. With just a click, transform microphone or audio file inputs of any length into "
|
118 |
+
"text with exceptional transcription quality."
|
119 |
+
)
|
120 |
allow_flagging="never",
|
121 |
)
|
122 |
|
123 |
+
mf_transcribe = gr.Interface(
|
124 |
fn=transcribe,
|
125 |
inputs=[
|
126 |
+
gr.Audio(sources=["microphone"], type="filepath"),
|
127 |
+
gr.Radio(choices=["transcribe", "translate"], label="Task"),
|
128 |
],
|
129 |
outputs="text",
|
130 |
title="Whisper Large V3 Turbo Shqip: Transcribe Audio",
|
131 |
description=(
|
132 |
+
"Easily transcribe long-form audio inputs in Albanian with high accuracy! This demo utilizes the fine-tuned "
|
133 |
+
f"Whisper model [{model_id}](https://huggingface.co/{model_id}), specially adapted for the Albanian language, "
|
134 |
+
"powered by 🤗 Transformers. With just a click, transform microphone or audio file inputs of any length into "
|
135 |
+
"text with exceptional transcription quality."
|
136 |
+
)
|
137 |
allow_flagging="never",
|
138 |
)
|
139 |
|
140 |
yt_transcribe = gr.Interface(
|
141 |
fn=yt_transcribe,
|
142 |
inputs=[
|
143 |
+
gr.Textbox(
|
144 |
+
lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
|
145 |
+
gr.Radio(choices=["transcribe", "translate"], label="Task")
|
146 |
],
|
147 |
outputs=["html", "text"],
|
148 |
+
title="Whisper Large V3 Turbo Shqip: Transcribe Audio",
|
149 |
description=(
|
150 |
+
"Easily transcribe long-form audio inputs in Albanian with high accuracy! This demo utilizes the fine-tuned "
|
151 |
+
f"Whisper model [{model_id}](https://huggingface.co/{model_id}), specially adapted for the Albanian language, "
|
152 |
+
"powered by 🤗 Transformers. With just a click, transform microphone or audio file inputs of any length into "
|
153 |
+
"text with exceptional transcription quality."
|
154 |
+
)
|
155 |
allow_flagging="never",
|
156 |
)
|
157 |
|
158 |
with demo:
|
159 |
+
gr.TabbedInterface([mf_transcribe, file_transcribe, yt_transcribe], ["Microphone", "Audio file", "YouTube"])
|
|
|
|
|
160 |
|
161 |
+
demo.launch()
|