initial commit

This commit is contained in:
wea_ondara
2024-04-17 18:58:50 +02:00
commit 41d5b4f591
22 changed files with 1611 additions and 0 deletions

61
utils/conversation.py Normal file
View File

@@ -0,0 +1,61 @@
import datetime
import json
import os
import pickle
def load_conversation(model_id):
folder = 'conversations/' + model_id.replace('/', '_')
mkdir('../conversations')
mkdir(folder)
files = os.listdir(folder)
files = [file for file in files if file.endswith(".pickle") and os.path.isfile(folder + '/' + file)]
files.sort(reverse=True)
if len(files) > 0:
pickle_filename = folder + '/' + files[0]
print('Loading last conversation from ' + pickle_filename)
with open(pickle_filename, 'rb') as file:
return pickle.load(file)
return []
def save_conversation(model_id, messages):
folder = 'conversations/' + model_id.replace('/', '_')
mkdir('../conversations')
mkdir(folder)
timestamp = datetime.datetime.utcnow().strftime('%Y%m%d%H%M%S')
pickle_filename = folder + '/' + timestamp + '.pickle'
with open(pickle_filename, 'wb') as file:
pickle.dump(messages, file)
def load_conversation_json(model_id):
folder = 'conversations/' + model_id.replace('/', '_')
mkdir('../conversations')
mkdir(folder)
files = os.listdir(folder)
files = [file for file in files if file.endswith(".json") and os.path.isfile(folder + '/' + file)]
files.sort(reverse=True)
if len(files) > 0:
pickle_filename = folder + '/' + files[0]
print('Loading last conversation from ' + pickle_filename)
with open(pickle_filename, 'r') as file:
return json.load(file)
return []
def save_conversation_json(model_id, messages):
folder = 'conversations/' + model_id.replace('/', '_')
mkdir('../conversations')
mkdir(folder)
timestamp = datetime.datetime.utcnow().strftime('%Y%m%d%H%M%S')
pickle_filename = folder + '/' + timestamp + '.json'
with open(pickle_filename, 'w') as file:
json.dump(messages, file)
def mkdir(path):
if not os.path.isdir(path):
os.mkdir(path)