Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import json
|
| 2 |
import requests
|
|
|
|
| 3 |
from relbert import RelBERT
|
| 4 |
import gradio as gr
|
| 5 |
|
|
@@ -21,6 +22,12 @@ def cosine_similarity(a, b, zero_vector_mask: float = -100):
|
|
| 21 |
return sum(map(lambda x: x[0] * x[1], zip(a, b)))/(norm_a * norm_b)
|
| 22 |
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def greet(
|
| 25 |
query,
|
| 26 |
candidate_1,
|
|
@@ -29,7 +36,7 @@ def greet(
|
|
| 29 |
candidate_4,
|
| 30 |
candidate_5,
|
| 31 |
candidate_6):
|
| 32 |
-
query = query.split(',')
|
| 33 |
# validate query
|
| 34 |
if len(query) == 0:
|
| 35 |
raise ValueError(f'ERROR: query is empty {query}')
|
|
@@ -50,7 +57,7 @@ def greet(
|
|
| 50 |
]):
|
| 51 |
if i == '':
|
| 52 |
continue
|
| 53 |
-
candidate = i.split(',')
|
| 54 |
if len(candidate) == 1:
|
| 55 |
raise ValueError(f'ERROR: candidate {n + 1} contains single word {candidate}')
|
| 56 |
if len(candidate) > 2:
|
|
@@ -64,9 +71,6 @@ def greet(
|
|
| 64 |
sims = []
|
| 65 |
for v in vectors:
|
| 66 |
sims.append(cosine_similarity(v, vector_q))
|
| 67 |
-
# output = list(zip(pairs_id, sims, pairs))
|
| 68 |
-
# output = sorted(list(zip(pairs_id, sims, pairs)), key=lambda _x: _x[1], reverse=True)
|
| 69 |
-
# output = {f'candidate {i}: [{p[0]}, {p[1]}]': s for n, (i, s, p) in enumerate(output)}
|
| 70 |
output = {f'candidate {i}: [{p[0]}, {p[1]}]': s for i, s, p in zip(pairs_id, sims, pairs)}
|
| 71 |
return output
|
| 72 |
|
|
|
|
| 1 |
import json
|
| 2 |
import requests
|
| 3 |
+
import re
|
| 4 |
from relbert import RelBERT
|
| 5 |
import gradio as gr
|
| 6 |
|
|
|
|
| 22 |
return sum(map(lambda x: x[0] * x[1], zip(a, b)))/(norm_a * norm_b)
|
| 23 |
|
| 24 |
|
| 25 |
+
def clean(text):
|
| 26 |
+
text = re.sub(r"\A\s+", "", text)
|
| 27 |
+
text = re.sub(r"\s+\Z", "", text)
|
| 28 |
+
return text
|
| 29 |
+
|
| 30 |
+
|
| 31 |
def greet(
|
| 32 |
query,
|
| 33 |
candidate_1,
|
|
|
|
| 36 |
candidate_4,
|
| 37 |
candidate_5,
|
| 38 |
candidate_6):
|
| 39 |
+
query = [clean(i) for i in query.split(',')]
|
| 40 |
# validate query
|
| 41 |
if len(query) == 0:
|
| 42 |
raise ValueError(f'ERROR: query is empty {query}')
|
|
|
|
| 57 |
]):
|
| 58 |
if i == '':
|
| 59 |
continue
|
| 60 |
+
candidate = [clean(x) for x in i.split(',')]
|
| 61 |
if len(candidate) == 1:
|
| 62 |
raise ValueError(f'ERROR: candidate {n + 1} contains single word {candidate}')
|
| 63 |
if len(candidate) > 2:
|
|
|
|
| 71 |
sims = []
|
| 72 |
for v in vectors:
|
| 73 |
sims.append(cosine_similarity(v, vector_q))
|
|
|
|
|
|
|
|
|
|
| 74 |
output = {f'candidate {i}: [{p[0]}, {p[1]}]': s for i, s, p in zip(pairs_id, sims, pairs)}
|
| 75 |
return output
|
| 76 |
|