Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

Dilemma App (#106)

Dmitrii Khizbullind5b649804aef
diff --git a/apps/common/auto_zip.py b/apps/common/auto_zip.py
new file mode 100644
index 000000000..cc6d1a1b9
--- /dev/null
+++ b/apps/common/auto_zip.py
@@ -0,0 +1,39 @@
+import json
+import os
+import zipfile
+
+
+class AutoZip:
+ def __init__(self, zip_path: str, ext: str = ".json"):
+ self.zip_path = zip_path
+ self.zip = zipfile.ZipFile(zip_path, "r")
+ self.fl = [f for f in self.zip.filelist if f.filename.endswith(ext)]
+
+ def __next__(self):
+ if self.index >= len(self.fl):
+ raise StopIteration
+ else:
+ finfo = self.fl[self.index]
+ with self.zip.open(finfo) as f:
+ raw_json = json.loads(f.read().decode("utf-8"))
+ self.index += 1
+ return raw_json
+
+ def __len__(self):
+ return len(self.fl)
+
+ def __iter__(self):
+ self.index = 0
+ return self
+
+ def as_dict(self, include_zip_name: bool = False):
+ d = dict()
+ for finfo in self.fl:
+ with self.zip.open(finfo) as f:
+ raw_text = f.read().decode("utf-8")
+ if include_zip_name:
+ key = os.path.split(self.zip_path)[1] + "/" + finfo.filename
+ else:
+ key = finfo.filename
+ d[key] = raw_text
+ return d
diff --git a/apps/common/test/test_archive_1.zip b/apps/common/test/test_archive_1.zip
new file mode 100644
index 000000000..77b4b81bd
Binary files /dev/null and b/apps/common/test/test_archive_1.zip differ
diff --git a/apps/common/test/test_auto_zip.py b/apps/common/test/test_auto_zip.py
new file mode 100644
index 000000000..2dccc0af9
--- /dev/null
+++ b/apps/common/test/test_auto_zip.py
@@ -0,0 +1,21 @@
+import os
+from unittest import TestCase
+
+from apps.common.auto_zip import AutoZip
+
+REPO_ROOT = os.path.realpath(
+ os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../.."))
+
+
+class TestAutoZip(TestCase):
+ def test_dict(self):
+ path = os.path.join(REPO_ROOT, "apps/common/test/test_archive_1.zip")
+ zip = AutoZip(path, ".txt")
+
+ d = zip.as_dict()
+ self.assertIsInstance(d, dict)
+ self.assertEqual(len(d), 3)
+
+ d = zip.as_dict(include_zip_name=True)
+ self.assertIsInstance(d, dict)
+ self.assertEqual(len(d), 3)
diff --git a/apps/data_explorer/loader.py b/apps/data_explorer/loader.py
index 4df77fd42..b221154b4 100644
--- a/apps/data_explorer/loader.py
+++ b/apps/data_explorer/loader.py
@@ -3,14 +3,14 @@ Everything related to parsing the data JSONs into UI-compatible format.
"""
import glob
-import json
import os
import re
-import zipfile
from typing import Any, Dict, List, Optional, Tuple, Union
from tqdm import tqdm
+from apps.common.auto_zip import AutoZip
+
ChatHistory = Dict[str, Any]
ParsedChatHistory = Dict[str, Any]
AllChats = Dict[str, Any]
@@ -20,30 +20,6 @@ REPO_ROOT = os.path.realpath(
os.path.join(os.path.dirname(os.path.abspath(__file__)), "../.."))
-class AutoZip:
- def __init__(self, zip_path: str, ext: str = ".json"):
- self.zip_path = zip_path
- self.zip = zipfile.ZipFile(zip_path, "r")
- self.fl = [f for f in self.zip.filelist if f.filename.endswith(ext)]
-
- def __next__(self):
- if self.index >= len(self.fl):
- raise StopIteration
- else:
- finfo = self.fl[self.index]
- with self.zip.open(finfo) as f:
- raw_json = json.loads(f.read().decode("utf-8"))
- self.index += 1
- return raw_json
-
- def __len__(self):
- return len(self.fl)
-
- def __iter__(self):
- self.index = 0
- return self
-
-
def parse(raw_chat: ChatHistory) -> Union[ParsedChatHistory, None]:
""" Gets the JSON raw chat data, validates it and transforms
into an easy to work with form.
diff --git a/apps/dilemma/database_connection.py b/apps/dilemma/database_connection.py
new file mode 100644
index 000000000..923e05d4a
--- /dev/null
+++ b/apps/dilemma/database_connection.py
@@ -0,0 +1,44 @@
+import sqlalchemy
+from google.cloud.sql.connector import Connector, IPTypes
+
+# import os
+# os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = \
+# "/path/to/camel-lm-XXXXXXXXX.json"
+
+
+class DatabaseConnection:
+ def __init__(self):
+
+ INSTANCE_CONNECTION_NAME = "camel-lm:me-central1:camel-dilemma"
+ print(f"Instance connection name is: {INSTANCE_CONNECTION_NAME}")
+ DB_USER = "dkuser"
+ DB_PASS = "camel230509"
+ DB_NAME = "dilemma_choices"
+
+ self.connector = Connector()
+
+ def getconn():
+ conn = self.connector.connect(INSTANCE_CONNECTION_NAME, "pymysql",
+ user=DB_USER, password=DB_PASS,
+ db=DB_NAME, ip_type=IPTypes.PRIVATE)
+ return conn
+
+ self.pool = sqlalchemy.create_engine(
+ "mysql+pymysql://",
+ creator=getconn,
+ )
+
+ def __del__(self):
+ self.connector.close()
+
+ def add_record(self, file_name: str, who_is_better: str):
+ with self.pool.connect() as db_conn:
+ insert_stmt = sqlalchemy.text(
+ "INSERT INTO choices2 (file_name, who_is_better, date)"
+ " VALUES (:file_name, :who_is_better, NOW())")
+ db_conn.execute(
+ insert_stmt, parameters={
+ "file_name": file_name,
+ "who_is_better": who_is_better,
+ })
+ db_conn.commit()
diff --git a/apps/dilemma/dilemma.py b/apps/dilemma/dilemma.py
new file mode 100644
index 000000000..44c9eeeac
--- /dev/null
+++ b/apps/dilemma/dilemma.py
@@ -0,0 +1,175 @@
+"""
+Gradio-based web UI to select between two
+options and save the answers to a database.
+"""
+
+import argparse
+import json
+import random
+from functools import partial
+from typing import Dict
+
+import gradio as gr
+from database_connection import DatabaseConnection
+
+from apps.common.auto_zip import AutoZip
+
+
+def parse_arguments():
+ """ Get command line arguments. """
+
+ parser = argparse.ArgumentParser("Dilemma tool")
+ parser.add_argument('--data-path', type=str, default=None,
+ help='Path to ZIP file containing JSONs')
+ parser.add_argument('--no-db', dest='no_db', action='store_true',
+ help="Set in development environment")
+ 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 load_dataset(data_path: str) -> Dict[str, Dict[str, str]]:
+ zip_inst = AutoZip(data_path, ext=".json")
+ text_dict = zip_inst.as_dict(include_zip_name=True)
+ res_dict = {}
+ for path, json_str in text_dict.items():
+ js = json.loads(json_str)
+ if 'summary' not in js:
+ continue
+ if 'gpt_solution' not in js:
+ continue
+ if 'specified_task' not in js:
+ continue
+ res_dict[path] = dict(summary=js['summary'],
+ gpt_solution=js['gpt_solution'],
+ specified_task=js['specified_task'])
+ return res_dict
+
+
+def construct_ui(blocks, dataset: Dict[str, Dict[str, str]],
+ has_connection: bool = True):
+ """ Build Gradio UI and populate with texts from JSONs.
+
+ Args:
+ blocks: Gradio blocks
+ dataset: Parsed multi-JSON dataset.
+ has_connection (bool): if the DB connection exists.
+
+ Returns:
+ None
+ """
+
+ db_conn = DatabaseConnection() if has_connection else None
+
+ gr.Markdown("## Dilemma app")
+ specified_task_ta = gr.TextArea(label="Specified task prompt", lines=1,
+ interactive=False)
+ with gr.Row():
+ left_better_bn = gr.Button("Left is better")
+ not_sure_bn = gr.Button("Not sure")
+ right_better_bn = gr.Button("Right is better")
+ with gr.Row():
+ with gr.Column(scale=1):
+ left_md = gr.Markdown("LOREM\n" "IPSUM\n")
+ with gr.Column(scale=1):
+ right_md = gr.Markdown("LOREM 2\n" "IPSUM 2\n")
+
+ state_st = gr.State(
+ dict(name="n", left=dict(who="a", text="at"),
+ right=dict(who="b", text="bt"), specified_task="st"))
+
+ def load_random(state):
+ items = random.sample(dataset.items(), 1)
+ if len(items) > 0:
+ name, rec = items[0]
+ else:
+ name, rec = "ERROR_NAME", dict(summary="ERROR_TEXT",
+ gpt_solution="ERROR_TEXT")
+ specified_task = rec['specified_task']
+ lst = list(
+ (k, v) for k, v in rec.items() if k in {'summary', 'gpt_solution'})
+ random.shuffle(lst)
+ state = dict(name=name, left=dict(who=lst[0][0], text=lst[0][1]),
+ right=dict(who=lst[1][0],
+ text=lst[1][1]), specified_task=specified_task)
+ return (state, state['left']['text'], state['right']['text'],
+ specified_task)
+
+ def record(choice: str, state):
+ assert choice in {'left', 'draw', 'right'}
+ if choice == 'draw':
+ who_is_better = 'none'
+ else:
+ who_is_better = state[choice]['who']
+ name = state['name']
+ print("choice=", choice, "who_is_better=", who_is_better, "name=",
+ name)
+ if db_conn is not None:
+ db_conn.add_record(name, who_is_better)
+
+ updated_controls = [state_st, left_md, right_md, specified_task_ta]
+
+ left_better_bn.click(partial(record, 'left'), state_st, None) \
+ .then(load_random, state_st, updated_controls)
+
+ not_sure_bn.click(partial(record, 'draw'), state_st, None) \
+ .then(load_random, state_st, updated_controls)
+
+ right_better_bn.click(partial(record, 'right'), state_st, None) \
+ .then(load_random, state_st, updated_controls)
+
+ blocks.load(load_random, state_st, updated_controls)
+
+
+def construct_blocks(data_path: str, has_connection: bool):
+ """ Construct Blocs app but do not launch it.
+
+ Args:
+ data_path (str): Path to the ZIP dataset with JOSNs inside.
+
+ Returns:
+ gr.Blocks: Blocks instance.
+ """
+
+ print("Loading the dataset...")
+ dataset = load_dataset(data_path)
+ print("Dataset is loaded")
+
+ print("Getting Dilemma web server online...")
+
+ with gr.Blocks() as blocks:
+ construct_ui(blocks, dataset, has_connection)
+
+ return blocks
+
+
+def main():
+ """ Entry point. """
+
+ args = parse_arguments()
+
+ blocks = construct_blocks(args.data_path, not args.no_db)
+
+ 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()
diff --git a/apps/dilemma/requirements.txt b/apps/dilemma/requirements.txt
new file mode 100644
index 000000000..00d62fac4
--- /dev/null
+++ b/apps/dilemma/requirements.txt
@@ -0,0 +1,2 @@
+cloud-sql-python-connector["pymysql"]
+SQLAlchemy==2.0.7