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