PizzAI/finetune_dialogs_tool/main.py

250 lines
7.3 KiB
Python
Raw Normal View History

2023-11-13 19:40:32 +01:00
# I know this code sucks and there are many repeating parts but it's just a simple temporary tool and I didn't bother to make it better
2023-11-13 22:33:13 +01:00
# This is not how something serious should be done
2023-11-13 19:40:32 +01:00
import os
2023-11-13 19:40:32 +01:00
import numpy as np
from order_items import possible_order_items
2023-11-13 22:33:13 +01:00
import json
from datetime import datetime
root= os.path.dirname(os.path.abspath(__file__))
2023-11-13 22:33:13 +01:00
vscode_available = any(os.access(os.path.join(path, "code"), os.X_OK) for path in os.environ["PATH"].split(os.pathsep))
2023-11-13 19:40:32 +01:00
def mkdir_if_not_exists_rel(path):
if not os.path.exists(root + "/" + path):
os.mkdir(root + "/" + path)
# create output dialogs folder if it does not exist
2023-11-13 19:40:32 +01:00
#if not os.path.exists(root + "/output_dialogs"):
# os.mkdir(root + "/output_dialogs")
mkdir_if_not_exists_rel("output_dialogs")
mkdir_if_not_exists_rel("temp")
2023-11-13 22:33:13 +01:00
# if temp/dialog_input.py does not exist, do the processing instead of just opening it
if not os.path.exists(root + "/temp/dialog_input.py"):
# read templates/dialog_input.txt
with open(root + "/templates/dialog_input.txt", "r", encoding="utf-8") as f:
dialog_input_template = f.read()
2023-11-13 19:40:32 +01:00
2023-11-13 22:33:13 +01:00
# random phone number string. for example "123 456 789"
phone_number = " ".join("".join(map(str, pack)) for pack in np.random.randint(0, 10, 9).reshape(-1, 3))
2023-11-13 19:40:32 +01:00
2023-11-13 22:33:13 +01:00
payment_method = np.random.choice([
"cash",
"card",
"Cash",
"Card",
"gotówka",
"karta",
"Gotówka",
"Karta",
"gotówką",
"kartą",
"Gotówką",
"Kartą",
])
2023-11-13 19:40:32 +01:00
2023-11-13 22:33:13 +01:00
order_items = ""
2023-11-13 19:40:32 +01:00
2023-11-13 22:33:13 +01:00
# randint high is exclusive so range 1 to 3 is actually 1 to 2 !!!
2023-11-13 19:40:32 +01:00
2023-11-13 22:33:13 +01:00
# pizza
pizza = possible_order_items["pizza"].copy()
for _ in range(np.random.randint(1, 3)):
item_idx = np.random.randint(0, len(pizza))
2023-11-13 19:40:32 +01:00
2023-11-13 22:33:13 +01:00
order_items += f" \"{np.random.randint(1, 4)}x Pizza {pizza[item_idx]}\",\n"
2023-11-13 19:40:32 +01:00
2023-11-13 22:33:13 +01:00
del pizza[item_idx]
2023-11-13 19:40:32 +01:00
2023-11-13 22:33:13 +01:00
# drink
drink = possible_order_items["drink"].copy()
for _ in range(np.random.randint(0, 3)):
item_idx = np.random.randint(0, len(drink))
2023-11-13 19:40:32 +01:00
2023-11-13 22:33:13 +01:00
order_items += f" \"{np.random.randint(1, 4)}x {drink[item_idx]}\",\n"
2023-11-13 19:40:32 +01:00
2023-11-13 22:33:13 +01:00
del drink[item_idx]
2023-11-13 19:40:32 +01:00
2023-11-13 22:33:13 +01:00
# sauce
sauce = possible_order_items["sauce"].copy()
for _ in range(np.random.randint(0, 3)):
item_idx = np.random.randint(0, len(sauce))
2023-11-13 19:40:32 +01:00
2023-11-13 22:33:13 +01:00
order_items += f" \"{np.random.randint(1, 4)}x {sauce[item_idx]}\",\n"
2023-11-13 19:40:32 +01:00
2023-11-13 22:33:13 +01:00
del sauce[item_idx]
2023-11-13 19:40:32 +01:00
2023-11-13 22:33:13 +01:00
order_items = order_items[:-2]
2023-11-13 19:40:32 +01:00
2023-11-13 22:33:13 +01:00
dialog_input_template = dialog_input_template.replace("{{phone_number}}", phone_number)
dialog_input_template = dialog_input_template.replace("{{payment_method}}", payment_method)
dialog_input_template = dialog_input_template.replace("{{order_items}}", order_items)
2023-11-13 19:40:32 +01:00
2023-11-13 22:33:13 +01:00
# save to temp/dialog_input.py
with open(root + "/temp/dialog_input.py", "w", encoding="utf-8") as f:
f.write(dialog_input_template)
2023-11-13 19:40:32 +01:00
while True:
# open it in notepad and wait for user to close it
# (or vscode if installed)
if vscode_available:
# open in new process of code. not the same window
os.system("code " + root + "/temp/dialog_input.py --new-window --wait")
else:
os.system("notepad " + root + "/temp/dialog_input.py")
# read and print
dialog_input_f = open(root + "/temp/dialog_input.py", "r", encoding="utf-8")
dialog_input = dialog_input_f.read()
dialog_input_f.close()
# execute part from the beginning to "## PYTHON END ##" as a python code here
python_end_idx = dialog_input.find("## PYTHON END ##")
2023-11-13 22:33:13 +01:00
phone_number, delivery_address, payment_method, order_items = None, None, None, None
2023-11-13 19:40:32 +01:00
exec(dialog_input[:python_end_idx])
# it will set variables: phone_number, payment_method and order_items
# check if theres any empty variables
not_ok = False
for val in [phone_number, delivery_address, payment_method, order_items]:
2023-11-13 22:33:13 +01:00
if val == "" or val is None:
2023-11-13 19:40:32 +01:00
print("Some values are empty. Please fill them.")
not_ok = True
break
if not_ok:
continue
# check the conversation
dialog = []
not_ok = False
now_pizzeria = True
for line in dialog_input[python_end_idx:].splitlines():
if line.startswith("piz ="):
if not now_pizzeria:
print("Wrong order of pizzeria (piz) and customer (bot) messages.")
not_ok = True
break
dialog.append(line[5:].strip())
now_pizzeria = not now_pizzeria
elif line.startswith("bot ="):
if now_pizzeria:
print("Wrong order of pizzeria (piz) and customer (bot) messages.")
not_ok = True
break
dialog.append(line[5:].strip())
now_pizzeria = not now_pizzeria
if not_ok:
continue
# check if dialog starts on pizzeria side and ends on customer side (length is even)
if len(dialog) % 2 != 0:
print("Dialog must start on pizzeria side and end on customer side.")
continue
break
dialog[-1] += " CALLEND"
now_pizzeria = True
2023-11-13 22:33:13 +01:00
print("=============================================")
2023-11-13 19:40:32 +01:00
for line in dialog:
2023-11-13 22:33:13 +01:00
print("🍕 -" if now_pizzeria else "🤖 -", line)
2023-11-13 19:40:32 +01:00
now_pizzeria = not now_pizzeria
2023-11-13 22:33:13 +01:00
print("=============================================")
2023-11-13 22:39:37 +01:00
user_input = input("Everything seems okay. Press enter to save it as a new JSON file. Type anything to abort and exit.")
if user_input != "":
print("Aborting and exiting.")
exit()
2023-11-13 22:33:13 +01:00
# read templates/system_instructions.txt
with open(root + "/templates/system_instructions.txt", "r", encoding="utf-8") as f:
system_instructions_template = f.read()
system_instructions_template = system_instructions_template.replace("{{phone_number}}", phone_number)
system_instructions_template = system_instructions_template.replace("{{delivery_location}}", delivery_address)
system_instructions_template = system_instructions_template.replace("{{payment_method}}", payment_method)
order_items_str = ""
for line in order_items:
order_items_str += f"{line}\n"
order_items_str = order_items_str[:-1]
system_instructions_template = system_instructions_template.replace("{{order_items}}", order_items_str)
# use all of these to create chatgpt finetuning JSON file
training_dict = {
"messages": []
}
# add system instructions
training_dict["messages"].append({
"role": "system",
"content": system_instructions_template
})
# add dialog
now_pizzeria = True
for line in dialog:
training_dict["messages"].append({
"role": "user" if now_pizzeria else "assistant",
"content": line
})
now_pizzeria = not now_pizzeria
## find all json files consisting of number (0.json, 1.json .... 123.json)
#files = [f for f in os.listdir(root + "/output_dialogs") if f.endswith(".json") and f[:-5].isdigit()]
#
## get name for next file (number higher than the highest number in the folder)
#if len(files) == 0:
# next_file = "0.json"
#else:
# next_file = str(max([int(f[:-5]) for f in files]) + 1) + ".json"
2023-11-13 23:07:20 +01:00
2023-11-13 19:40:32 +01:00
next_file = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + ".json"
2023-11-13 19:40:32 +01:00
2023-11-13 23:07:20 +01:00
# save to output_dialogs/next_file
with open(root + "/output_dialogs/" + next_file, "w", encoding="utf-8") as f:
json.dump(training_dict, f, indent=4, ensure_ascii=False)
2023-11-13 19:40:32 +01:00
2023-11-13 22:33:13 +01:00
# remove temp/dialog_input.py
os.remove(root + "/temp/dialog_input.py")
2023-11-13 19:40:32 +01:00
2023-11-13 22:33:13 +01:00
print(f"Saved as {next_file}")