Spaces:
Running
Running
File size: 13,131 Bytes
b25fb44 a25aa8f b25fb44 a25aa8f 7babab2 a25aa8f 521fc66 a25aa8f b25fb44 a25aa8f |
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
# Licensed under the Apache License, Version 2.0 (the “License”);
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an “AS IS” BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
"""
Gradio-based web UI to explore the Camel dataset.
"""
import argparse
import random
from typing import Dict, List, Optional, Tuple
import gradio as gr
from apps.data_explorer.loader import Datasets, load_datasets
def parse_arguments():
""" Get command line arguments. """
parser = argparse.ArgumentParser("Camel data explorer")
parser.add_argument(
'--data-path', type=str, default=None,
help='Path to the folder with ZIP datasets containing JSONs')
parser.add_argument('--default-dataset', type=str, default=None,
help='Default dataset name selected from ZIPs')
parser.add_argument('--share', type=bool, default=False,
help='Expose the web UI to Gradio')
parser.add_argument(
'--server-name', type=str, default="0.0.0.0",
help='localhost for local, 0.0.0.0 (default) for public')
parser.add_argument('--server-port', type=int, default=8080,
help='Port ot run the web page on')
parser.add_argument('--inbrowser', type=bool, default=False,
help='Open the web UI in the default browser on lunch')
parser.add_argument(
'--concurrency-count', type=int, default=10,
help='Number if concurrent threads at Gradio websocket queue. ' +
'Increase to serve more requests but keep an eye on RAM usage.')
args, unknown = parser.parse_known_args()
if len(unknown) > 0:
print("Unknown args: ", unknown)
return args
def construct_ui(blocks, datasets: Datasets,
default_dataset: Optional[str] = None):
""" Build Gradio UI and populate with chat data from JSONs.
Args:
blocks: Gradio blocks
datasets (Datasets): Several parsed
multi-JSON dataset with chats.
default_dataset (str): Default selection of the dataset.
Returns:
None
"""
if default_dataset is None:
default_dataset = "ai_society_chat"
misalignment_set_names = {"misalignment"}
ordinary_datasets = [
v for v in datasets.keys() if v not in misalignment_set_names
]
misalignment_datasets = [
v for v in datasets.keys() if v in misalignment_set_names
]
default_dataset_name = default_dataset \
if default_dataset in datasets.keys() \
else ordinary_datasets[0] if len(ordinary_datasets) > 0 \
else misalignment_datasets[0] if len(misalignment_datasets) > 0 \
else ""
dataset_names = list(datasets.keys())
with gr.Row():
with gr.Column(scale=2):
with gr.Row():
dataset_dd = gr.Dropdown(dataset_names, label="Select dataset",
value="NODEFAULT", interactive=True)
with gr.Row():
disclaimer_ta = gr.Markdown(
"## By clicking AGREE I consent to use the dataset "
"for purely educational and academic purposes and "
"not use it for any fraudulent activity; and I take "
"all the responsibility if the data is used in a "
"malicious application.", visible=False)
with gr.Row():
with gr.Column(scale=1):
accept_disclaimer_bn = gr.Button("AGREE", visible=False)
with gr.Column(scale=1):
decline_disclaimer_bn = gr.Button("DECLINE", visible=False)
with gr.Row():
with gr.Column(scale=3):
assistant_dd = gr.Dropdown([], label="ASSISTANT", value="",
interactive=True)
with gr.Column(scale=3):
user_dd = gr.Dropdown([], label="USER", value="",
interactive=True)
with gr.Column(scale=1):
gr.Markdown(
"## CAMEL: Communicative Agents for \"Mind\" Exploration"
" of Large Scale Language Model Society\n"
"Github repo: [https://github.com/lightaime/camel]"
"(https://github.com/lightaime/camel)\n"
'<div style="display:flex; justify-content:center;">'
'<img src="https://raw.githubusercontent.com/lightaime/camel/'
'master/misc/logo.png" alt="Logo" style="max-width:50%;">'
'</div>')
task_dd = gr.Dropdown([], label="Original task", value="",
interactive=True)
specified_task_ta = gr.TextArea(label="Specified task", lines=2)
chatbot = gr.Chatbot()
accepted_st = gr.State(False)
def set_default_dataset() -> Dict:
""" Trigger for app load.
Returns:
Dict: Update dict for dataset_dd.
"""
return gr.update(value=default_dataset_name)
def check_if_misalignment(dataset_name: str, accepted: bool) \
-> Tuple[Dict, Dict, Dict]:
""" Display AGREE/DECLINE if needed.
Returns:
Tuple: Visibility updates for the buttons.
"""
if dataset_name == "misalignment" and not accepted:
return gr.update(visible=True), \
gr.update(visible=True), gr.update(visible=True)
else:
return gr.update(visible=False), \
gr.update(visible=False), gr.update(visible=False)
def enable_misalignment() -> Tuple[bool, Dict, Dict, Dict]:
""" Update the state of the accepted disclaimer.
Returns:
Tuple: New state and visibility updates for the buttons.
"""
return True, gr.update(visible=False), \
gr.update(visible=False), gr.update(visible=False)
def disable_misalignment() -> Tuple[bool, Dict, Dict, Dict]:
""" Update the state of the accepted disclaimer.
Returns:
Tuple: New state and visibility updates for the buttons.
"""
return False, gr.update(visible=False), \
gr.update(visible=False), gr.update(visible=False)
def update_dataset_selection(dataset_name: str,
accepted: bool) -> Tuple[Dict, Dict]:
""" Update roles based on the selected dataset.
Args:
dataset_name (str): Name of the loaded .zip dataset.
accepted (bool): If the disclaimer thas been accepted.
Returns:
Tuple[Dict, Dict]: New Assistant and User roles.
"""
if dataset_name == "misalignment" and not accepted:
# If used did not accept the misalignment policy,
# keep the old selection.
return (gr.update(value="N/A",
choices=[]), gr.update(value="N/A", choices=[]))
dataset = datasets[dataset_name]
assistant_roles = dataset['assistant_roles']
user_roles = dataset['user_roles']
assistant_role = random.choice(assistant_roles) \
if len(assistant_roles) > 0 else ""
user_role = random.choice(user_roles) if len(user_roles) > 0 else ""
return (gr.update(value=assistant_role, choices=assistant_roles),
gr.update(value=user_role, choices=user_roles))
def roles_dd_change(dataset_name: str, assistant_role: str,
user_role: str) -> Dict:
""" Update the displayed chat upon inputs change.
Args:
assistant_role (str): Assistant dropdown value.
user_role (str): User dropdown value.
Returns:
Dict: New original roles state dictionary.
"""
matrix = datasets[dataset_name]['matrix']
if (assistant_role, user_role) in matrix:
record: Dict[str, Dict] = matrix[(assistant_role, user_role)]
original_task_options = list(record.keys())
original_task = original_task_options[0]
else:
original_task = "N/A"
original_task_options = []
choices = gr.Dropdown(choices=original_task_options,
value=original_task, interactive=True)
return choices
def build_chat_history(messages: Dict[int, Dict]) -> List[Tuple]:
""" Structures chatbot contents from the loaded data.
Args:
messages (Dict[int, Dict]): Messages loaded from JSON.
Returns:
List[Tuple]: Chat history in chatbot UI element format.
"""
history: List[Tuple] = []
curr_qa = (None, None)
for k in sorted(messages.keys()):
msg = messages[k]
content = msg['content']
if msg['role_type'] == "USER":
if curr_qa[0] is not None:
history.append(curr_qa)
curr_qa = (content, None)
else:
curr_qa = (content, None)
elif msg['role_type'] == "ASSISTANT":
curr_qa = (curr_qa[0], content)
history.append(curr_qa)
curr_qa = (None, None)
else:
pass
return history
def task_dd_change(dataset_name: str, assistant_role: str, user_role: str,
original_task: str) -> Tuple[str, List]:
""" Load task details and chatbot history into UI elements.
Args:
assistant_role (str): An assistan role.
user_role (str): An user role.
original_task (str): The original task.
Returns:
Tuple[str, List]: New contents of the specified task
and chatbot history UI elements.
"""
matrix = datasets[dataset_name]['matrix']
if (assistant_role, user_role) in matrix:
task_dict: Dict[str, Dict] = matrix[(assistant_role, user_role)]
if original_task in task_dict:
chat = task_dict[original_task]
specified_task = chat['specified_task']
history = build_chat_history(chat['messages'])
else:
specified_task = "N/A"
history = []
else:
specified_task = "N/A"
history = []
return specified_task, history
dataset_dd.change(check_if_misalignment, [dataset_dd, accepted_st],
[disclaimer_ta, accept_disclaimer_bn,
decline_disclaimer_bn]) \
.then(update_dataset_selection,
[dataset_dd, accepted_st],
[assistant_dd, user_dd])
accept_disclaimer_bn.click(enable_misalignment, None, [
accepted_st, disclaimer_ta, accept_disclaimer_bn, decline_disclaimer_bn
]) \
.then(update_dataset_selection,
[dataset_dd, accepted_st],
[assistant_dd, user_dd])
decline_disclaimer_bn.click(disable_misalignment, None, [
accepted_st, disclaimer_ta, accept_disclaimer_bn, decline_disclaimer_bn
]) \
.then(update_dataset_selection,
[dataset_dd, accepted_st],
[assistant_dd, user_dd])
func_args = (roles_dd_change, [dataset_dd, assistant_dd, user_dd], task_dd)
assistant_dd.change(*func_args)
user_dd.change(*func_args)
task_dd.change(task_dd_change,
[dataset_dd, assistant_dd, user_dd, task_dd],
[specified_task_ta, chatbot])
blocks.load(set_default_dataset, None, dataset_dd)
def construct_blocks(data_path: str, default_dataset: Optional[str]):
""" Construct Blocs app but do not launch it.
Args:
data_path (str): Path to the set of ZIP datasets.
default_dataset (Optional[str]): Name of the default dataset,
without extension.
Returns:
gr.Blocks: Blocks instance.
"""
print("Loading the dataset...")
datasets = load_datasets(data_path)
print("Dataset is loaded")
print("Getting Data Explorer web server online...")
with gr.Blocks() as blocks:
construct_ui(blocks, datasets, default_dataset)
return blocks
def main():
""" Entry point. """
args = parse_arguments()
blocks = construct_blocks(args.data_path, args.default_dataset)
blocks.queue(args.concurrency_count) \
.launch(share=args.share, inbrowser=args.inbrowser,
server_name=args.server_name, server_port=args.server_port)
print("Exiting.")
if __name__ == "__main__":
main()
|