File size: 12,572 Bytes
ecc934a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
502cdb1
ecc934a
 
 
 
502cdb1
ecc934a
 
 
 
 
 
 
 
 
 
502cdb1
 
ecc934a
 
 
 
 
 
 
502cdb1
 
ecc934a
 
 
 
 
502cdb1
ecc934a
 
 
 
502cdb1
 
ecc934a
502cdb1
ecc934a
502cdb1
 
 
 
 
 
 
 
 
 
 
 
ecc934a
 
 
 
502cdb1
ecc934a
 
 
 
 
502cdb1
 
 
 
 
 
 
 
 
 
 
 
 
ecc934a
 
 
 
 
 
 
 
 
 
 
 
 
502cdb1
ecc934a
 
 
 
 
 
 
502cdb1
 
ecc934a
 
 
 
 
 
 
 
502cdb1
ecc934a
 
 
 
 
 
502cdb1
ecc934a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
502cdb1
ecc934a
 
 
 
 
 
 
502cdb1
 
ecc934a
 
 
 
 
 
 
502cdb1
ecc934a
 
 
 
 
 
 
 
 
 
 
 
502cdb1
ecc934a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
502cdb1
ecc934a
 
502cdb1
 
 
 
 
 
 
 
 
 
 
 
ecc934a
502cdb1
ecc934a
502cdb1
ecc934a
 
 
 
 
502cdb1
ecc934a
502cdb1
ecc934a
502cdb1
ecc934a
 
 
 
 
 
390fc37
ecc934a
 
502cdb1
 
 
 
 
390fc37
 
502cdb1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
390fc37
502cdb1
 
 
 
 
 
 
 
ecc934a
 
 
 
 
 
 
 
 
 
 
502cdb1
ecc934a
 
 
 
 
 
 
502cdb1
ecc934a
 
 
 
 
 
502cdb1
 
ecc934a
 
 
 
 
 
 
 
 
 
 
 
390fc37
ecc934a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "807e94de-b600-46ca-9808-372619e38e69",
   "metadata": {},
   "source": [
    "# Making kurianbenoy/faster-speech-to-text-for-malayalam with Jupyter notebooks"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0e04921-4634-4d16-940f-bf8dd20bb63b",
   "metadata": {},
   "source": [
    "## Install packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7a6257dd-ea39-44e1-b103-3f9588d6cf4d",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install -Uqq nbdev gradio==3.31.0 faster-whisper==0.5.1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7ba223d-8043-4aab-8df3-f6cf3a4ac6b2",
   "metadata": {},
   "source": [
    "## Basic inference code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "22e6e9c5-7a3f-4546-8039-ecf98004235b",
   "metadata": {},
   "outputs": [],
   "source": [
    "#|export\n",
    "import gradio as gr\n",
    "from faster_whisper import WhisperModel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "81691362-0c73-4af0-9f99-96ffb7dc318b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'3.31.0'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gr.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "de8e21b9-449a-4ae3-bd64-bba334075fdd",
   "metadata": {},
   "outputs": [],
   "source": [
    "def t_asr(folder=\"vegam-whisper-medium-ml-fp16\", audio_file=\"vegam-whisper-medium-ml-fp16/00b38e80-80b8-4f70-babf-566e848879fc.webm\", compute_type=\"float16\", device=\"cpu\"):\n",
    "    model = WhisperModel(folder, device=device, compute_type=compute_type)\n",
    "    \n",
    "    segments, info = model.transcribe(audio_file, beam_size=5)\n",
    "    \n",
    "    for segment in segments:\n",
    "        print(\"[%.2fs -> %.2fs] %s\" % (segment.start, segment.end, segment.text))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "25e1413f-8f80-4704-a94e-26b8d9581a6a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.00s -> 4.58s] പാലം കടുക്കുവോളം നാരായണ പാലം കടന്നാലോ കൂരായണ\n",
      "CPU times: user 11.2 s, sys: 2.2 s, total: 13.4 s\n",
      "Wall time: 6.54 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "t_asr(compute_type=\"int8\", device=\"cuda\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b255a0f0-4987-4f04-b63c-5ca0167917b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "#|export \n",
    "def transcribe_malayalam_speech(audio_file, compute_type=\"int8\", device=\"cpu\", folder=\"vegam-whisper-medium-ml-fp16\"):\n",
    "    \n",
    "    model = WhisperModel(folder, device=device, compute_type=compute_type)\n",
    "    segments, info = model.transcribe(audio_file, beam_size=5)\n",
    "\n",
    "    lst = []\n",
    "    for segment in segments:\n",
    "        # print(\"[%.2fs -> %.2fs] %s\" % (segment.start, segment.end, segment.text))\n",
    "        lst.append(segment.text)\n",
    "\n",
    "    return(\" \".join(lst))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "48cd4ec3-512f-49d0-87ac-3ef989e25b80",
   "metadata": {},
   "outputs": [],
   "source": [
    "#|export\n",
    "def gr_transcribe_malayalam_speech(microphone, file_upload, compute_type=\"int8\", device=\"cpu\", folder=\"vegam-whisper-medium-ml-fp16\"):\n",
    "    warn_output = \"\"\n",
    "    if (microphone is not None) and (file_upload is not None):\n",
    "        warn_output = (\n",
    "            \"WARNING: You've uploaded an audio file and used the microphone. \"\n",
    "            \"The recorded file from the microphone will be used and the uploaded audio will be discarded.\\n\"\n",
    "        )\n",
    "\n",
    "    elif (microphone is None) and (file_upload is None):\n",
    "        return \"ERROR: You have to either use the microphone or upload an audio file\"\n",
    "\n",
    "    audio_file = microphone if microphone is not None else file_upload\n",
    "    \n",
    "    model = WhisperModel(folder, device=device, compute_type=compute_type)\n",
    "    segments, info = model.transcribe(audio_file, beam_size=5)\n",
    "\n",
    "    lst = []\n",
    "    for segment in segments:\n",
    "        # print(\"[%.2fs -> %.2fs] %s\" % (segment.start, segment.end, segment.text))\n",
    "        lst.append(segment.text)\n",
    "\n",
    "    return(\" \".join(lst))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "14fda29a-aee1-44b2-9269-048cc8b98ea8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 40.6 s, sys: 9.76 s, total: 50.3 s\n",
      "Wall time: 13.6 s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'പാലം കടുക്കുവോളം നാരായണ പാലം കടന്നാലോ കൂരായണ'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "transcribe_malayalam_speech(audio_file=\"vegam-whisper-medium-ml-fp16/00b38e80-80b8-4f70-babf-566e848879fc.webm\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bbdadecf-68d1-4183-8e43-7965c1aecf6a",
   "metadata": {},
   "outputs": [],
   "source": [
    "## Haha, You are burning GPUs and wasting CO2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45fade75-e0b1-4c5d-90a3-ebd7345a4d16",
   "metadata": {},
   "source": [
    "## Figure out Whisper  Demo by Huggingface"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "023ffa7c-b82f-49ea-b6ca-00f84e2c8698",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "fe37c9e1-bc56-422d-9547-be94ab4e4844",
   "metadata": {},
   "source": [
    "## Make an app with Gradio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9badfdcd-dd99-49ea-a318-eda88cddefb6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on local URL:  http://0.0.0.0:6006\n",
      "Running on public URL: https://9fa992d2ba37b0af49.gradio.live\n",
      "\n",
      "This share link expires in 72 hours. For free permanent hosting and GPU upgrades (NEW!), check out Spaces: https://huggingface.co/spaces\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"https://9fa992d2ba37b0af49.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import gradio as gr\n",
    "\n",
    "def greet(name):\n",
    "    return \"Hello \" + name + \"!!\"\n",
    "\n",
    "iface = gr.Interface(fn=greet, inputs=\"text\", outputs=\"text\")\n",
    "iface.launch(share=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "81f3b241-8a6d-4ff0-bb70-d389d4d4e93a",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/conda/lib/python3.10/site-packages/gradio/inputs.py:321: UserWarning: Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your components from gradio.components\n",
      "  warnings.warn(\n",
      "/opt/conda/lib/python3.10/site-packages/gradio/inputs.py:324: UserWarning: `optional` parameter is deprecated, and it has no effect\n",
      "  super().__init__(source=source, type=type, label=label, optional=optional)\n"
     ]
    }
   ],
   "source": [
    "#|export\n",
    "mf_transcribe = gr.Interface(\n",
    "    fn=gr_transcribe_malayalam_speech,\n",
    "    inputs=[\n",
    "        gr.inputs.Audio(source=\"microphone\", type=\"filepath\", optional=True),\n",
    "        gr.inputs.Audio(source=\"upload\", type=\"filepath\", optional=True),\n",
    "    ],\n",
    "    outputs=\"text\",\n",
    "    title=\"PALLAKKU (പല്ലക്ക്)\",\n",
    "    description=(\n",
    "        \"Pallakku is a Malayalam speech to text demo leveraging the model-weights of [vegam-whisper-medium-ml](https://huggingface.co/kurianbenoy/vegam-whisper-medium-ml-fp16).\"\n",
    "    ),\n",
    "    article=\"Please note that this demo now uses CPU only and in my testing for a 5 seconds audio file it can take upto 15 seconds for results to come. If you are interested to use a GPU based API instead, feel free to contact the author @ kurian.bkk@gmail.com\",\n",
    "    allow_flagging=\"never\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "b1e34fa5-8340-4329-a348-b641ca4db341",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Rerunning server... use `close()` to stop if you need to change `launch()` parameters.\n",
      "----\n",
      "Running on local URL:  http://0.0.0.0:6010\n",
      "Running on public URL: https://19b32861466405ac95.gradio.live\n",
      "\n",
      "This share link expires in 72 hours. For free permanent hosting and GPU upgrades (NEW!), check out Spaces: https://huggingface.co/spaces\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"https://19b32861466405ac95.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#|export\n",
    "mf_transcribe.launch(share=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ec1f78d-d9c0-46c7-9466-0408bc6c6cdc",
   "metadata": {},
   "source": [
    "## Create a requirements.txt file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "7c3e753f-5051-4c3b-a5ab-fa65c7e7cae9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing requirements.txt\n"
     ]
    }
   ],
   "source": [
    "%%writefile requirements.txt\n",
    "gradio==3.31.0\n",
    "faster-whisper==0.5.1\n",
    "torch"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43505375-9b3d-4661-93d1-11965cd8d6b5",
   "metadata": {},
   "source": [
    "## Convert this notebook into a Gradio app"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "fba83810-1f0f-4777-b831-aabb4cfead39",
   "metadata": {},
   "outputs": [],
   "source": [
    "from nbdev.export import nb_export\n",
    "nb_export('app.ipynb', lib_path='.', name='app')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2c7c52be-c7c4-4026-9886-ae9f71dec603",
   "metadata": {},
   "source": [
    "## Reference\n",
    "\n",
    "1. [Create A 🤗 Space From A Notebook](https://nbdev.fast.ai/blog/posts/2022-11-07-spaces/index.html)\n",
    "2. [Nbdev Demo](https://gist.github.com/hamelsmu/35be07d242f3f19063c3a3839127dc67)\n",
    "3. [Whisper-demo space by  🤗](https://huggingface.co/spaces/whisper-event/whisper-demo)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5384528f-9a83-4a0d-b4fd-8ed8458b0eda",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}