forked from NewsJelly/semantle-ko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
148 lines (124 loc) · 4.33 KB
/
main.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from fastapi import Depends, FastAPI, Request, BackgroundTasks
import time
from fastapi.responses import JSONResponse, RedirectResponse
from sqlalchemy.orm import Session
import pickle
import random
from fastapi.staticfiles import StaticFiles
import os
import word2vec
from process_similar import get_nearest
import crud
from database import engine, SessionLocal, Base
Base.metadata.create_all(bind=engine)
app = FastAPI(openapi_url=None, docs_url=None, redoc_url=None)
with open('data/valid_nearest.dat', 'rb') as f:
valid_nearest_words, valid_nearest_vecs = pickle.load(f)
with open('data/secrets.txt', 'r', encoding='utf-8') as f:
words = [l.strip() for l in f.readlines()]
app.mount("/static", StaticFiles(directory="xstatic"), name="static")
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
def deletedb(db: Session, sid: str):
time.sleep(43200)
ans = crud.d_get(db, sid).data
crud.d_delete(db, sid)
os.remove(f"data/near/{ans}.dat")
@app.get("/")
def root():
return RedirectResponse("/static/select.html")
@app.get('/start')
def start(request: Request, background_tasks: BackgroundTasks, db: Session = Depends(get_db)):
hdr = request.headers
sid = None
if "word-sess" in hdr:
sid = hdr["word-sess"]
if not crud.d_get(db, sid):
sid = None
if not sid:
sid = crud.d_new(db)
w = random.choice(words)
crud.d_modify(db, sid, w)
background_tasks.add_task(deletedb, db, sid)
ans = crud.d_get(db, sid).data
nearests = get_nearest(ans, ans, valid_nearest_words, valid_nearest_vecs) #just generate
return JSONResponse({"sess": sid})
@app.get('/check')
def start(request: Request, db: Session = Depends(get_db)):
hdr = request.headers
sid = None
if "word-sess" in hdr:
sid = hdr["word-sess"]
if not crud.d_get(db, sid):
sid = None
if not sid:
return JSONResponse({"error": "unknown"}, status_code=404)
return JSONResponse({"sess": sid})
@app.get('/guess')
def get_guess(request: Request, word: str, db: Session = Depends(get_db)):
hdr = request.headers
sid = None
if "word-sess" in hdr:
sid = hdr["word-sess"]
if not crud.d_get(db, sid):
sid = None
if not sid:
return JSONResponse({"error": "unknown"}, status_code=404)
ans = crud.d_get(db, sid).data
nearests = get_nearest(ans, ans, valid_nearest_words, valid_nearest_vecs)
if ans.lower() == word.lower():
word = ans
rtn = {"guess": word}
# check most similar
if word in nearests:
rtn["sim"] = nearests[word][1]
rtn["rank"] = nearests[word][0]
else:
try:
rtn["sim"] = word2vec.similarity(ans, word)
rtn["rank"] = "1000위 이상"
except KeyError:
return JSONResponse({"error": "unknown"}, status_code=404)
return JSONResponse(rtn)
@app.get('/similarity')
def get_similarity(request: Request, db: Session = Depends(get_db)):
hdr = request.headers
sid = None
if "word-sess" in hdr:
sid = hdr["word-sess"]
if not crud.d_get(db, sid):
sid = None
if not sid:
return JSONResponse({"error": "unknown"}, status_code=404)
ans = crud.d_get(db, sid).data
nearests = get_nearest(ans, ans, valid_nearest_words, valid_nearest_vecs)
nearest_dists = sorted([v[1] for v in nearests.values()])
return JSONResponse({"top": nearest_dists[-2], "top10": nearest_dists[-11], "rest": nearest_dists[0]})
@app.get('/giveup')
def giveup(request: Request, db: Session = Depends(get_db)):
hdr = request.headers
sid = None
if "word-sess" in hdr:
sid = hdr["word-sess"]
if not crud.d_get(db, sid):
sid = None
if not sid:
return JSONResponse({"error": "unknown"}, status_code=404)
ans = crud.d_get(db, sid).data
return JSONResponse({"word": ans})
@app.get('/near1k')
def near1k(request: Request, db: Session = Depends(get_db)):
hdr = request.headers
sid = None
if "word-sess" in hdr:
sid = hdr["word-sess"]
if not crud.d_get(db, sid):
sid = None
if not sid:
return JSONResponse({"error": "unknown"}, status_code=404)
ans = crud.d_get(db, sid).data
return JSONResponse(get_nearest(ans, ans, valid_nearest_words, valid_nearest_vecs))