askq / app.py
ulysses115's picture
Update app.py
75461b8
import gradio as gr
from pawansapi import PyGPT
async def start_bot(chat_gpt):
try:
await chat_gpt.connect()
except ConnectionError as e:
print("socketio.exceptions:", str(e))
return "socketio.exceptions.ConnectionError"
await chat_gpt.wait_for_ready()
async def greet():
chat_gpt = bots_config[k]['chat_gpt']
try:
if not chat_gpt.auth or not chat_gpt.validate_token(chat_gpt.auth):
await chat_gpt.get_tokens()
except TimeoutError as e:
return {"status": 404, "reply": "validate_token TimeoutError"}
try:
reply = await chat_gpt.ask(query_data)
except TimeoutError as e:
return {"status": 404, "reply": "ask TimeoutError"}
return reply
def get_session_id(bot_id):
return requests.post(url, data = {"bot_id":bot_id})
if __name__ == "__main__":
bots_config = {"b0":{}, "b1":{}, "b2":{}}
for k, v in bots_config.items():
bots_config[k]['chat_gpt'] = PyGPT(get_session_id(k))
await start_bot(bots_config[k]['chat_gpt'])
iface = gr.Interface(fn=await greet, inputs=["text"], outputs="text")
iface.launch()
for k, v in bots_config.items():
print(k, 'disconnect')
await bots_config[k]['chat_gpt'].disconnect()