deadpool007 sanchit-gandhi HF staff commited on
Commit
910fdf3
0 Parent(s):

Duplicate from sanchit-gandhi/whisper-large-v2

Browse files

Co-authored-by: Sanchit Gandhi <sanchit-gandhi@users.noreply.huggingface.co>

Files changed (5) hide show
  1. .gitattributes +34 -0
  2. README.md +15 -0
  3. app.py +127 -0
  4. packages.txt +1 -0
  5. requirements.txt +3 -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 Large V2
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: sanchit-gandhi/whisper-large-v2
13
+ ---
14
+
15
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+ import gradio as gr
4
+ import pytube as pt
5
+ from transformers import pipeline
6
+
7
+ MODEL_NAME = "openai/whisper-large-v2"
8
+ BATCH_SIZE = 8
9
+ FILE_LIMIT_MB = 1000
10
+ YT_ATTEMPT_LIMIT = 3
11
+
12
+ device = 0 if torch.cuda.is_available() else "cpu"
13
+
14
+ pipe = pipeline(
15
+ task="automatic-speech-recognition",
16
+ model=MODEL_NAME,
17
+ chunk_length_s=30,
18
+ device=device,
19
+ )
20
+
21
+
22
+ all_special_ids = pipe.tokenizer.all_special_ids
23
+ transcribe_token_id = all_special_ids[-5]
24
+ translate_token_id = all_special_ids[-6]
25
+
26
+
27
+ def transcribe(microphone, file_upload, task):
28
+ warn_output = ""
29
+ if (microphone is not None) and (file_upload is not None):
30
+ warn_output = (
31
+ "WARNING: You've uploaded an audio file and used the microphone. "
32
+ "The recorded file from the microphone will be used and the uploaded audio will be discarded.\n"
33
+ )
34
+
35
+ elif (microphone is None) and (file_upload is None):
36
+ raise gr.Error("You have to either use the microphone or upload an audio file")
37
+
38
+ file_size_mb = os.stat(inputs).st_size / (1024 * 1024)
39
+ if file_size_mb > FILE_LIMIT_MB:
40
+ raise gr.Error(
41
+ f"File size exceeds file size limit. Got file of size {file_size_mb:.2f}MB for a limit of {FILE_LIMIT_MB}MB."
42
+ )
43
+
44
+ file = microphone if microphone is not None else file_upload
45
+
46
+ pipe.model.config.forced_decoder_ids = [[2, transcribe_token_id if task=="transcribe" else translate_token_id]]
47
+
48
+ text = pipe(file, batch_size=BATCH_SIZE)["text"]
49
+
50
+ return warn_output + text
51
+
52
+
53
+ def _return_yt_html_embed(yt_url):
54
+ video_id = yt_url.split("?v=")[-1]
55
+ HTML_str = (
56
+ f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}"> </iframe>'
57
+ " </center>"
58
+ )
59
+ return HTML_str
60
+
61
+
62
+ def yt_transcribe(yt_url, task, max_filesize=75.0):
63
+ yt = pt.YouTube(yt_url)
64
+ html_embed_str = _return_yt_html_embed(yt_url)
65
+ for attempt in range(YT_ATTEMPT_LIMIT):
66
+ try:
67
+ yt = pytube.YouTube(yt_url)
68
+ stream = yt.streams.filter(only_audio=True)[0]
69
+ break
70
+ except KeyError:
71
+ if attempt + 1 == YT_ATTEMPT_LIMIT:
72
+ raise gr.Error("An error occurred while loading the YouTube video. Please try again.")
73
+
74
+ if stream.filesize_mb > max_filesize:
75
+ raise gr.Error(f"Maximum YouTube file size is {max_filesize}MB, got {stream.filesize_mb:.2f}MB.")
76
+
77
+ pipe.model.config.forced_decoder_ids = [[2, transcribe_token_id if task=="transcribe" else translate_token_id]]
78
+
79
+ text = pipe("audio.mp3", batch_size=BATCH_SIZE)["text"]
80
+
81
+ return html_embed_str, text
82
+
83
+
84
+ demo = gr.Blocks()
85
+
86
+ mf_transcribe = gr.Interface(
87
+ fn=transcribe,
88
+ inputs=[
89
+ gr.inputs.Audio(source="microphone", type="filepath", optional=True),
90
+ gr.inputs.Audio(source="upload", type="filepath", optional=True),
91
+ gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
92
+ ],
93
+ outputs="text",
94
+ layout="horizontal",
95
+ theme="huggingface",
96
+ title="Whisper Large V2: Transcribe Audio",
97
+ description=(
98
+ "Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the"
99
+ f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
100
+ " of arbitrary length."
101
+ ),
102
+ allow_flagging="never",
103
+ )
104
+
105
+ yt_transcribe = gr.Interface(
106
+ fn=yt_transcribe,
107
+ inputs=[
108
+ gr.inputs.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
109
+ gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe")
110
+ ],
111
+ outputs=["html", "text"],
112
+ layout="horizontal",
113
+ theme="huggingface",
114
+ title="Whisper Large V2: Transcribe YouTube",
115
+ description=(
116
+ "Transcribe long-form YouTube videos with the click of a button! Demo uses the checkpoint"
117
+ f" [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe video files of"
118
+ " arbitrary length."
119
+ ),
120
+ allow_flagging="never",
121
+ )
122
+
123
+ with demo:
124
+ gr.TabbedInterface([mf_transcribe, yt_transcribe], ["Transcribe Audio", "Transcribe YouTube"])
125
+
126
+ demo.launch(enable_queue=True)
127
+
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