Merge pull request #11 from MinusZoneAI/dev

Dev
This commit is contained in:
喵哩个咪
2024-06-01 20:40:36 +08:00
committed by GitHub
15 changed files with 942 additions and 517 deletions
+4 -1
View File
@@ -158,4 +158,7 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.vscode
.vscode
*.bat
exclude.txt
+123 -172
View File
@@ -8,6 +8,7 @@ from nodes import MAX_RESOLUTION
import comfy.utils
import shutil
import comfy.samplers
import folder_paths
WEB_DIRECTORY = "./web"
@@ -15,11 +16,11 @@ WEB_DIRECTORY = "./web"
AUTHOR_NAME = u"MinusZone"
CATEGORY_NAME = f"{AUTHOR_NAME} - Prompt"
sys.path.append(os.path.join(os.path.dirname(__file__)))
# sys.path.append(os.path.join(os.path.dirname(__file__)))
import importlib
import mz_prompt_webserver
from . import mz_prompt_webserver
mz_prompt_webserver.start_server()
NODE_CLASS_MAPPINGS = {
@@ -30,10 +31,98 @@ NODE_DISPLAY_NAME_MAPPINGS = {
}
import mz_llama3
import mz_phi3
import mz_llama_cpp
import mz_llava
from . import mz_llama_cpp
from . import mz_llava
class MZ_LLamaCPPModelConfig_ManualSelect:
@classmethod
def INPUT_TYPES(s):
gguf_dir = Utils.get_gguf_models_path()
if not os.path.exists(gguf_dir):
os.makedirs(gguf_dir)
gguf_files = []
# walk gguf_dir
for root, dirs, files in os.walk(gguf_dir):
for file in files:
if file.endswith(".gguf"):
gguf_files.append(
os.path.relpath(os.path.join(root, file), gguf_dir))
return {
"required": {
"llama_cpp_model": (gguf_files,),
},
"optional": {
},
}
RETURN_TYPES = ("LLamaCPPModelConfig",)
RETURN_NAMES = ("llama_cpp_model_config",)
FUNCTION = "create"
CATEGORY = CATEGORY_NAME
def create(self, **kwargs):
kwargs = kwargs.copy()
llama_cpp_model = kwargs.get("llama_cpp_model", "")
if llama_cpp_model != "":
llama_cpp_model = os.path.join(
Utils.get_gguf_models_path(), llama_cpp_model)
return ({
"type": "ManualSelect",
"model_path": llama_cpp_model,
},)
NODE_CLASS_MAPPINGS["MZ_LLamaCPPModelConfig_ManualSelect"] = MZ_LLamaCPPModelConfig_ManualSelect
NODE_DISPLAY_NAME_MAPPINGS[
"MZ_LLamaCPPModelConfig_ManualSelect"] = f"{AUTHOR_NAME} - ModelConfigManualSelect"
class MZ_LLamaCPPCLIPTextEncode:
@classmethod
def INPUT_TYPES(s):
importlib.reload(mz_llama_cpp)
result = {
"required": {
},
"optional": {
"llama_cpp_model": ("LLamaCPPModelConfig",),
},
}
common_input = getCommonCLIPTextEncodeInput()
for key in common_input["required"]:
result["required"][key] = common_input["required"][key]
for key in common_input["optional"]:
result["optional"][key] = common_input["optional"][key]
return result
RETURN_TYPES = ("STRING", "CONDITIONING",)
RETURN_NAMES = ("text", "conditioning",)
OUTPUT_NODE = True
FUNCTION = "encode"
CATEGORY = CATEGORY_NAME
DESCRIPTION = """
llama_cpp_model不设置时,将使用默认模型: Meta-Llama-3-8B-Instruct.Q4_K_M.gguf
"""
def encode(self, **kwargs):
kwargs = kwargs.copy()
from . import mz_llama_core_nodes
importlib.reload(mz_llama_core_nodes)
return mz_llama_core_nodes.llama_cpp_node_encode(kwargs)
NODE_CLASS_MAPPINGS["MZ_LLamaCPPCLIPTextEncode"] = MZ_LLamaCPPCLIPTextEncode
NODE_DISPLAY_NAME_MAPPINGS[
"MZ_LLamaCPPCLIPTextEncode"] = f"{AUTHOR_NAME} - CLIPTextEncode(llama.cpp通用)"
class MZ_LLamaCPPOptions:
@@ -69,6 +158,7 @@ class MZ_LLamaCPPOptions:
CATEGORY = CATEGORY_NAME
def create(self, **kwargs):
kwargs = kwargs.copy()
importlib.reload(mz_llama_cpp)
opt = {}
for key in kwargs:
@@ -83,13 +173,12 @@ NODE_DISPLAY_NAME_MAPPINGS["MZ_LLamaCPPOptions"] = f"{AUTHOR_NAME} - LLamaCPPOpt
class MZ_CustomizeInstruct:
@classmethod
def INPUT_TYPES(s):
import mz_prompts
from . import mz_prompts
return {
"required": {
"system": ("STRING", {"multiline": True, "default": mz_prompts.Long_prompt}),
"instruct": ("STRING", {"multiline": True, "default": "Short: %text%"}),
"start_str": ("STRING", {"default": "Long: "}),
"end_str": ("STRING", {"default": ""}),
},
}
@@ -99,6 +188,8 @@ class MZ_CustomizeInstruct:
CATEGORY = CATEGORY_NAME
def create(self, **kwargs):
kwargs = kwargs.copy()
return (kwargs,)
@@ -107,7 +198,8 @@ NODE_DISPLAY_NAME_MAPPINGS["MZ_CustomizeInstruct"] = f"{AUTHOR_NAME} - Customize
def getCommonCLIPTextEncodeInput():
style_presets = mz_llama_cpp.get_style_presets()
from . import mz_llama_core_nodes
style_presets = mz_llama_core_nodes.get_style_presets()
CommonCLIPTextEncodeInput = {
"required": {
"prompt_version": (["v1"], {"default": "v1"}),
@@ -129,162 +221,6 @@ def getCommonCLIPTextEncodeInput():
return CommonCLIPTextEncodeInput
class MZ_LLama3CLIPTextEncode:
@classmethod
def INPUT_TYPES(s):
m_models = mz_llama3.llama3_models.copy()
for i in range(len(m_models)):
if mz_llama3.get_exist_model(m_models[i]) is not None:
m_models[i] += "[downloaded]"
importlib.reload(mz_llama_cpp)
result = {
"required": {
"llama_cpp_model": (m_models, {"default": m_models[0]}),
"download_source": (
["none", "modelscope", "hf-mirror.com",],
{"default": "none"}
),
},
"optional": {},
}
common_input = getCommonCLIPTextEncodeInput()
for key in common_input["required"]:
result["required"][key] = common_input["required"][key]
for key in common_input["optional"]:
result["optional"][key] = common_input["optional"][key]
return result
RETURN_TYPES = ("STRING", "CONDITIONING",)
RETURN_NAMES = ("text", "conditioning",)
OUTPUT_NODE = True
FUNCTION = "encode"
CATEGORY = CATEGORY_NAME
def encode(self, **kwargs):
importlib.reload(mz_llama3)
kwargs["llama_cpp_model"] = kwargs.get(
"llama_cpp_model", "").replace("[downloaded]", "")
text = mz_llama3.query_beautify_prompt_text(kwargs)
conditionings = None
clip = kwargs.get("clip", None)
if clip is not None:
conditionings = Utils.a1111_clip_text_encode(clip, text, )
return {"ui": {"string": [text,]}, "result": (text, conditionings)}
NODE_CLASS_MAPPINGS["MZ_LLama3CLIPTextEncode"] = MZ_LLama3CLIPTextEncode
NODE_DISPLAY_NAME_MAPPINGS[
"MZ_LLama3CLIPTextEncode"] = f"{AUTHOR_NAME} - CLIPTextEncode(LLama3)"
class MZ_Phi3CLIPTextEncode:
@classmethod
def INPUT_TYPES(s):
m_models = mz_phi3.phi3_models.copy()
for i in range(len(m_models)):
if mz_llama3.get_exist_model(m_models[i]) is not None:
m_models[i] += "[downloaded]"
importlib.reload(mz_phi3)
result = {
"required": {
"llama_cpp_model": (m_models, {"default": m_models[0]}),
"download_source": (
["none", "modelscope", "hf-mirror.com",],
{"default": "none"}
),
},
"optional": {},
}
common_input = getCommonCLIPTextEncodeInput()
for key in common_input["required"]:
result["required"][key] = common_input["required"][key]
for key in common_input["optional"]:
result["optional"][key] = common_input["optional"][key]
return result
RETURN_TYPES = ("STRING", "CONDITIONING",)
RETURN_NAMES = ("text", "conditioning",)
OUTPUT_NODE = True
FUNCTION = "encode"
CATEGORY = CATEGORY_NAME
def encode(self, **kwargs):
importlib.reload(mz_llama3)
kwargs["llama_cpp_model"] = kwargs.get(
"llama_cpp_model", "").replace("[downloaded]", "")
text = mz_phi3.query_beautify_prompt_text(kwargs)
conditionings = None
clip = kwargs.get("clip", None)
if clip is not None:
conditionings = Utils.a1111_clip_text_encode(clip, text, )
return {"ui": {"string": [text,]}, "result": (text, conditionings)}
NODE_CLASS_MAPPINGS["MZ_Phi3CLIPTextEncode"] = MZ_Phi3CLIPTextEncode
NODE_DISPLAY_NAME_MAPPINGS[
"MZ_Phi3CLIPTextEncode"] = f"{AUTHOR_NAME} - CLIPTextEncode(Phi3)"
class MZ_BaseLLamaCPPCLIPTextEncode:
@classmethod
def INPUT_TYPES(s):
importlib.reload(mz_llama_cpp)
result = {
"required": {
"llama_cpp_model": ("STRING", {"default": "", "placeholder": "model_path"}),
},
"optional": {
},
}
common_input = getCommonCLIPTextEncodeInput()
for key in common_input["required"]:
result["required"][key] = common_input["required"][key]
for key in common_input["optional"]:
result["optional"][key] = common_input["optional"][key]
return result
RETURN_TYPES = ("STRING", "CONDITIONING",)
RETURN_NAMES = ("text", "conditioning",)
OUTPUT_NODE = True
FUNCTION = "encode"
CATEGORY = CATEGORY_NAME
def encode(self, **kwargs):
importlib.reload(mz_llama3)
kwargs["llama_cpp_model"] = kwargs.get(
"llama_cpp_model", "").replace("[downloaded]", "")
text = mz_llama_cpp.base_query_beautify_prompt_text(kwargs)
conditionings = None
clip = kwargs.get("clip", None)
if clip is not None:
conditionings = Utils.a1111_clip_text_encode(clip, text, )
return {"ui": {"string": [text,]}, "result": (text, conditionings)}
NODE_CLASS_MAPPINGS["MZ_BaseLLamaCPPCLIPTextEncode"] = MZ_BaseLLamaCPPCLIPTextEncode
NODE_DISPLAY_NAME_MAPPINGS[
"MZ_BaseLLamaCPPCLIPTextEncode"] = f"{AUTHOR_NAME} - CLIPTextEncode(BaseLLamaCPP)"
class MZ_LLavaImageInterrogator:
@classmethod
def INPUT_TYPES(s):
@@ -312,7 +248,7 @@ class MZ_LLavaImageInterrogator:
{"default": "none"}
),
"resolution": ("INT", {"default": 512, "min": 128, "max": 2048}),
"sd_format": (["none", "v1"], {"default": "none"}),
"sd_format": (["none", "v1", ], {"default": "none"}),
"keep_device": ([False, True], {"default": False}),
"seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}),
},
@@ -331,6 +267,8 @@ class MZ_LLavaImageInterrogator:
CATEGORY = CATEGORY_NAME
def interrogate(self, **kwargs):
kwargs = kwargs.copy()
importlib.reload(mz_llava)
kwargs["llama_cpp_model"] = kwargs.get(
@@ -349,7 +287,7 @@ class MZ_LLavaImageInterrogator:
if clip is not None:
conditionings = Utils.a1111_clip_text_encode(clip, text, )
return {"ui": {"string": [text,]}, "result": (text, conditionings)}
return {"ui": {"string": [Utils.to_debug_prompt(text),]}, "result": (text, conditionings)}
NODE_CLASS_MAPPINGS["MZ_LLavaImageInterrogator"] = MZ_LLavaImageInterrogator
@@ -384,6 +322,8 @@ class MZ_BaseLLavaImageInterrogator:
CATEGORY = CATEGORY_NAME
def interrogate(self, **kwargs):
kwargs = kwargs.copy()
importlib.reload(mz_llava)
if kwargs.get("image", None) is not None:
@@ -397,7 +337,7 @@ class MZ_BaseLLavaImageInterrogator:
if clip is not None:
conditionings = Utils.a1111_clip_text_encode(clip, text, )
return {"ui": {"string": [text,]}, "result": (text, conditionings)}
return {"ui": {"string": [Utils.to_debug_prompt(text),]}, "result": (text, conditionings)}
NODE_CLASS_MAPPINGS["MZ_BaseLLavaImageInterrogator"] = MZ_BaseLLavaImageInterrogator
@@ -413,6 +353,7 @@ class MZ_ImageCaptionerConfig:
"directory": ("STRING", {"default": "", "placeholder": "directory"}),
"caption_suffix": ("STRING", {"default": ".caption"}),
"force_update": ([False, True], {"default": False}),
"retry_keyword": ("STRING", {"default": "not,\",error"}),
},
"optional": {
@@ -426,6 +367,8 @@ class MZ_ImageCaptionerConfig:
CATEGORY = CATEGORY_NAME
def interrogate_batch(self, **kwargs):
kwargs = kwargs.copy()
return (kwargs, )
@@ -483,6 +426,8 @@ class MZ_OpenAIApiCLIPTextEncode:
CATEGORY = CATEGORY_NAME
def encode(self, **kwargs):
kwargs = kwargs.copy()
import mz_openaiapi
importlib.reload(mz_openaiapi)
@@ -511,13 +456,19 @@ class MZ_OpenAIApiCLIPTextEncode:
if clip is not None:
conditionings = Utils.a1111_clip_text_encode(clip, text, )
return {"ui": {"string": [text,]}, "result": (text, conditionings)}
return {"ui": {"string": [Utils.to_debug_prompt(text),]}, "result": (text, conditionings)}
NODE_CLASS_MAPPINGS["MZ_OpenAIApiCLIPTextEncode"] = MZ_OpenAIApiCLIPTextEncode
NODE_DISPLAY_NAME_MAPPINGS[
"MZ_OpenAIApiCLIPTextEncode"] = f"{AUTHOR_NAME} - CLIPTextEncode(OpenAIApi)"
import mz_gen_translate
from . import mz_gen_translate
mz_gen_translate.gen_translate(NODE_DISPLAY_NAME_MAPPINGS, NODE_CLASS_MAPPINGS)
from .v1.init import NODE_CLASS_MAPPINGS as DEPRECATED_NODE_CLASS_MAPPINGS
from .v1.init import NODE_DISPLAY_NAME_MAPPINGS as DEPRECATED_NODE_DISPLAY_NAME_MAPPINGS
NODE_CLASS_MAPPINGS.update(DEPRECATED_NODE_CLASS_MAPPINGS)
NODE_DISPLAY_NAME_MAPPINGS.update(DEPRECATED_NODE_DISPLAY_NAME_MAPPINGS)
+2
View File
@@ -26,6 +26,8 @@ ZH_Replace_Map = {
"ImageInterrogator": "图像反推",
"customize_instruct": "自定义指令",
"CustomizeInstruct": "自定义指令",
"deprecated": "已废弃",
"LLamaCPPModelConfigManualSelect": "llama.cpp模型配置手动选择",
}
+223
View File
@@ -0,0 +1,223 @@
import importlib
import json
from . import mz_prompt_utils
from . import mz_llama_cpp
from . import mz_prompts
def get_schema_base_type(t):
return {
"type": t,
}
def get_schema_obj(keys_type={}, required=[]):
item = {}
for key, value in keys_type.items():
if type(value) == str:
value = get_schema_base_type(value)
item[key] = value
return {
"type": "object",
"properties": item,
"required": required
}
def get_schema_array(item_type="string"):
if type(item_type) == str:
item_type = get_schema_base_type(item_type)
return {
"type": "array",
"items": item_type,
}
high_quality_prompt = "((high quality:1.4), (best quality:1.4), (masterpiece:1.4), (8K resolution), (2k wallpaper))"
style_presets_prompt = {
"none": "",
"high_quality": high_quality_prompt,
"photography": f"{high_quality_prompt}, (RAW photo, best quality), (realistic, photo-realistic:1.2), (bokeh, cinematic shot, dynamic composition, incredibly detailed, sharpen, details, intricate detail, professional lighting, film lighting, 35mm, anamorphic, lightroom, cinematography, bokeh, lens flare, film grain, HDR10, 8K)",
"illustration": f"{high_quality_prompt}, ((detailed matte painting, intricate detail, splash screen, complementary colors), (detailed),(intricate details),illustration,an extremely delicate and beautiful,ultra-detailed,highres,extremely detailed)",
}
def get_style_presets():
return [
"none",
"high_quality",
"photography",
"illustration",
]
def llama_cpp_node_encode(args_dict):
importlib.reload(mz_prompts)
importlib.reload(mz_llama_cpp)
model_config = args_dict.get("llama_cpp_model", {})
mz_prompt_utils.Utils.print_log(f"model_config: {model_config}")
model_file = model_config.get("model_path", "auto")
mz_prompt_utils.Utils.print_log(f"model_file: {model_file}")
text = args_dict.get("text", "")
style_presets = args_dict.get("style_presets", "")
options = args_dict.get("llama_cpp_options", {})
keep_device = args_dict.get("keep_device", False)
seed = args_dict.get("seed", -1)
options["seed"] = seed
customize_instruct = args_dict.get("customize_instruct", None)
mz_prompt_utils.Utils.print_log(
f"customize_instruct: {customize_instruct}")
try:
schema = None
if customize_instruct is None:
schema = get_schema_obj(
keys_type={
"description": get_schema_base_type("string"),
"long_prompt": get_schema_base_type("string"),
"main_color_word": get_schema_base_type("string"),
"camera_angle_word": get_schema_base_type("string"),
"style_words": get_schema_array("string"),
"subject_words": get_schema_array("string"),
"light_words": get_schema_array("string"),
"environment_words": get_schema_array("string"),
},
required=[
"description",
"long_prompt",
"main_color_word",
"camera_angle_word",
"style_words",
"subject_words",
"light_words",
"environment_words",
]
)
question = f"IDEA: {style_presets},{text}"
if style_presets == "none":
question = f"IDEA: {text}"
system_prompt = mz_prompts.Beautify_Prompt + mz_prompts.Long_prompt + "\n"
else:
system_prompt = customize_instruct.get("system", "")
question = customize_instruct.get("instruct", "%text%")
system_prompt = system_prompt.replace("%text%", text)
question = question.replace("%text%", text)
mz_prompt_utils.Utils.print_log(f"system_prompt: {system_prompt}")
mz_prompt_utils.Utils.print_log(f"question: {question}")
if schema is not None:
response_json = mz_llama_cpp.llama_cpp_simple_interrogator_to_json(
model_file=model_file,
system=system_prompt,
question=question,
schema=schema,
options=options,
)
mz_prompt_utils.Utils.print_log(f"response_json: {response_json}")
response = json.loads(response_json)
full_responses = []
if response["description"] != "":
full_responses.append(f"({response['description']})")
if response["long_prompt"] != "":
full_responses.append(f"({response['long_prompt']})")
if response["main_color_word"] != "":
full_responses.append(f"({response['main_color_word']})")
if response["camera_angle_word"] != "":
full_responses.append(f"({response['camera_angle_word']})")
response["style_words"] = [
x for x in response["style_words"] if x != ""]
if len(response["style_words"]) > 0:
full_responses.append(
f"({', '.join(response['style_words'])})")
response["subject_words"] = [
x for x in response["subject_words"] if x != ""]
if len(response["subject_words"]) > 0:
full_responses.append(
f"({', '.join(response['subject_words'])})")
response["light_words"] = [
x for x in response["light_words"] if x != ""]
if len(response["light_words"]) > 0:
full_responses.append(
f"({', '.join(response['light_words'])})")
response["environment_words"] = [
x for x in response["environment_words"] if x != ""]
if len(response["environment_words"]) > 0:
full_responses.append(
f"({', '.join(response['environment_words'])})")
full_response = ", ".join(full_responses)
else:
full_response = mz_llama_cpp.llama_cpp_simple_interrogator(
model_file=model_file,
system=system_prompt,
question=question,
options=options,
)
start_str = customize_instruct.get("start_str", "")
if start_str != "" and full_response.find(start_str) != -1:
full_response_list = full_response.split(start_str)
# 删除第一个元素
full_response_list.pop(0)
full_response = start_str.join(full_response_list)
end_str = customize_instruct.get("end_str", "")
if end_str != "" and full_response.find(end_str) != -1:
full_response_list = full_response.split(end_str)
# 删除最后一个元素
full_response_list.pop()
full_response = end_str.join(full_response_list)
if keep_device is False:
mz_llama_cpp.freed_gpu_memory(model_file=model_file)
# 去除换行
while full_response.find("\n") != -1:
full_response = full_response.replace("\n", " ")
# 句号换成逗号
while full_response.find(".") != -1:
full_response = full_response.replace(".", ",")
# 去除多余逗号
while full_response.find(",,") != -1:
full_response = full_response.replace(",,", ",")
while full_response.find(", ,") != -1:
full_response = full_response.replace(", ,", ",")
full_response = mz_prompt_utils.Utils.prompt_zh_to_en(full_response)
style_presets_prompt_text = style_presets_prompt.get(style_presets, "")
if style_presets_prompt_text != "":
full_response = f"{style_presets_prompt_text}, {full_response}"
except Exception as e:
mz_llama_cpp.freed_gpu_memory(model_file=model_file)
raise e
conditionings = None
clip = args_dict.get("clip", None)
if clip is not None:
conditionings = mz_prompt_utils.Utils.a1111_clip_text_encode(
clip, full_response, )
return {"ui": {"string": [full_response,]}, "result": (full_response, conditionings)}
+37 -293
View File
@@ -6,40 +6,12 @@ import subprocess
import sys
import torch
try:
import mz_prompt_utils
import mz_prompt_webserver
from . import mz_prompt_utils
from . import mz_prompt_webserver
except ImportError:
pass
def LlamaCppOptions():
return {
"n_ctx": 2048,
"n_batch": 2048,
"n_threads": 0,
"n_threads_batch": 0,
"split_mode": ["LLAMA_SPLIT_MODE_NONE", "LLAMA_SPLIT_MODE_LAYER", "LLAMA_SPLIT_MODE_ROW",],
"main_gpu": 0,
"n_gpu_layers": -1,
"max_tokens": 4096,
"temperature": 1.6,
"top_p": 0.95,
"min_p": 0.05,
"typical_p": 1.0,
"stop": "",
"frequency_penalty": 0.0,
"presence_penalty": 0.0,
"repeat_penalty": 1.1,
"top_k": 50,
"tfs_z": 1.0,
"mirostat_mode": ["none", "mirostat", "mirostat_v2"],
"mirostat_tau": 5.0,
"mirostat_eta": 0.1,
}
def check_llama_cpp_requirements():
last_version = "0.2.63"
try:
@@ -103,6 +75,32 @@ def check_llama_cpp_requirements():
print("llama_cpp installed successfully. (llama_cpp安装成功)")
def LlamaCppOptions():
return {
"n_ctx": 2048,
"n_batch": 2048,
"n_threads": 0,
"n_threads_batch": 0,
"split_mode": ["LLAMA_SPLIT_MODE_NONE", "LLAMA_SPLIT_MODE_LAYER", "LLAMA_SPLIT_MODE_ROW",],
"main_gpu": 0,
"n_gpu_layers": -1,
"max_tokens": 4096,
"temperature": 1.6,
"top_p": 0.95,
"min_p": 0.05,
"typical_p": 1.0,
"stop": "",
"frequency_penalty": 0.0,
"presence_penalty": 0.0,
"repeat_penalty": 1.1,
"top_k": 50,
"tfs_z": 1.0,
"mirostat_mode": ["none", "mirostat", "mirostat_v2"],
"mirostat_tau": 5.0,
"mirostat_eta": 0.1,
}
def freed_gpu_memory(model_file):
check_llama_cpp_requirements()
@@ -124,6 +122,7 @@ def freed_gpu_memory(model_file):
def llama_cpp_messages(model_file, chat_handler=None, messages=[], options={}):
if options is None:
options = {}
options = options.copy()
print(f"Find local model file: {model_file}")
init_opts = ["n_ctx", "logits_all", "chat_format", "n_gpu_layers"]
@@ -180,6 +179,8 @@ def llama_cpp_messages(model_file, chat_handler=None, messages=[], options={}):
f"llama_cpp_model_and_opt_{model_file}", model_and_opt)
model = model_and_opt.get("model")
model.set_seed(options.get("seed", -1))
model.reset()
response_format = options.get("response_format", None)
mz_prompt_utils.Utils.print_log(
@@ -237,35 +238,8 @@ def llama_cpp_messages(model_file, chat_handler=None, messages=[], options={}):
return result
def get_schema_base_type(t):
return {
"type": t,
}
def get_schema_obj(keys_type={}, required=[]):
item = {}
for key, value in keys_type.items():
if type(value) == str:
value = get_schema_base_type(value)
item[key] = value
return {
"type": "object",
"properties": item,
"required": required
}
def get_schema_array(item_type="string"):
if type(item_type) == str:
item_type = get_schema_base_type(item_type)
return {
"type": "array",
"items": item_type,
}
def llama_cpp_simple_interrogator_to_json(model_file, use_system=True, system=None, question="", schema={}, options={}):
options = options.copy()
if system is None:
system = ""
messages = [
@@ -317,6 +291,7 @@ def llama_cpp_simple_interrogator_to_json(model_file, use_system=True, system=No
def llama_cpp_simple_interrogator(model_file, use_system=True, system=None, question="", options={}):
if options is None:
options = {}
options = options.copy()
if system is None:
system = ""
messages = [
@@ -357,6 +332,8 @@ def llama_cpp_simple_interrogator(model_file, use_system=True, system=None, ques
def llava_cpp_messages(model_file, chat_handler, messages, options={}):
if options is None:
options = {}
options = options.copy()
options["logits_all"] = True
options["n_ctx"] = max(4096, options.get("n_ctx", 4096))
return llama_cpp_messages(model_file, chat_handler, messages, options)
@@ -367,7 +344,7 @@ def llava_cpp_simple_interrogator(
image=None, options={}):
if options is None:
options = {}
options = options.copy()
check_llama_cpp_requirements()
content = []
@@ -380,6 +357,7 @@ def llava_cpp_simple_interrogator(
check_llama_cpp_requirements()
from llama_cpp.llama_chat_format import Llava15ChatHandler
if mmproj_file is not None:
mz_prompt_utils.Utils.print_log(f"llava_cpp_simple_interrogator mmproj_file: {mmproj_file}")
chat_handler = Llava15ChatHandler(clip_model_path=mmproj_file)
return llava_cpp_messages(model_file, chat_handler, [
@@ -392,237 +370,3 @@ def llava_cpp_simple_interrogator(
"content": content,
},
], options=options)
def llava_cpp_simple_interrogator_to_json(
model_file, mmproj_file, system="You are an assistant who perfectly describes images.", question="Describe this image in detail please\nuse json format for output:",
image=None, schema={}, options={}):
response_format = {
"type": "json_object",
"schema": schema,
}
options["response_format"] = response_format
options["chat_format"] = "chatml"
result = llava_cpp_simple_interrogator(
model_file, mmproj_file, system=system, question=question, image=image, options=options)
return result
high_quality_prompt = "((high quality:1.4), (best quality:1.4), (masterpiece:1.4), (8K resolution), (2k wallpaper))"
style_presets_prompt = {
"none": "",
"high_quality": high_quality_prompt,
"photography": f"{high_quality_prompt}, (RAW photo, best quality), (realistic, photo-realistic:1.2), (bokeh, cinematic shot, dynamic composition, incredibly detailed, sharpen, details, intricate detail, professional lighting, film lighting, 35mm, anamorphic, lightroom, cinematography, bokeh, lens flare, film grain, HDR10, 8K)",
"illustration": f"{high_quality_prompt}, ((detailed matte painting, intricate detail, splash screen, complementary colors), (detailed),(intricate details),illustration,an extremely delicate and beautiful,ultra-detailed,highres,extremely detailed)",
}
def get_style_presets():
return [
"none",
"high_quality",
"photography",
"illustration",
]
def base_query_beautify_prompt_text(args_dict):
model_file = args_dict.get("llama_cpp_model", "")
text = args_dict.get("text", "")
style_presets = args_dict.get("style_presets", "")
options = args_dict.get("llama_cpp_options", {})
keep_device = args_dict.get("keep_device", False)
seed = args_dict.get("seed", -1)
options["seed"] = seed
import mz_prompts
importlib.reload(mz_prompts)
customize_instruct = args_dict.get("customize_instruct", None)
mz_prompt_utils.Utils.print_log(
f"customize_instruct: {customize_instruct}")
try:
schema = None
if customize_instruct is None:
schema = get_schema_obj(
keys_type={
"description": get_schema_base_type("string"),
"long_prompt": get_schema_base_type("string"),
"main_color_word": get_schema_base_type("string"),
"camera_angle_word": get_schema_base_type("string"),
"style_words": get_schema_array("string"),
"subject_words": get_schema_array("string"),
"light_words": get_schema_array("string"),
"environment_words": get_schema_array("string"),
},
required=[
"description",
"long_prompt",
"main_color_word",
"camera_angle_word",
"style_words",
"subject_words",
"light_words",
"environment_words",
]
)
question = f"IDEA: {style_presets},{text}"
if style_presets == "none":
question = f"IDEA: {text}"
system_prompt = mz_prompts.Beautify_Prompt + mz_prompts.Long_prompt + "\n"
else:
system_prompt = customize_instruct.get("system", "")
question = customize_instruct.get("instruct", "%text%")
system_prompt = system_prompt.replace("%text%", text)
question = question.replace("%text%", text)
mz_prompt_utils.Utils.print_log(f"system_prompt: {system_prompt}")
mz_prompt_utils.Utils.print_log(f"question: {question}")
if schema is not None:
response_json = llama_cpp_simple_interrogator_to_json(
model_file=model_file,
system=system_prompt,
question=question,
schema=schema,
options=options,
)
mz_prompt_utils.Utils.print_log(f"response_json: {response_json}")
response = json.loads(response_json)
full_responses = []
if response["description"] != "":
full_responses.append(f"({response['description']})")
if response["long_prompt"] != "":
full_responses.append(f"({response['long_prompt']})")
if response["main_color_word"] != "":
full_responses.append(f"({response['main_color_word']})")
if response["camera_angle_word"] != "":
full_responses.append(f"({response['camera_angle_word']})")
response["style_words"] = [
x for x in response["style_words"] if x != ""]
if len(response["style_words"]) > 0:
full_responses.append(
f"({', '.join(response['style_words'])})")
response["subject_words"] = [
x for x in response["subject_words"] if x != ""]
if len(response["subject_words"]) > 0:
full_responses.append(
f"({', '.join(response['subject_words'])})")
response["light_words"] = [
x for x in response["light_words"] if x != ""]
if len(response["light_words"]) > 0:
full_responses.append(
f"({', '.join(response['light_words'])})")
response["environment_words"] = [
x for x in response["environment_words"] if x != ""]
if len(response["environment_words"]) > 0:
full_responses.append(
f"({', '.join(response['environment_words'])})")
full_response = ", ".join(full_responses)
else:
full_response = llama_cpp_simple_interrogator(
model_file=model_file,
system=system_prompt,
question=question,
options=options,
)
start_str = customize_instruct.get("start_str", "")
if start_str != "" and full_response.find(start_str) != -1:
full_response_list = full_response.split(start_str)
# 删除第一个元素
full_response_list.pop(0)
full_response = start_str.join(full_response_list)
end_str = customize_instruct.get("end_str", "")
if end_str != "" and full_response.find(end_str) != -1:
full_response_list = full_response.split(end_str)
# 删除最后一个元素
full_response_list.pop()
full_response = end_str.join(full_response_list)
if keep_device is False:
freed_gpu_memory(model_file=model_file)
# 去除换行
while full_response.find("\n") != -1:
full_response = full_response.replace("\n", " ")
# 句号换成逗号
while full_response.find(".") != -1:
full_response = full_response.replace(".", ",")
# 去除多余逗号
while full_response.find(",,") != -1:
full_response = full_response.replace(",,", ",")
while full_response.find(", ,") != -1:
full_response = full_response.replace(", ,", ",")
full_response = mz_prompt_utils.Utils.prompt_zh_to_en(full_response)
style_presets_prompt_text = style_presets_prompt.get(style_presets, "")
if style_presets_prompt_text != "":
full_response = f"{style_presets_prompt_text}, {full_response}"
return full_response
except Exception as e:
freed_gpu_memory(model_file=model_file)
# mz_utils.Utils.print_log(f"Error in auto_prompt_text: {e}")
raise e
def get_image_composition_json_schema():
system = "Please make a specific composition of the proposed content. You need to have the width and height of the overall picture and a list of specific element positions."
return system, {
"type": "object",
"properties": {
"width": {
"type": "number"
},
"height": {
"type": "number"
},
"bbox": {
"type": "array",
"items": {
"type": "object",
"properties": {
"english_name": {
"type": "string"
},
"left": {
"type": "number"
},
"top": {
"type": "number"
},
"width": {
"type": "number"
},
"height": {
"type": "number"
}
},
"required": ["english_name", "left", "top", "width", "height"]
}
}
},
"required": ["width", "height", "bbox"]
}
+72 -23
View File
@@ -1,12 +1,13 @@
import json
import os
import mz_prompt_utils
import mz_llama_cpp
from . import mz_prompt_utils
from . import mz_llama_cpp
from . import mz_prompts
from . import mz_llama_core_nodes
from . import mz_prompt_webserver
import importlib
import mz_prompts
import mz_prompt_webserver
LLava_models = [
"llava-1.6-mistral-7b-gguf/llava-v1.6-mistral-7b.Q5_K_M.gguf",
@@ -65,6 +66,7 @@ def get_exist_model(model_name):
def image_interrogator(args_dict):
args_dict = args_dict.copy()
captioner_config = args_dict.get("captioner_config", None)
if captioner_config is not None:
@@ -72,6 +74,11 @@ def image_interrogator(args_dict):
directory = captioner_config.get("directory", None)
force_update = captioner_config.get("force_update", False)
caption_suffix = captioner_config.get("caption_suffix", "")
retry_keyword = captioner_config.get("retry_keyword", "")
retry_keywords = retry_keyword.split(",")
retry_keywords = [k.strip() for k in retry_keywords]
retry_keywords = [k for k in retry_keywords if k != ""]
pre_images = []
for root, dirs, files in os.walk(directory):
@@ -90,6 +97,8 @@ def image_interrogator(args_dict):
})
result = []
pb = mz_prompt_utils.Utils.progress_bar(len(pre_images))
for i in range(len(pre_images)):
pre_image = pre_images[i]
image_path = pre_image["image_path"]
@@ -104,21 +113,52 @@ def image_interrogator(args_dict):
if i < len(pre_images) - 1:
onec_args_dict["keep_device"] = True
mz_prompt_webserver.show_toast_success(
f"正在处理图片 {image_path}...", 1000)
pb.update(
i,
len(pre_images),
pil_image.copy(),
)
response = image_interrogator(onec_args_dict)
response = response.strip()
with open(caption_file, "w") as f:
f.write(response)
is_retry = response == ""
for k in retry_keywords:
if response.find(k) != -1:
print(f"存在需要重试的关键词 ; Retry keyword found: {k}")
is_retry = True
break
mz_prompt_utils.Utils.print_log(
"\n\nonec_args_dict: ", onec_args_dict)
if is_retry:
for retry_n in range(5):
print(f"Retry {retry_n+1}...")
onec_args_dict["seed"] = onec_args_dict["seed"] + 1
response = image_interrogator(onec_args_dict)
response = response.strip()
is_retry = response == ""
for k in retry_keywords:
if response.find(k) != -1:
print(f"存在需要重试的关键词 ; Retry keyword found: {k}")
is_retry = True
break
if is_retry is False:
break
if is_retry:
print(f"重试失败,图片被跳过 ; Retry failed")
response = ""
if response != "":
with open(caption_file, "w") as f:
f.write(response)
result.append(response)
mz_prompt_webserver.show_toast_success(
f"提示词保存成功(prompt saved successfully): {caption_file}",
1000,
)
# mz_prompt_webserver.show_toast_success(
# f"提示词保存成功(prompt saved successfully): {caption_file}",
# 1000,
# )
return result
@@ -184,15 +224,25 @@ def base_image_interrogator(args_dict):
options = args_dict.get("llama_cpp_options", {})
options["seed"] = seed
mz_prompt_utils.Utils.print_log(
"base_image_interrogator options: ", options)
# input("Press Enter to continue...")
image = mz_prompt_utils.Utils.resize_max(image, resolution, resolution)
customize_instruct = args_dict.get("customize_instruct", None)
if customize_instruct is None:
system_text = mz_prompts.GPT4VImageCaptioner_System
question_text = mz_prompts.GPT4VImageCaptioner_Prompt
response = mz_llama_cpp.llava_cpp_simple_interrogator(
model_file=model_file,
mmproj_file=mmproj_file,
image=image,
options=options,
# system=system_text,
# question=question_text,
)
else:
@@ -226,18 +276,17 @@ def base_image_interrogator(args_dict):
response = full_response
sd_format = args_dict.get("sd_format", "v1")
if sd_format == "v1" and customize_instruct is None:
mz_prompt_utils.Utils.print_log(f"response: {response}")
schema = mz_llama_cpp.get_schema_obj(
mz_prompt_utils.Utils.print_log(f"response v1: {response}")
schema = mz_llama_core_nodes.get_schema_obj(
keys_type={
"short_describes": mz_llama_cpp.get_schema_base_type("string"),
"subject_tags": mz_llama_cpp.get_schema_array("string"),
"action_tags": mz_llama_cpp.get_schema_array("string"),
"light_tags": mz_llama_cpp.get_schema_array("string"),
"scenes_tags": mz_llama_cpp.get_schema_array("string"),
"other_tags": mz_llama_cpp.get_schema_array("string"),
"short_describes": mz_llama_core_nodes.get_schema_base_type("string"),
"subject_tags": mz_llama_core_nodes.get_schema_array("string"),
"action_tags": mz_llama_core_nodes.get_schema_array("string"),
"light_tags": mz_llama_core_nodes.get_schema_array("string"),
"scenes_tags": mz_llama_core_nodes.get_schema_array("string"),
"other_tags": mz_llama_core_nodes.get_schema_array("string"),
},
required=[
"short_describes",
+3 -3
View File
@@ -3,9 +3,9 @@ import sys
import json
import subprocess
import mz_prompt_utils
import mz_llama_cpp
import mz_prompts
from . import mz_prompt_utils
from . import mz_llama_cpp
from . import mz_prompts
def zhipu_json_fix(input_data):
+84
View File
@@ -165,6 +165,12 @@ class Utils:
os.makedirs(models_path, exist_ok=True)
return models_path
def get_gguf_models_path():
models_path = os.path.join(
folder_paths.models_dir, "gguf")
os.makedirs(models_path, exist_ok=True)
return models_path
def translate_text(text, from_code, to_code):
try:
import argostranslate
@@ -549,6 +555,84 @@ class Utils:
save_path = Utils.download_file(url, save_path)
return save_path
def progress_bar(steps):
class pb:
def __init__(self, steps):
self.steps = steps
self.pbar = comfy.utils.ProgressBar(steps)
def update(self, step, total_steps, pil_img):
self.pbar.update_absolute(
step, total_steps, ("JPEG", pil_img, 512))
return pb(steps)
def split_en_to_zh(text):
# 中文标点转英文标点
text = text.replace(",", ",")
text = text.replace("。", ".")
text = text.replace("?", "?")
text = text.replace("!", "!")
text = text.replace(";", ";")
result = []
if text.find("\n") != -1:
text = text.split("\n")
for t in text:
if t != "":
result.append(Utils.split_en_to_zh(t))
return "\n".join(result)
if text.find(".") != -1:
text = text.split(".")
for t in text:
if t != "":
result.append(Utils.split_en_to_zh(t))
return ". ".join(result)
if text.find("?") != -1:
text = text.split("?")
for t in text:
if t != "":
result.append(Utils.split_en_to_zh(t))
return "? ".join(result)
if text.find("!") != -1:
text = text.split("!")
for t in text:
if t != "":
result.append(Utils.split_en_to_zh(t))
return "! ".join(result)
if text.find(";") != -1:
text = text.split(";")
for t in text:
if t != "":
result.append(Utils.split_en_to_zh(t))
return "; ".join(result)
if text.find(",") != -1:
text = text.split(",")
for t in text:
if t != "":
result.append(Utils.split_en_to_zh(t))
return ", ".join(result)
return Utils.en2zh(text)
def to_debug_prompt(p):
if p is None:
return ""
zh = Utils.en2zh(p)
p = p.strip()
return f"""
原文:
{p}
中文翻译:
{zh}
"""
modelscope_models_map = {
"llama3": {
+1 -1
View File
@@ -2,7 +2,7 @@
import asyncio
import uuid
import mz_prompt_utils
from . import mz_prompt_utils
web_msg_pool = {
+18
View File
@@ -18,3 +18,21 @@ Standardize_Prompt = """
Extract the content about Stable Diffusion style from the following input and combine it into a json array. Note that the output will be directly used in the program.
Please output the standardized json content.
"""
GPT4VImageCaptioner_System = """
As an AI image tagging expert, please provide precise tags for these images to enhance CLIP model's understanding of the content.
"""
# 来自https://github.com/jiayev/GPT4V-Image-Captioner
GPT4VImageCaptioner_Prompt = """
Employ succinct keywords or phrases, steering clear of elaborate sentences and extraneous conjunctions.
Prioritize the tags by relevance.
Your tags should capture key elements such as the main subject, setting, artistic style, composition, image quality, color tone, filter, and camera specifications, and any other tags crucial for the image.
When tagging photos of people, include specific details like gender, nationality, attire, actions, pose, expressions, accessories, makeup, composition type, age, etc.
For other image categories, apply appropriate and common descriptive tags as well.
Recognize and tag any celebrities, well-known landmark or IPs if clearly featured in the image.
Your tags should be accurate, non-duplicative, and within a 20-75 word count range.
These tags will use for image re-creation, so the closer the resemblance to the original image, the better the tag quality.
Tags should be comma-separated.
"""
+196
View File
@@ -0,0 +1,196 @@
from ..mz_prompt_utils import Utils
NODE_CLASS_MAPPINGS = {
}
NODE_DISPLAY_NAME_MAPPINGS = {
}
import importlib
from . import mz_llama3
from . import mz_phi3
from .. import mz_llama_cpp
from .. import mz_llama_core_nodes
from . import mz_deprecated
AUTHOR_NAME = u"MinusZone"
CATEGORY_NAME = f"{AUTHOR_NAME} - Prompt/v1"
def getCommonCLIPTextEncodeInput():
style_presets = mz_llama_core_nodes.get_style_presets()
CommonCLIPTextEncodeInput = {
"required": {
"prompt_version": (["v1"], {"default": "v1"}),
"style_presets": (
style_presets, {"default": style_presets[1]}
),
"text": ("STRING", {"multiline": True, }),
"keep_device": ([False, True], {"default": False}),
"seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}),
},
"optional": {
"clip": ("CLIP", ),
"llama_cpp_options": ("LLamaCPPOptions", ),
"customize_instruct": ("CustomizeInstruct", ),
# "customize_json_schema": ("STRING", ),
}
}
return CommonCLIPTextEncodeInput
class MZ_LLama3CLIPTextEncode:
@classmethod
def INPUT_TYPES(s):
m_models = mz_llama3.llama3_models.copy()
for i in range(len(m_models)):
if mz_llama3.get_exist_model(m_models[i]) is not None:
m_models[i] += "[downloaded]"
result = {
"required": {
"llama_cpp_model": (m_models, {"default": m_models[0]}),
"download_source": (
["none", "modelscope", "hf-mirror.com",],
{"default": "none"}
),
},
"optional": {},
}
common_input = getCommonCLIPTextEncodeInput()
for key in common_input["required"]:
result["required"][key] = common_input["required"][key]
for key in common_input["optional"]:
result["optional"][key] = common_input["optional"][key]
return result
RETURN_TYPES = ("STRING", "CONDITIONING",)
RETURN_NAMES = ("text", "conditioning",)
OUTPUT_NODE = True
FUNCTION = "encode"
CATEGORY = CATEGORY_NAME
def encode(self, **kwargs):
kwargs["llama_cpp_model"] = kwargs.get(
"llama_cpp_model", "").replace("[downloaded]", "")
text = mz_llama3.query_beautify_prompt_text(kwargs)
conditionings = None
clip = kwargs.get("clip", None)
if clip is not None:
conditionings = Utils.a1111_clip_text_encode(clip, text, )
return {"ui": {"string": [Utils.to_debug_prompt(text),]}, "result": (text, conditionings)}
NODE_CLASS_MAPPINGS["MZ_LLama3CLIPTextEncode"] = MZ_LLama3CLIPTextEncode
NODE_DISPLAY_NAME_MAPPINGS[
"MZ_LLama3CLIPTextEncode"] = f"{AUTHOR_NAME} - deprecated - CLIPTextEncode(LLama3)"
class MZ_Phi3CLIPTextEncode:
@classmethod
def INPUT_TYPES(s):
m_models = mz_phi3.phi3_models.copy()
for i in range(len(m_models)):
if mz_llama3.get_exist_model(m_models[i]) is not None:
m_models[i] += "[downloaded]"
importlib.reload(mz_phi3)
result = {
"required": {
"llama_cpp_model": (m_models, {"default": m_models[0]}),
"download_source": (
["none", "modelscope", "hf-mirror.com",],
{"default": "none"}
),
},
"optional": {},
}
common_input = getCommonCLIPTextEncodeInput()
for key in common_input["required"]:
result["required"][key] = common_input["required"][key]
for key in common_input["optional"]:
result["optional"][key] = common_input["optional"][key]
return result
RETURN_TYPES = ("STRING", "CONDITIONING",)
RETURN_NAMES = ("text", "conditioning",)
OUTPUT_NODE = True
FUNCTION = "encode"
CATEGORY = CATEGORY_NAME
def encode(self, **kwargs):
kwargs = kwargs.copy()
importlib.reload(mz_llama3)
kwargs["llama_cpp_model"] = kwargs.get(
"llama_cpp_model", "").replace("[downloaded]", "")
text = mz_phi3.query_beautify_prompt_text(kwargs)
conditionings = None
clip = kwargs.get("clip", None)
if clip is not None:
conditionings = Utils.a1111_clip_text_encode(clip, text, )
return {"ui": {"string": [Utils.to_debug_prompt(text),]}, "result": (text, conditionings)}
NODE_CLASS_MAPPINGS["MZ_Phi3CLIPTextEncode"] = MZ_Phi3CLIPTextEncode
NODE_DISPLAY_NAME_MAPPINGS[
"MZ_Phi3CLIPTextEncode"] = f"{AUTHOR_NAME} - deprecated - CLIPTextEncode(Phi3)"
class MZ_BaseLLamaCPPCLIPTextEncode:
@classmethod
def INPUT_TYPES(s):
importlib.reload(mz_llama_cpp)
result = {
"required": {
"llama_cpp_model": ("STRING", {"default": "", "placeholder": "model_path"}),
},
"optional": {
},
}
common_input = getCommonCLIPTextEncodeInput()
for key in common_input["required"]:
result["required"][key] = common_input["required"][key]
for key in common_input["optional"]:
result["optional"][key] = common_input["optional"][key]
return result
RETURN_TYPES = ("STRING", "CONDITIONING",)
RETURN_NAMES = ("text", "conditioning",)
OUTPUT_NODE = True
FUNCTION = "encode"
CATEGORY = CATEGORY_NAME
def encode(self, **kwargs):
kwargs = kwargs.copy()
kwargs["llama_cpp_model"] = kwargs.get(
"llama_cpp_model", "").replace("[downloaded]", "")
text = mz_deprecated.base_query_beautify_prompt_text(kwargs)
conditionings = None
clip = kwargs.get("clip", None)
if clip is not None:
conditionings = Utils.a1111_clip_text_encode(clip, text, )
return {"ui": {"string": [Utils.to_debug_prompt(text),]}, "result": (text, conditionings)}
NODE_CLASS_MAPPINGS["MZ_BaseLLamaCPPCLIPTextEncode"] = MZ_BaseLLamaCPPCLIPTextEncode
NODE_DISPLAY_NAME_MAPPINGS[
"MZ_BaseLLamaCPPCLIPTextEncode"] = f"{AUTHOR_NAME} - deprecated - CLIPTextEncode(BaseLLamaCPP)"
+160
View File
@@ -0,0 +1,160 @@
from ..mz_prompt_utils import Utils
from ..mz_llama_cpp import *
from ..mz_llama_core_nodes import *
from ..mz_prompts import *
def base_query_beautify_prompt_text(args_dict):
model_file = args_dict.get("llama_cpp_model", "")
text = args_dict.get("text", "")
style_presets = args_dict.get("style_presets", "")
options = args_dict.get("llama_cpp_options", {})
keep_device = args_dict.get("keep_device", False)
seed = args_dict.get("seed", -1)
options["seed"] = seed
customize_instruct = args_dict.get("customize_instruct", None)
Utils.print_log(
f"customize_instruct: {customize_instruct}")
try:
schema = None
if customize_instruct is None:
schema = get_schema_obj(
keys_type={
"description": get_schema_base_type("string"),
"long_prompt": get_schema_base_type("string"),
"main_color_word": get_schema_base_type("string"),
"camera_angle_word": get_schema_base_type("string"),
"style_words": get_schema_array("string"),
"subject_words": get_schema_array("string"),
"light_words": get_schema_array("string"),
"environment_words": get_schema_array("string"),
},
required=[
"description",
"long_prompt",
"main_color_word",
"camera_angle_word",
"style_words",
"subject_words",
"light_words",
"environment_words",
]
)
question = f"IDEA: {style_presets},{text}"
if style_presets == "none":
question = f"IDEA: {text}"
system_prompt = mz_prompts.Beautify_Prompt + mz_prompts.Long_prompt + "\n"
else:
system_prompt = customize_instruct.get("system", "")
question = customize_instruct.get("instruct", "%text%")
system_prompt = system_prompt.replace("%text%", text)
question = question.replace("%text%", text)
Utils.print_log(f"system_prompt: {system_prompt}")
Utils.print_log(f"question: {question}")
if schema is not None:
response_json = llama_cpp_simple_interrogator_to_json(
model_file=model_file,
system=system_prompt,
question=question,
schema=schema,
options=options,
)
Utils.print_log(f"response_json: {response_json}")
response = json.loads(response_json)
full_responses = []
if response["description"] != "":
full_responses.append(f"({response['description']})")
if response["long_prompt"] != "":
full_responses.append(f"({response['long_prompt']})")
if response["main_color_word"] != "":
full_responses.append(f"({response['main_color_word']})")
if response["camera_angle_word"] != "":
full_responses.append(f"({response['camera_angle_word']})")
response["style_words"] = [
x for x in response["style_words"] if x != ""]
if len(response["style_words"]) > 0:
full_responses.append(
f"({', '.join(response['style_words'])})")
response["subject_words"] = [
x for x in response["subject_words"] if x != ""]
if len(response["subject_words"]) > 0:
full_responses.append(
f"({', '.join(response['subject_words'])})")
response["light_words"] = [
x for x in response["light_words"] if x != ""]
if len(response["light_words"]) > 0:
full_responses.append(
f"({', '.join(response['light_words'])})")
response["environment_words"] = [
x for x in response["environment_words"] if x != ""]
if len(response["environment_words"]) > 0:
full_responses.append(
f"({', '.join(response['environment_words'])})")
full_response = ", ".join(full_responses)
else:
full_response = llama_cpp_simple_interrogator(
model_file=model_file,
system=system_prompt,
question=question,
options=options,
)
start_str = customize_instruct.get("start_str", "")
if start_str != "" and full_response.find(start_str) != -1:
full_response_list = full_response.split(start_str)
# 删除第一个元素
full_response_list.pop(0)
full_response = start_str.join(full_response_list)
end_str = customize_instruct.get("end_str", "")
if end_str != "" and full_response.find(end_str) != -1:
full_response_list = full_response.split(end_str)
# 删除最后一个元素
full_response_list.pop()
full_response = end_str.join(full_response_list)
if keep_device is False:
freed_gpu_memory(model_file=model_file)
# 去除换行
while full_response.find("\n") != -1:
full_response = full_response.replace("\n", " ")
# 句号换成逗号
while full_response.find(".") != -1:
full_response = full_response.replace(".", ",")
# 去除多余逗号
while full_response.find(",,") != -1:
full_response = full_response.replace(",,", ",")
while full_response.find(", ,") != -1:
full_response = full_response.replace(", ,", ",")
full_response = Utils.prompt_zh_to_en(full_response)
style_presets_prompt_text = style_presets_prompt.get(style_presets, "")
if style_presets_prompt_text != "":
full_response = f"{style_presets_prompt_text}, {full_response}"
return full_response
except Exception as e:
freed_gpu_memory(model_file=model_file)
# mz_utils.Utils.print_log(f"Error in auto_prompt_text: {e}")
raise e
+4 -8
View File
@@ -1,7 +1,7 @@
import json
import mz_prompt_utils
import mz_llama_cpp
from .. import mz_prompt_utils
from .. import mz_llama_cpp
from . import mz_deprecated
import importlib
@@ -46,10 +46,6 @@ def query_beautify_prompt_text(args_dict):
model_name = args_dict.get("llama_cpp_model", "")
download_source = args_dict.get("download_source", None)
import mz_prompts
importlib.reload(mz_prompts)
importlib.reload(mz_llama_cpp)
try:
model_file = get_exist_model(model_name)
@@ -66,7 +62,7 @@ def query_beautify_prompt_text(args_dict):
model_file = mz_prompt_utils.Utils.hf_download_model(model_url)
args_dict["llama_cpp_model"] = model_file
full_response = mz_llama_cpp.base_query_beautify_prompt_text(
full_response = mz_deprecated.base_query_beautify_prompt_text(
args_dict=args_dict)
return full_response
+4 -10
View File
@@ -1,9 +1,7 @@
import json
import mz_prompt_utils
import mz_llama_cpp
import importlib
from .. import mz_prompt_utils
from .. import mz_llama_cpp
from . import mz_deprecated
phi3_models = [
"Phi-3-mini-4k-instruct-q4.gguf"
@@ -33,10 +31,6 @@ def query_beautify_prompt_text(args_dict):
model_name = args_dict.get("llama_cpp_model", "")
download_source = args_dict.get("download_source", None)
import mz_prompts
importlib.reload(mz_prompts)
importlib.reload(mz_llama_cpp)
try:
model_file = get_exist_model(model_name)
@@ -53,7 +47,7 @@ def query_beautify_prompt_text(args_dict):
model_file = mz_prompt_utils.Utils.hf_download_model(model_url)
args_dict["llama_cpp_model"] = model_file
full_response = mz_llama_cpp.base_query_beautify_prompt_text(
full_response = mz_deprecated.base_query_beautify_prompt_text(
args_dict=args_dict)
return full_response
+11 -6
View File
@@ -41,25 +41,26 @@ async function waitMessage() {
};
websocket.onclose = async (event) => {
console.log("Connection closed", event);
await sleep(1000);
await waitMessage();
};
websocket.onerror = async (event) => {
console.log("Connection error", event);
await sleep(1000);
await waitMessage();
};
for (;;) {
await sleep(1000);
try {
if (websocket.readyState !== WebSocket.OPEN) {
return;
}
websocket.send(
JSON.stringify({
type: "ping",
})
);
} catch (error) {}
} catch (error) {
return;
}
}
}
@@ -88,6 +89,7 @@ const my_ui = {
case "MZ_BaseLLamaCPPCLIPTextEncode":
case "MZ_LLavaImageInterrogator":
case "MZ_BaseLLavaImageInterrogator":
case "MZ_LLamaCPPCLIPTextEncode":
// Node Created
const onNodeCreated = nodeType.prototype.onNodeCreated;
nodeType.prototype.onNodeCreated = function () {
@@ -95,6 +97,7 @@ const my_ui = {
? onNodeCreated.apply(this, arguments)
: undefined;
console.log("onNodeCreated:", this);
const nodeName = this.name + "_" + "customtext";
const wi = ComfyWidgets.STRING(
this,
@@ -133,12 +136,14 @@ const my_ui = {
const onConfigure = nodeType.prototype.onConfigure;
nodeType.prototype.onConfigure = function (w) {
onConfigure?.apply(this, arguments);
// outSet.call(this, a?.string);
};
// onExecuted
const onExecuted = nodeType.prototype.onExecuted;
nodeType.prototype.onExecuted = function (a, b) {
console.log("onExecuted:", arguments);
// console.log("onExecuted:", arguments);
onExecuted?.apply(this, arguments);
outSet.call(this, a?.string);