Spaces:
Runtime error
Runtime error
File size: 1,464 Bytes
0568b88 |
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 |
from typing import Optional
from fastapi import BackgroundTasks, FastAPI
from pydantic import BaseModel
import json
import time
import random
app = FastAPI()
answer_dict = dict()
class DialogInfo(BaseModel):
q_id: str = None
query: str = None
history: list = []
@app.get("/")
def read_root():
return {"Hello": "World!"}
@app.get("/chatbot/dialoginfo")
def read_dialog(dialogid: str):
# return dialogid
return answer_dict[dialogid]
@app.get("/chatbot/finishquery")
def read_dialog(dialogid: str):
# return dialogid
return random.choices([True, False], (0.3, 0.7))[0]
def background_process(diainfo: DialogInfo):
answer_dict[diainfo.q_id].q_id = diainfo.q_id
answer_dict[diainfo.q_id].history = diainfo.history
answer_dict[diainfo.q_id].query = diainfo.query
for _ in range(5):
answer_dict[diainfo.q_id].history = diainfo.history
if diainfo.q_id.startswith('0'):
answer_dict[diainfo.q_id].query = answer_dict[diainfo.q_id].query + 'i'
elif diainfo.q_id.startswith('9'):
answer_dict[diainfo.q_id].query = answer_dict[diainfo.q_id].query + 'o'
time.sleep(0.5)
print(answer_dict[diainfo.q_id])
@app.post("/chatbot/")
def read_query(chat: DialogInfo, background_tasks: BackgroundTasks):
answer_dict[chat.q_id] = chat
background_tasks.add_task(background_process, chat)
return {"code": 202, "msg": f"Query id {chat.q_id} accepted."}
|