Spaces:
Runtime error
Runtime error
Commit
·
0568b88
1
Parent(s):
ccec9b3
Create server.py
Browse files
server.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Optional
|
| 2 |
+
from fastapi import BackgroundTasks, FastAPI
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
import json
|
| 5 |
+
import time
|
| 6 |
+
import random
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
app = FastAPI()
|
| 10 |
+
answer_dict = dict()
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class DialogInfo(BaseModel):
|
| 14 |
+
q_id: str = None
|
| 15 |
+
query: str = None
|
| 16 |
+
history: list = []
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@app.get("/")
|
| 20 |
+
def read_root():
|
| 21 |
+
return {"Hello": "World!"}
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
@app.get("/chatbot/dialoginfo")
|
| 25 |
+
def read_dialog(dialogid: str):
|
| 26 |
+
# return dialogid
|
| 27 |
+
return answer_dict[dialogid]
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
@app.get("/chatbot/finishquery")
|
| 31 |
+
def read_dialog(dialogid: str):
|
| 32 |
+
# return dialogid
|
| 33 |
+
return random.choices([True, False], (0.3, 0.7))[0]
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def background_process(diainfo: DialogInfo):
|
| 37 |
+
answer_dict[diainfo.q_id].q_id = diainfo.q_id
|
| 38 |
+
answer_dict[diainfo.q_id].history = diainfo.history
|
| 39 |
+
answer_dict[diainfo.q_id].query = diainfo.query
|
| 40 |
+
for _ in range(5):
|
| 41 |
+
answer_dict[diainfo.q_id].history = diainfo.history
|
| 42 |
+
if diainfo.q_id.startswith('0'):
|
| 43 |
+
answer_dict[diainfo.q_id].query = answer_dict[diainfo.q_id].query + 'i'
|
| 44 |
+
elif diainfo.q_id.startswith('9'):
|
| 45 |
+
answer_dict[diainfo.q_id].query = answer_dict[diainfo.q_id].query + 'o'
|
| 46 |
+
time.sleep(0.5)
|
| 47 |
+
print(answer_dict[diainfo.q_id])
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
@app.post("/chatbot/")
|
| 51 |
+
def read_query(chat: DialogInfo, background_tasks: BackgroundTasks):
|
| 52 |
+
answer_dict[chat.q_id] = chat
|
| 53 |
+
background_tasks.add_task(background_process, chat)
|
| 54 |
+
return {"code": 202, "msg": f"Query id {chat.q_id} accepted."}
|