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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from urllib import request, parse
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import random
|
| 5 |
+
import os
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
import matplotlib.image as mpimg
|
| 8 |
+
from PIL import Image as PILImage
|
| 9 |
+
|
| 10 |
+
prompt_text = """
|
| 11 |
+
{
|
| 12 |
+
"10": {
|
| 13 |
+
"inputs": {
|
| 14 |
+
"ckpt_name": "DreamShaper_8_pruned.safetensors",
|
| 15 |
+
"vae_name": "vae-ft-mse-840000-ema-pruned.ckpt",
|
| 16 |
+
"clip_skip": -2,
|
| 17 |
+
"lora_name": "None",
|
| 18 |
+
"lora_model_strength": 1,
|
| 19 |
+
"lora_clip_strength": 1,
|
| 20 |
+
"positive": "In the style of Grand Theft Auto, loading screens, (palm trees), GTA style artwork, highly detailed, urban scene with numerous palm trees, neon lights, and graffiti, trending on ArtStation, preserving the individual's race, color and hair.",
|
| 21 |
+
"negative": "(worst quality, low quality = 1.3)",
|
| 22 |
+
"token_normalization": "none",
|
| 23 |
+
"weight_interpretation": "comfy",
|
| 24 |
+
"empty_latent_width": [
|
| 25 |
+
"65",
|
| 26 |
+
0
|
| 27 |
+
],
|
| 28 |
+
"empty_latent_height": [
|
| 29 |
+
"65",
|
| 30 |
+
1
|
| 31 |
+
],
|
| 32 |
+
"batch_size": 1,
|
| 33 |
+
"cnet_stack": [
|
| 34 |
+
"11",
|
| 35 |
+
0
|
| 36 |
+
]
|
| 37 |
+
},
|
| 38 |
+
"class_type": "Efficient Loader",
|
| 39 |
+
"_meta": {
|
| 40 |
+
"title": "Efficient Loader"
|
| 41 |
+
}
|
| 42 |
+
},
|
| 43 |
+
"11": {
|
| 44 |
+
"inputs": {
|
| 45 |
+
"switch_1": "On",
|
| 46 |
+
"controlnet_1": "control_v11p_sd15_lineart_fp16.safetensors",
|
| 47 |
+
"controlnet_strength_1": 0.7000000000000001,
|
| 48 |
+
"start_percent_1": 0,
|
| 49 |
+
"end_percent_1": 1,
|
| 50 |
+
"switch_2": "On",
|
| 51 |
+
"controlnet_2": "control_v11p_sd15_openpose_fp16.safetensors",
|
| 52 |
+
"controlnet_strength_2": 1,
|
| 53 |
+
"start_percent_2": 0,
|
| 54 |
+
"end_percent_2": 1,
|
| 55 |
+
"switch_3": "Off",
|
| 56 |
+
"controlnet_3": "None",
|
| 57 |
+
"controlnet_strength_3": 0.99,
|
| 58 |
+
"start_percent_3": 0,
|
| 59 |
+
"end_percent_3": 1,
|
| 60 |
+
"image_1": [
|
| 61 |
+
"107",
|
| 62 |
+
0
|
| 63 |
+
],
|
| 64 |
+
"image_2": [
|
| 65 |
+
"109",
|
| 66 |
+
0
|
| 67 |
+
]
|
| 68 |
+
},
|
| 69 |
+
"class_type": "CR Multi-ControlNet Stack",
|
| 70 |
+
"_meta": {
|
| 71 |
+
"title": "🕹️ CR Multi-ControlNet Stack"
|
| 72 |
+
}
|
| 73 |
+
},
|
| 74 |
+
"12": {
|
| 75 |
+
"inputs": {
|
| 76 |
+
"low_threshold": 100,
|
| 77 |
+
"high_threshold": 200,
|
| 78 |
+
"resolution": 2048,
|
| 79 |
+
"image": [
|
| 80 |
+
"14",
|
| 81 |
+
0
|
| 82 |
+
]
|
| 83 |
+
},
|
| 84 |
+
"class_type": "CannyEdgePreprocessor",
|
| 85 |
+
"_meta": {
|
| 86 |
+
"title": "Canny Edge"
|
| 87 |
+
}
|
| 88 |
+
},
|
| 89 |
+
"13": {
|
| 90 |
+
"inputs": {
|
| 91 |
+
"images": [
|
| 92 |
+
"12",
|
| 93 |
+
0
|
| 94 |
+
]
|
| 95 |
+
},
|
| 96 |
+
"class_type": "PreviewImage",
|
| 97 |
+
"_meta": {
|
| 98 |
+
"title": "Preview Image"
|
| 99 |
+
}
|
| 100 |
+
},
|
| 101 |
+
"14": {
|
| 102 |
+
"inputs": {
|
| 103 |
+
"image": "IMG_7593_2 (10).jpg",
|
| 104 |
+
"upload": "image"
|
| 105 |
+
},
|
| 106 |
+
"class_type": "LoadImage",
|
| 107 |
+
"_meta": {
|
| 108 |
+
"title": "Load Image"
|
| 109 |
+
}
|
| 110 |
+
},
|
| 111 |
+
"64": {
|
| 112 |
+
"inputs": {
|
| 113 |
+
"seed": 4091745839,
|
| 114 |
+
"steps": 10,
|
| 115 |
+
"cfg": 4,
|
| 116 |
+
"sampler_name": "dpm_fast",
|
| 117 |
+
"scheduler": "karras",
|
| 118 |
+
"denoise": 1,
|
| 119 |
+
"preview_method": "auto",
|
| 120 |
+
"vae_decode": "true",
|
| 121 |
+
"model": [
|
| 122 |
+
"10",
|
| 123 |
+
0
|
| 124 |
+
],
|
| 125 |
+
"positive": [
|
| 126 |
+
"10",
|
| 127 |
+
1
|
| 128 |
+
],
|
| 129 |
+
"negative": [
|
| 130 |
+
"10",
|
| 131 |
+
2
|
| 132 |
+
],
|
| 133 |
+
"latent_image": [
|
| 134 |
+
"10",
|
| 135 |
+
3
|
| 136 |
+
],
|
| 137 |
+
"optional_vae": [
|
| 138 |
+
"10",
|
| 139 |
+
4
|
| 140 |
+
],
|
| 141 |
+
"script": [
|
| 142 |
+
"101",
|
| 143 |
+
0
|
| 144 |
+
]
|
| 145 |
+
},
|
| 146 |
+
"class_type": "KSampler (Efficient)",
|
| 147 |
+
"_meta": {
|
| 148 |
+
"title": "KSampler (Efficient)"
|
| 149 |
+
}
|
| 150 |
+
},
|
| 151 |
+
"65": {
|
| 152 |
+
"inputs": {
|
| 153 |
+
"width": 512,
|
| 154 |
+
"height": 512,
|
| 155 |
+
"aspect_ratio": "SD1.5 - 2:3 portrait 512x768",
|
| 156 |
+
"swap_dimensions": "Off",
|
| 157 |
+
"upscale_factor": 1,
|
| 158 |
+
"prescale_factor": 1,
|
| 159 |
+
"batch_size": 1
|
| 160 |
+
},
|
| 161 |
+
"class_type": "CR Aspect Ratio",
|
| 162 |
+
"_meta": {
|
| 163 |
+
"title": "🔳 CR Aspect Ratio"
|
| 164 |
+
}
|
| 165 |
+
},
|
| 166 |
+
"99": {
|
| 167 |
+
"inputs": {
|
| 168 |
+
"filename_prefix": "image",
|
| 169 |
+
"images": [
|
| 170 |
+
"64",
|
| 171 |
+
5
|
| 172 |
+
]
|
| 173 |
+
},
|
| 174 |
+
"class_type": "SaveImage",
|
| 175 |
+
"_meta": {
|
| 176 |
+
"title": "Save Image"
|
| 177 |
+
}
|
| 178 |
+
},
|
| 179 |
+
"101": {
|
| 180 |
+
"inputs": {
|
| 181 |
+
"grid_spacing": 0,
|
| 182 |
+
"XY_flip": "False",
|
| 183 |
+
"Y_label_orientation": "Horizontal",
|
| 184 |
+
"cache_models": "True",
|
| 185 |
+
"ksampler_output_image": "Images",
|
| 186 |
+
"X": [
|
| 187 |
+
"102",
|
| 188 |
+
0
|
| 189 |
+
]
|
| 190 |
+
},
|
| 191 |
+
"class_type": "XY Plot",
|
| 192 |
+
"_meta": {
|
| 193 |
+
"title": "XY Plot"
|
| 194 |
+
}
|
| 195 |
+
},
|
| 196 |
+
"102": {
|
| 197 |
+
"inputs": {
|
| 198 |
+
"batch_count": 1,
|
| 199 |
+
"first_cfg": 4,
|
| 200 |
+
"last_cfg": 4
|
| 201 |
+
},
|
| 202 |
+
"class_type": "XY Input: CFG Scale",
|
| 203 |
+
"_meta": {
|
| 204 |
+
"title": "XY Input: CFG Scale"
|
| 205 |
+
}
|
| 206 |
+
},
|
| 207 |
+
"107": {
|
| 208 |
+
"inputs": {
|
| 209 |
+
"mode": "anime",
|
| 210 |
+
"image": [
|
| 211 |
+
"14",
|
| 212 |
+
0
|
| 213 |
+
]
|
| 214 |
+
},
|
| 215 |
+
"class_type": "Lineart_Detector_Preprocessor",
|
| 216 |
+
"_meta": {
|
| 217 |
+
"title": "Lineart_Detector_Preprocessor"
|
| 218 |
+
}
|
| 219 |
+
},
|
| 220 |
+
"108": {
|
| 221 |
+
"inputs": {
|
| 222 |
+
"images": [
|
| 223 |
+
"107",
|
| 224 |
+
0
|
| 225 |
+
]
|
| 226 |
+
},
|
| 227 |
+
"class_type": "PreviewImage",
|
| 228 |
+
"_meta": {
|
| 229 |
+
"title": "Preview Image"
|
| 230 |
+
}
|
| 231 |
+
},
|
| 232 |
+
"109": {
|
| 233 |
+
"inputs": {
|
| 234 |
+
"include_face": true,
|
| 235 |
+
"include_hand": true,
|
| 236 |
+
"include_body": true,
|
| 237 |
+
"image": [
|
| 238 |
+
"14",
|
| 239 |
+
0
|
| 240 |
+
]
|
| 241 |
+
},
|
| 242 |
+
"class_type": "Openpose_Detector_Preprocessor",
|
| 243 |
+
"_meta": {
|
| 244 |
+
"title": "Openpose_Detector_Preprocessor"
|
| 245 |
+
}
|
| 246 |
+
},
|
| 247 |
+
"110": {
|
| 248 |
+
"inputs": {
|
| 249 |
+
"images": [
|
| 250 |
+
"109",
|
| 251 |
+
0
|
| 252 |
+
]
|
| 253 |
+
},
|
| 254 |
+
"class_type": "PreviewImage",
|
| 255 |
+
"_meta": {
|
| 256 |
+
"title": "Preview Image"
|
| 257 |
+
}
|
| 258 |
+
}
|
| 259 |
+
}
|
| 260 |
+
"""
|
| 261 |
+
|
| 262 |
+
def queue_prompt(prompt):
|
| 263 |
+
p = {"prompt": prompt}
|
| 264 |
+
data = json.dumps(p).encode('utf-8')
|
| 265 |
+
req = request.Request("http://127.0.0.1:8188/prompt", data=data)
|
| 266 |
+
request.urlopen(req)
|
| 267 |
+
|
| 268 |
+
def generate_images(positive_prompt, negative_prompt, seed, image_path):
|
| 269 |
+
prompt = json.loads(prompt_text)
|
| 270 |
+
# Set the text prompt for our positive CLIPTextEncode
|
| 271 |
+
prompt["10"]["inputs"]["positive"] = positive_prompt
|
| 272 |
+
prompt["10"]["inputs"]["negative"] = negative_prompt
|
| 273 |
+
prompt["14"]["inputs"]["image"] = image_path
|
| 274 |
+
|
| 275 |
+
queue_prompt(prompt)
|
| 276 |
+
output_dir = "/Users/paresh/ComfyUI/output/"
|
| 277 |
+
current_count = 56
|
| 278 |
+
next_filename = f"image_{str(current_count + 1).zfill(5)}_.png"
|
| 279 |
+
output_image_path = os.path.join(output_dir, next_filename)
|
| 280 |
+
generated_image = PILImage.open(output_image_path)
|
| 281 |
+
|
| 282 |
+
return generated_image
|
| 283 |
+
|
| 284 |
+
# Gradio Interface
|
| 285 |
+
seed = gr.Number(value=43857297359, label="Seed")
|
| 286 |
+
image = gr.Image(type="filepath", label="Upload Image")
|
| 287 |
+
positive_prompt = gr.Textbox(lines=2, placeholder="Enter positive prompt")
|
| 288 |
+
negative_prompt = gr.Textbox(lines=1, placeholder="Enter negative prompt")
|
| 289 |
+
|
| 290 |
+
interface = gr.Interface(
|
| 291 |
+
fn=generate_images,
|
| 292 |
+
inputs=[positive_prompt, negative_prompt, seed, image],
|
| 293 |
+
outputs="text",
|
| 294 |
+
title="Image Generation with Custom Prompts",
|
| 295 |
+
description="Generate images in the style of GTA/Anime with customizable prompts with a image input.",
|
| 296 |
+
)
|
| 297 |
+
|
| 298 |
+
interface.launch()
|