-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
57 lines (47 loc) · 1.5 KB
/
server.py
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
from model import model
from typing import Union
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from cryptography.fernet import Fernet
import random
file = open('dataset.txt', 'r')
words = [word[:-1] for word in file]
fernetKey = Fernet.generate_key()
fernet = Fernet(fernetKey)
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=['*']
)
@app.get("/random_word")
def random_word() -> bytes:
while True:
word = random.choice(words)
if not word + "_NOUN" in model:
continue
mostSimilar = model.most_similar(word + "_NOUN", topn=1)[0]
if mostSimilar[1] < 0.7 or not mostSimilar[0].endswith("_NOUN"):
continue
return fernet.encrypt(random.choice(words).encode())
@app.get("/similarity")
def similarity(encWord: bytes, word: str) -> float:
return float(model.similarity(
fernet.decrypt(encWord).decode('utf-8') + "_NOUN",
word.lower() + "_NOUN"
))
@app.get("/check")
def check(encWord: bytes, word: str) -> bool:
return fernet.decrypt(encWord).decode('utf-8') == word.lower()
@app.get("/hint")
def hint(encWord: bytes, bestGuess: float):
try:
hints = [
entry for entry in model.most_similar(
fernet.decrypt(encWord).decode('utf-8') + "_NOUN",
topn=100
) if entry[1] > bestGuess
]
hint = random.choice(hints)
return (hint[0].split('_')[0], hint[1])
except IndexError:
return None