File size: 16,126 Bytes
5cc1949
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Generative AI for Audio Application"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Restart Kernel (If needed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'status': 'ok', 'restart': True}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import IPython\n",
    "\n",
    "app = IPython.Application.instance()\n",
    "app.kernel.do_shutdown(True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting gradio\n",
      "  Using cached gradio-4.44.1-py3-none-any.whl (18.1 MB)\n",
      "Collecting aiofiles<24.0,>=22.0\n",
      "  Using cached aiofiles-23.2.1-py3-none-any.whl (15 kB)\n",
      "Collecting uvicorn>=0.14.0\n",
      "  Using cached uvicorn-0.34.0-py3-none-any.whl (62 kB)\n",
      "Collecting typing-extensions~=4.0\n",
      "  Using cached typing_extensions-4.12.2-py3-none-any.whl (37 kB)\n",
      "Collecting jinja2<4.0\n",
      "  Using cached jinja2-3.1.5-py3-none-any.whl (134 kB)\n",
      "Collecting huggingface-hub>=0.19.3\n",
      "  Using cached huggingface_hub-0.27.1-py3-none-any.whl (450 kB)\n",
      "Collecting pydub\n",
      "  Using cached pydub-0.25.1-py2.py3-none-any.whl (32 kB)\n",
      "Collecting markupsafe~=2.0\n",
      "  Using cached MarkupSafe-2.1.5-cp39-cp39-macosx_10_9_x86_64.whl (14 kB)\n",
      "Collecting numpy<3.0,>=1.0\n",
      "  Using cached numpy-2.0.2-cp39-cp39-macosx_10_9_x86_64.whl (21.2 MB)\n",
      "Collecting tomlkit==0.12.0\n",
      "  Using cached tomlkit-0.12.0-py3-none-any.whl (37 kB)\n",
      "Collecting pandas<3.0,>=1.0\n",
      "  Using cached pandas-2.2.3-cp39-cp39-macosx_10_9_x86_64.whl (12.6 MB)\n",
      "Collecting ffmpy\n",
      "  Using cached ffmpy-0.5.0-py3-none-any.whl (6.0 kB)\n",
      "Collecting importlib-resources<7.0,>=1.3\n",
      "  Using cached importlib_resources-6.5.2-py3-none-any.whl (37 kB)\n",
      "Collecting fastapi<1.0\n",
      "  Using cached fastapi-0.115.7-py3-none-any.whl (94 kB)\n",
      "Collecting pillow<11.0,>=8.0\n",
      "  Using cached pillow-10.4.0-cp39-cp39-macosx_10_10_x86_64.whl (3.5 MB)\n",
      "Collecting gradio-client==1.3.0\n",
      "  Using cached gradio_client-1.3.0-py3-none-any.whl (318 kB)\n",
      "Collecting httpx>=0.24.1\n",
      "  Using cached httpx-0.28.1-py3-none-any.whl (73 kB)\n",
      "Collecting anyio<5.0,>=3.0\n",
      "  Using cached anyio-4.8.0-py3-none-any.whl (96 kB)\n",
      "Collecting pydantic>=2.0\n",
      "  Using cached pydantic-2.10.6-py3-none-any.whl (431 kB)\n",
      "Collecting pyyaml<7.0,>=5.0\n",
      "  Using cached PyYAML-6.0.2-cp39-cp39-macosx_10_9_x86_64.whl (184 kB)\n",
      "Collecting semantic-version~=2.0\n",
      "  Using cached semantic_version-2.10.0-py2.py3-none-any.whl (15 kB)\n",
      "Collecting orjson~=3.0\n",
      "  Using cached orjson-3.10.15-cp39-cp39-macosx_10_15_x86_64.macosx_11_0_arm64.macosx_10_15_universal2.whl (250 kB)\n",
      "Collecting python-multipart>=0.0.9\n",
      "  Using cached python_multipart-0.0.20-py3-none-any.whl (24 kB)\n",
      "Collecting ruff>=0.2.2\n",
      "  Using cached ruff-0.9.3-py3-none-macosx_10_12_x86_64.whl (11.6 MB)\n",
      "Collecting matplotlib~=3.0\n",
      "  Using cached matplotlib-3.9.4-cp39-cp39-macosx_10_12_x86_64.whl (7.9 MB)\n",
      "Collecting urllib3~=2.0\n",
      "  Using cached urllib3-2.3.0-py3-none-any.whl (128 kB)\n",
      "Collecting typer<1.0,>=0.12\n",
      "  Using cached typer-0.15.1-py3-none-any.whl (44 kB)\n",
      "Collecting packaging\n",
      "  Using cached packaging-24.2-py3-none-any.whl (65 kB)\n",
      "Collecting websockets<13.0,>=10.0\n",
      "  Using cached websockets-12.0-cp39-cp39-macosx_10_9_x86_64.whl (121 kB)\n",
      "Collecting fsspec\n",
      "  Using cached fsspec-2024.12.0-py3-none-any.whl (183 kB)\n",
      "Collecting exceptiongroup>=1.0.2\n",
      "  Using cached exceptiongroup-1.2.2-py3-none-any.whl (16 kB)\n",
      "Collecting idna>=2.8\n",
      "  Using cached idna-3.10-py3-none-any.whl (70 kB)\n",
      "Collecting sniffio>=1.1\n",
      "  Using cached sniffio-1.3.1-py3-none-any.whl (10 kB)\n",
      "Collecting starlette<0.46.0,>=0.40.0\n",
      "  Using cached starlette-0.45.3-py3-none-any.whl (71 kB)\n",
      "Collecting httpcore==1.*\n",
      "  Using cached httpcore-1.0.7-py3-none-any.whl (78 kB)\n",
      "Collecting certifi\n",
      "  Using cached certifi-2024.12.14-py3-none-any.whl (164 kB)\n",
      "Collecting h11<0.15,>=0.13\n",
      "  Using cached h11-0.14.0-py3-none-any.whl (58 kB)\n",
      "Collecting requests\n",
      "  Using cached requests-2.32.3-py3-none-any.whl (64 kB)\n",
      "Collecting tqdm>=4.42.1\n",
      "  Using cached tqdm-4.67.1-py3-none-any.whl (78 kB)\n",
      "Collecting filelock\n",
      "  Using cached filelock-3.17.0-py3-none-any.whl (16 kB)\n",
      "Collecting zipp>=3.1.0\n",
      "  Using cached zipp-3.21.0-py3-none-any.whl (9.6 kB)\n",
      "Collecting fonttools>=4.22.0\n",
      "  Using cached fonttools-4.55.6-cp39-cp39-macosx_10_9_x86_64.whl (2.3 MB)\n",
      "Collecting pyparsing>=2.3.1\n",
      "  Using cached pyparsing-3.2.1-py3-none-any.whl (107 kB)\n",
      "Collecting python-dateutil>=2.7\n",
      "  Using cached python_dateutil-2.9.0.post0-py2.py3-none-any.whl (229 kB)\n",
      "Collecting contourpy>=1.0.1\n",
      "  Using cached contourpy-1.3.0-cp39-cp39-macosx_10_9_x86_64.whl (265 kB)\n",
      "Collecting cycler>=0.10\n",
      "  Using cached cycler-0.12.1-py3-none-any.whl (8.3 kB)\n",
      "Collecting kiwisolver>=1.3.1\n",
      "  Using cached kiwisolver-1.4.7-cp39-cp39-macosx_10_9_x86_64.whl (65 kB)\n",
      "Collecting pytz>=2020.1\n",
      "  Using cached pytz-2024.2-py2.py3-none-any.whl (508 kB)\n",
      "Collecting tzdata>=2022.7\n",
      "  Using cached tzdata-2025.1-py2.py3-none-any.whl (346 kB)\n",
      "Collecting pydantic-core==2.27.2\n",
      "  Using cached pydantic_core-2.27.2-cp39-cp39-macosx_10_12_x86_64.whl (1.9 MB)\n",
      "Collecting annotated-types>=0.6.0\n",
      "  Using cached annotated_types-0.7.0-py3-none-any.whl (13 kB)\n",
      "Collecting six>=1.5\n",
      "  Using cached six-1.17.0-py2.py3-none-any.whl (11 kB)\n",
      "Collecting shellingham>=1.3.0\n",
      "  Using cached shellingham-1.5.4-py2.py3-none-any.whl (9.8 kB)\n",
      "Collecting rich>=10.11.0\n",
      "  Using cached rich-13.9.4-py3-none-any.whl (242 kB)\n",
      "Collecting click>=8.0.0\n",
      "  Using cached click-8.1.8-py3-none-any.whl (98 kB)\n",
      "Collecting pygments<3.0.0,>=2.13.0\n",
      "  Using cached pygments-2.19.1-py3-none-any.whl (1.2 MB)\n",
      "Collecting markdown-it-py>=2.2.0\n",
      "  Using cached markdown_it_py-3.0.0-py3-none-any.whl (87 kB)\n",
      "Collecting mdurl~=0.1\n",
      "  Using cached mdurl-0.1.2-py3-none-any.whl (10.0 kB)\n",
      "Collecting charset-normalizer<4,>=2\n",
      "  Using cached charset_normalizer-3.4.1-cp39-cp39-macosx_10_9_universal2.whl (197 kB)\n",
      "Installing collected packages: urllib3, typing-extensions, sniffio, mdurl, idna, h11, exceptiongroup, charset-normalizer, certifi, zipp, tqdm, six, requests, pyyaml, pygments, pydantic-core, packaging, numpy, markdown-it-py, httpcore, fsspec, filelock, anyio, annotated-types, websockets, tzdata, starlette, shellingham, rich, pytz, python-dateutil, pyparsing, pydantic, pillow, markupsafe, kiwisolver, importlib-resources, huggingface-hub, httpx, fonttools, cycler, contourpy, click, uvicorn, typer, tomlkit, semantic-version, ruff, python-multipart, pydub, pandas, orjson, matplotlib, jinja2, gradio-client, ffmpy, fastapi, aiofiles, gradio\n",
      "Successfully installed aiofiles-23.2.1 annotated-types-0.7.0 anyio-4.8.0 certifi-2024.12.14 charset-normalizer-3.4.1 click-8.1.8 contourpy-1.3.0 cycler-0.12.1 exceptiongroup-1.2.2 fastapi-0.115.7 ffmpy-0.5.0 filelock-3.17.0 fonttools-4.55.6 fsspec-2024.12.0 gradio-4.44.1 gradio-client-1.3.0 h11-0.14.0 httpcore-1.0.7 httpx-0.28.1 huggingface-hub-0.27.1 idna-3.10 importlib-resources-6.5.2 jinja2-3.1.5 kiwisolver-1.4.7 markdown-it-py-3.0.0 markupsafe-2.1.5 matplotlib-3.9.4 mdurl-0.1.2 numpy-2.0.2 orjson-3.10.15 packaging-24.2 pandas-2.2.3 pillow-10.4.0 pydantic-2.10.6 pydantic-core-2.27.2 pydub-0.25.1 pygments-2.19.1 pyparsing-3.2.1 python-dateutil-2.9.0.post0 python-multipart-0.0.20 pytz-2024.2 pyyaml-6.0.2 requests-2.32.3 rich-13.9.4 ruff-0.9.3 semantic-version-2.10.0 shellingham-1.5.4 six-1.17.0 sniffio-1.3.1 starlette-0.45.3 tomlkit-0.12.0 tqdm-4.67.1 typer-0.15.1 typing-extensions-4.12.2 tzdata-2025.1 urllib3-2.3.0 uvicorn-0.34.0 websockets-12.0 zipp-3.21.0\n",
      "\u001b[33mWARNING: You are using pip version 21.2.3; however, version 24.3.1 is available.\n",
      "You should consider upgrading via the '/Users/voldemort/Downloads/Solution 2/venv/bin/python3 -m pip install --upgrade pip' command.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "!pip install gradio"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Import Libraries\n",
    "Most of the complexity for the chatbot is in [customizable_chatbot.py](./customizable_chatbot.py) that uses [audio.py](./audio.py) internally for the audio capabilities."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'gradio'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[4], line 2\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mos\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mgradio\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mgr\u001b[39;00m\n\u001b[1;32m      3\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mgenai_voice\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mbots\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mchatbot\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m ChatBot\n\u001b[1;32m      4\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mgenai_voice\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mconfig\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mdefaults\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Config\n",
      "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'gradio'"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import gradio as gr\n",
    "from genai_voice.bots.chatbot import ChatBot\n",
    "from genai_voice.config.defaults import Config\n",
    "from genai_voice.logger.log_utils import log, LogLevels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "log(f'Configuration: {Config()}', log_level=LogLevels.ON)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Set Current Working Directory  \n",
    "\n",
    "We want to simulate running this notebook from the project root just as it would work when using `Poetry` scripts."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "curr_wrk_dir = os.getcwd()\n",
    "log(f'Before directory change: {curr_wrk_dir}')\n",
    "if curr_wrk_dir.endswith('app'):\n",
    "    log(f'Changing directory to root')\n",
    "    os.chdir('..')\n",
    "    curr_wrk_dir = os.getcwd()\n",
    "log(f'Before directory change: {curr_wrk_dir}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Create Data for LLM Context  \n",
    "\n",
    "`Poetry` scripts allow us to install our code as a package and run functions executables. \n",
    "\n",
    "We will use the `ExtractWebPagesAndSaveData` script, that is defined in `pyproject.toml` to scrape, extract and generate the file that the LLM will use as context data.\n",
    "\n",
    "`SAMPLE_URLS` have been defined under provided under `genai_voice.data_utils.urls.py`. Feel free to modify the links in that file to customize the source of data. \n",
    "\n",
    "> **DISCLAIMER:** Be responsible when scraping data that is not yours, complying with the EULA of the sites and conducting it in a legal fashion. Also remember that most sites will throttle scrapes, so do this with caution.  \n",
    "\n",
    "> **NOTE:** This is an optional step. It just shows you have you can get custom data for your LLM context. We have provided the data for this project. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!poetry run ExtractWebPagesAndSaveData"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Gradio Interface\n",
    "This launches the UI, you will probably need to allow the browser to use the microphone to enable the audio functions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'ChatBot' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[5], line 2\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;124;03m\"\"\"Run Chatbot app\"\"\"\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m chatbot \u001b[38;5;241m=\u001b[39m \u001b[43mChatBot\u001b[49m(enable_speakers\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m, threaded\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m      3\u001b[0m history \u001b[38;5;241m=\u001b[39m []\n\u001b[1;32m      5\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mget_response\u001b[39m(audio):\n",
      "\u001b[0;31mNameError\u001b[0m: name 'ChatBot' is not defined"
     ]
    }
   ],
   "source": [
    "\"\"\"Run Chatbot app\"\"\"\n",
    "chatbot = ChatBot(enable_speakers=True, threaded=True)\n",
    "history = []\n",
    "\n",
    "def get_response(audio):\n",
    "    \"\"\"Get Audio Response From Chatbot\"\"\"\n",
    "    if not audio:\n",
    "        raise ValueError(\"No audio file provided.\")\n",
    "    prompt = chatbot.get_prompt_from_gradio_audio(audio)\n",
    "    log(f\"Transcribed prompt: {prompt}\", log_level=LogLevels.ON)\n",
    "    response = chatbot.respond(prompt, history)\n",
    "    log(f\"Chatbot response: {response}\", log_level=LogLevels.ON)\n",
    "    history.append([prompt, response])\n",
    "    return response\n",
    "\n",
    "demo = gr.Interface(\n",
    "        get_response,\n",
    "        gr.Audio(sources=\"microphone\"),\n",
    "        None,\n",
    "        title=\"Wanderwise Travel Assistant\"\n",
    ")\n",
    "demo.launch()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}