Spaces:
Sleeping
Sleeping
add tiny whisper
Browse files
app.py
CHANGED
|
@@ -1,7 +1,49 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import yt_dlp
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
from faster_whisper import WhisperModel
|
| 6 |
+
# tiny, tiny.en, base, base.en, small, small.en, medium, medium.en, large-v1, large-v2, large-v3, or large
|
| 7 |
+
model_name = 'tiny'
|
| 8 |
+
model = WhisperModel(model_name, device="cpu", local_files_only=True)
|
| 9 |
|
| 10 |
+
ydl_opts = {
|
| 11 |
+
'outtmpl': 'demo.m4a',
|
| 12 |
+
'format': 'm4a/bestaudio/best',
|
| 13 |
+
'postprocessors': [{ # Extract audio using ffmpeg
|
| 14 |
+
'key': 'FFmpegExtractAudio',
|
| 15 |
+
'preferredcodec': 'm4a',
|
| 16 |
+
}],
|
| 17 |
+
# 'proxy': 'socks5://192.168.2.18:20170',
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
def download_audio(url):
|
| 21 |
+
if os.path.exists('demo.m4a'):
|
| 22 |
+
os.remove('demo.m4a')
|
| 23 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 24 |
+
code = ydl.download([url])
|
| 25 |
+
assert code == 0, "Failed to download audio"
|
| 26 |
+
|
| 27 |
+
segments, info = model.transcribe("demo.m4a", beam_size=5)
|
| 28 |
+
partial_message = ""
|
| 29 |
+
for segment in segments:
|
| 30 |
+
msg = "[%.2fs -> %.2fs] %s\n" % (segment.start, segment.end, segment.text)
|
| 31 |
+
partial_message += msg
|
| 32 |
+
yield partial_message
|
| 33 |
+
|
| 34 |
+
with gr.Blocks() as demo:
|
| 35 |
+
with gr.Column():
|
| 36 |
+
name = gr.Textbox(label="Enter your youtube url")
|
| 37 |
+
button = gr.Button("Download")
|
| 38 |
+
|
| 39 |
+
with gr.Column():
|
| 40 |
+
output = gr.TextArea(label="Output")
|
| 41 |
+
|
| 42 |
+
button.click(
|
| 43 |
+
download_audio,
|
| 44 |
+
inputs=[name],
|
| 45 |
+
outputs=[output],
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
demo.launch()
|