Spaces:
Sleeping
Sleeping
akh1r0ck
commited on
Commit
•
3160eb7
1
Parent(s):
6fcdeec
initialize
Browse files- Dockerfile +11 -0
- main.py +62 -0
- requirements.txt +5 -0
Dockerfile
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
|
3 |
+
WORKDIR /code
|
4 |
+
|
5 |
+
COPY ./requirements.txt /code/requirements.txt
|
6 |
+
|
7 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
8 |
+
|
9 |
+
COPY . .
|
10 |
+
|
11 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
import fasttext
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
|
6 |
+
model = "cc.en.300.bin"
|
7 |
+
model = fasttext.load_model(model)
|
8 |
+
|
9 |
+
def get_cosine(a, b):
|
10 |
+
cosine_similarity = np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
|
11 |
+
return cosine_similarity
|
12 |
+
|
13 |
+
|
14 |
+
def get_score(targets, answers, th):
|
15 |
+
th = float(th)
|
16 |
+
|
17 |
+
results = []
|
18 |
+
targets = targets.split(",")
|
19 |
+
target_len = len(targets)
|
20 |
+
answers = answers.split(",")
|
21 |
+
|
22 |
+
# th = 0.6
|
23 |
+
for i, target in enumerate(targets):
|
24 |
+
if target[0]==" ": target=target[1:]
|
25 |
+
# if i>2:break
|
26 |
+
target_vector = model.get_word_vector(target.lower())
|
27 |
+
|
28 |
+
cosine_list = []
|
29 |
+
# compare all of answer data
|
30 |
+
for answer in answers:
|
31 |
+
answer_vector = model.get_word_vector(answer.lower())
|
32 |
+
cosine = get_cosine(target_vector, answer_vector)
|
33 |
+
cosine_list.append(cosine)
|
34 |
+
|
35 |
+
# comparison is over threshold -> true else -> false
|
36 |
+
# results.append(max(cosine_list)>th)
|
37 |
+
|
38 |
+
# reject answer once it is used
|
39 |
+
if max(cosine_list)>th:
|
40 |
+
argmax = cosine_list.index(max(cosine_list))
|
41 |
+
results.append(max(cosine_list))
|
42 |
+
answers.remove(answers[argmax])
|
43 |
+
|
44 |
+
if len(answers)==0: break
|
45 |
+
if len(results)==0: return 0
|
46 |
+
result = int(sum(results) / len(results) * 100)
|
47 |
+
# result = sum(results) / target_len
|
48 |
+
return result
|
49 |
+
|
50 |
+
|
51 |
+
|
52 |
+
app = FastAPI()
|
53 |
+
|
54 |
+
|
55 |
+
@app.get("/ping")
|
56 |
+
def read_root():
|
57 |
+
return {"ok"}
|
58 |
+
|
59 |
+
|
60 |
+
@app.post("/scores/")
|
61 |
+
def my_app(targets, answers, th=0.6):
|
62 |
+
return get_score(targets, answers, th)
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi==0.74.*
|
2 |
+
requests==2.27.*
|
3 |
+
uvicorn[standard]==0.17.*
|
4 |
+
fasttext
|
5 |
+
numpy
|