Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- app.py +96 -0
- packages.txt +1 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
import pytube as pt
|
5 |
+
from transformers import pipeline
|
6 |
+
|
7 |
+
|
8 |
+
MODEL_NAME = "whispy/whisper_italian"
|
9 |
+
|
10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
+
|
12 |
+
pipe = pipeline(
|
13 |
+
task="automatic-speech-recognition",
|
14 |
+
model=MODEL_NAME,
|
15 |
+
chunk_length_s=30,
|
16 |
+
device=device,
|
17 |
+
)
|
18 |
+
|
19 |
+
|
20 |
+
def transcribe(microphone, file_upload):
|
21 |
+
warn_output = ""
|
22 |
+
if (microphone is not None) and (file_upload is not None):
|
23 |
+
warn_output = (
|
24 |
+
"WARNING: You've uploaded an audio file and used the microphone. "
|
25 |
+
"The recorded file from the microphone will be used and the uploaded audio will be discarded.\n"
|
26 |
+
)
|
27 |
+
|
28 |
+
elif (microphone is None) and (file_upload is None):
|
29 |
+
return "ERROR: You have to either use the microphone or upload an audio file"
|
30 |
+
|
31 |
+
file = microphone if microphone is not None else file_upload
|
32 |
+
|
33 |
+
text = pipe(file)["text"]
|
34 |
+
|
35 |
+
return warn_output + text
|
36 |
+
|
37 |
+
|
38 |
+
def _return_yt_html_embed(yt_url):
|
39 |
+
video_id = yt_url.split("?v=")[-1]
|
40 |
+
HTML_str = (
|
41 |
+
f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}"> </iframe>'
|
42 |
+
" </center>"
|
43 |
+
)
|
44 |
+
return HTML_str
|
45 |
+
|
46 |
+
|
47 |
+
def yt_transcribe(yt_url):
|
48 |
+
yt = pt.YouTube(yt_url)
|
49 |
+
html_embed_str = _return_yt_html_embed(yt_url)
|
50 |
+
stream = yt.streams.filter(only_audio=True)[0]
|
51 |
+
stream.download(filename="audio.mp3")
|
52 |
+
|
53 |
+
text = pipe("audio.mp3")["text"]
|
54 |
+
|
55 |
+
return html_embed_str, text
|
56 |
+
|
57 |
+
|
58 |
+
demo = gr.Blocks()
|
59 |
+
|
60 |
+
mf_transcribe = gr.Interface(
|
61 |
+
fn=transcribe,
|
62 |
+
inputs=[
|
63 |
+
gr.inputs.Audio(source="microphone", type="filepath", optional=True),
|
64 |
+
gr.inputs.Audio(source="upload", type="filepath", optional=True),
|
65 |
+
],
|
66 |
+
outputs="text",
|
67 |
+
layout="horizontal",
|
68 |
+
theme="huggingface",
|
69 |
+
title="Whisper Demo: Transcribe Audio",
|
70 |
+
description=(
|
71 |
+
"Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the the fine-tuned"
|
72 |
+
f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
|
73 |
+
" of arbitrary length."
|
74 |
+
),
|
75 |
+
allow_flagging="never",
|
76 |
+
)
|
77 |
+
|
78 |
+
yt_transcribe = gr.Interface(
|
79 |
+
fn=yt_transcribe,
|
80 |
+
inputs=[gr.inputs.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL")],
|
81 |
+
outputs=["html", "text"],
|
82 |
+
layout="horizontal",
|
83 |
+
theme="huggingface",
|
84 |
+
title="Whisper Demo: Transcribe YouTube",
|
85 |
+
description=(
|
86 |
+
"Transcribe long-form YouTube videos with the click of a button! Demo uses the the fine-tuned checkpoint:"
|
87 |
+
f" [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files of"
|
88 |
+
" arbitrary length."
|
89 |
+
),
|
90 |
+
allow_flagging="never",
|
91 |
+
)
|
92 |
+
|
93 |
+
with demo:
|
94 |
+
gr.TabbedInterface([mf_transcribe, yt_transcribe], ["Transcribe Audio", "Transcribe YouTube"])
|
95 |
+
|
96 |
+
demo.launch(enable_queue=True)
|
packages.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
ffmpeg
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
torch
|
3 |
+
#pytube
|