Liusuthu commited on
Commit
d246ded
Β·
verified Β·
1 Parent(s): 38c9a1f

Create test_chat.py

Browse files
Files changed (1) hide show
  1. test_chat.py +70 -0
test_chat.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ # import random
4
+ import time
5
+
6
+ import gradio as gr
7
+
8
+ # from langchain.schema import AIMessage, HumanMessage
9
+ # from langchain_community.chat_models import ChatOpenAI
10
+
11
+
12
+ # import openai
13
+
14
+
15
+ os.environ["no_proxy"] = "localhost,127.0.0.1,::1"
16
+
17
+
18
+ def print_like_dislike(x: gr.LikeData):
19
+ print(x.index, x.value, x.liked)
20
+
21
+
22
+ def add_text(history, text):
23
+ history = history + [(text, None)]
24
+ return history, gr.Textbox(value="", interactive=False)
25
+
26
+
27
+ def add_file(history, file):
28
+ history = history + [((file.name,), None)]
29
+ return history
30
+
31
+
32
+ def bot(history):
33
+ response = "**That's cool!**"
34
+ history[-1][1] = ""
35
+ for character in response:
36
+ history[-1][1] += character
37
+ time.sleep(0.05)
38
+ yield history
39
+
40
+
41
+ with gr.Blocks() as demo:
42
+ chatbot = gr.Chatbot(
43
+ [],
44
+ elem_id="chatbot",
45
+ bubble_full_width=False,
46
+ # avatar_images=(None, (os.path.join(os.path.dirname(__file__), "avatar.png"))),
47
+ )
48
+
49
+ with gr.Row():
50
+ txt = gr.Textbox(
51
+ scale=4,
52
+ show_label=False,
53
+ placeholder="Enter text and press enter, or upload an image",
54
+ container=False,
55
+ )
56
+ btn = gr.UploadButton("πŸ“", file_types=["image", "video", "audio"])
57
+
58
+ txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
59
+ bot, chatbot, chatbot, api_name="bot_response"
60
+ )
61
+ txt_msg.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
62
+ file_msg = btn.upload(add_file, [chatbot, btn], [chatbot], queue=False).then(
63
+ bot, chatbot, chatbot
64
+ )
65
+
66
+ chatbot.like(print_like_dislike, None, None)
67
+
68
+
69
+ demo.queue()
70
+ demo.launch()