|
import json |
|
import os |
|
import re |
|
|
|
from internals.data.dataAccessor import getCharacters |
|
from internals.util.config import root_dir |
|
|
|
|
|
class Avatar: |
|
__avatars = {} |
|
|
|
def load_local(self, model_dir): |
|
self.__find_available_characters(model_dir) |
|
if len(self.__avatars.items()) > 0: |
|
print("Local characters", self.__avatars) |
|
|
|
def fetch_from_network(self, model_id: int): |
|
if not model_id: |
|
return |
|
characters = getCharacters(str(model_id)) |
|
if characters is not None: |
|
for character in characters: |
|
item = { |
|
"avatarName": str(character["title"]).lower(), |
|
"codename": character["tag"], |
|
"extraPrompt": character["extraData"]["extraPrompt"], |
|
} |
|
self.__avatars[item["avatarName"]] = item |
|
|
|
def add_code_names(self, prompt): |
|
array_of_objects = self.__avatars.values() |
|
|
|
for obj in array_of_objects: |
|
prompt = ( |
|
re.sub( |
|
r"\b" + obj["avatarName"] + r"\b", |
|
obj["extraPrompt"], |
|
prompt, |
|
flags=re.IGNORECASE, |
|
) |
|
+ " " |
|
) |
|
print(prompt) |
|
return prompt |
|
|
|
def __find_available_characters(self, path: str): |
|
if os.path.exists(path + "/characters.json"): |
|
print(path) |
|
try: |
|
print("Loading characters") |
|
with open(path + "/characters.json") as f: |
|
data = json.load(f) |
|
print("Characters: ", data) |
|
if "avatarName" in data[0]: |
|
for item in data: |
|
self.__avatars[item["avatarName"]] = item |
|
print("Avatars", self.__avatars) |
|
else: |
|
print("Invalid characters.json file") |
|
except Exception as e: |
|
print("Error Loading characters", e) |
|
|