File size: 1,869 Bytes
80769bf
3ab5c22
80769bf
5301720
0515673
80769bf
3ab5c22
 
 
2f30cc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80769bf
 
5301720
80769bf
 
5301720
 
80769bf
5301720
 
2f30cc1
3ab5c22
 
2f30cc1
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
from pyChatGPT import ChatGPT
import gradio as gr
import os
from loguru import logger
import random

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

# def chat(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


# demo = gr.Interface(chat,
#               inputs = [gr.Textbox(label = 'Input: ')], 
#                outputs = gr.outputs.Textbox(type="text",label="from ChatGPT:"), 
#                title = "Talk with ChatGPT",
#                description= "")

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, history):
    history = history or []
    response = get_response_from_chatbot(message)
    # message = message.lower()
    # if message.startswith("how many"):
    #     response = random.randint(1, 10)
    # elif message.startswith("how"):
    #     response = random.choice(["Great", "Good", "Okay", "Bad"])
    # elif message.startswith("where"):
    #     response = random.choice(["Here", "There", "Somewhere"])
    # else:
    #     response = "I don't know"
    history.append((message, response))
    return history, history

chatbot = gr.Chatbot().style(color_map=("green", "pink"))
demo = gr.Interface(
    chat,
    ["text", "state"],
    [chatbot, "state"],
    allow_flagging="never",
)

demo.launch(debug = True)