File size: 7,518 Bytes
4281bc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install sounddevice scipy torch transformers lang_trans nltk tqdm pyquran"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from os import path\n",
    "import sounddevice as sd\n",
    "import scipy.io.wavfile as wav\n",
    "import torch\n",
    "from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor\n",
    "from lang_trans.arabic import buckwalter\n",
    "from nltk import edit_distance\n",
    "from tqdm import tqdm\n",
    "import pyquran as q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def record():\n",
    "    fs = 16000  # Sample rate\n",
    "    seconds = 5  # Duration of recording\n",
    "    print(\"Recording...\")\n",
    "    myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=1)\n",
    "    sd.wait()  # Wait until recording is finished\n",
    "    print(\"Finished recording.\")\n",
    "    return fs , myrecording[:,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_Quran_fine_tuned_elgeish_xlsr_53_model_and_processor():\n",
    "    global loaded_model, loaded_processor\n",
    "    loaded_model = Wav2Vec2ForCTC.from_pretrained(\"Nuwaisir/Quran_speech_recognizer\").eval()\n",
    "    loaded_processor = Wav2Vec2Processor.from_pretrained(\"Nuwaisir/Quran_speech_recognizer\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_elgeish_xlsr_53_model_and_processor():\n",
    "    global loaded_model, loaded_processor\n",
    "    loaded_model = Wav2Vec2ForCTC.from_pretrained(\"elgeish/wav2vec2-large-xlsr-53-arabic\").eval()\n",
    "    loaded_processor = Wav2Vec2Processor.from_pretrained(\"elgeish/wav2vec2-large-xlsr-53-arabic\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def predict(single):\n",
    "    inputs = loaded_processor(single[\"speech\"], sampling_rate=16000, return_tensors=\"pt\", padding=True)\n",
    "    with torch.no_grad():\n",
    "        predicted = torch.argmax(loaded_model(inputs.input_values).logits, dim=-1)\n",
    "    predicted[predicted == -100] = loaded_processor.tokenizer.pad_token_id  # see fine-tuning script\n",
    "    pred_1 = loaded_processor.tokenizer.batch_decode(predicted)[0]\n",
    "    single[\"predicted\"] = buckwalter.untrans(pred_1)\n",
    "    return single"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def last_para_str(taskeel=False):\n",
    "    quran_string = ''\n",
    "    for i in range (78, 115):\n",
    "        quran_string += ' '.join(q.quran.get_sura(i, with_tashkeel=taskeel,basmalah=False))\n",
    "        quran_string += ' '\n",
    "    return quran_string\n",
    "\n",
    "def find_match_2(q_str, s, spaces, threshhold = 10):\n",
    "  len_q = len(q_str)\n",
    "  len_s = len(s)\n",
    "  min_dist = 1000000000\n",
    "  min_dist_pos = []\n",
    "  for i in tqdm(spaces):\n",
    "    j = i+1\n",
    "    k = j + len_s + len_s // 3\n",
    "    if k > len_q:\n",
    "      break\n",
    "    dist = edit_distance(q_str[j:k],s)\n",
    "    if dist < min_dist:\n",
    "      min_dist = dist\n",
    "      min_dist_pos = [j]\n",
    "    elif dist == min_dist:\n",
    "      min_dist_pos.append(j)\n",
    "  return min_dist, min_dist_pos\n",
    "\n",
    "def find_all_index(s, ch):\n",
    "    return [i for i, ltr in enumerate(s) if ltr == ch]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "last_para = last_para_str(taskeel=True)\n",
    "last_para_spaces = find_all_index(last_para,' ')\n",
    "last_para_spaces.insert(0, -1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def pipeline():\n",
    "    fs, myrecording = record()\n",
    "    single_example = {\n",
    "        \"speech\": myrecording,\n",
    "        \"sampling_rate\": fs,\n",
    "    }\n",
    "    predicted = predict(single_example)\n",
    "    print(predicted[\"predicted\"])\n",
    "    dist,poses = find_match_2(last_para, predicted['predicted'], spaces=last_para_spaces)\n",
    "    print(\"distance:\",dist)\n",
    "    print(\"number of matches:\", len(poses))\n",
    "    for i in poses:\n",
    "        print(last_para[i:i+200],'\\n')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load the elgeish_xlsr_53 model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# load_elgeish_xlsr_53_model_and_processor()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load Quran fine-tuned elgeish_xlsr_53 model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "load_Quran_fine_tuned_elgeish_xlsr_53_model_and_processor()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Recording...\n",
      "Finished recording.\n",
      "ู„ูุฅูู„ูŽุง ูู ู‚ู’ุฑูŽุงูŠุดู ุฅูู„ูŽุง ูููŠู‡ู\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–‰| 2304/2309 [00:03<00:00, 587.76it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "distance: 23\n",
      "number of matches: 1\n",
      "ู„ูุฅููŠู„ูŽูู ู‚ูุฑูŽูŠู’ุดู ุฅูู„ูŽููู‡ูู…ู’ ุฑูุญู’ู„ูŽุฉูŽ ุงู„ุดู‘ูุชูŽุงุกู ูˆูŽุงู„ุตู‘ูŽูŠู’ูู ููŽู„ู’ูŠูŽุนู’ุจูุฏููˆุง ุฑูŽุจู‘ูŽ ู‡ูŽุฐูŽุง ุงู„ู’ุจูŽูŠู’ุชู ุงู„ู‘ูŽุฐูู‰ ุฃูŽุทู’ุนูŽู…ูŽู‡ูู… ู…ู‘ูู† ุฌููˆุนู ูˆูŽุกูŽุงู…ูŽู†ูŽู‡ูู… ู…ู‘ูู†ู’ ุฎูŽูˆู’ูู ุฃูŽุฑูŽุกูŽูŠู’ุชูŽ ุงู„ู‘ูŽุฐูู‰ ูŠููƒูŽุฐู‘ูุจู ุจูุงู„ุฏู‘ููŠู†ู ููŽุฐูŽ \n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "# Recite after running this cell. The first 5 seconds will capture your audio\n",
    "pipeline()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "35541def04ad193058c9b5b3afd24560c7277f209ee76d36789dee7d6c5bcde6"
  },
  "kernelspec": {
   "display_name": "Python 3.10.2 64-bit",
   "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.8.10"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}