This commit is contained in:
Saifeddine ALOUI 2024-06-16 03:17:55 +02:00
commit 0cbe144f05
3 changed files with 112 additions and 107 deletions

View File

@ -409,7 +409,7 @@ class LollmsApplication(LoLLMsCom):
from lollms.services.comfyui.lollms_comfyui import LollmsComfyUI from lollms.services.comfyui.lollms_comfyui import LollmsComfyUI
self.comfyui = LollmsComfyUI(self, comfyui_base_url=self.config.comfyui_base_url) self.comfyui = LollmsComfyUI(self, comfyui_base_url=self.config.comfyui_base_url)
except: except:
self.warning(f"Couldn't load SD") self.warning(f"Couldn't load Comfyui")
if self.config.enable_motion_ctrl_service and self.motion_ctrl is None: if self.config.enable_motion_ctrl_service and self.motion_ctrl is None:
try: try:

View File

@ -69,13 +69,17 @@ def build_image(prompt, negative_prompt, width, height, processor:APScript, clie
elif processor.personality.config.active_tti_service=="comfyui": elif processor.personality.config.active_tti_service=="comfyui":
if not processor.personality.app.tti: if not processor.personality.app.tti:
from lollms.services.comfyui.lollms_comfyui import LollmsComfyUI from lollms.services.comfyui.lollms_comfyui import LollmsComfyUI
processor.step_start("Loading dalle service") processor.step_start("Loading comfyui service")
processor.personality.app.tti = LollmsComfyUI(processor.personality.app, comfyui_base_url=processor.personality) processor.personality.app.tti = LollmsComfyUI(
processor.personality.app,
comfyui_base_url=processor.config.comfyui_base_url
)
processor.personality.app.dalle = processor.personality.app.tti processor.personality.app.dalle = processor.personality.app.tti
processor.step_end("Loading dalle service") processor.step_end("Loading comfyui service")
processor.step_start("Painting") processor.step_start("Painting")
file = processor.personality.app.tti.paint( file = processor.personality.app.tti.paint(
prompt, prompt,
negative_prompt,
width = width, width = width,
height = height, height = height,
output_path=client.discussion.discussion_folder output_path=client.discussion.discussion_folder
@ -87,7 +91,7 @@ def build_image(prompt, negative_prompt, width, height, processor:APScript, clie
return f'\n![]({escaped_url})' return f'\n![]({escaped_url})'
except Exception as ex: except Exception as ex:
trace_exception(ex) trace_exception(ex)
return "Couldn't generate image. Make sure Auto1111's stable diffusion service is installed" return f"Couldn't generate image. Make sure {processor.personality.config.active_tti_service} service is installed"
def build_image_function(processor, client): def build_image_function(processor, client):

View File

@ -163,6 +163,7 @@ class LollmsComfyUI(LollmsTTI):
share=False, share=False,
wait_for_service=True wait_for_service=True
): ):
super().__init__("comfyui", app)
if comfyui_base_url=="" or comfyui_base_url=="http://127.0.0.1:8188/": if comfyui_base_url=="" or comfyui_base_url=="http://127.0.0.1:8188/":
comfyui_base_url = None comfyui_base_url = None
# Get the current directory # Get the current directory
@ -272,22 +273,28 @@ class LollmsComfyUI(LollmsTTI):
restore_faces=True, restore_faces=True,
output_path=None output_path=None
): ):
if output_path is None:
output_path = self.output_dir
client_id = str(uuid.uuid4()) client_id = str(uuid.uuid4())
url = self.comfyui_base_url[7:-1]
def queue_prompt(prompt): def queue_prompt(prompt):
p = {"prompt": prompt, "client_id": client_id} p = {"prompt": prompt, "client_id": client_id}
data = json.dumps(p).encode('utf-8') data = json.dumps(p).encode('utf-8')
req = request.Request("http://{}/prompt".format(self.comfyui_base_url), data=data) full_url = "http://{}/prompt".format(url)
req = request.Request(full_url, data=data)
return json.loads(request.urlopen(req).read()) return json.loads(request.urlopen(req).read())
def get_image(filename, subfolder, folder_type): def get_image(filename, subfolder):
data = {"filename": filename, "subfolder": subfolder, "type": folder_type} data = {"filename": filename, "subfolder": subfolder}
url_values = parse.urlencode(data) url_values = parse.urlencode(data)
with request.urlopen("http://{}/view?{}".format(self.comfyui_base_url, url_values)) as response: full_url = "http://{}/view?{}".format(url, url_values)
with request.urlopen(full_url) as response:
return response.read() return response.read()
def get_history(prompt_id): def get_history(prompt_id):
with request.urlopen("http://{}/history/{}".format(self.comfyui_base_url, prompt_id)) as response: url_values = "http://{}/history/{}".format(url, prompt_id)
with request.urlopen(url_values) as response:
return json.loads(response.read()) return json.loads(response.read())
def get_images(ws, prompt): def get_images(ws, prompt):
@ -311,116 +318,110 @@ class LollmsComfyUI(LollmsTTI):
if 'images' in node_output: if 'images' in node_output:
images_output = [] images_output = []
for image in node_output['images']: for image in node_output['images']:
image_data = get_image(image['filename'], image['subfolder'], image['type']) if image["type"]=="output":
image_data = get_image(image['filename'], image['subfolder'])
images_output.append(image_data) images_output.append(image_data)
output_images[node_id] = images_output
return output_images return images_output
def save_images(images:dict, folder_path:str|Path):
# Create the folder if it doesn't exist
folder = Path(folder_path)
folder.mkdir(parents=True, exist_ok=True)
# Save each image to the folder
for i, img_data in enumerate(images):
img_path = folder / f'image_{i+1}.png'
with open(img_path, 'wb') as img_file:
img_file.write(img_data)
# Return the path to the first image
return str(folder / 'image_1.png')
prompt_text = """ prompt_text = """
{ {
"3": { "1": {
"class_type": "KSampler",
"inputs": { "inputs": {
"cfg": 8, "base_ckpt_name": "juggernaut.safetensors",
"denoise": 1, "base_clip_skip": -2,
"latent_image": [ "refiner_ckpt_name": "None",
"5", "refiner_clip_skip": -2,
0 "positive_ascore": 6,
], "negative_ascore": 2,
"model": [ "vae_name": "Baked VAE",
"4", "positive": "smart robot icon, slick, flat design, high res, W in the center, black background",
0 "negative": "ugly, deformed, badly rendered, fuzzy",
], "token_normalization": "none",
"negative": [ "weight_interpretation": "comfy",
"7", "empty_latent_width": 1024,
0 "empty_latent_height": 1024,
], "batch_size": 1
"positive": [ },
"6", "class_type": "Eff. Loader SDXL",
0 "_meta": {
], "title": "Eff. Loader SDXL"
}
},
"2": {
"inputs": {
"noise_seed": 74738751167752,
"steps": 20,
"cfg": 7,
"sampler_name": "euler", "sampler_name": "euler",
"scheduler": "normal", "scheduler": "normal",
"seed": 8566257, "start_at_step": 0,
"steps": 20 "refine_at_step": -1,
} "preview_method": "auto",
}, "vae_decode": "true",
"4": { "sdxl_tuple": [
"class_type": "CheckpointLoaderSimple", "1",
"inputs": {
"""+f"""
"ckpt_name": "{self.app.config.comfyui_model}"
"""+"""
}
},
"5": {
"class_type": "EmptyLatentImage",
"inputs": {
"batch_size": 1,
"height": 512,
"width": 512
}
},
"6": {
"class_type": "CLIPTextEncode",
"inputs": {
"clip": [
"4",
1
],"""+f"""
"text": "{positive_prompt}"
"""+"""
}
},
"7": {
"class_type": "CLIPTextEncode",
"inputs": {
"clip": [
"4",
1
],"""+f"""
"text": "{negative_prompt}"
"""+"""
}
},
"8": {
"class_type": "VAEDecode",
"inputs": {
"samples": [
"3",
0 0
], ],
"vae": [ "latent_image": [
"4", "1",
1
],
"optional_vae": [
"1",
2 2
] ]
},
"class_type": "KSampler SDXL (Eff.)",
"_meta": {
"title": "KSampler SDXL (Eff.)"
} }
}, },
"9": { "3": {
"class_type": "SaveImage",
"inputs": { "inputs": {
"filename_prefix": "ComfyUI", "filename_prefix": "ComfyUI",
"images": [ "images": [
"8", "2",
0 3
] ]
},
"class_type": "SaveImage",
"_meta": {
"title": "Save Image"
} }
} }
} }
""" """
prompt = json.loads(prompt_text) prompt = json.loads(prompt_text)
#set the text prompt for our positive CLIPTextEncode #set the text prompt for our positive CLIPTextEncode
prompt["6"]["inputs"]["text"] = "masterpiece best quality man" prompt["1"]["inputs"]["positive"] = prompt_text
prompt["1"]["inputs"]["negative"] = negative_prompt
prompt["1"]["inputs"]["empty_latent_width"] = width
prompt["1"]["inputs"]["empty_latent_height"] = height
#set the seed for our KSampler node prompt["1"]["inputs"]["base_ckpt_name"] = self.app.config.comfyui_model
prompt["3"]["inputs"]["seed"] = 5
ws = websocket.WebSocket() ws = websocket.WebSocket()
ws.connect("ws://{}/ws?clientId={}".format(self.comfyui_base_url, client_id)) ws.connect("ws://{}/ws?clientId={}".format(url, client_id))
images = get_images(ws, prompt) images = get_images(ws, prompt)
return None
return save_images(images, output_path), {"prompt":prompt,"negative_prompt":negative_prompt}
def paint_from_images(self, positive_prompt: str, def paint_from_images(self, positive_prompt: str,
images: List[str], images: List[str],