update
Browse files
app.py
CHANGED
@@ -24,6 +24,15 @@ def greet(
|
|
24 |
candidate_8,
|
25 |
candidate_9,
|
26 |
candidate_10):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
pairs = []
|
28 |
pairs_id = []
|
29 |
for n, i in enumerate([
|
@@ -38,32 +47,25 @@ def greet(
|
|
38 |
candidate_9,
|
39 |
candidate_10
|
40 |
]):
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
vector_q = vectors.pop(-1)
|
61 |
-
sims = []
|
62 |
-
for v in vectors:
|
63 |
-
sims.append(cosine_similarity(v, vector_q))
|
64 |
-
output = sorted(list(zip(pairs_id, sims, pairs)), key=lambda _x: _x[1], reverse=True)
|
65 |
-
output = {f'candidate {n}: [{p[0]}, {p[1]}]': s for n, (i, s, p) in enumerate(output)}
|
66 |
-
return output
|
67 |
|
68 |
|
69 |
demo = gr.Interface(
|
|
|
24 |
candidate_8,
|
25 |
candidate_9,
|
26 |
candidate_10):
|
27 |
+
query = query.split(',')
|
28 |
+
# validate query
|
29 |
+
if len(query) == 0:
|
30 |
+
raise ValueError(f'ERROR: query is empty {query}')
|
31 |
+
if len(query) == 1:
|
32 |
+
raise ValueError(f'ERROR: query contains single word {query}')
|
33 |
+
if len(query) > 2:
|
34 |
+
raise ValueError(f'ERROR: query contains more than two word {query}')
|
35 |
+
|
36 |
pairs = []
|
37 |
pairs_id = []
|
38 |
for n, i in enumerate([
|
|
|
47 |
candidate_9,
|
48 |
candidate_10
|
49 |
]):
|
50 |
+
if i == '':
|
51 |
+
continue
|
52 |
+
candidate = i.split(',')
|
53 |
+
if len(candidate) == 1:
|
54 |
+
raise ValueError(f'ERROR: candidate {n + 1} contains single word {candidate}')
|
55 |
+
if len(candidate) > 2:
|
56 |
+
raise ValueError(f'ERROR: candidate {n + 1} contains more than two word {candidate}')
|
57 |
+
pairs.append(candidate)
|
58 |
+
pairs_id.append(n+1)
|
59 |
+
if len(pairs_id) < 2:
|
60 |
+
raise ValueError(f'ERROR: please specify at least two candidates: {pairs}')
|
61 |
+
vectors = model.get_embedding(pairs+[query])
|
62 |
+
vector_q = vectors.pop(-1)
|
63 |
+
sims = []
|
64 |
+
for v in vectors:
|
65 |
+
sims.append(cosine_similarity(v, vector_q))
|
66 |
+
output = sorted(list(zip(pairs_id, sims, pairs)), key=lambda _x: _x[1], reverse=True)
|
67 |
+
output = {f'candidate {n}: [{p[0]}, {p[1]}]': s for n, (i, s, p) in enumerate(output)}
|
68 |
+
return output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
|
71 |
demo = gr.Interface(
|