shamik-lseg commited on
Commit
d611082
β€’
1 Parent(s): 7b3c699

Added all the files for the demo to run.

Browse files
Files changed (5) hide show
  1. README.md +2 -2
  2. app.py +95 -0
  3. example1.flac +0 -0
  4. example2.flac +0 -0
  5. requirements.txt +2 -0
README.md CHANGED
@@ -1,6 +1,6 @@
1
  ---
2
- title: Whisper Small English
3
- emoji: πŸŒ–
4
  colorFrom: gray
5
  colorTo: indigo
6
  sdk: gradio
 
1
  ---
2
+ title: Whisper Small English Transcription and Translation
3
+ emoji: πŸ“ πŸ—£οΈ ✍️ πŸ“
4
  colorFrom: gray
5
  colorTo: indigo
6
  sdk: gradio
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import pipeline
3
+ import gradio as gr
4
+
5
+ MODEL_NAME = "openai/whisper-small"
6
+ BATCH_SIZE = 8
7
+
8
+ device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
9
+
10
+ pipe = pipeline(
11
+ task="automatic-speech-recognition",
12
+ model=MODEL_NAME,
13
+ chunk_length_s=30,
14
+ device=device,
15
+ )
16
+
17
+
18
+ # Copied from https://github.com/openai/whisper/blob/c09a7ae299c4c34c5839a76380ae407e7d785914/whisper/utils.py#L50
19
+ def format_timestamp(seconds: float, always_include_hours: bool = False, decimal_marker: str = "."):
20
+ if seconds is not None:
21
+ milliseconds = round(seconds * 1000.0)
22
+
23
+ hours = milliseconds // 3_600_000
24
+ milliseconds -= hours * 3_600_000
25
+
26
+ minutes = milliseconds // 60_000
27
+ milliseconds -= minutes * 60_000
28
+
29
+ seconds = milliseconds // 1_000
30
+ milliseconds -= seconds * 1_000
31
+
32
+ hours_marker = f"{hours:02d}:" if always_include_hours or hours > 0 else ""
33
+ return f"{hours_marker}{minutes:02d}:{seconds:02d}{decimal_marker}{milliseconds:03d}"
34
+ else:
35
+ # we have a malformed timestamp so just return it as is
36
+ return seconds
37
+
38
+ def transcribe(file, task, return_timestamps):
39
+ outputs = pipe(file, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=return_timestamps)
40
+ text = outputs["text"]
41
+ if return_timestamps:
42
+ timestamps = outputs["chunks"]
43
+ timestamps = [
44
+ f"[{format_timestamp(chunk['timestamp'][0])} -> {format_timestamp(chunk['timestamp'][1])}] {chunk['text']}"
45
+ for chunk in timestamps
46
+ ]
47
+ text = "\n".join(str(feature) for feature in timestamps)
48
+ return text
49
+
50
+ demo = gr.Blocks()
51
+
52
+ mic_transcribe = gr.Interface(
53
+ fn=transcribe,
54
+ inputs=[
55
+ gr.Audio(sources="microphone", type="filepath"),
56
+ gr.Radio(["transcribe", "translate"],value="transcribe",label="Task"),
57
+ gr.Checkbox(value=False, label="Return timestamps"),
58
+ ],
59
+ outputs="text",
60
+ title="Whisper English Speech Transcription and Translation",
61
+ description=(
62
+ "Transcribe long-form microphone audio with the click of a button! Demo uses the"
63
+ f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and πŸ€— Transformers to transcribe audio files"
64
+ " of arbitrary length."
65
+ ),
66
+ allow_flagging="never",
67
+ )
68
+
69
+ file_transcribe = gr.Interface(
70
+ fn=transcribe,
71
+ inputs=[
72
+ gr.Audio(sources="upload", label="Audio file", type="filepath"),
73
+ gr.Radio(["transcribe", "translate"],value="transcribe",label="Task"),
74
+ gr.Checkbox(value=False, label="Return timestamps"),
75
+ ],
76
+ outputs="text",
77
+ title="Whisper English Speech Transcription and Translation",
78
+ description=(
79
+ "Transcribe long-form audio inputs with the click of a button! Demo uses the"
80
+ f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and πŸ€— Transformers to transcribe audio files"
81
+ " of arbitrary length."
82
+ ),
83
+ examples=[
84
+ ["./example1.flac","transcribe", True],
85
+ ["./example2.flac","translate", True],
86
+ ],
87
+ cache_examples=True,
88
+ allow_flagging="never",
89
+ )
90
+
91
+ with demo:
92
+ gr.TabbedInterface([mic_transcribe, file_transcribe], ["Transcribe Microphone", "Transcribe Audio File"])
93
+
94
+ # demo.queue()
95
+ demo.launch()
example1.flac ADDED
Binary file (75.5 kB). View file
 
example2.flac ADDED
Binary file (134 kB). View file
 
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ transformers
2
+ torch