makcrx commited on
Commit
05da059
·
1 Parent(s): 07c503a
Files changed (4) hide show
  1. app.py +21 -0
  2. requirements.txt +4 -0
  3. reranking.py +14 -0
  4. test.ipynb +444 -0
app.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.vectorstores import FAISS
2
+ from langchain.embeddings import SentenceTransformerEmbeddings
3
+ import gradio as gr
4
+ import reranking
5
+
6
+ embeddings = SentenceTransformerEmbeddings(model_name="multi-qa-MiniLM-L6-cos-v1")
7
+ db = FAISS.load_local('faiss_qa', embeddings)
8
+
9
+ def main(query):
10
+ result_docs = db.similarity_search_with_score(query, k=20)
11
+ sentences = [doc[0].page_content for doc in result_docs]
12
+ score, index = reranking.search(query, sentences)
13
+ return result_docs[index][0].metadata['answer'], score, result_docs[index][0].page_content
14
+
15
+ demo = gr.Interface(fn=main, inputs="text", outputs=[
16
+ gr.Textbox(label="Ответ, который будет показан клиенту"),
17
+ gr.Textbox(label="Score"),
18
+ gr.Textbox(label="Вопрос, по которому был найден ответ"),
19
+ ])
20
+
21
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ faiss-cpu
2
+ langchain
3
+ sentence_transformers
4
+ pydantic==1.10.7
reranking.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ from sentence_transformers.cross_encoder import CrossEncoder
3
+ from more_itertools import windowed
4
+ model = CrossEncoder('cross-encoder/mmarco-mMiniLMv2-L12-H384-v1', max_length=512, device='cpu')
5
+
6
+ def rerank(sentence_combinations):
7
+ similarity_scores = model.predict(sentence_combinations)
8
+ scores = [(score_max,idx) for idx,score_max in enumerate(similarity_scores)]
9
+ sim_scores_argsort = sorted(scores, key=lambda x: x[0], reverse=True)
10
+ return sim_scores_argsort
11
+
12
+ def search(query, sentences):
13
+ scores = rerank([[query, s] for s in sentences])
14
+ return scores[0]
test.ipynb ADDED
@@ -0,0 +1,444 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 6,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "import sqlite3, json\n",
10
+ "from contextlib import closing\n",
11
+ "\n",
12
+ "def load_questions(sqlite_filename):\n",
13
+ " all_questions = []\n",
14
+ " with closing(sqlite3.connect(sqlite_filename)) as db:\n",
15
+ " db.row_factory = sqlite3.Row\n",
16
+ " with closing(db.cursor()) as cursor:\n",
17
+ " results = cursor.execute(\n",
18
+ " \"SELECT id, articleId, title, category, section, questions FROM articles WHERE articleType = ? AND doNotUse IS NULL OR doNotUse = 0\",\n",
19
+ " ('article',)\n",
20
+ " ).fetchall()\n",
21
+ " \n",
22
+ " for res in results:\n",
23
+ " \n",
24
+ " questions = json.loads(res['questions'])\n",
25
+ " questions_copy = questions.copy()\n",
26
+ " \n",
27
+ " for q in questions:\n",
28
+ " q['query'] = \" \".join(res['section'].split() + res['title'].split() + q['question'].split())\n",
29
+ " q['articleId'] = res['articleId']\n",
30
+ " \n",
31
+ " for q in questions_copy:\n",
32
+ " q['query'] = q['question']\n",
33
+ " q['articleId'] = res['articleId']\n",
34
+ "\n",
35
+ " all_questions += questions\n",
36
+ " all_questions += questions_copy\n",
37
+ " return all_questions\n",
38
+ "\n",
39
+ "questions = load_questions(\"omnidesk-ai-chatgpt-questions.sqlite\")"
40
+ ]
41
+ },
42
+ {
43
+ "cell_type": "code",
44
+ "execution_count": 9,
45
+ "metadata": {},
46
+ "outputs": [],
47
+ "source": [
48
+ "from langchain.vectorstores import FAISS\n",
49
+ "from langchain.docstore.document import Document\n",
50
+ "from langchain.embeddings import SentenceTransformerEmbeddings"
51
+ ]
52
+ },
53
+ {
54
+ "cell_type": "code",
55
+ "execution_count": 10,
56
+ "metadata": {},
57
+ "outputs": [],
58
+ "source": [
59
+ "docs = [\n",
60
+ " Document(page_content=q['query'], metadata={ 'answer': q['answer'], 'articleId': q['articleId'] })\n",
61
+ " for q in questions\n",
62
+ "]"
63
+ ]
64
+ },
65
+ {
66
+ "cell_type": "code",
67
+ "execution_count": 15,
68
+ "metadata": {},
69
+ "outputs": [],
70
+ "source": [
71
+ "embeddings = SentenceTransformerEmbeddings(model_name=\"multi-qa-MiniLM-L6-cos-v1\")"
72
+ ]
73
+ },
74
+ {
75
+ "cell_type": "code",
76
+ "execution_count": 2,
77
+ "metadata": {},
78
+ "outputs": [],
79
+ "source": [
80
+ "output_dir = 'faiss_qa'"
81
+ ]
82
+ },
83
+ {
84
+ "cell_type": "code",
85
+ "execution_count": 17,
86
+ "metadata": {},
87
+ "outputs": [],
88
+ "source": [
89
+ "db = FAISS.from_documents(docs, embeddings)\n",
90
+ "db.save_local(output_dir)"
91
+ ]
92
+ },
93
+ {
94
+ "cell_type": "code",
95
+ "execution_count": null,
96
+ "metadata": {},
97
+ "outputs": [],
98
+ "source": [
99
+ "%pip install faiss-cpu langchain\n",
100
+ "%pip install -U pydantic==1.10.7"
101
+ ]
102
+ },
103
+ {
104
+ "cell_type": "code",
105
+ "execution_count": 1,
106
+ "metadata": {},
107
+ "outputs": [
108
+ {
109
+ "name": "stderr",
110
+ "output_type": "stream",
111
+ "text": [
112
+ "/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",
113
+ " warnings.warn(\n",
114
+ "2023-08-03 23:36:22.968666: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n"
115
+ ]
116
+ }
117
+ ],
118
+ "source": [
119
+ "from langchain.vectorstores import FAISS\n",
120
+ "from langchain.embeddings import SentenceTransformerEmbeddings\n",
121
+ "\n",
122
+ "embeddings = SentenceTransformerEmbeddings(model_name=\"multi-qa-MiniLM-L6-cos-v1\")\n",
123
+ "db = FAISS.load_local('faiss_qa', embeddings)"
124
+ ]
125
+ },
126
+ {
127
+ "cell_type": "code",
128
+ "execution_count": 7,
129
+ "metadata": {},
130
+ "outputs": [],
131
+ "source": [
132
+ "query = 'Почему у триггеров Почт России симваольный код не тот?'\n",
133
+ "result_docs = db.similarity_search_with_score(query, k=20)"
134
+ ]
135
+ },
136
+ {
137
+ "cell_type": "code",
138
+ "execution_count": 8,
139
+ "metadata": {},
140
+ "outputs": [],
141
+ "source": [
142
+ "import reranking\n",
143
+ "\n",
144
+ "sentences = [doc[0].page_content for doc in result_docs]\n",
145
+ "score, index = reranking.search(query, sentences)"
146
+ ]
147
+ },
148
+ {
149
+ "cell_type": "code",
150
+ "execution_count": 16,
151
+ "metadata": {},
152
+ "outputs": [
153
+ {
154
+ "data": {
155
+ "text/plain": [
156
+ "'Символьный код доставки Почта России - russian-post.'"
157
+ ]
158
+ },
159
+ "execution_count": 16,
160
+ "metadata": {},
161
+ "output_type": "execute_result"
162
+ }
163
+ ],
164
+ "source": [
165
+ "result_docs[index][0].metadata['answer']"
166
+ ]
167
+ },
168
+ {
169
+ "cell_type": "code",
170
+ "execution_count": 17,
171
+ "metadata": {},
172
+ "outputs": [
173
+ {
174
+ "name": "stderr",
175
+ "output_type": "stream",
176
+ "text": [
177
+ "Exception in thread Thread-9 (run):\n",
178
+ "Traceback (most recent call last):\n",
179
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 27, in run\n",
180
+ " loop = asyncio.get_event_loop()\n",
181
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 45, in _get_event_loop\n",
182
+ " loop = events.get_event_loop_policy().get_event_loop()\n",
183
+ " File \"/home/makcrx/anaconda3/lib/python3.10/asyncio/events.py\", line 671, in get_event_loop\n",
184
+ " raise RuntimeError('There is no current event loop in thread %r.'\n",
185
+ "RuntimeError: There is no current event loop in thread 'Thread-9 (run)'.\n",
186
+ "\n",
187
+ "During handling of the above exception, another exception occurred:\n",
188
+ "\n",
189
+ "Traceback (most recent call last):\n",
190
+ " File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 1016, in _bootstrap_inner\n",
191
+ " self.run()\n",
192
+ " File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 953, in run\n",
193
+ " self._target(*self._args, **self._kwargs)\n",
194
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/uvicorn/server.py\", line 61, in run\n",
195
+ " return asyncio.run(self.serve(sockets=sockets))\n",
196
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 31, in run\n",
197
+ " _patch_loop(loop)\n",
198
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 175, in _patch_loop\n",
199
+ " raise ValueError('Can\\'t patch loop of type %s' % type(loop))\n",
200
+ "ValueError: Can't patch loop of type <class 'uvloop.Loop'>\n",
201
+ "/home/makcrx/anaconda3/lib/python3.10/threading.py:1018: RuntimeWarning: coroutine 'Server.serve' was never awaited\n",
202
+ " self._invoke_excepthook(self)\n",
203
+ "RuntimeWarning: Enable tracemalloc to get the object allocation traceback\n",
204
+ "Exception in thread Thread-10 (run):\n",
205
+ "Traceback (most recent call last):\n",
206
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 27, in run\n",
207
+ " loop = asyncio.get_event_loop()\n",
208
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 45, in _get_event_loop\n",
209
+ " loop = events.get_event_loop_policy().get_event_loop()\n",
210
+ " File \"/home/makcrx/anaconda3/lib/python3.10/asyncio/events.py\", line 671, in get_event_loop\n",
211
+ " raise RuntimeError('There is no current event loop in thread %r.'\n",
212
+ "RuntimeError: There is no current event loop in thread 'Thread-10 (run)'.\n",
213
+ "\n",
214
+ "During handling of the above exception, another exception occurred:\n",
215
+ "\n",
216
+ "Traceback (most recent call last):\n",
217
+ " File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 1016, in _bootstrap_inner\n",
218
+ " self.run()\n",
219
+ " File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 953, in run\n",
220
+ " self._target(*self._args, **self._kwargs)\n",
221
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/uvicorn/server.py\", line 61, in run\n",
222
+ " return asyncio.run(self.serve(sockets=sockets))\n",
223
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 31, in run\n",
224
+ " _patch_loop(loop)\n",
225
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 175, in _patch_loop\n",
226
+ " raise ValueError('Can\\'t patch loop of type %s' % type(loop))\n",
227
+ "ValueError: Can't patch loop of type <class 'uvloop.Loop'>\n",
228
+ "Exception in thread Thread-11 (run):\n",
229
+ "Traceback (most recent call last):\n",
230
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 27, in run\n",
231
+ " loop = asyncio.get_event_loop()\n",
232
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 45, in _get_event_loop\n",
233
+ " loop = events.get_event_loop_policy().get_event_loop()\n",
234
+ " File \"/home/makcrx/anaconda3/lib/python3.10/asyncio/events.py\", line 671, in get_event_loop\n",
235
+ " raise RuntimeError('There is no current event loop in thread %r.'\n",
236
+ "RuntimeError: There is no current event loop in thread 'Thread-11 (run)'.\n",
237
+ "\n",
238
+ "During handling of the above exception, another exception occurred:\n",
239
+ "\n",
240
+ "Traceback (most recent call last):\n",
241
+ " File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 1016, in _bootstrap_inner\n",
242
+ " self.run()\n",
243
+ " File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 953, in run\n",
244
+ " self._target(*self._args, **self._kwargs)\n",
245
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/uvicorn/server.py\", line 61, in run\n",
246
+ " return asyncio.run(self.serve(sockets=sockets))\n",
247
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 31, in run\n",
248
+ " _patch_loop(loop)\n",
249
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 175, in _patch_loop\n",
250
+ " raise ValueError('Can\\'t patch loop of type %s' % type(loop))\n",
251
+ "ValueError: Can't patch loop of type <class 'uvloop.Loop'>\n",
252
+ "Exception in thread Thread-12 (run):\n",
253
+ "Traceback (most recent call last):\n",
254
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 27, in run\n",
255
+ " loop = asyncio.get_event_loop()\n",
256
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 45, in _get_event_loop\n",
257
+ " loop = events.get_event_loop_policy().get_event_loop()\n",
258
+ " File \"/home/makcrx/anaconda3/lib/python3.10/asyncio/events.py\", line 671, in get_event_loop\n",
259
+ " raise RuntimeError('There is no current event loop in thread %r.'\n",
260
+ "RuntimeError: There is no current event loop in thread 'Thread-12 (run)'.\n",
261
+ "\n",
262
+ "During handling of the above exception, another exception occurred:\n",
263
+ "\n",
264
+ "Traceback (most recent call last):\n",
265
+ " File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 1016, in _bootstrap_inner\n",
266
+ " self.run()\n",
267
+ " File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 953, in run\n",
268
+ " self._target(*self._args, **self._kwargs)\n",
269
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/uvicorn/server.py\", line 61, in run\n",
270
+ " return asyncio.run(self.serve(sockets=sockets))\n",
271
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 31, in run\n",
272
+ " _patch_loop(loop)\n",
273
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 175, in _patch_loop\n",
274
+ " raise ValueError('Can\\'t patch loop of type %s' % type(loop))\n",
275
+ "ValueError: Can't patch loop of type <class 'uvloop.Loop'>\n",
276
+ "Exception in thread Thread-13 (run):\n",
277
+ "Traceback (most recent call last):\n",
278
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 27, in run\n",
279
+ " loop = asyncio.get_event_loop()\n",
280
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 45, in _get_event_loop\n",
281
+ " loop = events.get_event_loop_policy().get_event_loop()\n",
282
+ " File \"/home/makcrx/anaconda3/lib/python3.10/asyncio/events.py\", line 671, in get_event_loop\n",
283
+ " raise RuntimeError('There is no current event loop in thread %r.'\n",
284
+ "RuntimeError: There is no current event loop in thread 'Thread-13 (run)'.\n",
285
+ "\n",
286
+ "During handling of the above exception, another exception occurred:\n",
287
+ "\n",
288
+ "Traceback (most recent call last):\n",
289
+ " File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 1016, in _bootstrap_inner\n",
290
+ " self.run()\n",
291
+ " File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 953, in run\n",
292
+ " self._target(*self._args, **self._kwargs)\n",
293
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/uvicorn/server.py\", line 61, in run\n",
294
+ " return asyncio.run(self.serve(sockets=sockets))\n",
295
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 31, in run\n",
296
+ " _patch_loop(loop)\n",
297
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 175, in _patch_loop\n",
298
+ " raise ValueError('Can\\'t patch loop of type %s' % type(loop))\n",
299
+ "ValueError: Can't patch loop of type <class 'uvloop.Loop'>\n",
300
+ "Exception in thread Thread-14 (run):\n",
301
+ "Traceback (most recent call last):\n",
302
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 27, in run\n",
303
+ " loop = asyncio.get_event_loop()\n",
304
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 45, in _get_event_loop\n",
305
+ " loop = events.get_event_loop_policy().get_event_loop()\n",
306
+ " File \"/home/makcrx/anaconda3/lib/python3.10/asyncio/events.py\", line 671, in get_event_loop\n",
307
+ " raise RuntimeError('There is no current event loop in thread %r.'\n",
308
+ "RuntimeError: There is no current event loop in thread 'Thread-14 (run)'.\n",
309
+ "\n",
310
+ "During handling of the above exception, another exception occurred:\n",
311
+ "\n",
312
+ "Traceback (most recent call last):\n",
313
+ " File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 1016, in _bootstrap_inner\n",
314
+ " self.run()\n",
315
+ " File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 953, in run\n",
316
+ " self._target(*self._args, **self._kwargs)\n",
317
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/uvicorn/server.py\", line 61, in run\n",
318
+ " return asyncio.run(self.serve(sockets=sockets))\n",
319
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 31, in run\n",
320
+ " _patch_loop(loop)\n",
321
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 175, in _patch_loop\n",
322
+ " raise ValueError('Can\\'t patch loop of type %s' % type(loop))\n",
323
+ "ValueError: Can't patch loop of type <class 'uvloop.Loop'>\n",
324
+ "Exception in thread Thread-15 (run):\n",
325
+ "Traceback (most recent call last):\n",
326
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 27, in run\n",
327
+ " loop = asyncio.get_event_loop()\n",
328
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 45, in _get_event_loop\n",
329
+ " loop = events.get_event_loop_policy().get_event_loop()\n",
330
+ " File \"/home/makcrx/anaconda3/lib/python3.10/asyncio/events.py\", line 671, in get_event_loop\n",
331
+ " raise RuntimeError('There is no current event loop in thread %r.'\n",
332
+ "RuntimeError: There is no current event loop in thread 'Thread-15 (run)'.\n",
333
+ "\n",
334
+ "During handling of the above exception, another exception occurred:\n",
335
+ "\n",
336
+ "Traceback (most recent call last):\n",
337
+ " File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 1016, in _bootstrap_inner\n",
338
+ " self.run()\n",
339
+ " File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 953, in run\n",
340
+ " self._target(*self._args, **self._kwargs)\n",
341
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/uvicorn/server.py\", line 61, in run\n",
342
+ " return asyncio.run(self.serve(sockets=sockets))\n",
343
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 31, in run\n",
344
+ " _patch_loop(loop)\n",
345
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 175, in _patch_loop\n",
346
+ " raise ValueError('Can\\'t patch loop of type %s' % type(loop))\n",
347
+ "ValueError: Can't patch loop of type <class 'uvloop.Loop'>\n",
348
+ "Exception in thread Thread-16 (run):\n",
349
+ "Traceback (most recent call last):\n",
350
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 27, in run\n",
351
+ " loop = asyncio.get_event_loop()\n",
352
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 45, in _get_event_loop\n",
353
+ " loop = events.get_event_loop_policy().get_event_loop()\n",
354
+ " File \"/home/makcrx/anaconda3/lib/python3.10/asyncio/events.py\", line 671, in get_event_loop\n",
355
+ " raise RuntimeError('There is no current event loop in thread %r.'\n",
356
+ "RuntimeError: There is no current event loop in thread 'Thread-16 (run)'.\n",
357
+ "\n",
358
+ "During handling of the above exception, another exception occurred:\n",
359
+ "\n",
360
+ "Traceback (most recent call last):\n",
361
+ " File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 1016, in _bootstrap_inner\n",
362
+ " self.run()\n",
363
+ " File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 953, in run\n",
364
+ " self._target(*self._args, **self._kwargs)\n",
365
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/uvicorn/server.py\", line 61, in run\n",
366
+ " return asyncio.run(self.serve(sockets=sockets))\n",
367
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 31, in run\n",
368
+ " _patch_loop(loop)\n",
369
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 175, in _patch_loop\n",
370
+ " raise ValueError('Can\\'t patch loop of type %s' % type(loop))\n",
371
+ "ValueError: Can't patch loop of type <class 'uvloop.Loop'>\n",
372
+ "Exception in thread Thread-17 (run):\n",
373
+ "Traceback (most recent call last):\n",
374
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 27, in run\n",
375
+ " loop = asyncio.get_event_loop()\n",
376
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 45, in _get_event_loop\n",
377
+ " loop = events.get_event_loop_policy().get_event_loop()\n",
378
+ " File \"/home/makcrx/anaconda3/lib/python3.10/asyncio/events.py\", line 671, in get_event_loop\n",
379
+ " raise RuntimeError('There is no current event loop in thread %r.'\n",
380
+ "RuntimeError: There is no current event loop in thread 'Thread-17 (run)'.\n",
381
+ "\n",
382
+ "During handling of the above exception, another exception occurred:\n",
383
+ "\n",
384
+ "Traceback (most recent call last):\n",
385
+ " File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 1016, in _bootstrap_inner\n",
386
+ " self.run()\n",
387
+ " File \"/home/makcrx/anaconda3/lib/python3.10/threading.py\", line 953, in run\n",
388
+ " self._target(*self._args, **self._kwargs)\n",
389
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/uvicorn/server.py\", line 61, in run\n",
390
+ " return asyncio.run(self.serve(sockets=sockets))\n",
391
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 31, in run\n",
392
+ " _patch_loop(loop)\n",
393
+ " File \"/home/makcrx/anaconda3/lib/python3.10/site-packages/nest_asyncio.py\", line 175, in _patch_loop\n",
394
+ " raise ValueError('Can\\'t patch loop of type %s' % type(loop))\n",
395
+ "ValueError: Can't patch loop of type <class 'uvloop.Loop'>\n"
396
+ ]
397
+ }
398
+ ],
399
+ "source": [
400
+ "import gradio as gr\n",
401
+ "import reranking\n",
402
+ "\n",
403
+ "def main(query):\n",
404
+ " result_docs = db.similarity_search_with_score(query, k=20)\n",
405
+ " sentences = [doc[0].page_content for doc in result_docs]\n",
406
+ " score, index = reranking.search(query, sentences)\n",
407
+ " return result_docs[index][0].metadata['answer']\n",
408
+ "\n",
409
+ "demo = gr.Interface(fn=main, inputs=\"text\", outputs=\"text\")\n",
410
+ "\n",
411
+ "demo.launch()"
412
+ ]
413
+ },
414
+ {
415
+ "cell_type": "code",
416
+ "execution_count": null,
417
+ "metadata": {},
418
+ "outputs": [],
419
+ "source": []
420
+ }
421
+ ],
422
+ "metadata": {
423
+ "kernelspec": {
424
+ "display_name": "Python 3 (ipykernel)",
425
+ "language": "python",
426
+ "name": "python3"
427
+ },
428
+ "language_info": {
429
+ "codemirror_mode": {
430
+ "name": "ipython",
431
+ "version": 3
432
+ },
433
+ "file_extension": ".py",
434
+ "mimetype": "text/x-python",
435
+ "name": "python",
436
+ "nbconvert_exporter": "python",
437
+ "pygments_lexer": "ipython3",
438
+ "version": "3.10.9"
439
+ },
440
+ "orig_nbformat": 4
441
+ },
442
+ "nbformat": 4,
443
+ "nbformat_minor": 2
444
+ }