Spaces:
Runtime error
Runtime error
File size: 1,820 Bytes
10d6a0c 58e40db 10d6a0c 58e40db 10d6a0c 3832185 783173e 10d6a0c 58e40db 10d6a0c 58e40db 10d6a0c 9a0450c |
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
import fasttext
import gradio as gr
from gradio import components as components
import numpy as np
model = "cc.en.300.bin"
model = fasttext.load_model(model)
def get_cosine(a, b):
cosine_similarity = np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
return cosine_similarity
def get_score(targets, answers, th):
th = float(th)
results = []
targets = targets.split(",")
target_len = len(targets)
answers = answers.split(",")
# th = 0.6
for i, target in enumerate(targets):
if target[0]==" ": target=target[1:]
# if i>2:break
target_vector = model.get_word_vector(target.lower())
cosine_list = []
# compare all of answer data
for answer in answers:
answer_vector = model.get_word_vector(answer.lower())
cosine = get_cosine(target_vector, answer_vector)
cosine_list.append(cosine)
# comparison is over threshold -> true else -> false
# results.append(max(cosine_list)>th)
# reject answer once it is used
if max(cosine_list)>th:
argmax = cosine_list.index(max(cosine_list))
results.append(max(cosine_list))
answers.remove(answers[argmax])
if len(answers)==0: break
if len(results)==0: return 0
result = int(sum(results) / len(results) * 100)
# result = sum(results) / target_len
return result
def my_app(targets, answers, th):
return get_score(targets, answers, th)
inputs = [
components.Textbox(label="Targets"),
components.Textbox(label="Answers"),
components.Textbox(label="Threshold", value=0.6),
]
output = components.Textbox(label="Output")
app = gr.Interface(
fn=my_app,
inputs=inputs,
outputs=output
)
app.launch(
app_kwargs={"docs_url":"/docs"}
)
|