bofenghuang bofenghuang commited on
Commit
8aaccc9
0 Parent(s):

Duplicate from bofenghuang/whisper-demo-french

Browse files

Co-authored-by: bofeng huang <bofenghuang@users.noreply.huggingface.co>

Files changed (7) hide show
  1. .gitattributes +34 -0
  2. README.md +15 -0
  3. app.py +1 -0
  4. packages.txt +1 -0
  5. requirements.txt +3 -0
  6. run_demo.py +97 -0
  7. run_demo_multi_models.py +148 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Whisper French Demo
3
+ emoji: 🤫
4
+ colorFrom: indigo
5
+ colorTo: red
6
+ sdk: gradio
7
+ sdk_version: 3.9.1
8
+ app_file: app.py
9
+ pinned: false
10
+ tags:
11
+ - whisper-event
12
+ duplicated_from: bofenghuang/whisper-demo-french
13
+ ---
14
+
15
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1 @@
 
 
1
+ run_demo_multi_models.py
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ ffmpeg
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ git+https://github.com/huggingface/transformers
2
+ torch
3
+ pytube
run_demo.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = "bofenghuang/whisper-medium-cv11-french-punct"
9
+ CHUNK_LENGTH_S = 30
10
+
11
+ device = 0 if torch.cuda.is_available() else "cpu"
12
+ pipe = pipeline(
13
+ task="automatic-speech-recognition",
14
+ model=MODEL_NAME,
15
+ chunk_length_s=CHUNK_LENGTH_S,
16
+ device=device,
17
+ )
18
+
19
+ pipe.model.config.forced_decoder_ids = pipe.tokenizer.get_decoder_prompt_ids(language="fr", task="transcribe")
20
+
21
+ def transcribe(microphone, file_upload):
22
+ warn_output = ""
23
+ if (microphone is not None) and (file_upload is not None):
24
+ warn_output = (
25
+ "WARNING: You've uploaded an audio file and used the microphone. "
26
+ "The recorded file from the microphone will be used and the uploaded audio will be discarded.\n"
27
+ )
28
+
29
+ elif (microphone is None) and (file_upload is None):
30
+ return "ERROR: You have to either use the microphone or upload an audio file"
31
+
32
+ file = microphone if microphone is not None else file_upload
33
+
34
+ text = pipe(file)["text"]
35
+
36
+ return warn_output + 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/{video_id}"> </iframe>'
43
+ " </center>"
44
+ )
45
+ return HTML_str
46
+
47
+
48
+ def yt_transcribe(yt_url):
49
+ yt = pt.YouTube(yt_url)
50
+ html_embed_str = _return_yt_html_embed(yt_url)
51
+ stream = yt.streams.filter(only_audio=True)[0]
52
+ stream.download(filename="audio.mp3")
53
+
54
+ text = pipe("audio.mp3")["text"]
55
+
56
+ return html_embed_str, text
57
+
58
+
59
+ demo = gr.Blocks()
60
+
61
+ mf_transcribe = gr.Interface(
62
+ fn=transcribe,
63
+ inputs=[
64
+ gr.inputs.Audio(source="microphone", type="filepath", optional=True),
65
+ gr.inputs.Audio(source="upload", type="filepath", optional=True),
66
+ ],
67
+ outputs="text",
68
+ layout="horizontal",
69
+ theme="huggingface",
70
+ title="Whisper Demo: Transcribe Audio",
71
+ description=(
72
+ "Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the the fine-tuned"
73
+ f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
74
+ " of arbitrary length."
75
+ ),
76
+ allow_flagging="never",
77
+ )
78
+
79
+ yt_transcribe = gr.Interface(
80
+ fn=yt_transcribe,
81
+ inputs=[gr.inputs.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL")],
82
+ outputs=["html", "text"],
83
+ layout="horizontal",
84
+ theme="huggingface",
85
+ title="Whisper Demo: Transcribe YouTube",
86
+ description=(
87
+ "Transcribe long-form YouTube videos with the click of a button! Demo uses the the fine-tuned checkpoint:"
88
+ f" [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files of"
89
+ " arbitrary length."
90
+ ),
91
+ allow_flagging="never",
92
+ )
93
+
94
+ with demo:
95
+ gr.TabbedInterface([mf_transcribe, yt_transcribe], ["Transcribe Audio", "Transcribe YouTube"])
96
+
97
+ demo.launch(enable_queue=True)
run_demo_multi_models.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ import warnings
3
+
4
+ import gradio as gr
5
+ import pytube as pt
6
+ import torch
7
+ from huggingface_hub import model_info
8
+ from transformers import pipeline
9
+ from transformers.utils.logging import disable_progress_bar
10
+
11
+ warnings.filterwarnings("ignore")
12
+ disable_progress_bar()
13
+
14
+ DEFAULT_MODEL_NAME = "bofenghuang/whisper-medium-cv11-french-punct"
15
+ MODEL_NAMES = [
16
+ "openai/whisper-small",
17
+ "openai/whisper-medium",
18
+ "openai/whisper-large-v2",
19
+ "bofenghuang/whisper-small-cv11-french",
20
+ "bofenghuang/whisper-small-cv11-french-punct",
21
+ "bofenghuang/whisper-medium-cv11-french",
22
+ "bofenghuang/whisper-medium-cv11-french-punct",
23
+ ]
24
+ CHUNK_LENGTH_S = 30
25
+ MAX_NEW_TOKENS = 225
26
+
27
+ logging.basicConfig(
28
+ format="%(asctime)s [%(levelname)s] [%(name)s] %(message)s",
29
+ datefmt="%Y-%m-%dT%H:%M:%SZ",
30
+ )
31
+ logger = logging.getLogger(__name__)
32
+ logger.setLevel(logging.DEBUG)
33
+
34
+ device = 0 if torch.cuda.is_available() else "cpu"
35
+ logger.info(f"Model will be loaded on device {device}")
36
+
37
+ cached_models = {}
38
+
39
+ def maybe_load_cached_pipeline(model_name):
40
+ pipe = cached_models.get(model_name)
41
+ if pipe is None:
42
+ # load pipeline
43
+ # todo: set decoding option for pipeline
44
+ pipe = pipeline(
45
+ task="automatic-speech-recognition",
46
+ model=model_name,
47
+ chunk_length_s=CHUNK_LENGTH_S,
48
+ device=device,
49
+ )
50
+ # set forced_decoder_ids
51
+ pipe.model.config.forced_decoder_ids = pipe.tokenizer.get_decoder_prompt_ids(language="fr", task="transcribe")
52
+ # limit genneration max length
53
+ pipe.model.config.max_length = MAX_NEW_TOKENS + 1
54
+
55
+ logger.info(f"`{model_name}` pipeline has been initialized")
56
+
57
+ cached_models[model_name] = pipe
58
+ return pipe
59
+
60
+
61
+ def transcribe(microphone, file_upload, model_name):
62
+ warn_output = ""
63
+ if (microphone is not None) and (file_upload is not None):
64
+ warn_output = (
65
+ "WARNING: You've uploaded an audio file and used the microphone. "
66
+ "The recorded file from the microphone will be used and the uploaded audio will be discarded.\n"
67
+ )
68
+
69
+ elif (microphone is None) and (file_upload is None):
70
+ return "ERROR: You have to either use the microphone or upload an audio file"
71
+
72
+ file = microphone if microphone is not None else file_upload
73
+
74
+ pipe = maybe_load_cached_pipeline(model_name)
75
+ text = pipe(file)["text"]
76
+
77
+ logger.info(f"Transcription: {text}")
78
+
79
+ return warn_output + text
80
+
81
+
82
+ def _return_yt_html_embed(yt_url):
83
+ video_id = yt_url.split("?v=")[-1]
84
+ HTML_str = (
85
+ f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}"> </iframe>'
86
+ " </center>"
87
+ )
88
+ return HTML_str
89
+
90
+
91
+ def yt_transcribe(yt_url, model_name):
92
+ yt = pt.YouTube(yt_url)
93
+ html_embed_str = _return_yt_html_embed(yt_url)
94
+ stream = yt.streams.filter(only_audio=True)[0]
95
+ stream.download(filename="audio.mp3")
96
+
97
+ pipe = maybe_load_cached_pipeline(model_name)
98
+ text = pipe("audio.mp3")["text"]
99
+
100
+ logger.info(f"Transcription: {text}")
101
+
102
+ return html_embed_str, text
103
+
104
+
105
+ # load default model
106
+ maybe_load_cached_pipeline(DEFAULT_MODEL_NAME)
107
+
108
+ demo = gr.Blocks()
109
+
110
+ mf_transcribe = gr.Interface(
111
+ fn=transcribe,
112
+ inputs=[
113
+ gr.inputs.Audio(source="microphone", type="filepath", optional=True, label="Record"),
114
+ gr.inputs.Audio(source="upload", type="filepath", optional=True, label="Upload File"),
115
+ gr.inputs.Dropdown(choices=MODEL_NAMES, default=DEFAULT_MODEL_NAME, label="Whisper Model"),
116
+ ],
117
+ # outputs="text",
118
+ outputs=gr.outputs.Textbox(label="Transcription"),
119
+ layout="horizontal",
120
+ theme="huggingface",
121
+ title="Whisper Demo: Transcribe Audio",
122
+ description="Transcribe long-form microphone or audio inputs with the click of a button!",
123
+ allow_flagging="never",
124
+ )
125
+
126
+ yt_transcribe = gr.Interface(
127
+ fn=yt_transcribe,
128
+ inputs=[
129
+ gr.inputs.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
130
+ gr.inputs.Dropdown(choices=MODEL_NAMES, default=DEFAULT_MODEL_NAME, label="Whisper Model"),
131
+ ],
132
+ # outputs=["html", "text"],
133
+ outputs=[
134
+ gr.outputs.HTML(label="YouTube Page"),
135
+ gr.outputs.Textbox(label="Transcription"),
136
+ ],
137
+ layout="horizontal",
138
+ theme="huggingface",
139
+ title="Whisper Demo: Transcribe YouTube",
140
+ description="Transcribe long-form YouTube videos with the click of a button!",
141
+ allow_flagging="never",
142
+ )
143
+
144
+ with demo:
145
+ gr.TabbedInterface([mf_transcribe, yt_transcribe], ["Transcribe Audio", "Transcribe YouTube"])
146
+
147
+ # demo.launch(server_name="0.0.0.0", debug=True, share=True)
148
+ demo.launch(enable_queue=True)