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