File size: 1,865 Bytes
80769bf
3ab5c22
e20880a
5301720
0515673
80769bf
3ab5c22
 
 
2f30cc1
80769bf
 
5301720
80769bf
 
5301720
 
80769bf
5301720
 
e20880a
 
 
 
113ddc7
c18076e
e20880a
 
6c8c1b9
c18076e
e20880a
626d1e7
c18076e
94e1353
c18076e
ea13408
 
c18076e
 
 
 
 
 
 
 
 
 
 
 
3ab5c22
5301720
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
from pyChatGPT import ChatGPT
import gradio as gr
import os, json
from loguru import logger
import random

session_token = os.environ.get('SessionToken')      
logger.info(f"session_token_: {session_token}")

def get_response_from_chatbot(text):
    try:
      api = ChatGPT(session_token) 
      resp = api.send_message(text)    
      api.refresh_auth() 
      api.reset_conversation() 
      response = resp['message']
      logger.info(f"response_: {response}")
    except:
      response = "Sorry, I'm am tired."
    return response

def chat(message, chat_history):  
    out_chat = []
    if chat_history != '':
        out_chat = json.loads(chat_history)
    # print(f'chat_1_{chat_history}')
    response = get_response_from_chatbot(message)
    out_chat.append((message, response))
    chat_history = json.dumps(out_chat)
    # print(f'chat_2_{chat_history}')
    return out_chat, chat_history
    
with gr.Blocks(title='chat with chatgpt') as demo:
    with gr.Group(elem_id="page_1", visible=True) as page_1:
        with gr.Row(elem_id="prompt_row"):
            chatbot = gr.Chatbot(elem_id="chat_bot").style(color_map=("green", "blue"))
            chatbot1 = gr.Chatbot(elem_id="chat_bot1").style(color_map=("green", "blue"))
        with gr.Row():
            prompt_input0 = gr.Textbox(lines=1, label="prompt",show_label=False)
            chat_history = gr.Textbox(lines=4, label="prompt", visible=False)
            submit_btn = gr.Button(value = "submit",elem_id="submit-btn").style(
                    margin=True,
                    rounded=(True, True, True, True),
                    width=100
                )
            submit_btn.click(fn=chat, 
                             inputs=[prompt_input0, chat_history], 
                             outputs=[chatbot, chat_history],
                            )

demo.launch(debug = True)