shamik-lseg commited on
Commit
abd90ce
Β·
1 Parent(s): a8dc43c

Added the necessary files and scripts for running the app.

Browse files
Files changed (5) hide show
  1. README.md +2 -2
  2. app.py +96 -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 Bengali
3
- emoji: 🐠
4
  colorFrom: indigo
5
  colorTo: yellow
6
  sdk: gradio
 
1
  ---
2
+ title: Whisper Small Bengali Transcription
3
+ emoji: πŸ’¬ πŸ€™
4
  colorFrom: indigo
5
  colorTo: yellow
6
  sdk: gradio
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = "Shamik/whisper-small-bn"
7
+ BATCH_SIZE = 8
8
+
9
+ device = torch.device("cuda") if torch.cuda.is_available() else torch.device("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": "transcribe",
42
+ "language": "bengali"}, return_timestamps=return_timestamps)
43
+ text = outputs["text"]
44
+ if return_timestamps:
45
+ timestamps = outputs["chunks"]
46
+ timestamps = [
47
+ f"[{format_timestamp(chunk['timestamp'][0])} -> {format_timestamp(chunk['timestamp'][1])}] {chunk['text']}"
48
+ for chunk in timestamps
49
+ ]
50
+ text = "\n".join(str(feature) for feature in timestamps)
51
+ return text
52
+
53
+ demo = gr.Blocks()
54
+
55
+ mic_transcribe = gr.Interface(
56
+ fn=transcribe,
57
+ inputs=[
58
+ gr.Audio(sources="microphone", type="filepath"),
59
+ gr.Checkbox(value=False, label="Return timestamps"),
60
+ ],
61
+ outputs="text",
62
+ title="Whisper Bengali Speech Transcription",
63
+ description=(
64
+ "Transcribe long-form microphone audio with the click of a button! Demo uses the"
65
+ f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and πŸ€— Transformers to transcribe audio files"
66
+ " of arbitrary length."
67
+ ),
68
+ allow_flagging="never",
69
+ )
70
+
71
+ file_transcribe = gr.Interface(
72
+ fn=transcribe,
73
+ inputs=[
74
+ gr.Audio(sources="upload", label="Audio file", type="filepath"),
75
+ gr.Checkbox(value=False, label="Return timestamps"),
76
+ ],
77
+ outputs="text",
78
+ title="Whisper Bengali Speech Transcription",
79
+ description=(
80
+ "Transcribe long-form audio inputs with the click of a button! Demo uses the"
81
+ f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and πŸ€— Transformers to transcribe audio files"
82
+ " of arbitrary length."
83
+ ),
84
+ examples=[
85
+ ["./example1.flac", False],
86
+ ["./example1.flac", True],
87
+ ],
88
+ cache_examples=True,
89
+ allow_flagging="never",
90
+ )
91
+
92
+ with demo:
93
+ gr.TabbedInterface([mic_transcribe, file_transcribe], ["Transcribe Microphone", "Transcribe Audio File"])
94
+
95
+ # demo.queue()
96
+ demo.launch()
example1.flac ADDED
Binary file (90.3 kB). View file
 
example2.flac ADDED
Binary file (118 kB). View file
 
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ transformers
2
+ torch