-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
39 lines (33 loc) · 1.04 KB
/
app.py
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
import numpy as np
from sentence_transformers import SentenceTransformer
NUM_NEIGHBORS=5
documents = []
documents_raw = []
with open('data.txt', 'r') as f:
entry = ""
lines = f.readlines()
for line in lines:
if len(line.strip()) > 0:
entry += line
else:
documents.append("<A>" + entry.strip())
documents_raw.append(entry)
entry = ""
model = SentenceTransformer('clips/mfaq', "en")
model.eval()
d_rep = model.encode(documents, batch_size=16)
while True:
query = "<Q>" + input("Question: ") + "?"
q_rep_i = model.encode([query])[0]
scores = []
for j, d_rep_i in enumerate(d_rep):
score = q_rep_i @ d_rep_i.T
scores.append(score)
scores_sortidx = list(reversed(np.argsort(scores)))
print("")
for idx in range(NUM_NEIGHBORS):
best_idx = scores_sortidx[idx]
score = scores[best_idx]
print("(*) FAQ ({}):".format(score), documents_raw[best_idx])
print("")
print("=============================================")