Joserzapata sanchit-gandhi HF staff commited on
Commit
306c260
0 Parent(s):

Duplicate from course-demos/whisper-small

Browse files

Co-authored-by: Sanchit Gandhi <[email protected]>

Files changed (6) hide show
  1. .gitattributes +34 -0
  2. README.md +13 -0
  3. app.py +101 -0
  4. example.flac +0 -0
  5. packages.txt +1 -0
  6. 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,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Whisper Small
3
+ emoji: 🌍
4
+ colorFrom: pink
5
+ colorTo: pink
6
+ sdk: gradio
7
+ sdk_version: 3.29.0
8
+ app_file: app.py
9
+ pinned: false
10
+ duplicated_from: course-demos/whisper-small
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import pipeline
3
+ from transformers.pipelines.audio_utils import ffmpeg_read
4
+ import gradio as gr
5
+
6
+ MODEL_NAME = "openai/whisper-small"
7
+ BATCH_SIZE = 8
8
+
9
+ device = 0 if torch.cuda.is_available() else "cpu"
10
+
11
+ pipe = pipeline(
12
+ task="automatic-speech-recognition",
13
+ model=MODEL_NAME,
14
+ chunk_length_s=30,
15
+ device=device,
16
+ )
17
+
18
+
19
+ # Copied from https://github.com/openai/whisper/blob/c09a7ae299c4c34c5839a76380ae407e7d785914/whisper/utils.py#L50
20
+ def format_timestamp(seconds: float, always_include_hours: bool = False, decimal_marker: str = "."):
21
+ if seconds is not None:
22
+ milliseconds = round(seconds * 1000.0)
23
+
24
+ hours = milliseconds // 3_600_000
25
+ milliseconds -= hours * 3_600_000
26
+
27
+ minutes = milliseconds // 60_000
28
+ milliseconds -= minutes * 60_000
29
+
30
+ seconds = milliseconds // 1_000
31
+ milliseconds -= seconds * 1_000
32
+
33
+ hours_marker = f"{hours:02d}:" if always_include_hours or hours > 0 else ""
34
+ return f"{hours_marker}{minutes:02d}:{seconds:02d}{decimal_marker}{milliseconds:03d}"
35
+ else:
36
+ # we have a malformed timestamp so just return it as is
37
+ return seconds
38
+
39
+
40
+ def transcribe(file, task, return_timestamps):
41
+ outputs = pipe(file, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=return_timestamps)
42
+ text = outputs["text"]
43
+ if return_timestamps:
44
+ timestamps = outputs["chunks"]
45
+ timestamps = [
46
+ f"[{format_timestamp(chunk['timestamp'][0])} -> {format_timestamp(chunk['timestamp'][1])}] {chunk['text']}"
47
+ for chunk in timestamps
48
+ ]
49
+ text = "\n".join(str(feature) for feature in timestamps)
50
+ return text
51
+
52
+
53
+ demo = gr.Blocks()
54
+
55
+ mic_transcribe = gr.Interface(
56
+ fn=transcribe,
57
+ inputs=[
58
+ gr.inputs.Audio(source="microphone", type="filepath", optional=True),
59
+ gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
60
+ gr.inputs.Checkbox(default=False, label="Return timestamps"),
61
+ ],
62
+ outputs="text",
63
+ layout="horizontal",
64
+ theme="huggingface",
65
+ title="Whisper Demo: Transcribe Audio",
66
+ description=(
67
+ "Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the"
68
+ f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
69
+ " of arbitrary length."
70
+ ),
71
+ allow_flagging="never",
72
+ )
73
+
74
+ file_transcribe = gr.Interface(
75
+ fn=transcribe,
76
+ inputs=[
77
+ gr.inputs.Audio(source="upload", optional=True, label="Audio file", type="filepath"),
78
+ gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
79
+ gr.inputs.Checkbox(default=False, label="Return timestamps"),
80
+ ],
81
+ outputs="text",
82
+ layout="horizontal",
83
+ theme="huggingface",
84
+ title="Whisper Demo: Transcribe Audio",
85
+ description=(
86
+ "Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the"
87
+ f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
88
+ " of arbitrary length."
89
+ ),
90
+ examples=[
91
+ ["./example.flac", "transcribe", False],
92
+ ["./example.flac", "transcribe", True],
93
+ ],
94
+ cache_examples=True,
95
+ allow_flagging="never",
96
+ )
97
+
98
+ with demo:
99
+ gr.TabbedInterface([mic_transcribe, file_transcribe], ["Transcribe Microphone", "Transcribe Audio File"])
100
+
101
+ demo.launch(enable_queue=True)
example.flac ADDED
Binary file (282 kB). View file
 
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ ffmpeg
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ --extra-index-url https://download.pytorch.org/whl/cu113
2
+ torch
3
+ transformers