File size: 10,923 Bytes
44cdd99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "dc09394e-2130-4bd4-af30-01346d8ee355",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on local URL:  http://127.0.0.1:7860\n",
      "\n",
      "To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"http://127.0.0.1:7860/\" 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": 1,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\WCHL\\anaconda3\\envs\\RunInference2\\Lib\\site-packages\\gradio\\analytics.py:106: UserWarning: IMPORTANT: You are using gradio version 4.37.2, however version 4.44.1 is available, please upgrade. \n",
      "--------\n",
      "  warnings.warn(\n",
      "ERROR:    Exception in ASGI application\n",
      "Traceback (most recent call last):\n",
      "  File \"C:\\Users\\WCHL\\anaconda3\\envs\\RunInference2\\Lib\\site-packages\\uvicorn\\protocols\\http\\h11_impl.py\", line 404, in run_asgi\n",
      "    result = await app(  # type: ignore[func-returns-value]\n",
      "             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\WCHL\\anaconda3\\envs\\RunInference2\\Lib\\site-packages\\uvicorn\\middleware\\proxy_headers.py\", line 84, in __call__\n",
      "    return await self.app(scope, receive, send)\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\WCHL\\anaconda3\\envs\\RunInference2\\Lib\\site-packages\\fastapi\\applications.py\", line 1054, in __call__\n",
      "    await super().__call__(scope, receive, send)\n",
      "  File \"C:\\Users\\WCHL\\anaconda3\\envs\\RunInference2\\Lib\\site-packages\\starlette\\applications.py\", line 123, in __call__\n",
      "    await self.middleware_stack(scope, receive, send)\n",
      "  File \"C:\\Users\\WCHL\\anaconda3\\envs\\RunInference2\\Lib\\site-packages\\starlette\\middleware\\errors.py\", line 186, in __call__\n",
      "    raise exc\n",
      "  File \"C:\\Users\\WCHL\\anaconda3\\envs\\RunInference2\\Lib\\site-packages\\starlette\\middleware\\errors.py\", line 164, in __call__\n",
      "    await self.app(scope, receive, _send)\n",
      "  File \"C:\\Users\\WCHL\\anaconda3\\envs\\RunInference2\\Lib\\site-packages\\gradio\\route_utils.py\", line 714, in __call__\n",
      "    await self.app(scope, receive, send)\n",
      "  File \"C:\\Users\\WCHL\\anaconda3\\envs\\RunInference2\\Lib\\site-packages\\starlette\\middleware\\exceptions.py\", line 62, in __call__\n",
      "    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)\n",
      "  File \"C:\\Users\\WCHL\\anaconda3\\envs\\RunInference2\\Lib\\site-packages\\starlette\\_exception_handler.py\", line 64, in wrapped_app\n",
      "    raise exc\n",
      "  File \"C:\\Users\\WCHL\\anaconda3\\envs\\RunInference2\\Lib\\site-packages\\starlette\\_exception_handler.py\", line 53, in wrapped_app\n",
      "    await app(scope, receive, sender)\n",
      "  File \"C:\\Users\\WCHL\\anaconda3\\envs\\RunInference2\\Lib\\site-packages\\starlette\\routing.py\", line 762, in __call__\n",
      "    await self.middleware_stack(scope, receive, send)\n",
      "  File \"C:\\Users\\WCHL\\anaconda3\\envs\\RunInference2\\Lib\\site-packages\\starlette\\routing.py\", line 782, in app\n",
      "    await route.handle(scope, receive, send)\n",
      "  File \"C:\\Users\\WCHL\\anaconda3\\envs\\RunInference2\\Lib\\site-packages\\starlette\\routing.py\", line 297, in handle\n",
      "    await self.app(scope, receive, send)\n",
      "  File \"C:\\Users\\WCHL\\anaconda3\\envs\\RunInference2\\Lib\\site-packages\\starlette\\routing.py\", line 77, in app\n",
      "    await wrap_app_handling_exceptions(app, request)(scope, receive, send)\n",
      "  File \"C:\\Users\\WCHL\\anaconda3\\envs\\RunInference2\\Lib\\site-packages\\starlette\\_exception_handler.py\", line 64, in wrapped_app\n",
      "    raise exc\n",
      "  File \"C:\\Users\\WCHL\\anaconda3\\envs\\RunInference2\\Lib\\site-packages\\starlette\\_exception_handler.py\", line 53, in wrapped_app\n",
      "    await app(scope, receive, sender)\n",
      "  File \"C:\\Users\\WCHL\\anaconda3\\envs\\RunInference2\\Lib\\site-packages\\starlette\\routing.py\", line 75, in app\n",
      "    await response(scope, receive, send)\n",
      "  File \"C:\\Users\\WCHL\\anaconda3\\envs\\RunInference2\\Lib\\site-packages\\starlette\\responses.py\", line 346, in __call__\n",
      "    await send(\n",
      "  File \"C:\\Users\\WCHL\\anaconda3\\envs\\RunInference2\\Lib\\site-packages\\starlette\\_exception_handler.py\", line 50, in sender\n",
      "    await send(message)\n",
      "  File \"C:\\Users\\WCHL\\anaconda3\\envs\\RunInference2\\Lib\\site-packages\\starlette\\_exception_handler.py\", line 50, in sender\n",
      "    await send(message)\n",
      "  File \"C:\\Users\\WCHL\\anaconda3\\envs\\RunInference2\\Lib\\site-packages\\starlette\\middleware\\errors.py\", line 161, in _send\n",
      "    await send(message)\n",
      "  File \"C:\\Users\\WCHL\\anaconda3\\envs\\RunInference2\\Lib\\site-packages\\uvicorn\\protocols\\http\\h11_impl.py\", line 508, in send\n",
      "    output = self.conn.send(event=h11.EndOfMessage())\n",
      "             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\WCHL\\anaconda3\\envs\\RunInference2\\Lib\\site-packages\\h11\\_connection.py\", line 512, in send\n",
      "    data_list = self.send_with_data_passthrough(event)\n",
      "                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\WCHL\\anaconda3\\envs\\RunInference2\\Lib\\site-packages\\h11\\_connection.py\", line 545, in send_with_data_passthrough\n",
      "    writer(event, data_list.append)\n",
      "  File \"C:\\Users\\WCHL\\anaconda3\\envs\\RunInference2\\Lib\\site-packages\\h11\\_writers.py\", line 67, in __call__\n",
      "    self.send_eom(event.headers, write)\n",
      "  File \"C:\\Users\\WCHL\\anaconda3\\envs\\RunInference2\\Lib\\site-packages\\h11\\_writers.py\", line 96, in send_eom\n",
      "    raise LocalProtocolError(\"Too little data for declared Content-Length\")\n",
      "h11._util.LocalProtocolError: Too little data for declared Content-Length\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "எண்பது\n",
      "எண்பது\n",
      "எண்பது\n",
      "eighty\n",
      "80\n"
     ]
    }
   ],
   "source": [
    "import gradio as gr\n",
    "import librosa\n",
    "import numpy as np\n",
    "import pywt\n",
    "import nbimporter\n",
    "from scipy.signal import butter, lfilter, wiener\n",
    "from scipy.io.wavfile import write\n",
    "from transformers import pipeline\n",
    "from text2int import text_to_int\n",
    "from isNumber import is_number\n",
    "from Text2List import text_to_list\n",
    "from convert2list import convert_to_list\n",
    "from processDoubles import process_doubles\n",
    "from replaceWords import replace_words\n",
    "\n",
    "asr_model = pipeline(\"automatic-speech-recognition\", model=\"cdactvm/w2v-bert-tamil_new\")\n",
    "\n",
    "# Function to apply a high-pass filter\n",
    "def high_pass_filter(audio, sr, cutoff=300):\n",
    "    nyquist = 0.5 * sr\n",
    "    normal_cutoff = cutoff / nyquist\n",
    "    b, a = butter(1, normal_cutoff, btype='high', analog=False)\n",
    "    filtered_audio = lfilter(b, a, audio)\n",
    "    return filtered_audio\n",
    "\n",
    "# Function to apply wavelet denoising\n",
    "def wavelet_denoise(audio, wavelet='db1', level=1):\n",
    "    coeffs = pywt.wavedec(audio, wavelet, mode='per')\n",
    "    sigma = np.median(np.abs(coeffs[-level])) / 0.5\n",
    "    uthresh = sigma * np.sqrt(2 * np.log(len(audio)))\n",
    "    coeffs[1:] = [pywt.threshold(i, value=uthresh, mode='soft') for i in coeffs[1:]]\n",
    "    return pywt.waverec(coeffs, wavelet, mode='per')\n",
    "\n",
    "# Function to apply a Wiener filter for noise reduction\n",
    "def apply_wiener_filter(audio):\n",
    "    return wiener(audio)\n",
    "\n",
    "# Function to handle speech recognition\n",
    "def recognize_speech(audio_file):\n",
    "    audio, sr = librosa.load(audio_file, sr=16000)\n",
    "    audio = high_pass_filter(audio, sr)\n",
    "    audio = apply_wiener_filter(audio)\n",
    "    denoised_audio = wavelet_denoise(audio)\n",
    "    result = asr_model(denoised_audio)\n",
    "    text_value = result['text']\n",
    "    cleaned_text = text_value.replace(\"<s>\", \"\")\n",
    "    print(cleaned_text)\n",
    "    converted_to_list = convert_to_list(cleaned_text, text_to_list())\n",
    "    print(converted_to_list)\n",
    "    processed_doubles = process_doubles(converted_to_list)\n",
    "    print(processed_doubles)\n",
    "    replaced_words = replace_words(processed_doubles)\n",
    "    print(replaced_words)\n",
    "    converted_text = text_to_int(replaced_words)\n",
    "    print(converted_text)\n",
    "    return converted_text\n",
    "\n",
    "# Gradio Interface\n",
    "gr.Interface(\n",
    "    fn=recognize_speech,\n",
    "    inputs=gr.Audio(sources=[\"microphone\",\"upload\"], type=\"filepath\"),\n",
    "    outputs=\"text\",\n",
    "    title=\"Speech Recognition with Advanced Noise Reduction & Hindi ASR\",\n",
    "    description=\"Upload an audio file, and the system will use high-pass filtering, Wiener filtering, and wavelet-based denoising, then a Hindi ASR model will transcribe the clean audio.\"\n",
    ").launch()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d4565cfb-a8e0-49a1-8878-6e5b1cd105e6",
   "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.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}