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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
def greet(name):
|
4 |
-
return "Hello " + name + "!!"
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
1 |
+
import sys
|
2 |
+
sys.path.append("../../Gradio_Config")
|
3 |
+
|
4 |
+
from gradio_base import UIHelper, WebUI
|
5 |
+
import os
|
6 |
+
from gradio_base import WebUI, UIHelper, PORT, HOST, Client
|
7 |
+
from gradio_config import GradioConfig as gc
|
8 |
+
from typing import List, Tuple, Any
|
9 |
import gradio as gr
|
10 |
+
import time
|
11 |
+
|
12 |
+
class DebateUI(WebUI):
|
13 |
+
FORMAT = "{}\n<debate topic>\n{}\nAffirmative viewpoint:{}\nNegative viewpoint:{}\n<debate topic>{}"
|
14 |
+
AUDIENCE = "Audience"
|
15 |
+
cache = {}
|
16 |
+
all_agents_name = []
|
17 |
+
receive_server = None
|
18 |
+
|
19 |
+
@classmethod
|
20 |
+
def extract(cls, content):
|
21 |
+
topic = content.split("<debate topic>")[1].split("Affirmative viewpoint:")[0]
|
22 |
+
positive = content.split("<debate topic>")[1].split("Affirmative viewpoint:")[1].split("negative viewpoint:")[0]
|
23 |
+
negative = content.split("<debate topic>")[1].split("Affirmative viewpoint:")[1].split("negative viewpoint:")[1]
|
24 |
+
return topic.strip(), positive.strip(), negative.strip()
|
25 |
+
|
26 |
+
@classmethod
|
27 |
+
def merge(cls, theme, positive, negative, origin_content) -> str:
|
28 |
+
return cls.FORMAT.format(
|
29 |
+
origin_content.split("<debate topic>")[0],
|
30 |
+
theme, positive, negative,
|
31 |
+
origin_content.split("<debate topic>")[-1]
|
32 |
+
)
|
33 |
+
|
34 |
+
@classmethod
|
35 |
+
def convert2list4agentname(cls, sop):
|
36 |
+
only_name = []
|
37 |
+
agent_name = []
|
38 |
+
roles_to_names = sop.roles_to_names
|
39 |
+
for state_name,roles_names in roles_to_names.items():
|
40 |
+
for role,name in roles_names.items():
|
41 |
+
agent_name.append(f"{name}({role})")
|
42 |
+
only_name.append(name)
|
43 |
+
agent_name.append(cls.AUDIENCE)
|
44 |
+
agent_name = list(set(agent_name))
|
45 |
+
agent_name.sort()
|
46 |
+
return agent_name, only_name
|
47 |
+
|
48 |
+
def render_and_register_ui(self):
|
49 |
+
gc.add_agent(self.cache["only_name"])
|
50 |
+
|
51 |
+
def __init__(
|
52 |
+
self,
|
53 |
+
client_cmd: list,
|
54 |
+
socket_host: str = HOST,
|
55 |
+
socket_port: int = PORT,
|
56 |
+
bufsize: int = 1024,
|
57 |
+
ui_name: str = "DebateUI"
|
58 |
+
):
|
59 |
+
super(DebateUI, self).__init__(client_cmd, socket_host, socket_port, bufsize, ui_name)
|
60 |
+
self.first_recieve_from_client()
|
61 |
+
self.data_history = list()
|
62 |
+
self.caller = 0
|
63 |
+
|
64 |
+
def handle_message(self, history:list,
|
65 |
+
state, agent_name, token, node_name):
|
66 |
+
if state % 10 == 0:
|
67 |
+
self.data_history.append({agent_name: token})
|
68 |
+
elif state % 10 == 1:
|
69 |
+
# Same state. Need to add new bubble in same bubble.
|
70 |
+
self.data_history[-1][agent_name] += token
|
71 |
+
elif state % 10 == 2:
|
72 |
+
# New state. Need to add new bubble.
|
73 |
+
history.append([None, ""])
|
74 |
+
self.data_history.clear()
|
75 |
+
self.data_history.append({agent_name: token})
|
76 |
+
else:
|
77 |
+
assert False, "Invalid state."
|
78 |
+
render_data = self.render_bubble(history, self.data_history, node_name, render_node_name= True or state % 10 == 2)
|
79 |
+
return render_data
|
80 |
+
|
81 |
+
def start_button_when_click(self, theme, positive, negative, choose, mode):
|
82 |
+
"""
|
83 |
+
inputs=[self.text_theme, self.text_positive, self.text_negative, self.radio_choose],
|
84 |
+
outputs=[self.chatbot, self.btn_send]
|
85 |
+
"""
|
86 |
+
cosplay = None if choose == self.AUDIENCE else choose.split("(")[0]
|
87 |
+
message = dict(theme=theme, positive=positive, negative=negative, cosplay=cosplay, mode=mode)
|
88 |
+
self.send_start_cmd(message=message)
|
89 |
+
return gr.Chatbot.update(
|
90 |
+
visible=True
|
91 |
+
), gr.Button.update(visible=False)
|
92 |
+
|
93 |
+
def start_button_after_click(self, history):
|
94 |
+
"""
|
95 |
+
inputs=[self.chatbot],
|
96 |
+
outputs=[self.chatbot, self.text_user, self.btn_send, self.btn_reset, self.btn_next]
|
97 |
+
"""
|
98 |
+
if self.caller == 0:
|
99 |
+
# not single mode
|
100 |
+
self.data_history = list()
|
101 |
+
self.caller = 0
|
102 |
+
receive_server = self.receive_server
|
103 |
+
while True:
|
104 |
+
data_list: List = receive_server.send(None)
|
105 |
+
for item in data_list:
|
106 |
+
data = eval(item)
|
107 |
+
assert isinstance(data, list)
|
108 |
+
state, agent_name, token, node_name = data
|
109 |
+
assert isinstance(state, int)
|
110 |
+
if state == 30:
|
111 |
+
# user input
|
112 |
+
yield history,\
|
113 |
+
gr.Textbox.update(visible=True, interactive=True), \
|
114 |
+
gr.Button.update(visible=True, interactive=True),\
|
115 |
+
gr.Button.update(visible=True, interactive=True),\
|
116 |
+
gr.Button.update(visible=False)
|
117 |
+
return
|
118 |
+
elif state == 99:
|
119 |
+
# finish
|
120 |
+
yield history, gr.Textbox.update(visible=True, interactive=False, value="finish!"), \
|
121 |
+
gr.Button.update(visible=True, interactive=False, value="finish!"), gr.Button.update(visible=True, interactive=True),\
|
122 |
+
gr.Button.update(visible=False)
|
123 |
+
elif state == 98:
|
124 |
+
yield history, \
|
125 |
+
gr.Textbox.update(visible=False, interactive=False), \
|
126 |
+
gr.Button.update(visible=False, interactive=False),\
|
127 |
+
gr.Button.update(visible=False, interactive=False),\
|
128 |
+
gr.Button.update(visible=True, value=f"Next Agent: 🤖{agent_name} | Next Node: ⭕{node_name}")
|
129 |
+
return
|
130 |
+
else:
|
131 |
+
history = self.handle_message(history, state, agent_name, token, node_name)
|
132 |
+
yield history, \
|
133 |
+
gr.Textbox.update(visible=False, interactive=False), \
|
134 |
+
gr.Button.update(visible=False, interactive=False),\
|
135 |
+
gr.Button.update(visible=False, interactive=False),\
|
136 |
+
gr.Button.update(visible=False)
|
137 |
+
|
138 |
+
def send_button_when_click(self, text_user, history:list):
|
139 |
+
"""
|
140 |
+
inputs=[self.text_user, self.chatbot],
|
141 |
+
outputs=[self.text_user, self.btn_send, self.chatbot]
|
142 |
+
"""
|
143 |
+
history.append(
|
144 |
+
[UIHelper.wrap_css(text_user, "User"), None]
|
145 |
+
)
|
146 |
+
# print(f"server: send {text_user} to client")
|
147 |
+
self.send_message("<USER>"+text_user+self.SIGN["SPLIT"])
|
148 |
+
return gr.Textbox.update(value="", visible=False),\
|
149 |
+
gr.Button.update(visible=False), \
|
150 |
+
history,\
|
151 |
+
gr.Button.update(visible=False)
|
152 |
+
|
153 |
+
def reset_button_when_click(self, history, text_positive, text_negative, text_theme, text_user, btn_send, btn_start, btn_reset):
|
154 |
+
"""
|
155 |
+
self.chatbot,
|
156 |
+
self.text_positive,
|
157 |
+
self.text_negative,
|
158 |
+
self.text_theme,
|
159 |
+
self.text_user,
|
160 |
+
self.btn_send,
|
161 |
+
self.btn_start,
|
162 |
+
self.btn_reset
|
163 |
+
self.btn_next
|
164 |
+
"""
|
165 |
+
self.caller = 0
|
166 |
+
return None, \
|
167 |
+
"", \
|
168 |
+
"", \
|
169 |
+
"", \
|
170 |
+
"", \
|
171 |
+
gr.Button.update(value="Restarting...", interactive=False, visible=True),\
|
172 |
+
gr.Button.update(value="Restarting...", interactive=False, visible=True),\
|
173 |
+
gr.Button.update(value="Restarting...", interactive=False, visible=True),\
|
174 |
+
gr.Button.update(value="Restarting...", interactive=False, visible=False)
|
175 |
+
|
176 |
+
def reset_button_after_click(self, history, text_positive, text_negative, text_theme, text_user, btn_send, btn_start, btn_reset):
|
177 |
+
self.reset()
|
178 |
+
self.first_recieve_from_client(reset_mode=True)
|
179 |
+
return gr.Chatbot.update(value=None, visible=False),\
|
180 |
+
gr.Textbox.update(value=f"{self.cache['positive']}", interactive=True, visible=True),\
|
181 |
+
gr.Textbox.update(value=f"{self.cache['negative']}", interactive=True, visible=True),\
|
182 |
+
gr.Textbox.update(value=f"{self.cache['theme']}", interactive=True, visible=True),\
|
183 |
+
gr.Textbox.update(value=f"", interactive=True, visible=False),\
|
184 |
+
gr.Button.update(interactive=True, visible=False, value="Send"),\
|
185 |
+
gr.Button.update(interactive=True, visible=True, value="Start"),\
|
186 |
+
gr.Button.update(interactive=False, visible=False, value="Restart"),\
|
187 |
+
gr.Button.update(interactive=True, visible=False, value="Next Agent")
|
188 |
+
|
189 |
+
def btn_next_when_click(self):
|
190 |
+
yield gr.Button.update(visible=False)
|
191 |
+
self.send_message("nothing")
|
192 |
+
self.caller = 1 # will note clear the self.data_history
|
193 |
+
time.sleep(0.5)
|
194 |
+
return
|
195 |
+
|
196 |
+
def construct_ui(
|
197 |
+
self,
|
198 |
+
theme:str=None,
|
199 |
+
positive:str=None,
|
200 |
+
negative:str=None,
|
201 |
+
agents_name:List=None,
|
202 |
+
default_cos_play_id:int=None
|
203 |
+
):
|
204 |
+
theme = self.cache["theme"] if theme is None else theme
|
205 |
+
positive = self.cache["positive"] if positive is None else positive
|
206 |
+
negative = self.cache["negative"] if negative is None else negative
|
207 |
+
agents_name = self.cache["agents_name"] if agents_name is None else agents_name
|
208 |
+
default_cos_play_id = self.cache["default_cos_play_id"] if default_cos_play_id is None else default_cos_play_id
|
209 |
+
|
210 |
+
with gr.Blocks(css=gc.CSS) as demo:
|
211 |
+
with gr.Row():
|
212 |
+
with gr.Column():
|
213 |
+
self.radio_mode = gr.Radio(
|
214 |
+
[Client.AUTO_MODE, Client.SINGLE_MODE],
|
215 |
+
value=Client.AUTO_MODE,
|
216 |
+
interactive=True,
|
217 |
+
label = Client.MODE_LABEL,
|
218 |
+
info = Client.MODE_INFO
|
219 |
+
)
|
220 |
+
self.text_theme = gr.Textbox(
|
221 |
+
label="Debate Topic:",
|
222 |
+
value=theme,
|
223 |
+
placeholder="Please input the Debate Topic"
|
224 |
+
)
|
225 |
+
self.text_positive = gr.Textbox(
|
226 |
+
label="Affirmative viewpoint:",
|
227 |
+
value=positive,
|
228 |
+
placeholder="Please input the Affirmative viewpoint"
|
229 |
+
)
|
230 |
+
self.text_negative = gr.Textbox(
|
231 |
+
label="Negative viewpoint:",
|
232 |
+
value=negative,
|
233 |
+
placeholder="Please input the Negative viewpoint"
|
234 |
+
)
|
235 |
+
self.radio_choose = gr.Radio(
|
236 |
+
agents_name,
|
237 |
+
value=agents_name[default_cos_play_id],
|
238 |
+
label="User'agent",
|
239 |
+
interactive=True
|
240 |
+
)
|
241 |
+
self.btn_start = gr.Button(
|
242 |
+
value="run"
|
243 |
+
)
|
244 |
+
VISIBLE = False
|
245 |
+
with gr.Column():
|
246 |
+
self.chatbot = gr.Chatbot(
|
247 |
+
height= 650,
|
248 |
+
elem_id="chatbot1",
|
249 |
+
label="Dialog",
|
250 |
+
visible=VISIBLE
|
251 |
+
)
|
252 |
+
self.btn_next = gr.Button(
|
253 |
+
value="Next Agent Start",
|
254 |
+
visible=False
|
255 |
+
)
|
256 |
+
self.text_user = gr.Textbox(
|
257 |
+
label="Input",
|
258 |
+
placeholder="Input here",
|
259 |
+
visible=VISIBLE
|
260 |
+
)
|
261 |
+
self.btn_send = gr.Button(
|
262 |
+
value="Send",
|
263 |
+
visible=VISIBLE
|
264 |
+
)
|
265 |
+
self.btn_reset = gr.Button(
|
266 |
+
value="Restart",
|
267 |
+
visible=VISIBLE
|
268 |
+
)
|
269 |
+
|
270 |
+
self.btn_start.click(
|
271 |
+
fn=self.start_button_when_click,
|
272 |
+
inputs=[self.text_theme, self.text_positive, self.text_negative, self.radio_choose, self.radio_mode],
|
273 |
+
outputs=[self.chatbot, self.btn_start]
|
274 |
+
).then(
|
275 |
+
fn=self.start_button_after_click,
|
276 |
+
inputs=[self.chatbot],
|
277 |
+
outputs=[self.chatbot, self.text_user, self.btn_send, self.btn_reset, self.btn_next]
|
278 |
+
)
|
279 |
+
|
280 |
+
self.btn_send.click(
|
281 |
+
fn=self.send_button_when_click,
|
282 |
+
inputs=[self.text_user, self.chatbot],
|
283 |
+
outputs=[self.text_user, self.btn_send, self.chatbot, self.btn_reset]
|
284 |
+
).then(
|
285 |
+
fn=self.start_button_after_click,
|
286 |
+
inputs=[self.chatbot],
|
287 |
+
outputs=[self.chatbot, self.text_user, self.btn_send, self.btn_reset, self.btn_next]
|
288 |
+
)
|
289 |
+
|
290 |
+
self.btn_reset.click(
|
291 |
+
fn=self.reset_button_when_click,
|
292 |
+
inputs=[
|
293 |
+
self.chatbot,
|
294 |
+
self.text_positive,
|
295 |
+
self.text_negative,
|
296 |
+
self.text_theme,
|
297 |
+
self.text_user,
|
298 |
+
self.btn_send,
|
299 |
+
self.btn_start,
|
300 |
+
self.btn_reset
|
301 |
+
],
|
302 |
+
outputs=[
|
303 |
+
self.chatbot,
|
304 |
+
self.text_positive,
|
305 |
+
self.text_negative,
|
306 |
+
self.text_theme,
|
307 |
+
self.text_user,
|
308 |
+
self.btn_send,
|
309 |
+
self.btn_start,
|
310 |
+
self.btn_reset,
|
311 |
+
self.btn_next
|
312 |
+
]
|
313 |
+
).then(
|
314 |
+
fn=self.reset_button_after_click,
|
315 |
+
inputs=[
|
316 |
+
self.chatbot,
|
317 |
+
self.text_positive,
|
318 |
+
self.text_negative,
|
319 |
+
self.text_theme,
|
320 |
+
self.text_user,
|
321 |
+
self.btn_send,
|
322 |
+
self.btn_start,
|
323 |
+
self.btn_reset
|
324 |
+
],
|
325 |
+
outputs=[
|
326 |
+
self.chatbot,
|
327 |
+
self.text_positive,
|
328 |
+
self.text_negative,
|
329 |
+
self.text_theme,
|
330 |
+
self.text_user,
|
331 |
+
self.btn_send,
|
332 |
+
self.btn_start,
|
333 |
+
self.btn_reset,
|
334 |
+
self.btn_next
|
335 |
+
]
|
336 |
+
)
|
337 |
+
|
338 |
+
self.btn_next.click(
|
339 |
+
fn=self.btn_next_when_click,
|
340 |
+
inputs=[],
|
341 |
+
outputs=[self.btn_next]
|
342 |
+
).then(
|
343 |
+
fn=self.start_button_after_click,
|
344 |
+
inputs=[self.chatbot],
|
345 |
+
outputs=[self.chatbot, self.text_user, self.btn_send, self.btn_reset, self.btn_next]
|
346 |
+
)
|
347 |
+
|
348 |
+
self.demo = demo
|
349 |
|
|
|
|
|
350 |
|
351 |
+
if __name__ == '__main__':
|
352 |
+
ui = DebateUI(client_cmd=["python","gradio_backend.py"])
|
353 |
+
ui.construct_ui()
|
354 |
+
ui.run(share=True)
|
run_gradio.py
DELETED
@@ -1,354 +0,0 @@
|
|
1 |
-
import sys
|
2 |
-
sys.path.append("../../Gradio_Config")
|
3 |
-
|
4 |
-
from gradio_base import UIHelper, WebUI
|
5 |
-
import os
|
6 |
-
from gradio_base import WebUI, UIHelper, PORT, HOST, Client
|
7 |
-
from gradio_config import GradioConfig as gc
|
8 |
-
from typing import List, Tuple, Any
|
9 |
-
import gradio as gr
|
10 |
-
import time
|
11 |
-
|
12 |
-
class DebateUI(WebUI):
|
13 |
-
FORMAT = "{}\n<debate topic>\n{}\nAffirmative viewpoint:{}\nNegative viewpoint:{}\n<debate topic>{}"
|
14 |
-
AUDIENCE = "Audience"
|
15 |
-
cache = {}
|
16 |
-
all_agents_name = []
|
17 |
-
receive_server = None
|
18 |
-
|
19 |
-
@classmethod
|
20 |
-
def extract(cls, content):
|
21 |
-
topic = content.split("<debate topic>")[1].split("Affirmative viewpoint:")[0]
|
22 |
-
positive = content.split("<debate topic>")[1].split("Affirmative viewpoint:")[1].split("negative viewpoint:")[0]
|
23 |
-
negative = content.split("<debate topic>")[1].split("Affirmative viewpoint:")[1].split("negative viewpoint:")[1]
|
24 |
-
return topic.strip(), positive.strip(), negative.strip()
|
25 |
-
|
26 |
-
@classmethod
|
27 |
-
def merge(cls, theme, positive, negative, origin_content) -> str:
|
28 |
-
return cls.FORMAT.format(
|
29 |
-
origin_content.split("<debate topic>")[0],
|
30 |
-
theme, positive, negative,
|
31 |
-
origin_content.split("<debate topic>")[-1]
|
32 |
-
)
|
33 |
-
|
34 |
-
@classmethod
|
35 |
-
def convert2list4agentname(cls, sop):
|
36 |
-
only_name = []
|
37 |
-
agent_name = []
|
38 |
-
roles_to_names = sop.roles_to_names
|
39 |
-
for state_name,roles_names in roles_to_names.items():
|
40 |
-
for role,name in roles_names.items():
|
41 |
-
agent_name.append(f"{name}({role})")
|
42 |
-
only_name.append(name)
|
43 |
-
agent_name.append(cls.AUDIENCE)
|
44 |
-
agent_name = list(set(agent_name))
|
45 |
-
agent_name.sort()
|
46 |
-
return agent_name, only_name
|
47 |
-
|
48 |
-
def render_and_register_ui(self):
|
49 |
-
gc.add_agent(self.cache["only_name"])
|
50 |
-
|
51 |
-
def __init__(
|
52 |
-
self,
|
53 |
-
client_cmd: list,
|
54 |
-
socket_host: str = HOST,
|
55 |
-
socket_port: int = PORT,
|
56 |
-
bufsize: int = 1024,
|
57 |
-
ui_name: str = "DebateUI"
|
58 |
-
):
|
59 |
-
super(DebateUI, self).__init__(client_cmd, socket_host, socket_port, bufsize, ui_name)
|
60 |
-
self.first_recieve_from_client()
|
61 |
-
self.data_history = list()
|
62 |
-
self.caller = 0
|
63 |
-
|
64 |
-
def handle_message(self, history:list,
|
65 |
-
state, agent_name, token, node_name):
|
66 |
-
if state % 10 == 0:
|
67 |
-
self.data_history.append({agent_name: token})
|
68 |
-
elif state % 10 == 1:
|
69 |
-
# Same state. Need to add new bubble in same bubble.
|
70 |
-
self.data_history[-1][agent_name] += token
|
71 |
-
elif state % 10 == 2:
|
72 |
-
# New state. Need to add new bubble.
|
73 |
-
history.append([None, ""])
|
74 |
-
self.data_history.clear()
|
75 |
-
self.data_history.append({agent_name: token})
|
76 |
-
else:
|
77 |
-
assert False, "Invalid state."
|
78 |
-
render_data = self.render_bubble(history, self.data_history, node_name, render_node_name= True or state % 10 == 2)
|
79 |
-
return render_data
|
80 |
-
|
81 |
-
def start_button_when_click(self, theme, positive, negative, choose, mode):
|
82 |
-
"""
|
83 |
-
inputs=[self.text_theme, self.text_positive, self.text_negative, self.radio_choose],
|
84 |
-
outputs=[self.chatbot, self.btn_send]
|
85 |
-
"""
|
86 |
-
cosplay = None if choose == self.AUDIENCE else choose.split("(")[0]
|
87 |
-
message = dict(theme=theme, positive=positive, negative=negative, cosplay=cosplay, mode=mode)
|
88 |
-
self.send_start_cmd(message=message)
|
89 |
-
return gr.Chatbot.update(
|
90 |
-
visible=True
|
91 |
-
), gr.Button.update(visible=False)
|
92 |
-
|
93 |
-
def start_button_after_click(self, history):
|
94 |
-
"""
|
95 |
-
inputs=[self.chatbot],
|
96 |
-
outputs=[self.chatbot, self.text_user, self.btn_send, self.btn_reset, self.btn_next]
|
97 |
-
"""
|
98 |
-
if self.caller == 0:
|
99 |
-
# not single mode
|
100 |
-
self.data_history = list()
|
101 |
-
self.caller = 0
|
102 |
-
receive_server = self.receive_server
|
103 |
-
while True:
|
104 |
-
data_list: List = receive_server.send(None)
|
105 |
-
for item in data_list:
|
106 |
-
data = eval(item)
|
107 |
-
assert isinstance(data, list)
|
108 |
-
state, agent_name, token, node_name = data
|
109 |
-
assert isinstance(state, int)
|
110 |
-
if state == 30:
|
111 |
-
# user input
|
112 |
-
yield history,\
|
113 |
-
gr.Textbox.update(visible=True, interactive=True), \
|
114 |
-
gr.Button.update(visible=True, interactive=True),\
|
115 |
-
gr.Button.update(visible=True, interactive=True),\
|
116 |
-
gr.Button.update(visible=False)
|
117 |
-
return
|
118 |
-
elif state == 99:
|
119 |
-
# finish
|
120 |
-
yield history, gr.Textbox.update(visible=True, interactive=False, value="finish!"), \
|
121 |
-
gr.Button.update(visible=True, interactive=False, value="finish!"), gr.Button.update(visible=True, interactive=True),\
|
122 |
-
gr.Button.update(visible=False)
|
123 |
-
elif state == 98:
|
124 |
-
yield history, \
|
125 |
-
gr.Textbox.update(visible=False, interactive=False), \
|
126 |
-
gr.Button.update(visible=False, interactive=False),\
|
127 |
-
gr.Button.update(visible=False, interactive=False),\
|
128 |
-
gr.Button.update(visible=True, value=f"Next Agent: 🤖{agent_name} | Next Node: ⭕{node_name}")
|
129 |
-
return
|
130 |
-
else:
|
131 |
-
history = self.handle_message(history, state, agent_name, token, node_name)
|
132 |
-
yield history, \
|
133 |
-
gr.Textbox.update(visible=False, interactive=False), \
|
134 |
-
gr.Button.update(visible=False, interactive=False),\
|
135 |
-
gr.Button.update(visible=False, interactive=False),\
|
136 |
-
gr.Button.update(visible=False)
|
137 |
-
|
138 |
-
def send_button_when_click(self, text_user, history:list):
|
139 |
-
"""
|
140 |
-
inputs=[self.text_user, self.chatbot],
|
141 |
-
outputs=[self.text_user, self.btn_send, self.chatbot]
|
142 |
-
"""
|
143 |
-
history.append(
|
144 |
-
[UIHelper.wrap_css(text_user, "User"), None]
|
145 |
-
)
|
146 |
-
# print(f"server: send {text_user} to client")
|
147 |
-
self.send_message("<USER>"+text_user+self.SIGN["SPLIT"])
|
148 |
-
return gr.Textbox.update(value="", visible=False),\
|
149 |
-
gr.Button.update(visible=False), \
|
150 |
-
history,\
|
151 |
-
gr.Button.update(visible=False)
|
152 |
-
|
153 |
-
def reset_button_when_click(self, history, text_positive, text_negative, text_theme, text_user, btn_send, btn_start, btn_reset):
|
154 |
-
"""
|
155 |
-
self.chatbot,
|
156 |
-
self.text_positive,
|
157 |
-
self.text_negative,
|
158 |
-
self.text_theme,
|
159 |
-
self.text_user,
|
160 |
-
self.btn_send,
|
161 |
-
self.btn_start,
|
162 |
-
self.btn_reset
|
163 |
-
self.btn_next
|
164 |
-
"""
|
165 |
-
self.caller = 0
|
166 |
-
return None, \
|
167 |
-
"", \
|
168 |
-
"", \
|
169 |
-
"", \
|
170 |
-
"", \
|
171 |
-
gr.Button.update(value="Restarting...", interactive=False, visible=True),\
|
172 |
-
gr.Button.update(value="Restarting...", interactive=False, visible=True),\
|
173 |
-
gr.Button.update(value="Restarting...", interactive=False, visible=True),\
|
174 |
-
gr.Button.update(value="Restarting...", interactive=False, visible=False)
|
175 |
-
|
176 |
-
def reset_button_after_click(self, history, text_positive, text_negative, text_theme, text_user, btn_send, btn_start, btn_reset):
|
177 |
-
self.reset()
|
178 |
-
self.first_recieve_from_client(reset_mode=True)
|
179 |
-
return gr.Chatbot.update(value=None, visible=False),\
|
180 |
-
gr.Textbox.update(value=f"{self.cache['positive']}", interactive=True, visible=True),\
|
181 |
-
gr.Textbox.update(value=f"{self.cache['negative']}", interactive=True, visible=True),\
|
182 |
-
gr.Textbox.update(value=f"{self.cache['theme']}", interactive=True, visible=True),\
|
183 |
-
gr.Textbox.update(value=f"", interactive=True, visible=False),\
|
184 |
-
gr.Button.update(interactive=True, visible=False, value="Send"),\
|
185 |
-
gr.Button.update(interactive=True, visible=True, value="Start"),\
|
186 |
-
gr.Button.update(interactive=False, visible=False, value="Restart"),\
|
187 |
-
gr.Button.update(interactive=True, visible=False, value="Next Agent")
|
188 |
-
|
189 |
-
def btn_next_when_click(self):
|
190 |
-
yield gr.Button.update(visible=False)
|
191 |
-
self.send_message("nothing")
|
192 |
-
self.caller = 1 # will note clear the self.data_history
|
193 |
-
time.sleep(0.5)
|
194 |
-
return
|
195 |
-
|
196 |
-
def construct_ui(
|
197 |
-
self,
|
198 |
-
theme:str=None,
|
199 |
-
positive:str=None,
|
200 |
-
negative:str=None,
|
201 |
-
agents_name:List=None,
|
202 |
-
default_cos_play_id:int=None
|
203 |
-
):
|
204 |
-
theme = self.cache["theme"] if theme is None else theme
|
205 |
-
positive = self.cache["positive"] if positive is None else positive
|
206 |
-
negative = self.cache["negative"] if negative is None else negative
|
207 |
-
agents_name = self.cache["agents_name"] if agents_name is None else agents_name
|
208 |
-
default_cos_play_id = self.cache["default_cos_play_id"] if default_cos_play_id is None else default_cos_play_id
|
209 |
-
|
210 |
-
with gr.Blocks(css=gc.CSS) as demo:
|
211 |
-
with gr.Row():
|
212 |
-
with gr.Column():
|
213 |
-
self.radio_mode = gr.Radio(
|
214 |
-
[Client.AUTO_MODE, Client.SINGLE_MODE],
|
215 |
-
value=Client.AUTO_MODE,
|
216 |
-
interactive=True,
|
217 |
-
label = Client.MODE_LABEL,
|
218 |
-
info = Client.MODE_INFO
|
219 |
-
)
|
220 |
-
self.text_theme = gr.Textbox(
|
221 |
-
label="Debate Topic:",
|
222 |
-
value=theme,
|
223 |
-
placeholder="Please input the Debate Topic"
|
224 |
-
)
|
225 |
-
self.text_positive = gr.Textbox(
|
226 |
-
label="Affirmative viewpoint:",
|
227 |
-
value=positive,
|
228 |
-
placeholder="Please input the Affirmative viewpoint"
|
229 |
-
)
|
230 |
-
self.text_negative = gr.Textbox(
|
231 |
-
label="Negative viewpoint:",
|
232 |
-
value=negative,
|
233 |
-
placeholder="Please input the Negative viewpoint"
|
234 |
-
)
|
235 |
-
self.radio_choose = gr.Radio(
|
236 |
-
agents_name,
|
237 |
-
value=agents_name[default_cos_play_id],
|
238 |
-
label="User'agent",
|
239 |
-
interactive=True
|
240 |
-
)
|
241 |
-
self.btn_start = gr.Button(
|
242 |
-
value="run"
|
243 |
-
)
|
244 |
-
VISIBLE = False
|
245 |
-
with gr.Column():
|
246 |
-
self.chatbot = gr.Chatbot(
|
247 |
-
height= 650,
|
248 |
-
elem_id="chatbot1",
|
249 |
-
label="Dialog",
|
250 |
-
visible=VISIBLE
|
251 |
-
)
|
252 |
-
self.btn_next = gr.Button(
|
253 |
-
value="Next Agent Start",
|
254 |
-
visible=False
|
255 |
-
)
|
256 |
-
self.text_user = gr.Textbox(
|
257 |
-
label="Input",
|
258 |
-
placeholder="Input here",
|
259 |
-
visible=VISIBLE
|
260 |
-
)
|
261 |
-
self.btn_send = gr.Button(
|
262 |
-
value="Send",
|
263 |
-
visible=VISIBLE
|
264 |
-
)
|
265 |
-
self.btn_reset = gr.Button(
|
266 |
-
value="Restart",
|
267 |
-
visible=VISIBLE
|
268 |
-
)
|
269 |
-
|
270 |
-
self.btn_start.click(
|
271 |
-
fn=self.start_button_when_click,
|
272 |
-
inputs=[self.text_theme, self.text_positive, self.text_negative, self.radio_choose, self.radio_mode],
|
273 |
-
outputs=[self.chatbot, self.btn_start]
|
274 |
-
).then(
|
275 |
-
fn=self.start_button_after_click,
|
276 |
-
inputs=[self.chatbot],
|
277 |
-
outputs=[self.chatbot, self.text_user, self.btn_send, self.btn_reset, self.btn_next]
|
278 |
-
)
|
279 |
-
|
280 |
-
self.btn_send.click(
|
281 |
-
fn=self.send_button_when_click,
|
282 |
-
inputs=[self.text_user, self.chatbot],
|
283 |
-
outputs=[self.text_user, self.btn_send, self.chatbot, self.btn_reset]
|
284 |
-
).then(
|
285 |
-
fn=self.start_button_after_click,
|
286 |
-
inputs=[self.chatbot],
|
287 |
-
outputs=[self.chatbot, self.text_user, self.btn_send, self.btn_reset, self.btn_next]
|
288 |
-
)
|
289 |
-
|
290 |
-
self.btn_reset.click(
|
291 |
-
fn=self.reset_button_when_click,
|
292 |
-
inputs=[
|
293 |
-
self.chatbot,
|
294 |
-
self.text_positive,
|
295 |
-
self.text_negative,
|
296 |
-
self.text_theme,
|
297 |
-
self.text_user,
|
298 |
-
self.btn_send,
|
299 |
-
self.btn_start,
|
300 |
-
self.btn_reset
|
301 |
-
],
|
302 |
-
outputs=[
|
303 |
-
self.chatbot,
|
304 |
-
self.text_positive,
|
305 |
-
self.text_negative,
|
306 |
-
self.text_theme,
|
307 |
-
self.text_user,
|
308 |
-
self.btn_send,
|
309 |
-
self.btn_start,
|
310 |
-
self.btn_reset,
|
311 |
-
self.btn_next
|
312 |
-
]
|
313 |
-
).then(
|
314 |
-
fn=self.reset_button_after_click,
|
315 |
-
inputs=[
|
316 |
-
self.chatbot,
|
317 |
-
self.text_positive,
|
318 |
-
self.text_negative,
|
319 |
-
self.text_theme,
|
320 |
-
self.text_user,
|
321 |
-
self.btn_send,
|
322 |
-
self.btn_start,
|
323 |
-
self.btn_reset
|
324 |
-
],
|
325 |
-
outputs=[
|
326 |
-
self.chatbot,
|
327 |
-
self.text_positive,
|
328 |
-
self.text_negative,
|
329 |
-
self.text_theme,
|
330 |
-
self.text_user,
|
331 |
-
self.btn_send,
|
332 |
-
self.btn_start,
|
333 |
-
self.btn_reset,
|
334 |
-
self.btn_next
|
335 |
-
]
|
336 |
-
)
|
337 |
-
|
338 |
-
self.btn_next.click(
|
339 |
-
fn=self.btn_next_when_click,
|
340 |
-
inputs=[],
|
341 |
-
outputs=[self.btn_next]
|
342 |
-
).then(
|
343 |
-
fn=self.start_button_after_click,
|
344 |
-
inputs=[self.chatbot],
|
345 |
-
outputs=[self.chatbot, self.text_user, self.btn_send, self.btn_reset, self.btn_next]
|
346 |
-
)
|
347 |
-
|
348 |
-
self.demo = demo
|
349 |
-
|
350 |
-
|
351 |
-
if __name__ == '__main__':
|
352 |
-
ui = DebateUI(client_cmd=["python","gradio_backend.py"])
|
353 |
-
ui.construct_ui()
|
354 |
-
ui.run(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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