-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
108 lines (87 loc) · 3.7 KB
/
util.py
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
# Clutters I don't wanna put in the function
import json
import os
import re
import base64
from PIL import Image
from typing import Any
import io
import datetime
def clean_user_message(user_input: str) -> str:
# Remove the bot's tag from the input since it's not needed.
user_input = user_input.replace("@Kobold", "")
user_input = user_input.replace("<|endoftext|>", "")
# Remove any spaces before and after the text.
user_input = user_input.strip()
return user_input
# Mistral-medium hallucinates some stuff in parentheses on newlines and then it hallucinates more
def truncate_from_newline_parenthesis(text: str) -> str:
# This regex pattern matches an open parenthesis at the start of any line within the text
pattern = r'^\('
# Use the MULTILINE flag to ensure ^ matches the start of each line
match = re.search(pattern, text, re.MULTILINE)
# If a match is found, return the substring up to that point, else return the original string
if match:
return text[:match.start()]
else:
return text
def encode_image_to_base64(image_bytes: bytes) -> str:
return base64.b64encode(image_bytes).decode('utf-8')
async def convert_webp_bytes_to_png(image_bytes: bytes) -> bytes:
with io.BytesIO(image_bytes) as image_file:
with Image.open(image_file) as img:
output_buffer = io.BytesIO()
img.save(output_buffer, format="PNG")
return output_buffer.getvalue()
def get_file_name(directory: str, file_name: str) -> str:
# Create the file path from name and directory and return that information
filepath = os.path.join(directory, file_name)
return filepath
# Read in a JSON file and spit it out, usefully or "None" if file's not there or we have an issue
def clean_username(username: str) -> str:
# Replace invalid characters with an underscore
cleaned_username = re.sub(r'[<>:"/\\|?*]', '_', username)
# Remove any trailing spaces or periods (as they are not allowed at the end of Windows filenames)
cleaned_username = cleaned_username.rstrip('. ')
return cleaned_username
def get_json_file(filename: str) -> dict[str, Any] | None:
# Try to go read the file!
try:
with open(filename, 'r') as file:
contents = json.load(file)
return contents
# Be very sad if the file isn't there to read
except FileNotFoundError:
# await write_to_log("File " + filename + "not found. Where did you lose it?")
return None
# Be also sad if the file isn't a JSON or is malformed somehow
except json.JSONDecodeError:
# await write_to_log("Unable to parse " + filename + " as JSON.")
return None
# Be super sad if we have no idea what's going on here
except Exception as e:
#await write_to_log(f"An unexpected error occurred: {e}")
return None
# Write a line to the log file
async def write_to_log(information: str) -> None:
file = get_file_name("", "log.txt")
# Add a time stamp to the provided error message
current_time = datetime.datetime.now()
rounded_time = current_time.replace(microsecond=0)
text = str(rounded_time) + " " + information + "\n"
await append_text_file(file, text)
async def append_text_file(file: str, text: str) -> None:
with open(file, 'a+', encoding="utf-8") as context:
context.write(text)
context.close()
def get_file_list(directory: str) -> list[str]:
# Try to get the list of character files from the directory provided.
try:
dir_path = directory + "\\"
files = os.listdir(dir_path)
except FileNotFoundError:
files = []
except OSError:
files = []
# Return either the list of files or a blank list.
return files