This commit is contained in:
wailovet
2024-04-27 23:39:41 +08:00
parent 29439f3345
commit ca970efedd
5 changed files with 376 additions and 318 deletions
+120 -158
View File
@@ -32,29 +32,10 @@ import mz_llava
llama3_models = [
"Meta-Llama-3-8B-Instruct.Q4_K_M.gguf",
"Meta-Llama-3-8B-Instruct.Q2_K.gguf",
"Meta-Llama-3-8B-Instruct.Q3_K_L.gguf",
"Meta-Llama-3-8B-Instruct.Q3_K_M.gguf",
"Meta-Llama-3-8B-Instruct.Q3_K_S.gguf",
"Meta-Llama-3-8B-Instruct.Q4_0.gguf",
"Meta-Llama-3-8B-Instruct.Q4_1.gguf",
"Meta-Llama-3-8B-Instruct.Q4_K_S.gguf",
"Meta-Llama-3-8B-Instruct.Q5_0.gguf",
"Meta-Llama-3-8B-Instruct.Q5_1.gguf",
"Meta-Llama-3-8B-Instruct.Q5_K_M.gguf",
"Meta-Llama-3-8B-Instruct.Q5_K_S.gguf",
"Meta-Llama-3-8B-Instruct.Q6_K.gguf",
"Meta-Llama-3-8B-Instruct.Q8_0.gguf",
]
class MZ_LLamaCPPOptions:
@classmethod
def INPUT_TYPES(s):
opt = mz_llama_cpp.LlamaCppOptions()
value = opt.value
value = mz_llama_cpp.LlamaCppOptions()
result = {}
for key in value:
@@ -66,6 +47,8 @@ class MZ_LLamaCPPOptions:
result[key] = ("FLOAT", {"default": value[key], "min": 0, "max": 0xffffffffffffffff})
elif type(value[key]) == str:
result[key] = ("STRING", {"default": value[key]})
elif type(value[key]) == list:
result[key] = (value[key], {"default": value[key][0]})
else:
raise Exception(f"Unknown type: {type(value[key])}")
@@ -78,59 +61,79 @@ class MZ_LLamaCPPOptions:
FUNCTION = "create"
CATEGORY = CATEGORY_NAME
def create(self, **kwargs):
importlib.reload(mz_llama_cpp)
opt = mz_llama_cpp.LlamaCppOptions()
importlib.reload(mz_llama_cpp)
opt = {}
for key in kwargs:
opt.value[key] = kwargs[key]
opt[key] = kwargs[key]
return (opt,)
NODE_CLASS_MAPPINGS["MZ_LLamaCPPOptions"] = MZ_LLamaCPPOptions
NODE_DISPLAY_NAME_MAPPINGS["MZ_LLamaCPPOptions"] = f"{AUTHOR_NAME} - LLamaCPPOptions"
def getCommonCLIPTextEncodeInput():
style_presets = mz_llama_cpp.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", ),
}
}
return CommonCLIPTextEncodeInput
class MZ_LLama3CLIPTextEncode:
@classmethod
def INPUT_TYPES(s):
m_llama3_models = llama3_models.copy()
m_llama3_models = mz_llama3.llama3_models.copy()
for i in range(len(m_llama3_models)):
if mz_llama3.get_exist_model(m_llama3_models[i]) is not None:
m_llama3_models[i] += "[downloaded]"
importlib.reload(mz_llama_cpp)
style_presets = mz_llama_cpp.get_style_presets()
return {
result = {
"required": {
"llama_cpp_model": (m_llama3_models, {"default": m_llama3_models[0]}),
"download_source": (
["none", "modelscope", "hf-mirror.com",],
{"default": "none"}
),
"style_presets": (
style_presets, {"default": style_presets[0]}
),
"text": ("STRING", {"multiline": True,}),
"seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}),
"n_gpu_layers": ("INT", {"default": 40, "min": -1, "max": 0xffffffffffffffff}),
},
"optional": {
"clip": ("CLIP", ),
"llama_cpp_options": ("LLamaCPPOptions", ),
},
"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",)
FUNCTION = "encode"
CATEGORY = CATEGORY_NAME
def encode(self, text, llama_cpp_model, style_presets, clip=None, download_source=None, seed=0, n_gpu_layers=40, llama_cpp_options=None):
def encode(self, **kwargs):
importlib.reload(mz_llama3)
llama_cpp_model = llama_cpp_model.replace("[downloaded]", "")
options = {}
if llama_cpp_options is not None:
options = llama_cpp_options.value
text = mz_llama3.query_beautify_prompt_text(llama_cpp_model, n_gpu_layers, text, style_presets, download_source, options)
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:
tokens = clip.tokenize(text)
cond, pooled = clip.encode_from_tokens(tokens, return_pooled=True,)
@@ -139,34 +142,62 @@ class MZ_LLama3CLIPTextEncode:
return (text, conditionings)
NODE_CLASS_MAPPINGS["MZ_LLama3CLIPTextEncode"] = MZ_LLama3CLIPTextEncode
NODE_DISPLAY_NAME_MAPPINGS["MZ_LLama3CLIPTextEncode"] = f"{AUTHOR_NAME} - LLama3CLIPTextEncode"
NODE_DISPLAY_NAME_MAPPINGS["MZ_LLama3CLIPTextEncode"] = f"{AUTHOR_NAME} - CLIPTextEncode(LLama3)"
LLava_models = [
"ggml_bakllava-1/ggml-model-q4_k.gguf",
"ggml_bakllava-1/ggml-model-q5_k.gguf",
"ggml_bakllava-1/ggml-model-f16.gguf",
"ggml_llava-v1.5-7b/ggml-model-q4_k.gguf",
"ggml_llava-v1.5-7b/ggml-model-q5_k.gguf",
"ggml_llava-v1.5-7b/ggml-model-f16.gguf",
]
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",)
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:
tokens = clip.tokenize(text)
cond, pooled = clip.encode_from_tokens(tokens, return_pooled=True,)
conditionings = [[cond, {"pooled_output": pooled}]]
return (text, conditionings)
NODE_CLASS_MAPPINGS["MZ_BaseLLamaCPPCLIPTextEncode"] = MZ_BaseLLamaCPPCLIPTextEncode
NODE_DISPLAY_NAME_MAPPINGS["MZ_BaseLLamaCPPCLIPTextEncode"] = f"{AUTHOR_NAME} - CLIPTextEncode(BaseLLamaCPP)"
LLava_mmproj_models = [
# "llava-1.6-mistral-7b-gguf/mmproj-model-f16.gguf",
"ggml_bakllava-1/mmproj-model-f16.gguf",
"ggml_llava-v1.5-7b/mmproj-model-f16.gguf",
]
class MZ_LLavaImageInterrogator:
@classmethod
def INPUT_TYPES(s):
m_llava_models = LLava_models.copy()
importlib.reload(mz_llava)
m_llava_models = mz_llava.LLava_models.copy()
for i in range(len(m_llava_models)):
if mz_llava.get_exist_model(m_llava_models[i]) is not None:
m_llava_models[i] += "[downloaded]"
m_llava_mmproj_models = LLava_mmproj_models.copy()
m_llava_mmproj_models = mz_llava.LLava_mmproj_models.copy()
for i in range(len(m_llava_mmproj_models)):
if mz_llava.get_exist_model(m_llava_mmproj_models[i]) is not None:
m_llava_mmproj_models[i] += "[downloaded]"
@@ -185,9 +216,9 @@ class MZ_LLavaImageInterrogator:
),
"image": ("IMAGE",),
"resolution": ("INT", {"default": 512, "min": 128, "max": 2048}),
# "prefix" : ("STRING", {"default": "(masterpiece)", "multiline": True, }),
"seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}),
"n_gpu_layers": ("INT", {"default": 40, "min": -1, "max": 0xffffffffffffffff}),
"sd_format": (["none","v1"], {"default": "none"}),
"keep_device": ([False,True], {"default": False}),
"seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}),
},
"optional": {
"clip": ("CLIP", ),
@@ -196,33 +227,17 @@ class MZ_LLavaImageInterrogator:
RETURN_TYPES = ("STRING", "CONDITIONING",)
FUNCTION = "interrogate"
CATEGORY = CATEGORY_NAME
def interrogate(self, llama_cpp_model, mmproj_model, image, resolution, download_source=None, seed=0, clip=None, n_gpu_layers=40, llama_cpp_options=None):
def interrogate(self, **kwargs):
importlib.reload(mz_llava)
llama_cpp_model = llama_cpp_model.replace("[downloaded]", "")
mmproj_model = mmproj_model.replace("[downloaded]", "")
kwargs["llama_cpp_model"] = kwargs.get("llama_cpp_model", "").replace("[downloaded]", "")
kwargs["mmproj_model"] = kwargs.get("mmproj_model", "").replace("[downloaded]", "")
kwargs["image"] = Utils.tensor2pil(kwargs["image"])
# prefix = Utils.prompt_zh_to_en(prefix)
# if not prefix.endswith(","):
# prefix += ","
image_pil = Utils.tensor2pil(image)
options = {}
if llama_cpp_options is not None:
options = llama_cpp_options.value
response = mz_llava.image_interrogator(
llama_cpp_model,
mmproj_model,
n_gpu_layers,
image_pil,
resolution,
download_source,
options,
)
response = mz_llava.image_interrogator(kwargs)
conditionings = None
clip = kwargs.get("clip", None)
if clip is not None:
tokens = clip.tokenize(response)
cond, pooled = clip.encode_from_tokens(tokens, return_pooled=True,)
@@ -230,7 +245,7 @@ class MZ_LLavaImageInterrogator:
return (response, conditionings)
NODE_CLASS_MAPPINGS["MZ_LLavaImageInterrogator"] = MZ_LLavaImageInterrogator
NODE_DISPLAY_NAME_MAPPINGS["MZ_LLavaImageInterrogator"] = f"{AUTHOR_NAME} - LLavaImageInterrogator"
NODE_DISPLAY_NAME_MAPPINGS["MZ_LLavaImageInterrogator"] = f"{AUTHOR_NAME} - ImageInterrogator(LLava)"
class MZ_BaseLLavaImageInterrogator:
@classmethod
def INPUT_TYPES(s):
@@ -239,8 +254,9 @@ class MZ_BaseLLavaImageInterrogator:
"mmproj_model": ("STRING", {"default": ""}),
"image": ("IMAGE",),
"resolution": ("INT", {"default": 512, "min": 128, "max": 2048}),
"seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}),
"n_gpu_layers": ("INT", {"default": 40, "min": -1, "max": 0xffffffffffffffff}),
"sd_format": (["none","v1"], {"default": "none"}),
"keep_device": ([False,True], {"default": False}),
"seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}),
},
"optional": {
"clip": ("CLIP", ),
@@ -249,25 +265,14 @@ class MZ_BaseLLavaImageInterrogator:
RETURN_TYPES = ("STRING", "CONDITIONING",)
FUNCTION = "interrogate"
CATEGORY = CATEGORY_NAME
def interrogate(self, llama_cpp_model, mmproj_model, image, resolution, seed=0, clip=None, n_gpu_layers=40, llama_cpp_options=None):
def interrogate(self, **kwargs):
importlib.reload(mz_llava)
kwargs["image"] = Utils.tensor2pil(kwargs["image"])
image_pil = Utils.tensor2pil(image)
options = {}
if llama_cpp_options is not None:
options = llama_cpp_options.value
response = mz_llava.base_image_interrogator(
llama_cpp_model,
mmproj_model,
n_gpu_layers,
image_pil,
resolution,
options,
)
response = mz_llava.base_image_interrogator(kwargs)
conditionings = None
clip = kwargs.get("clip", None)
if clip is not None:
tokens = clip.tokenize(response)
cond, pooled = clip.encode_from_tokens(tokens, return_pooled=True,)
@@ -275,9 +280,11 @@ class MZ_BaseLLavaImageInterrogator:
return (response, conditionings)
NODE_CLASS_MAPPINGS["MZ_BaseLLavaImageInterrogator"] = MZ_BaseLLavaImageInterrogator
NODE_DISPLAY_NAME_MAPPINGS["MZ_BaseLLavaImageInterrogator"] = f"{AUTHOR_NAME} - BaseLLavaImageInterrogator"
NODE_DISPLAY_NAME_MAPPINGS["MZ_BaseLLavaImageInterrogator"] = f"{AUTHOR_NAME} - ImageInterrogator(BaseLLava)"
class MZ_LLamaCPPInterrogator:
@classmethod
def INPUT_TYPES(s):
@@ -287,13 +294,13 @@ class MZ_LLamaCPPInterrogator:
"use_system": ([True, False], {"default": True}),
"system": ("STRING", {"multiline": True, "default": "You are a helpful assistant."}),
"prompt": ("STRING", {"multiline": True,}),
"n_gpu_layers": ("INT", {"default": 40, "min": -1, "max": 0xffffffffffffffff}),
"n_gpu_layers": ("INT", {"default": -1, "min": -1, "max": 0xffffffffffffffff}),
},
}
RETURN_TYPES = ("STRING",)
FUNCTION = "simple_interrogator"
CATEGORY = CATEGORY_NAME
def simple_interrogator(self, model_file, prompt, use_system=True, system="You are a helpful assistant.", n_gpu_layers=40):
def simple_interrogator(self, model_file, prompt, use_system=True, system="You are a helpful assistant.", n_gpu_layers=-1):
importlib.reload(mz_llama_cpp)
result = mz_llama_cpp.llama_cpp_simple_interrogator(
model_file,
@@ -304,51 +311,6 @@ class MZ_LLamaCPPInterrogator:
)
return (result,)
NODE_CLASS_MAPPINGS["MZ_LLamaCPPInterrogator"] = MZ_LLamaCPPInterrogator
NODE_DISPLAY_NAME_MAPPINGS["MZ_LLamaCPPInterrogator"] = f"{AUTHOR_NAME} - LLamaCPP simple interrogator"
class MZ_BaseLLamaCPPCLIPTextEncode:
@classmethod
def INPUT_TYPES(s):
importlib.reload(mz_llama_cpp)
style_presets = mz_llama_cpp.get_style_presets()
return {
"required": {
"llama_cpp_model": ("STRING", {"default": "", "placeholder": "model_path"}),
"style_presets": (
style_presets, {"default": style_presets[0]}
),
"text": ("STRING", {"multiline": True,}),
"seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}),
"n_gpu_layers": ("INT", {"default": 40, "min": -1, "max": 0xffffffffffffffff}),
},
"optional": {
"clip": ("CLIP", ),
"llama_cpp_options": ("LLamaCPPOptions", ),
},
}
RETURN_TYPES = ("STRING", "CONDITIONING",)
FUNCTION = "encode"
CATEGORY = CATEGORY_NAME
def encode(self, text, llama_cpp_model, style_presets, clip=None, seed=0, n_gpu_layers=40, llama_cpp_options=None):
importlib.reload(mz_llama3)
options = {}
if llama_cpp_options is not None:
options = llama_cpp_options.value
text = mz_llama_cpp.base_query_beautify_prompt_text(llama_cpp_model, n_gpu_layers, text, style_presets, options)
conditionings = None
if clip is not None:
tokens = clip.tokenize(text)
cond, pooled = clip.encode_from_tokens(tokens, return_pooled=True,)
conditionings = [[cond, {"pooled_output": pooled}]]
return (text, conditionings)
NODE_CLASS_MAPPINGS["MZ_BaseLLamaCPPCLIPTextEncode"] = MZ_BaseLLamaCPPCLIPTextEncode
NODE_DISPLAY_NAME_MAPPINGS["MZ_BaseLLamaCPPCLIPTextEncode"] = f"{AUTHOR_NAME} - BaseLLamaCPPCLIPTextEncode"
# NODE_CLASS_MAPPINGS["MZ_LLamaCPPInterrogator"] = MZ_LLamaCPPInterrogator
# NODE_DISPLAY_NAME_MAPPINGS["MZ_LLamaCPPInterrogator"] = f"{AUTHOR_NAME} - LLamaCPP simple interrogator"
+28 -97
View File
@@ -6,6 +6,26 @@ import importlib
llama3_models = [
"Meta-Llama-3-8B-Instruct.Q4_K_M.gguf",
"Meta-Llama-3-8B-Instruct.Q2_K.gguf",
"Meta-Llama-3-8B-Instruct.Q3_K_L.gguf",
"Meta-Llama-3-8B-Instruct.Q3_K_M.gguf",
"Meta-Llama-3-8B-Instruct.Q3_K_S.gguf",
"Meta-Llama-3-8B-Instruct.Q4_0.gguf",
"Meta-Llama-3-8B-Instruct.Q4_1.gguf",
"Meta-Llama-3-8B-Instruct.Q4_K_S.gguf",
"Meta-Llama-3-8B-Instruct.Q5_0.gguf",
"Meta-Llama-3-8B-Instruct.Q5_1.gguf",
"Meta-Llama-3-8B-Instruct.Q5_K_M.gguf",
"Meta-Llama-3-8B-Instruct.Q5_K_S.gguf",
"Meta-Llama-3-8B-Instruct.Q6_K.gguf",
"Meta-Llama-3-8B-Instruct.Q8_0.gguf",
]
def get_exist_model(model_name):
modelscope_model_path = mz_prompt_utils.Utils.modelscope_download_model(
model_type="llama3",
@@ -24,9 +44,10 @@ def get_exist_model(model_name):
return None
def query_beautify_prompt_text(model_name, n_gpu_layers, text, style_presets, download_source=None, options={}):
if options is None:
options = {}
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)
@@ -47,100 +68,10 @@ def query_beautify_prompt_text(model_name, n_gpu_layers, text, style_presets, do
if download_source == "hf-mirror.com":
model_url = f"https://hf-mirror.com/QuantFactory/Meta-Llama-3-8B-Instruct-GGUF/resolve/main/{model_name}"
model_file = mz_prompt_utils.Utils.hf_download_model(model_url)
schema = mz_llama_cpp.get_schema_obj(
keys_type={
"description": mz_llama_cpp.get_schema_base_type("string"),
"long_prompt": mz_llama_cpp.get_schema_base_type("string"),
"main_color_word": mz_llama_cpp.get_schema_base_type("string"),
"camera_angle_word": mz_llama_cpp.get_schema_base_type("string"),
"style_words": mz_llama_cpp.get_schema_array("string"),
"subject_words": mz_llama_cpp.get_schema_array("string"),
"light_words": mz_llama_cpp.get_schema_array("string"),
"environment_words": mz_llama_cpp.get_schema_array("string"),
},
required=[
"description",
"long_prompt",
"main_color_word",
"camera_angle_word",
"style_words",
"subject_words",
"light_words",
"environment_words",
]
)
options["max_tokens"] = options.get("max_tokens", 2048)
options["temperature"] = options.get("temperature", 1.6)
response_json = mz_llama_cpp.llama_cpp_simple_interrogator_to_json(
model_file=model_file,
n_gpu_layers=n_gpu_layers,
system=mz_prompts.Beautify_Prompt,
question=f"IDEA: {style_presets},{text}",
schema=schema,
options=options,
)
mz_prompt_utils.Utils.print_log(f"response_json: {response_json}")
response = json.loads(response_json)
mz_llama_cpp.freed_gpu_memory(model_file=model_file)
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)
# 去除换行
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 = mz_llama_cpp.style_presets_prompt.get(style_presets, "")
full_response = f"{style_presets_prompt_text}, {full_response}"
args_dict["llama_cpp_model"] = model_file
full_response = mz_llama_cpp.base_query_beautify_prompt_text(args_dict=args_dict)
return full_response
except Exception as e:
+133 -40
View File
@@ -4,6 +4,7 @@ import os
import shutil
import subprocess
import sys
import llama_cpp
import torch
try:
import mz_prompt_utils
@@ -11,11 +12,31 @@ except ImportError:
pass
class LlamaCppOptions:
value = {
"max_tokens": 2048,
"temperature": 1.6,
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():
@@ -93,11 +114,11 @@ def freed_gpu_memory(model_file):
mz_prompt_utils.Utils.cache_set(f"llama_cpp_model_and_opt_{model_file}", None)
def llama_cpp_messages(model_file, n_gpu_layers, chat_handler=None, messages=[], options={}):
def llama_cpp_messages(model_file, chat_handler=None, messages=[], options={}):
if options is None:
options = {}
print(f"Find local model file: {model_file}")
init_opts = ["n_ctx", "logits_all", "chat_format",]
init_opts = ["n_ctx", "logits_all", "chat_format", "n_gpu_layers"]
check_llama_cpp_requirements()
@@ -113,9 +134,7 @@ def llama_cpp_messages(model_file, n_gpu_layers, chat_handler=None, messages=[],
if model_and_opt.get("options").get(opt) != options.get(opt):
is_opts_changed = True
break
if model_and_opt.get("n_gpu_layers") != n_gpu_layers:
is_opts_changed = True
if model_and_opt is None or is_opts_changed:
print("llama_cpp: loading model...")
@@ -123,19 +142,32 @@ def llama_cpp_messages(model_file, n_gpu_layers, chat_handler=None, messages=[],
if os.environ.get("MZ_DEV", None) is not None:
verbose = True
split_mode_int = llama_cpp.LLAMA_SPLIT_MODE_LAYER
if options.get("split_mode", "LLAMA_SPLIT_MODE_LAYER") == "LLAMA_SPLIT_MODE_ROW":
split_mode_int = llama_cpp.LLAMA_SPLIT_MODE_ROW
elif options.get("split_mode", "LLAMA_SPLIT_MODE_LAYER") == "LLAMA_SPLIT_MODE_NONE":
split_mode_int = llama_cpp.LLAMA_SPLIT_MODE_NONE
model = Llama(
model_path=model_file,
n_gpu_layers=n_gpu_layers,
n_gpu_layers=options.get("n_gpu_layers", -1),
n_ctx=options.get("n_ctx", 2048),
n_batch=options.get("n_batch", 2048),
n_threads=options.get("n_threads", 0) if options.get("n_threads", 0) > 0 else None,
n_threads_batch=options.get("n_threads_batch", 0) if options.get("n_threads_batch", 0) > 0 else None,
main_gpu=options.get("main_gpu", 0),
split_mode=split_mode_int,
logits_all=options.get("logits_all", False),
chat_handler=chat_handler,
chat_format=options.get("chat_format", None),
seed=options.get("seed", -1),
verbose=verbose,
)
model_and_opt = {
"model": model,
"options": options,
"n_gpu_layers": n_gpu_layers,
}
mz_prompt_utils.Utils.cache_set(f"llama_cpp_model_and_opt_{model_file}", model_and_opt)
@@ -147,11 +179,47 @@ def llama_cpp_messages(model_file, n_gpu_layers, chat_handler=None, messages=[],
mz_prompt_utils.Utils.print_log(f"======================================================LLAMA_CPP======================================================")
# mz_utils.Utils.print_log("llama_cpp messages:", messages)
mz_prompt_utils.Utils.print_log("llama_cpp response_format:", response_format)
stop = options.get("stop", "")
if stop == "":
stop = []
else:
# 所有转译序列
escape_sequence = {
"\\n": "\n",
"\\t": "\t",
"\\r": "\r",
"\\b": "\b",
"\\f": "\f",
}
for key, value in escape_sequence.items():
stop = stop.replace(key, value)
stop = stop.split(",")
mirostat_mode = 0
if options.get("mirostat_mode", "none") == "mirostat":
mirostat_mode = 1
elif options.get("mirostat_mode", "none") == "mirostat_v2":
mirostat_mode = 2
output = model.create_chat_completion(
messages=messages,
max_tokens=options.get("max_tokens", None),
response_format=response_format,
temperature=options.get("temperature", 0.8),
max_tokens=options.get("max_tokens", 4096),
temperature=options.get("temperature", 1.6),
top_p=options.get("top_p", 0.95),
min_p=options.get("min_p", 0.05),
typical_p=options.get("typical_p", 1.0),
stop=stop,
frequency_penalty=options.get("frequency_penalty", 0.0),
presence_penalty=options.get("presence_penalty", 0.0),
repeat_penalty=options.get("repeat_penalty", 1.1),
top_k=options.get("top_k", 50),
tfs_z=options.get("tfs_z", 1.0),
mirostat_mode=mirostat_mode,
mirostat_tau=options.get("mirostat_tau", 5.0),
mirostat_eta=options.get("mirostat_eta", 0.1),
)
mz_prompt_utils.Utils.print_log(f"LLAMA_CPP: \n{output}")
choices = output.get("choices", [])
@@ -170,6 +238,8 @@ def get_schema_base_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",
@@ -187,7 +257,7 @@ def get_schema_array(item_type="string"):
def llama_cpp_simple_interrogator_to_json(model_file, n_gpu_layers, use_system=True, system=None, question="", schema={}, options={}):
def llama_cpp_simple_interrogator_to_json(model_file, use_system=True, system=None, question="", schema={}, options={}):
if system is None:
system = ""
messages = [
@@ -230,17 +300,13 @@ def llama_cpp_simple_interrogator_to_json(model_file, n_gpu_layers, use_system=T
options["response_format"] = response_format
options["chat_format"] = "chatml"
if options.get("temperature", None) is None:
options["temperature"] = 0.8
if options.get("max_tokens", None) is None:
options["max_tokens"] = 2048
options["chat_format"] = "chatml"
result = llama_cpp_messages(model_file, n_gpu_layers, None, messages, options=options)
result = llama_cpp_messages(model_file, None, messages, options=options)
result = result.replace("\n", " ")
return result
def llama_cpp_simple_interrogator(model_file, n_gpu_layers, use_system=True, system=None, question="", options={}):
def llama_cpp_simple_interrogator(model_file, use_system=True, system=None, question="", options={}):
if options is None:
options = {}
if system is None:
@@ -277,20 +343,20 @@ def llama_cpp_simple_interrogator(model_file, n_gpu_layers, use_system=True, sys
"content": question
},
]
return llama_cpp_messages(model_file, n_gpu_layers, None, messages, options=options)
return llama_cpp_messages(model_file, None, messages, options=options)
def llava_cpp_messages(model_file, n_gpu_layers, chat_handler, messages, options={}):
def llava_cpp_messages(model_file, chat_handler, messages, options={}):
if options is None:
options = {}
options["logits_all"] = True
options["n_ctx"] = max(2048, options.get("n_ctx", 2048))
return llama_cpp_messages(model_file, n_gpu_layers, chat_handler, messages, options)
return llama_cpp_messages(model_file, chat_handler, messages, options)
def llava_cpp_simple_interrogator(
model_file, mmproj_file, n_gpu_layers, system="You are an assistant who perfectly describes images.", question="Describe this image in detail please.",
model_file, mmproj_file, system="You are an assistant who perfectly describes images.", question="Describe this image in detail please.",
image=None, options={}):
if options is None:
options = {}
@@ -310,7 +376,7 @@ def llava_cpp_simple_interrogator(
if mmproj_file is not None:
chat_handler = Llava15ChatHandler(clip_model_path=mmproj_file)
return llava_cpp_messages(model_file, n_gpu_layers, chat_handler, [
return llava_cpp_messages(model_file, chat_handler, [
{
"role": "system",
"content": system,
@@ -322,24 +388,49 @@ def llava_cpp_simple_interrogator(
], 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(model_file, n_gpu_layers, text, style_presets, options={}):
if options is None:
options = {}
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)
@@ -367,22 +458,21 @@ def base_query_beautify_prompt_text(model_file, n_gpu_layers, text, style_preset
"environment_words",
]
)
options["max_tokens"] = options.get("max_tokens", 2048)
options["temperature"] = options.get("temperature", 1.6)
question = f"IDEA: {style_presets},{text}"
if style_presets == "none":
question = f"IDEA: {text}"
response_json = llama_cpp_simple_interrogator_to_json(
model_file=model_file,
n_gpu_layers=n_gpu_layers,
system=mz_prompts.Beautify_Prompt,
question=f"IDEA: {style_presets},{text}",
model_file=model_file,
system=mz_prompts.Beautify_Prompt + mz_prompts.Long_prompt + "\n",
question=question,
schema=schema,
options=options,
)
mz_prompt_utils.Utils.print_log(f"response_json: {response_json}")
response = json.loads(response_json)
freed_gpu_memory(model_file=model_file)
if keep_device is False:
freed_gpu_memory(model_file=model_file)
full_responses = []
@@ -437,7 +527,10 @@ def base_query_beautify_prompt_text(model_file, n_gpu_layers, text, style_preset
style_presets_prompt_text = style_presets_prompt.get(style_presets, "")
full_response = f"{style_presets_prompt_text}, {full_response}"
if style_presets_prompt_text != "":
full_response = f"{style_presets_prompt_text}, {full_response}"
return full_response
except Exception as e:
+94 -22
View File
@@ -5,6 +5,23 @@ import mz_llama_cpp
import importlib
import mz_prompts
LLava_models = [
"ggml_llava-v1.5-7b/ggml-model-q4_k.gguf",
"ggml_llava-v1.5-7b/ggml-model-q5_k.gguf",
"ggml_llava-v1.5-7b/ggml-model-f16.gguf",
"ggml_bakllava-1/ggml-model-q4_k.gguf",
"ggml_bakllava-1/ggml-model-q5_k.gguf",
"ggml_bakllava-1/ggml-model-f16.gguf",
]
LLava_mmproj_models = [
# "llava-1.6-mistral-7b-gguf/mmproj-model-f16.gguf",
"ggml_llava-v1.5-7b/mmproj-model-f16.gguf",
"ggml_bakllava-1/mmproj-model-f16.gguf",
]
huggingface_models_map = {
@@ -45,11 +62,10 @@ def get_exist_model(model_name):
return None
def image_interrogator(model_name, mmproj_name, n_gpu_layers, image, resolution, download_source=None, options={}):
if options is None:
options = {}
image = mz_prompt_utils.Utils.resize_max(image, resolution, resolution)
def image_interrogator(args_dict):
model_name = args_dict.get("llama_cpp_model", "")
mmproj_name = args_dict.get("mmproj_model", "")
download_source = args_dict.get("download_source", None)
model_file = get_exist_model(model_name)
mmproj_file = get_exist_model(mmproj_name)
@@ -93,34 +109,90 @@ def image_interrogator(model_name, mmproj_name, n_gpu_layers, image, resolution,
mmproj_file = mz_prompt_utils.Utils.hf_download_model(mmproj_url)
response = mz_llama_cpp.llava_cpp_simple_interrogator(
model_file=model_file,
mmproj_file=mmproj_file,
n_gpu_layers=n_gpu_layers,
image=image,
options=options,
)
mz_llama_cpp.freed_gpu_memory(model_file=model_file)
args_dict["llama_cpp_model"] = model_file
args_dict["mmproj_model"] = mmproj_file
response = base_image_interrogator(args_dict=args_dict)
return response
def base_image_interrogator(model_file, mmproj_file, n_gpu_layers, image, resolution, options={}):
if options is None:
options = {}
def base_image_interrogator(args_dict):
model_file = args_dict.get("llama_cpp_model", "")
mmproj_file = args_dict.get("mmproj_model", "")
image = args_dict.get("image", None)
resolution = args_dict.get("resolution", 512)
keep_device = args_dict.get("keep_device", False)
seed = args_dict.get("seed", -1)
options = args_dict.get("llama_cpp_options", {})
options["seed"] = seed
image = mz_prompt_utils.Utils.resize_max(image, resolution, resolution)
response = mz_llama_cpp.llava_cpp_simple_interrogator(
model_file=model_file,
mmproj_file=mmproj_file,
n_gpu_layers=n_gpu_layers,
mmproj_file=mmproj_file,
image=image,
options=options,
)
mz_llama_cpp.freed_gpu_memory(model_file=model_file)
sd_format = args_dict.get("sd_format", "v1")
if sd_format == "v1":
mz_prompt_utils.Utils.print_log(f"response: {response}")
schema = mz_llama_cpp.get_schema_obj(
keys_type={
"short_desc": 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"),
"other_tags": mz_llama_cpp.get_schema_array("string"),
},
required=[
"short_desc",
"subject_tags",
"action_tags",
"light_tags",
"other_tags",
]
)
response = mz_llama_cpp.llama_cpp_simple_interrogator_to_json(
model_file=model_file,
system=mz_prompts.Beautify_Prompt,
question=f"IDEA: {response}",
schema=schema,
options=options,
)
response_json = json.loads(response)
responses = []
for key, value in response_json.items():
if type(value) == list:
# 去除开头.和空格
value = [v.strip().lstrip(".") for v in value]
# 去除空字符串
value = [v for v in value if v != ""]
if len(value) > 0:
responses.append(f"({', '.join(value)})")
else:
if value != "":
responses.append(f"({value})")
response = ", ".join(responses)
if keep_device is False:
mz_llama_cpp.freed_gpu_memory(model_file=model_file)
return response
+1 -1
View File
@@ -10,9 +10,9 @@ Below is a list of prompts that can be used to generate images with Stable Diffu
- closeup portrait shot of a large strong female biomechanic woman in a scenic scifi environment, intricate, elegant, highly detailed, centered, digital painting, artstation, concept art, smooth, sharp focus, warframe, illustration, thomas kinkade, tomasz alen kopera, peter mohrbacher, donato giancola, leyendecker, boris vallejo
- ultra realistic illustration of steve urkle as the hulk, intricate, elegant, highly detailed, digital painting, artstation, concept art, smooth, sharp focus, illustration, art by artgerm and greg rutkowski and alphonse mucha
I want you to write me a list of detailed prompts exactly about the idea written after IDEA. Follow the structure of the example prompts. This means a very short description of the scene, followed by modifiers divided by commas to alter the mood, style, lighting, and more.
Long prompt version should consist of 3 to 5 sentences. Long prompt version must sepcify the color, shape, texture or spatial relation of the included objects. DO NOT generate sentences that describe any atmosphere!!!
"""
Long_prompt = "Long prompt version should consist of 3 to 5 sentences. Long prompt version must sepcify the color, shape, texture or spatial relation of the included objects. DO NOT generate sentences that describe any atmosphere!!!"
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.