-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
59 lines (40 loc) · 1.29 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
from akinator import Akinator
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from tree import BinaryDecisionTreeClassifier
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
tree = BinaryDecisionTreeClassifier('tree.json')
sessions = {}
class AnswerBody(BaseModel):
answer: int
class AddPersonBody(BaseModel):
name: str
feature: str
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/start")
def start():
akinator = Akinator(tree)
sessions[akinator.id] = akinator
return {"session_id": akinator.id, "question": akinator.current_question}
@app.post("/answer/{session_id}")
def answer(session_id: int, body: AnswerBody):
akinator = sessions[session_id]
return akinator.answer_question(body.answer)
@app.post("/person/{session_id}")
def add_person(session_id: int, body: AddPersonBody):
akinator = sessions[session_id]
akinator.add_person(body.name, body.feature)
@app.post("/continue/{session_id}")
def continue_game(session_id: int):
akinator = sessions[session_id]
return akinator.continue_game()