File size: 1,289 Bytes
df1f98c
75461b8
df1f98c
75461b8
 
 
 
 
 
 
df1f98c
75461b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()