first commit

This commit is contained in:
HasegawaSkip
2023-09-16 19:05:03 +07:00
commit cc7532502d
13 changed files with 151242 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
# Ignore _pycache directories
__pycache__/
# Ignore all files with .bin extension
*.bin
+17
View File
@@ -0,0 +1,17 @@
from .prompt_expansion import NODE_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS
from .prompt_expansion import fooocus_expansion_path
from .model_loader import load_file_from_url
__all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS']
def download_models():
url = 'https://huggingface.co/lllyasviel/misc/resolve/main/fooocus_expansion.bin'
model_dir = fooocus_expansion_path
file_name = 'pytorch_model.bin'
load_file_from_url(url=url, model_dir=model_dir, file_name=file_name)
download_models()
+40
View File
@@ -0,0 +1,40 @@
{
"_name_or_path": "gpt2",
"activation_function": "gelu_new",
"architectures": [
"GPT2LMHeadModel"
],
"attn_pdrop": 0.1,
"bos_token_id": 50256,
"embd_pdrop": 0.1,
"eos_token_id": 50256,
"pad_token_id": 50256,
"initializer_range": 0.02,
"layer_norm_epsilon": 1e-05,
"model_type": "gpt2",
"n_ctx": 1024,
"n_embd": 768,
"n_head": 12,
"n_inner": null,
"n_layer": 12,
"n_positions": 1024,
"reorder_and_upcast_attn": false,
"resid_pdrop": 0.1,
"scale_attn_by_inverse_layer_idx": false,
"scale_attn_weights": true,
"summary_activation": null,
"summary_first_dropout": 0.1,
"summary_proj_to_labels": true,
"summary_type": "cls_index",
"summary_use_proj": true,
"task_specific_params": {
"text-generation": {
"do_sample": true,
"max_length": 50
}
},
"torch_dtype": "float32",
"transformers_version": "4.23.0.dev0",
"use_cache": true,
"vocab_size": 50257
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,5 @@
{
"bos_token": "<|endoftext|>",
"eos_token": "<|endoftext|>",
"unk_token": "<|endoftext|>"
}
File diff suppressed because it is too large Load Diff
+10
View File
@@ -0,0 +1,10 @@
{
"add_prefix_space": false,
"bos_token": "<|endoftext|>",
"eos_token": "<|endoftext|>",
"model_max_length": 1024,
"name_or_path": "gpt2",
"special_tokens_map_file": null,
"tokenizer_class": "GPT2Tokenizer",
"unk_token": "<|endoftext|>"
}
File diff suppressed because one or more lines are too long
+26
View File
@@ -0,0 +1,26 @@
import os
from urllib.parse import urlparse
from typing import Optional
def load_file_from_url(
url: str,
*,
model_dir: str,
progress: bool = True,
file_name: Optional[str] = None,
) -> str:
"""Download a file from `url` into `model_dir`, using the file present if possible.
Returns the path to the downloaded file.
"""
os.makedirs(model_dir, exist_ok=True)
if not file_name:
parts = urlparse(url)
file_name = os.path.basename(parts.path)
cached_file = os.path.abspath(os.path.join(model_dir, file_name))
if not os.path.exists(cached_file):
print(f'Downloading: "{url}" to {cached_file}\n')
from torch.hub import download_url_to_file
download_url_to_file(url, cached_file, progress=progress)
return cached_file
+139
View File
@@ -0,0 +1,139 @@
import os
import random
import sys
import torch
# Get the parent directory of 'comfy' and add it to the Python path
comfy_parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../'))
sys.path.append(comfy_parent_dir)
# Suppress console output
original_stdout = sys.stdout
sys.stdout = open(os.devnull, 'w')
# Import the required modules
import comfy.model_management as model_management
from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed
from comfy.model_patcher import ModelPatcher
from .util import join_prompts, remove_empty_str
# Restore the original stdout
sys.stdout = original_stdout
fooocus_expansion_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
'fooocus_expansion'))
fooocus_magic_split = [
', extremely',
', intricate,',
]
dangrous_patterns = '[]【】()()|::'
def safe_str(x):
x = str(x)
for _ in range(16):
x = x.replace(' ', ' ')
return x.strip(",. \r\n")
def remove_pattern(x, pattern):
for p in pattern:
x = x.replace(p, '')
return x
class FooocusExpansion:
def __init__(self):
self.tokenizer = AutoTokenizer.from_pretrained(fooocus_expansion_path)
self.model = AutoModelForCausalLM.from_pretrained(fooocus_expansion_path)
self.model.eval()
load_device = model_management.text_encoder_device()
if 'mps' in load_device.type:
load_device = torch.device('cpu')
if 'cpu' not in load_device.type and model_management.should_use_fp16():
self.model.half()
offload_device = model_management.text_encoder_offload_device()
self.patcher = ModelPatcher(self.model, load_device=load_device, offload_device=offload_device)
# print(f'Fooocus Expansion engine loaded for {load_device}.')
def __call__(self, prompt, seed):
model_management.load_model_gpu(self.patcher)
seed = int(seed)
set_seed(seed)
origin = safe_str(prompt)
prompt = origin + fooocus_magic_split[seed % len(fooocus_magic_split)]
tokenized_kwargs = self.tokenizer(prompt, return_tensors="pt")
tokenized_kwargs.data['input_ids'] = tokenized_kwargs.data['input_ids'].to(self.patcher.load_device)
tokenized_kwargs.data['attention_mask'] = tokenized_kwargs.data['attention_mask'].to(self.patcher.load_device)
# https://huggingface.co/blog/introducing-csearch
# https://huggingface.co/docs/transformers/generation_strategies
features = self.model.generate(**tokenized_kwargs,
num_beams=1,
max_new_tokens=256,
do_sample=True)
response = self.tokenizer.batch_decode(features, skip_special_tokens=True)
result = response[0][len(origin):]
result = safe_str(result)
result = remove_pattern(result, dangrous_patterns)
return result
class PromptExpansion:
# Define the expected input types for the node
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"text": ("STRING", {"multiline": True}),
"seed": ("INT", {"default": 0, "min": 0, "max": 0xFFFFFFFF}),
"log_prompt": (["No", "Yes"], {"default": "No"}),
},
}
RETURN_TYPES = ("STRING", "INT",)
RETURN_NAMES = ("final_prompt", "seed",)
FUNCTION = "expand_prompt" # Function name
CATEGORY = "utils" # Category for organization
@staticmethod
@torch.no_grad()
def expand_prompt(text, seed, log_prompt):
expansion = FooocusExpansion()
prompt = remove_empty_str([safe_str(text)], default='')[0]
max_seed = int(1024 * 1024 * 1024)
if not isinstance(seed, int):
seed = random.randint(1, max_seed)
if seed < 0:
seed = - seed
seed = seed % max_seed
expansion_text = expansion(prompt, seed)
final_prompt = join_prompts(prompt, expansion_text)
if log_prompt == "Yes":
print(f"[Prompt Expansion] New suffix: {expansion_text}")
print(f"Final prompt: {final_prompt}")
return final_prompt, seed
# Define a mapping of node class names to their respective classes
NODE_CLASS_MAPPINGS = {
"PromptExpansion": PromptExpansion
}
# A dictionary that contains human-readable titles for the nodes
NODE_DISPLAY_NAME_MAPPINGS = {
"PromptExpansion": "Prompt Expansion"
}
+14
View File
@@ -0,0 +1,14 @@
def remove_empty_str(items, default=None):
items = [x for x in items if x != ""]
if len(items) == 0 and default is not None:
return [default]
return items
def join_prompts(*args, **kwargs):
prompts = [str(x) for x in args if str(x) != ""]
if len(prompts) == 0:
return ""
if len(prompts) == 1:
return prompts[0]
return ', '.join(prompts)
+680
View File
@@ -0,0 +1,680 @@
{
"last_node_id": 22,
"last_link_id": 41,
"nodes": [
{
"id": 12,
"type": "PrimitiveNode",
"pos": [
30,
270
],
"size": {
"0": 280,
"1": 175
},
"flags": {
"pinned": false
},
"order": 0,
"mode": 0,
"outputs": [
{
"name": "STRING",
"type": "STRING",
"links": [
12
],
"slot_index": 0,
"widget": {
"name": "text",
"config": [
"STRING",
{
"multiline": true
}
]
}
}
],
"title": "Positive prompt",
"properties": {},
"widgets_values": [
"portrait of robot Terminator, cyborg, evil, in dynamics, highly detailed, packed with hidden details, style, high dynamic range, hyper realistic, realistic attention to detail, highly detailed"
],
"color": "#232",
"bgcolor": "#353"
},
{
"id": 19,
"type": "ShowText|pysssss",
"pos": [
-298,
272
],
"size": {
"0": 280,
"1": 175
},
"flags": {
"pinned": false
},
"order": 7,
"mode": 0,
"inputs": [
{
"name": "text",
"type": "STRING",
"link": 30,
"widget": {
"name": "text",
"config": [
"STRING",
{
"forceInput": true
}
]
}
}
],
"outputs": [
{
"name": "STRING",
"type": "STRING",
"links": null,
"shape": 6
}
],
"properties": {
"Node name for S&R": "ShowText|pysssss"
},
"widgets_values": [
"portrait of robot Terminator, cyborg, evil, in dynamics, highly detailed, packed with hidden details, style, high dynamic range, hyper realistic, realistic attention to detail, highly detailed, extremely lifelike, digital painting, artstation, illustration, concept art, smooth, sharp focus, 8k"
],
"color": "#222",
"bgcolor": "#000"
},
{
"id": 1,
"type": "PromptExpansion",
"pos": [
456,
64
],
"size": {
"0": 315,
"1": 168
},
"flags": {
"pinned": false
},
"order": 4,
"mode": 0,
"inputs": [
{
"name": "text",
"type": "STRING",
"link": 12,
"widget": {
"name": "text",
"config": [
"STRING",
{
"multiline": true
}
]
},
"slot_index": 0
}
],
"outputs": [
{
"name": "STRING",
"type": "STRING",
"links": [
13,
30
],
"shape": 3,
"slot_index": 0
},
{
"name": "seed",
"type": "INT",
"links": [
35
],
"shape": 3,
"slot_index": 1
}
],
"properties": {
"Node name for S&R": "PromptExpansion"
},
"widgets_values": [
"portrait of robot Terminator, cyborg, evil, in dynamics, highly detailed, packed with hidden details, style, high dynamic range, hyper realistic, realistic attention to detail, highly detailed",
3703294023,
"randomize",
"Yes"
]
},
{
"id": 13,
"type": "CLIPTextEncode",
"pos": [
503,
327
],
"size": {
"0": 400,
"1": 200
},
"flags": {
"collapsed": true,
"pinned": false
},
"order": 6,
"mode": 0,
"inputs": [
{
"name": "clip",
"type": "CLIP",
"link": 14,
"slot_index": 0
},
{
"name": "text",
"type": "STRING",
"link": 13,
"widget": {
"name": "text",
"config": [
"STRING",
{
"multiline": true
}
]
}
}
],
"outputs": [
{
"name": "CONDITIONING",
"type": "CONDITIONING",
"links": [
37
],
"shape": 3,
"slot_index": 0
}
],
"properties": {
"Node name for S&R": "CLIPTextEncode"
},
"widgets_values": [
""
],
"color": "#232",
"bgcolor": "#353"
},
{
"id": 9,
"type": "CLIPTextEncode",
"pos": [
502,
574
],
"size": {
"0": 400,
"1": 200
},
"flags": {
"collapsed": true,
"pinned": false
},
"order": 5,
"mode": 0,
"inputs": [
{
"name": "clip",
"type": "CLIP",
"link": 7,
"slot_index": 0
},
{
"name": "text",
"type": "STRING",
"link": 6,
"widget": {
"name": "text",
"config": [
"STRING",
{
"multiline": true
}
]
}
}
],
"outputs": [
{
"name": "CONDITIONING",
"type": "CONDITIONING",
"links": [
38
],
"shape": 3,
"slot_index": 0
}
],
"properties": {
"Node name for S&R": "CLIPTextEncode"
},
"widgets_values": [
"text, watermark, low-quality, signature, moiré pattern, downsampling, aliasing, distorted, blurry, glossy, blur, jpeg artifacts, compression artifacts, poorly drawn, low-resolution, bad, distortion, twisted, excessive, exaggerated pose, exaggerated limbs, grainy, symmetrical, duplicate, error, pattern, beginner, pixelated, fake, hyper, glitch, overexposed, high-contrast, bad-contrast"
],
"color": "#322",
"bgcolor": "#533"
},
{
"id": 7,
"type": "CheckpointLoaderSimple",
"pos": [
21,
56
],
"size": {
"0": 315,
"1": 98
},
"flags": {
"pinned": false
},
"order": 1,
"mode": 0,
"outputs": [
{
"name": "MODEL",
"type": "MODEL",
"links": [
39
],
"shape": 3,
"slot_index": 0
},
{
"name": "CLIP",
"type": "CLIP",
"links": [
7,
14
],
"shape": 3,
"slot_index": 1
},
{
"name": "VAE",
"type": "VAE",
"links": [
27
],
"shape": 3,
"slot_index": 2
}
],
"properties": {
"Node name for S&R": "CheckpointLoaderSimple"
},
"widgets_values": [
"sd_xl_base_1.0.safetensors"
]
},
{
"id": 15,
"type": "EmptyLatentImage",
"pos": [
450,
391
],
"size": {
"0": 315,
"1": 106
},
"flags": {
"pinned": false
},
"order": 2,
"mode": 0,
"outputs": [
{
"name": "LATENT",
"type": "LATENT",
"links": [
36
],
"shape": 3,
"slot_index": 0
}
],
"properties": {
"Node name for S&R": "EmptyLatentImage"
},
"widgets_values": [
1024,
1024,
1
]
},
{
"id": 17,
"type": "VAEDecode",
"pos": [
1300,
114
],
"size": {
"0": 210,
"1": 46
},
"flags": {
"collapsed": true,
"pinned": false
},
"order": 9,
"mode": 0,
"inputs": [
{
"name": "samples",
"type": "LATENT",
"link": 40
},
{
"name": "vae",
"type": "VAE",
"link": 27
}
],
"outputs": [
{
"name": "IMAGE",
"type": "IMAGE",
"links": [
41
],
"shape": 3,
"slot_index": 0
}
],
"properties": {
"Node name for S&R": "VAEDecode"
}
},
{
"id": 22,
"type": "SaveImage",
"pos": [
1487,
85
],
"size": {
"0": 315,
"1": 270
},
"flags": {
"pinned": false
},
"order": 10,
"mode": 0,
"inputs": [
{
"name": "images",
"type": "IMAGE",
"link": 41
}
],
"properties": {},
"widgets_values": [
"%date:yyyy-MM-dd%/base"
]
},
{
"id": 20,
"type": "KSampler",
"pos": [
903,
64
],
"size": {
"0": 315,
"1": 262
},
"flags": {
"pinned": false
},
"order": 8,
"mode": 0,
"inputs": [
{
"name": "model",
"type": "MODEL",
"link": 39
},
{
"name": "positive",
"type": "CONDITIONING",
"link": 37
},
{
"name": "negative",
"type": "CONDITIONING",
"link": 38
},
{
"name": "latent_image",
"type": "LATENT",
"link": 36
},
{
"name": "seed",
"type": "INT",
"link": 35,
"widget": {
"name": "seed",
"config": [
"INT",
{
"default": 0,
"min": 0,
"max": 18446744073709552000
}
]
}
}
],
"outputs": [
{
"name": "LATENT",
"type": "LATENT",
"links": [
40
],
"shape": 3,
"slot_index": 0
}
],
"properties": {
"Node name for S&R": "KSampler"
},
"widgets_values": [
41310521337040,
"randomize",
30,
8,
"dpmpp_2m",
"karras",
1
]
},
{
"id": 8,
"type": "PrimitiveNode",
"pos": [
31,
542
],
"size": {
"0": 280,
"1": 175
},
"flags": {
"pinned": false
},
"order": 3,
"mode": 0,
"outputs": [
{
"name": "STRING",
"type": "STRING",
"links": [
6
],
"slot_index": 0,
"widget": {
"name": "text",
"config": [
"STRING",
{
"multiline": true
}
]
}
}
],
"title": "Negative prompt",
"properties": {},
"widgets_values": [
"text, watermark, low-quality, signature, moiré pattern, downsampling, aliasing, distorted, blurry, glossy, blur, jpeg artifacts, compression artifacts, poorly drawn, low-resolution, bad, distortion, twisted, excessive, exaggerated pose, exaggerated limbs, grainy, symmetrical, duplicate, error, pattern, beginner, pixelated, fake, hyper, glitch, overexposed, high-contrast, bad-contrast"
],
"color": "#322",
"bgcolor": "#533"
}
],
"links": [
[
6,
8,
0,
9,
1,
"STRING"
],
[
7,
7,
1,
9,
0,
"CLIP"
],
[
12,
12,
0,
1,
0,
"STRING"
],
[
13,
1,
0,
13,
1,
"STRING"
],
[
14,
7,
1,
13,
0,
"CLIP"
],
[
27,
7,
2,
17,
1,
"VAE"
],
[
30,
1,
0,
19,
0,
"STRING"
],
[
35,
1,
1,
20,
4,
"INT"
],
[
36,
15,
0,
20,
3,
"LATENT"
],
[
37,
13,
0,
20,
1,
"CONDITIONING"
],
[
38,
9,
0,
20,
2,
"CONDITIONING"
],
[
39,
7,
0,
20,
0,
"MODEL"
],
[
40,
20,
0,
17,
0,
"LATENT"
],
[
41,
17,
0,
22,
0,
"IMAGE"
]
],
"groups": [],
"config": {},
"extra": {},
"version": 0.4
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 388 KiB