File size: 26,354 Bytes
05da059
 
 
 
d869f0d
05da059
 
 
 
 
 
9647155
05da059
 
 
 
 
 
 
 
 
 
 
 
 
454c118
05da059
 
454c118
05da059
 
454c118
 
 
05da059
 
454c118
05da059
 
 
 
 
 
 
d869f0d
05da059
 
 
 
 
 
 
 
 
 
d869f0d
05da059
 
 
 
 
 
 
 
 
 
 
d869f0d
05da059
91856d0
05da059
91856d0
 
05da059
 
 
 
9647155
05da059
d869f0d
 
9647155
 
 
91856d0
9647155
d869f0d
 
 
91856d0
d869f0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05da059
 
 
 
 
 
 
d869f0d
05da059
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sqlite3, json\n",
    "from contextlib import closing\n",
    "\n",
    "# use test.py to update questions in db!!!\n",
    "def load_questions(sqlite_filename):\n",
    "  all_questions = []\n",
    "  with closing(sqlite3.connect(sqlite_filename)) as db:\n",
    "    db.row_factory = sqlite3.Row\n",
    "    with closing(db.cursor()) as cursor:\n",
    "      results = cursor.execute(\n",
    "        \"SELECT id, articleId, title, category, section, questions FROM articles WHERE articleType = ? AND doNotUse IS NULL OR doNotUse = 0\",\n",
    "        ('article',)\n",
    "      ).fetchall()\n",
    "      \n",
    "      for res in results:\n",
    "        \n",
    "        questions = json.loads(res['questions'])\n",
    "        # questions_copy = questions.copy()\n",
    "        \n",
    "        for q in questions:\n",
    "          q['query'] = \" \".join(res['section'].split() + res['title'].split() + q['question'].split()).lower()\n",
    "          q['articleId'] = res['articleId']\n",
    "          \n",
    "        # for q in questions_copy:\n",
    "        #   q['query'] = q['question']\n",
    "        #   q['articleId'] = res['articleId']\n",
    "\n",
    "        all_questions += questions\n",
    "        # all_questions += questions_copy\n",
    "  return all_questions\n",
    "\n",
    "questions = load_questions(\"omnidesk-ai-chatgpt-questions.sqlite\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.vectorstores import FAISS\n",
    "from langchain.docstore.document import Document\n",
    "from langchain.embeddings import SentenceTransformerEmbeddings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "docs = [\n",
    "  Document(page_content=q['query'], metadata={ 'answer': q['answer'], 'articleId': q['articleId'] })\n",
    "  for q in questions\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from extract_keywords import normalize_word, canonical_keywords, merge_keywords, tokenize_sentence, extract_keywords, init_keyword_extractor\n",
    "#init_keyword_extractor()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['почта', 'россия']\n"
     ]
    }
   ],
   "source": [
    "print(tokenize_sentence('почты росии'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #800000; text-decoration-color: #800000\">╭─────────────────────────────── </span><span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">Traceback </span><span style=\"color: #bf7f7f; text-decoration-color: #bf7f7f; font-weight: bold\">(most recent call last)</span><span style=\"color: #800000; text-decoration-color: #800000\"> ────────────────────────────────╮</span>\n",
       "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #bfbf7f; text-decoration-color: #bfbf7f\">/tmp/ipykernel_1594240/</span><span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\">2036088539.py</span>:<span style=\"color: #0000ff; text-decoration-color: #0000ff\">1</span> in <span style=\"color: #00ff00; text-decoration-color: #00ff00\">&lt;module&gt;</span>                                               <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
       "<span style=\"color: #800000; text-decoration-color: #800000\">│</span>                                                                                                  <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
       "<span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000; font-style: italic\">[Errno 2] No such file or directory: '/tmp/ipykernel_1594240/2036088539.py'</span>                      <span style=\"color: #800000; text-decoration-color: #800000\">│</span>\n",
       "<span style=\"color: #800000; text-decoration-color: #800000\">╰──────────────────────────────────────────────────────────────────────────────────────────────────╯</span>\n",
       "<span style=\"color: #ff0000; text-decoration-color: #ff0000; font-weight: bold\">NameError: </span>name <span style=\"color: #008000; text-decoration-color: #008000\">'SentenceTransformerEmbeddings'</span> is not defined\n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[31m╭─\u001b[0m\u001b[31m──────────────────────────────\u001b[0m\u001b[31m \u001b[0m\u001b[1;31mTraceback \u001b[0m\u001b[1;2;31m(most recent call last)\u001b[0m\u001b[31m \u001b[0m\u001b[31m───────────────────────────────\u001b[0m\u001b[31m─╮\u001b[0m\n",
       "\u001b[31m│\u001b[0m \u001b[2;33m/tmp/ipykernel_1594240/\u001b[0m\u001b[1;33m2036088539.py\u001b[0m:\u001b[94m1\u001b[0m in \u001b[92m<module>\u001b[0m                                               \u001b[31m│\u001b[0m\n",
       "\u001b[31m│\u001b[0m                                                                                                  \u001b[31m│\u001b[0m\n",
       "\u001b[31m│\u001b[0m \u001b[3;31m[Errno 2] No such file or directory: '/tmp/ipykernel_1594240/2036088539.py'\u001b[0m                      \u001b[31m│\u001b[0m\n",
       "\u001b[31m╰──────────────────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m\n",
       "\u001b[1;91mNameError: \u001b[0mname \u001b[32m'SentenceTransformerEmbeddings'\u001b[0m is not defined\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "embeddings = SentenceTransformerEmbeddings(model_name=\"multi-qa-MiniLM-L6-cos-v1\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "output_dir = 'faiss_qa'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "db = FAISS.from_documents(docs, embeddings)\n",
    "db.save_local(output_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install faiss-cpu langchain\n",
    "%pip install -U pydantic==1.10.7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/makcrx/anaconda3/lib/python3.10/site-packages/deeplake/util/check_latest_version.py:32: UserWarning: A newer version of deeplake (3.6.15) is available. It's recommended that you update to the latest version using `pip install -U deeplake`.\n",
      "  warnings.warn(\n",
      "2023-08-03 23:36:22.968666: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n"
     ]
    }
   ],
   "source": [
    "from langchain.vectorstores import FAISS\n",
    "from langchain.embeddings import SentenceTransformerEmbeddings\n",
    "\n",
    "embeddings = SentenceTransformerEmbeddings(model_name=\"multi-qa-MiniLM-L6-cos-v1\")\n",
    "db = FAISS.load_local('faiss_qa', embeddings)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "query = 'Почему у триггеров Почт России симваольный код не тот?'\n",
    "result_docs = db.similarity_search_with_score(query, k=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import reranking\n",
    "\n",
    "sentences = [doc[0].page_content for doc in result_docs]\n",
    "score, index = reranking.search(query, sentences)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Символьный код доставки Почта России - russian-post.'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result_docs[index][0].metadata['answer']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception in thread Thread-9 (run):\n",
      "Traceback (most recent call last):\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 27, in run\n",
      "    loop = asyncio.get_event_loop()\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 45, in _get_event_loop\n",
      "    loop = events.get_event_loop_policy().get_event_loop()\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/asyncio/events.py\", line 671, in get_event_loop\n",
      "    raise RuntimeError('There is no current event loop in thread %r.'\n",
      "RuntimeError: There is no current event loop in thread 'Thread-9 (run)'.\n",
      "\n",
      "During handling of the above exception, another exception occurred:\n",
      "\n",
      "Traceback (most recent call last):\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 1016, in _bootstrap_inner\n",
      "    self.run()\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 953, in run\n",
      "    self._target(*self._args, **self._kwargs)\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/uvicorn/server.py\", line 61, in run\n",
      "    return asyncio.run(self.serve(sockets=sockets))\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 31, in run\n",
      "    _patch_loop(loop)\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 175, in _patch_loop\n",
      "    raise ValueError('Can\\'t patch loop of type %s' % type(loop))\n",
      "ValueError: Can't patch loop of type <class 'uvloop.Loop'>\n",
      "/home/makcrx/anaconda3/lib/python3.10/threading.py:1018: RuntimeWarning: coroutine 'Server.serve' was never awaited\n",
      "  self._invoke_excepthook(self)\n",
      "RuntimeWarning: Enable tracemalloc to get the object allocation traceback\n",
      "Exception in thread Thread-10 (run):\n",
      "Traceback (most recent call last):\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 27, in run\n",
      "    loop = asyncio.get_event_loop()\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 45, in _get_event_loop\n",
      "    loop = events.get_event_loop_policy().get_event_loop()\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/asyncio/events.py\", line 671, in get_event_loop\n",
      "    raise RuntimeError('There is no current event loop in thread %r.'\n",
      "RuntimeError: There is no current event loop in thread 'Thread-10 (run)'.\n",
      "\n",
      "During handling of the above exception, another exception occurred:\n",
      "\n",
      "Traceback (most recent call last):\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 1016, in _bootstrap_inner\n",
      "    self.run()\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 953, in run\n",
      "    self._target(*self._args, **self._kwargs)\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/uvicorn/server.py\", line 61, in run\n",
      "    return asyncio.run(self.serve(sockets=sockets))\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 31, in run\n",
      "    _patch_loop(loop)\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 175, in _patch_loop\n",
      "    raise ValueError('Can\\'t patch loop of type %s' % type(loop))\n",
      "ValueError: Can't patch loop of type <class 'uvloop.Loop'>\n",
      "Exception in thread Thread-11 (run):\n",
      "Traceback (most recent call last):\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 27, in run\n",
      "    loop = asyncio.get_event_loop()\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 45, in _get_event_loop\n",
      "    loop = events.get_event_loop_policy().get_event_loop()\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/asyncio/events.py\", line 671, in get_event_loop\n",
      "    raise RuntimeError('There is no current event loop in thread %r.'\n",
      "RuntimeError: There is no current event loop in thread 'Thread-11 (run)'.\n",
      "\n",
      "During handling of the above exception, another exception occurred:\n",
      "\n",
      "Traceback (most recent call last):\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 1016, in _bootstrap_inner\n",
      "    self.run()\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 953, in run\n",
      "    self._target(*self._args, **self._kwargs)\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/uvicorn/server.py\", line 61, in run\n",
      "    return asyncio.run(self.serve(sockets=sockets))\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 31, in run\n",
      "    _patch_loop(loop)\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 175, in _patch_loop\n",
      "    raise ValueError('Can\\'t patch loop of type %s' % type(loop))\n",
      "ValueError: Can't patch loop of type <class 'uvloop.Loop'>\n",
      "Exception in thread Thread-12 (run):\n",
      "Traceback (most recent call last):\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 27, in run\n",
      "    loop = asyncio.get_event_loop()\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 45, in _get_event_loop\n",
      "    loop = events.get_event_loop_policy().get_event_loop()\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/asyncio/events.py\", line 671, in get_event_loop\n",
      "    raise RuntimeError('There is no current event loop in thread %r.'\n",
      "RuntimeError: There is no current event loop in thread 'Thread-12 (run)'.\n",
      "\n",
      "During handling of the above exception, another exception occurred:\n",
      "\n",
      "Traceback (most recent call last):\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 1016, in _bootstrap_inner\n",
      "    self.run()\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 953, in run\n",
      "    self._target(*self._args, **self._kwargs)\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/uvicorn/server.py\", line 61, in run\n",
      "    return asyncio.run(self.serve(sockets=sockets))\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 31, in run\n",
      "    _patch_loop(loop)\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 175, in _patch_loop\n",
      "    raise ValueError('Can\\'t patch loop of type %s' % type(loop))\n",
      "ValueError: Can't patch loop of type <class 'uvloop.Loop'>\n",
      "Exception in thread Thread-13 (run):\n",
      "Traceback (most recent call last):\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 27, in run\n",
      "    loop = asyncio.get_event_loop()\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 45, in _get_event_loop\n",
      "    loop = events.get_event_loop_policy().get_event_loop()\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/asyncio/events.py\", line 671, in get_event_loop\n",
      "    raise RuntimeError('There is no current event loop in thread %r.'\n",
      "RuntimeError: There is no current event loop in thread 'Thread-13 (run)'.\n",
      "\n",
      "During handling of the above exception, another exception occurred:\n",
      "\n",
      "Traceback (most recent call last):\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 1016, in _bootstrap_inner\n",
      "    self.run()\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 953, in run\n",
      "    self._target(*self._args, **self._kwargs)\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/uvicorn/server.py\", line 61, in run\n",
      "    return asyncio.run(self.serve(sockets=sockets))\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 31, in run\n",
      "    _patch_loop(loop)\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 175, in _patch_loop\n",
      "    raise ValueError('Can\\'t patch loop of type %s' % type(loop))\n",
      "ValueError: Can't patch loop of type <class 'uvloop.Loop'>\n",
      "Exception in thread Thread-14 (run):\n",
      "Traceback (most recent call last):\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 27, in run\n",
      "    loop = asyncio.get_event_loop()\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 45, in _get_event_loop\n",
      "    loop = events.get_event_loop_policy().get_event_loop()\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/asyncio/events.py\", line 671, in get_event_loop\n",
      "    raise RuntimeError('There is no current event loop in thread %r.'\n",
      "RuntimeError: There is no current event loop in thread 'Thread-14 (run)'.\n",
      "\n",
      "During handling of the above exception, another exception occurred:\n",
      "\n",
      "Traceback (most recent call last):\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 1016, in _bootstrap_inner\n",
      "    self.run()\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 953, in run\n",
      "    self._target(*self._args, **self._kwargs)\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/uvicorn/server.py\", line 61, in run\n",
      "    return asyncio.run(self.serve(sockets=sockets))\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 31, in run\n",
      "    _patch_loop(loop)\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 175, in _patch_loop\n",
      "    raise ValueError('Can\\'t patch loop of type %s' % type(loop))\n",
      "ValueError: Can't patch loop of type <class 'uvloop.Loop'>\n",
      "Exception in thread Thread-15 (run):\n",
      "Traceback (most recent call last):\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 27, in run\n",
      "    loop = asyncio.get_event_loop()\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 45, in _get_event_loop\n",
      "    loop = events.get_event_loop_policy().get_event_loop()\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/asyncio/events.py\", line 671, in get_event_loop\n",
      "    raise RuntimeError('There is no current event loop in thread %r.'\n",
      "RuntimeError: There is no current event loop in thread 'Thread-15 (run)'.\n",
      "\n",
      "During handling of the above exception, another exception occurred:\n",
      "\n",
      "Traceback (most recent call last):\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 1016, in _bootstrap_inner\n",
      "    self.run()\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 953, in run\n",
      "    self._target(*self._args, **self._kwargs)\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/uvicorn/server.py\", line 61, in run\n",
      "    return asyncio.run(self.serve(sockets=sockets))\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 31, in run\n",
      "    _patch_loop(loop)\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 175, in _patch_loop\n",
      "    raise ValueError('Can\\'t patch loop of type %s' % type(loop))\n",
      "ValueError: Can't patch loop of type <class 'uvloop.Loop'>\n",
      "Exception in thread Thread-16 (run):\n",
      "Traceback (most recent call last):\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 27, in run\n",
      "    loop = asyncio.get_event_loop()\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 45, in _get_event_loop\n",
      "    loop = events.get_event_loop_policy().get_event_loop()\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/asyncio/events.py\", line 671, in get_event_loop\n",
      "    raise RuntimeError('There is no current event loop in thread %r.'\n",
      "RuntimeError: There is no current event loop in thread 'Thread-16 (run)'.\n",
      "\n",
      "During handling of the above exception, another exception occurred:\n",
      "\n",
      "Traceback (most recent call last):\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 1016, in _bootstrap_inner\n",
      "    self.run()\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 953, in run\n",
      "    self._target(*self._args, **self._kwargs)\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/uvicorn/server.py\", line 61, in run\n",
      "    return asyncio.run(self.serve(sockets=sockets))\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 31, in run\n",
      "    _patch_loop(loop)\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 175, in _patch_loop\n",
      "    raise ValueError('Can\\'t patch loop of type %s' % type(loop))\n",
      "ValueError: Can't patch loop of type <class 'uvloop.Loop'>\n",
      "Exception in thread Thread-17 (run):\n",
      "Traceback (most recent call last):\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 27, in run\n",
      "    loop = asyncio.get_event_loop()\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 45, in _get_event_loop\n",
      "    loop = events.get_event_loop_policy().get_event_loop()\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/asyncio/events.py\", line 671, in get_event_loop\n",
      "    raise RuntimeError('There is no current event loop in thread %r.'\n",
      "RuntimeError: There is no current event loop in thread 'Thread-17 (run)'.\n",
      "\n",
      "During handling of the above exception, another exception occurred:\n",
      "\n",
      "Traceback (most recent call last):\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 1016, in _bootstrap_inner\n",
      "    self.run()\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 953, in run\n",
      "    self._target(*self._args, **self._kwargs)\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/uvicorn/server.py\", line 61, in run\n",
      "    return asyncio.run(self.serve(sockets=sockets))\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 31, in run\n",
      "    _patch_loop(loop)\n",
      "  File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 175, in _patch_loop\n",
      "    raise ValueError('Can\\'t patch loop of type %s' % type(loop))\n",
      "ValueError: Can't patch loop of type <class 'uvloop.Loop'>\n"
     ]
    }
   ],
   "source": [
    "import gradio as gr\n",
    "import reranking\n",
    "\n",
    "def main(query):\n",
    "    result_docs = db.similarity_search_with_score(query, k=20)\n",
    "    sentences = [doc[0].page_content for doc in result_docs]\n",
    "    score, index = reranking.search(query, sentences)\n",
    "    return result_docs[index][0].metadata['answer']\n",
    "\n",
    "demo = gr.Interface(fn=main, inputs=\"text\", outputs=\"text\")\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.10.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}