Skip to content

Commit

Permalink
v0.12.0 ChinesePrompt && PromptGenerate
Browse files Browse the repository at this point in the history
> ChinesePrompt && PromptGenerate,中文prompt节点,直接用中文书写你的prompt

![](./assets/ChinesePrompt_workflow.svg)

> Web App增加图片编辑器
  • Loading branch information
shadowcz007 committed Jan 13, 2024
1 parent efa3bae commit aafd87e
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 18 deletions.
24 changes: 9 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ https://github.com/shadowcz007/comfyui-mixlab-nodes/assets/12645064/e7e77f90-e43

> PromptImage & PromptSimplification,Assist in simplifying prompt words, comparing images and prompt word nodes.
> ChinesePrompt && PromptGenerate,中文prompt节点,直接用中文书写你的prompt
![](./assets/ChinesePrompt_workflow.svg)


### Layers
> A new layer class node has been added, allowing you to separate the image into layers. After merging the images, you can input the controlnet for further processing.
Expand Down Expand Up @@ -159,23 +163,16 @@ An improvement has been made to directly redirect to GitHub to search for missin
![node-not-found](./assets/node-not-found.png)


### Update
v0.8.0 🚀🚗🚚🏃‍ LaMaInpainting
- 新增 LaMaInpainting
- 优化color节点的输出
- 修复高清显示屏上定位节点不准的情况

- Add LaMaInpainting
- Optimize the output of the color node
- Fix the issue of inaccurate positioning node on high-definition display screens


### Models
[Download CLIPSeg](https://huggingface.co/CIDAS/clipseg-rd64-refined/tree/main), move to : models/clipseg

[Download lama](https://github.com/enesmsahin/simple-lama-inpainting/releases/download/v0.1.0/big-lama.pt), move to : models/lama

[Download Salesforce\blip-image-captioning-base](https://huggingface.co/Salesforce/blip-image-captioning-base), move to : models/clip_interrogator/Salesforce/blip-image-captioning-base
[Download Salesforce/blip-image-captioning-base](https://huggingface.co/Salesforce/blip-image-captioning-base), move to : models/clip_interrogator/Salesforce/blip-image-captioning-base

[Download succinctly/text2image-prompt-generator](https://huggingface.co/succinctly/text2image-prompt-generator/tree/main),move to:text_generator/text2image-prompt-generator

[Download Helsinki-NLP/opus-mt-zh-en](https://huggingface.co/Helsinki-NLP/opus-mt-zh-en/tree/main),move to:prompt_generator/opus-mt-zh-en

## Installation

Expand Down Expand Up @@ -216,9 +213,6 @@ pip3 install -r requirements.txt
#### discussions:
[discussions](https://github.com/shadowcz007/comfyui-mixlab-nodes/discussions)

### TODO:
- 音频播放节点:带可视化、支持多音轨、可配置音轨音量
- vector https://github.com/GeorgLegato/stable-diffusion-webui-vectorstudio


<picture>
Expand Down
14 changes: 13 additions & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,8 @@ def new_add_routes(self):
"DynamicDelayProcessor":"DynamicDelayByText ♾️Mixlab",
"LaMaInpainting":"LaMaInpainting ♾️Mixlab",
"PromptSlide":"PromptSlide ♾️Mixlab",

"PromptGenerate_Mix":"PromptGenerate ♾️Mixlab",
"ChinesePrompt_Mix":"ChinesePrompt ♾️Mixlab",
"GamePal":"GamePal ♾️Mixlab"
}

Expand All @@ -645,6 +646,17 @@ def new_add_routes(self):
NODE_CLASS_MAPPINGS['ClipInterrogator']=ClipInterrogator
except:
print('ClipInterrogator.available',False)

try:
from .nodes.TextGenerateNode import PromptGenerate,ChinesePrompt
print('PromptGenerate.available',PromptGenerate.available)
if PromptGenerate.available:
NODE_CLASS_MAPPINGS['PromptGenerate_Mix']=PromptGenerate
print('ChinesePrompt.available',ChinesePrompt.available)
if ChinesePrompt.available:
NODE_CLASS_MAPPINGS['ChinesePrompt_Mix']=ChinesePrompt
except:
print('TextGenerateNode.available',False)


print('\033[93m -------------- \033[0m')
1 change: 1 addition & 0 deletions assets/ChinesePrompt_workflow.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions data/extension-node-map.json
Original file line number Diff line number Diff line change
Expand Up @@ -4761,6 +4761,8 @@
],
"https://github.com/shadowcz007/comfyui-mixlab-nodes": [
[
"PromptGenerate_Mix",
"ChinesePrompt_Mix",
"3DImage",
"AppInfo",
"IntNumber",
Expand Down
2 changes: 1 addition & 1 deletion nodes/ClipInterrogator.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ def load_caption_model(model_path,config,t='blip-base'):
caption_model_path=os.path.join(folder_paths.models_dir, "clip_interrogator/Salesforce/blip-image-captioning-base")
if not os.path.exists(caption_model_path):
print(f"## clip_interrogator_model not found: {caption_model_path}, pls download from https://huggingface.co/Salesforce/blip-image-captioning-base")
caption_model_path='Salesforce/blip-image-captioning-base'

cache_path=os.path.join(folder_paths.models_dir, "clip_interrogator")



# Tensor to PIL
def tensor2pil(image):
return Image.fromarray(np.clip(255. * image.cpu().numpy().squeeze(), 0, 255).astype(np.uint8))
Expand Down
215 changes: 215 additions & 0 deletions nodes/TextGenerateNode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
from transformers import pipeline, set_seed,AutoTokenizer, AutoModelForSeq2SeqLM
import random
import re

import os,sys
import folder_paths

# from PIL import Image
# import importlib.util

import comfy.utils
# import numpy as np
import torch
import random

global _available
_available=True


text_generator_model_path=os.path.join(folder_paths.models_dir, "prompt_generator/text2image-prompt-generator")
if not os.path.exists(text_generator_model_path):
print(f"## text_generator_model not found: {text_generator_model_path}, pls download from https://huggingface.co/succinctly/text2image-prompt-generator/tree/main")
text_generator_model_path='succinctly/text2image-prompt-generator'

zh_en_model_path=os.path.join(folder_paths.models_dir, "prompt_generator/opus-mt-zh-en")
if not os.path.exists(zh_en_model_path):
print(f"## zh_en_model not found: {zh_en_model_path}, pls download from https://huggingface.co/Helsinki-NLP/opus-mt-zh-en/tree/main")
zh_en_model_path='Helsinki-NLP/opus-mt-zh-en'



def translate(zh_en_tokenizer,zh_en_model,texts):
with torch.no_grad():
encoded = zh_en_tokenizer(texts, return_tensors="pt")
encoded.to(zh_en_model.device)
sequences = zh_en_model.generate(**encoded)
return zh_en_tokenizer.batch_decode(sequences, skip_special_tokens=True)

# input = "青春不能回头,所以青春没有终点。 ——《火影忍者》"
# print(input, translate(input))



def text_generate(text_pipe,input,seed=None):

if seed==None:
seed = random.randint(100, 1000000)

set_seed(seed)

for count in range(6):
sequences = text_pipe(input, max_length=random.randint(60, 90), num_return_sequences=8)
list = []
for sequence in sequences:
line = sequence['generated_text'].strip()
if line != input and len(line) > (len(input) + 4) and line.endswith((":", "-", "—")) is False:
list.append(line)

result = "\n".join(list)
result = re.sub('[^ ]+\.[^ ]+','', result)
result = result.replace("<", "").replace(">", "")
if result != "":
return result
if count == 5:
return result

# input = "Youth can't turn back, so there's no end to youth."
# print(input, text_generate(input))


class ChinesePrompt:

global _available
available=_available

@classmethod
def INPUT_TYPES(s):
return {"required": {
"text": ("STRING",{"multiline": True,"default": "", "dynamicPrompts": False}),

},

"optional":{
"seed":("INT", {"default": 100, "min": 100, "max": 1000000}),
},

}

RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("prompt",)

FUNCTION = "run"

CATEGORY = "♾️Mixlab/Prompt"
OUTPUT_NODE = True
INPUT_IS_LIST = True
OUTPUT_IS_LIST = (True,)

global text_pipe,zh_en_model,zh_en_tokenizer

text_pipe= None
zh_en_model=None
zh_en_tokenizer=None

def run(self,text,seed):
global text_pipe,zh_en_model,zh_en_tokenizer

seed=seed[0]

# 进度条
pbar = comfy.utils.ProgressBar(len(text)+1)

if zh_en_model==None:
zh_en_model = AutoModelForSeq2SeqLM.from_pretrained(zh_en_model_path).eval()
zh_en_tokenizer = AutoTokenizer.from_pretrained(zh_en_model_path)

zh_en_model.to("cuda" if torch.cuda.is_available() else "cpu")
# zh_en_tokenizer.to("cuda" if torch.cuda.is_available() else "cpu")

text_pipe=pipeline('text-generation', model=text_generator_model_path,device="cuda" if torch.cuda.is_available() else "cpu")

# text_pipe.model.to("cuda" if torch.cuda.is_available() else "cpu")

prompt_result=[]

# print('zh_en_model device',zh_en_model.device,text_pipe.model.device,torch.cuda.current_device() )
en_text=translate(zh_en_tokenizer,zh_en_model,text)
zh_en_model.to('cpu')
# en_text.to("cuda" if torch.cuda.is_available() else "cpu")

pbar.update(1)
for t in en_text:
prompt =text_generate(text_pipe,t,seed)
# 多条,还是单条
lines = prompt.split("\n")
longest_line = max(lines, key=len)
# print(longest_line)
prompt_result.append(longest_line)
pbar.update(1)

text_pipe.model.to('cpu')



return {
"ui":{
"prompt": prompt_result
},
"result": (prompt_result,)}


class PromptGenerate:

global _available
available=_available

@classmethod
def INPUT_TYPES(s):
return {"required": {
"text": ("STRING",{"multiline": True,"default": "", "dynamicPrompts": False}),
},

"optional":{
"multiple": (["off","on"],),
"seed":("INT", {"default": 100, "min": 100, "max": 1000000}),
},

}

RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("prompt",)

FUNCTION = "run"

CATEGORY = "♾️Mixlab/Prompt"
OUTPUT_NODE = True
INPUT_IS_LIST = True
OUTPUT_IS_LIST = (True,)

global text_pipe

text_pipe= None
#

def run(self,text,multiple,seed):
global text_pipe

seed=seed[0]

multiple=multiple[0]

# 进度条
pbar = comfy.utils.ProgressBar(len(text))

text_pipe=pipeline('text-generation', model=text_generator_model_path,device="cuda" if torch.cuda.is_available() else "cpu")

prompt_result=[]

for t in text:
prompt =text_generate(text_pipe,t,seed)
prompt = prompt.split("\n")
if multiple=='off':
prompt = [max(prompt, key=len)]

for p in prompt:
prompt_result.append(p)
pbar.update(1)

text_pipe.model.to('cpu')

return {
"ui":{
"prompt": prompt_result
},
"result": (prompt_result,)}
2 changes: 1 addition & 1 deletion web/javascript/checkVersion_mixlab.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { app } from '../../../scripts/app.js'
const repoOwner = 'shadowcz007' // 替换为仓库的所有者
const repoName = 'comfyui-mixlab-nodes' // 替换为仓库的名称

const version = 'v0.11.4'
const version = 'v0.12.0'

fetch(`https://api.github.com/repos/${repoOwner}/${repoName}/releases/latest`)
.then(response => response.json())
Expand Down

0 comments on commit aafd87e

Please sign in to comment.