SiqiXia commited on
Commit
0e016f4
1 Parent(s): 3758ecf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -28
app.py CHANGED
@@ -34,35 +34,53 @@ def history_gradio2gpt(gradio_history):
34
  return gpt_history
35
 
36
 
37
-
38
- chat_history = []
39
-
40
  with gr.Blocks() as demo:
41
- chatbot = gr.Chatbot()
42
- msg = gr.Textbox()
43
- clear = gr.Button("Clear")
44
-
45
-
46
- print("chat_history")
47
- def user(user_message, history):
48
- chat_history.append({"role":"user","content":user_message})
49
- return "", history + [[user_message, None]]
50
-
51
- def bot(history):
52
- bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
53
- chat_history.append({"role":"assistant","content":bot_message})
54
- history[-1][1] = ""
55
- with open('chatbot.json', 'w') as f:
56
- json.dump(chat_history, f)
57
- for character in bot_message:
58
- history[-1][1] += character
59
- time.sleep(0.05)
60
- yield history
61
-
62
- msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False, api_name="chat").then(
63
- bot, chatbot, chatbot
64
- )
65
- clear.click(lambda: None, None, chatbot, queue=False, api_name="clear")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
 
68
 
 
34
  return gpt_history
35
 
36
 
 
 
 
37
  with gr.Blocks() as demo:
38
+ chatbot_blk = gr.Chatbot(label="Chat")
39
+ input_blk = gr.Textbox(lines=1, label="Message", placeholder="Type your message here")
40
+ clear = gr.ClearButton([input_blk, chatbot_blk])
41
+
42
+ def respond(input_text, gradio_history):
43
+ if gradio_history is not None:
44
+ gpt_history = history_gradio2gpt(gradio_history)
45
+
46
+ output_text = gpt4(input_text,gpt_history)
47
+ gradio_history.append((input_text,output_text))
48
+ gpt_history = history_gradio2gpt(gradio_history)
49
+ ask_for_suggestion = "Based on the above conversation, please suggest my next message. Give just the message and nothing else."
50
+ prompt_suggestion = gpt4(ask_for_suggestion,gpt_history)
51
+
52
+ return prompt_suggestion, gradio_history
53
+
54
+ input_blk.submit(respond, [input_blk, chatbot_blk], [input_blk, chatbot_blk])
55
+
56
+ # chat_history = []
57
+
58
+ # with gr.Blocks() as demo:
59
+ # chatbot = gr.Chatbot()
60
+ # msg = gr.Textbox()
61
+ # clear = gr.Button("Clear")
62
+
63
+
64
+ # print("chat_history")
65
+ # def user(user_message, history):
66
+ # chat_history.append({"role":"user","content":user_message})
67
+ # return "", history + [[user_message, None]]
68
+
69
+ # def bot(history):
70
+ # bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
71
+ # chat_history.append({"role":"assistant","content":bot_message})
72
+ # history[-1][1] = ""
73
+ # with open('chatbot.json', 'w') as f:
74
+ # json.dump(chat_history, f)
75
+ # for character in bot_message:
76
+ # history[-1][1] += character
77
+ # time.sleep(0.05)
78
+ # yield history
79
+
80
+ # msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False, api_name="chat").then(
81
+ # bot, chatbot, chatbot
82
+ # )
83
+ # clear.click(lambda: None, None, chatbot, queue=False, api_name="clear")
84
 
85
 
86