Spaces:
Runtime error
Runtime error
first commit
Browse files- app.py +67 -0
- cc.en.300.bin +3 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import fasttext
|
2 |
+
import gradio as gr
|
3 |
+
from gradio import components as components
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
model = "cc.en.300.bin"
|
7 |
+
model = fasttext.load_model(model)
|
8 |
+
|
9 |
+
def get_cosine(a, b):
|
10 |
+
# vectorize
|
11 |
+
# a = model.get_word_vector(a.lower())
|
12 |
+
# b = model.get_word_vector(b.lower())
|
13 |
+
cosine_similarity = np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
|
14 |
+
return cosine_similarity
|
15 |
+
|
16 |
+
|
17 |
+
def get_score(targets, answers):
|
18 |
+
|
19 |
+
results = []
|
20 |
+
targets = targets.split(",")
|
21 |
+
target_len = len(targets)
|
22 |
+
answers = answers.split(",")
|
23 |
+
|
24 |
+
th = 0.6
|
25 |
+
for i, target in enumerate(targets):
|
26 |
+
if target[0]==" ": target=target[1:]
|
27 |
+
# if i>2:break
|
28 |
+
target_vector = model.get_word_vector(target.lower())
|
29 |
+
|
30 |
+
cosine_list = []
|
31 |
+
# compare all of answer data
|
32 |
+
for answer in answers:
|
33 |
+
answer_vector = model.get_word_vector(answer.lower())
|
34 |
+
cosine = get_cosine(target_vector, answer_vector)
|
35 |
+
cosine_list.append(cosine)
|
36 |
+
|
37 |
+
# comparison is over threshold -> true else -> false
|
38 |
+
# results.append(max(cosine_list)>th)
|
39 |
+
|
40 |
+
# reject answer once it is used
|
41 |
+
if max(cosine_list)>th:
|
42 |
+
argmax = cosine_list.index(max(cosine_list))
|
43 |
+
results.append(max(cosine_list))
|
44 |
+
answers.remove(answers[argmax])
|
45 |
+
|
46 |
+
if len(answers)==0: break
|
47 |
+
if len(results)==0: return 0
|
48 |
+
result = sum(results)/len(results)
|
49 |
+
# result = sum(results) / target_len
|
50 |
+
return result
|
51 |
+
|
52 |
+
|
53 |
+
def my_app(input1, input2):
|
54 |
+
return get_score(input1, input2)
|
55 |
+
|
56 |
+
inputs = [
|
57 |
+
components.Textbox(label="Input 1"),
|
58 |
+
components.Textbox(label="Input 2")
|
59 |
+
]
|
60 |
+
output = components.Textbox(label="Output")
|
61 |
+
|
62 |
+
app = gr.Interface(
|
63 |
+
fn=my_app,
|
64 |
+
inputs=inputs,
|
65 |
+
outputs=output
|
66 |
+
)
|
67 |
+
app.launch()
|
cc.en.300.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:14c7167b130056944cbdc37b7451f055867fe9a4e3fed3bbc1ecc0e74f6763ca
|
3 |
+
size 7237176312
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
fasttext
|