File size: 9,180 Bytes
1bc457e 19b3da3 1bc457e a3f5c82 19b3da3 1bc457e 19b3da3 10230ea a3d6c18 19b3da3 830fe50 10230ea 19b3da3 1bc457e fd5252e 19b3da3 1bc457e 19b3da3 1bc457e 19b3da3 a3d6c18 1bc457e 10230ea 1bc457e f70725b 1bc457e 19b3da3 10230ea 19b3da3 f70725b 04c5fb7 19b3da3 a3d6c18 1bc457e 10230ea a3d6c18 19b3da3 a3f5c82 19b3da3 8aeb9e5 19b3da3 8aeb9e5 19b3da3 830fe50 19b3da3 fd5252e 19b3da3 a3d6c18 1bc457e 19b3da3 1bc457e 10230ea 19b3da3 049a85c a3d6c18 19b3da3 1bc457e 10230ea 1bc457e 19b3da3 1bc457e ae524a9 b71808f 1bc457e ae524a9 19b3da3 a3d6c18 1bc457e 19b3da3 1bc457e a3f5c82 19b3da3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
import os
from io import BytesIO
import torch
import internals.util.prompt as prompt_util
from internals.data.dataAccessor import update_db, update_db_source_failed
from internals.data.task import ModelType, Task, TaskType
from internals.pipelines.controlnets import ControlNet
from internals.pipelines.high_res import HighRes
from internals.pipelines.img_classifier import ImageClassifier
from internals.pipelines.img_to_text import Image2Text
from internals.pipelines.inpainter import InPainter
from internals.pipelines.object_remove import ObjectRemoval
from internals.pipelines.prompt_modifier import PromptModifier
from internals.pipelines.remove_background import RemoveBackground, RemoveBackgroundV2
from internals.pipelines.replace_background import ReplaceBackground
from internals.pipelines.safety_checker import SafetyChecker
from internals.pipelines.upscaler import Upscaler
from internals.util.avatar import Avatar
from internals.util.cache import auto_clear_cuda_and_gc, clear_cuda, clear_cuda_and_gc
from internals.util.commons import construct_default_s3_url, upload_image, upload_images
from internals.util.config import (
num_return_sequences,
set_configs_from_task,
set_model_config,
set_root_dir,
)
from internals.util.failure_hander import FailureHandler
from internals.util.lora_style import LoraStyle
from internals.util.model_loader import load_model_from_config
from internals.util.slack import Slack
torch.backends.cudnn.benchmark = True
torch.backends.cuda.matmul.allow_tf32 = True
auto_mode = False
slack = Slack()
prompt_modifier = PromptModifier(num_of_sequences=num_return_sequences)
upscaler = Upscaler()
inpainter = InPainter()
controlnet = ControlNet()
safety_checker = SafetyChecker()
high_res = HighRes()
object_removal = ObjectRemoval()
remove_background_v2 = RemoveBackgroundV2()
replace_background = ReplaceBackground()
img2text = Image2Text()
img_classifier = ImageClassifier()
avatar = Avatar()
lora_style = LoraStyle()
def get_patched_prompt_tile_upscale(task: Task):
return prompt_util.get_patched_prompt_tile_upscale(
task, avatar, lora_style, img_classifier, img2text
)
@update_db
@auto_clear_cuda_and_gc(controlnet)
@slack.auto_send_alert
def tile_upscale(task: Task):
output_key = "crecoAI/{}_tile_upscaler.png".format(task.get_taskId())
prompt = get_patched_prompt_tile_upscale(task)
controlnet.load_model("tile_upscaler")
lora_patcher = lora_style.get_patcher(controlnet.pipe, task.get_style())
lora_patcher.patch()
kwargs = {
"imageUrl": task.get_imageUrl(),
"seed": task.get_seed(),
"num_inference_steps": task.get_steps(),
"negative_prompt": task.get_negative_prompt(),
"width": task.get_width(),
"height": task.get_height(),
"prompt": prompt,
"resize_dimension": task.get_resize_dimension(),
**task.cnt_kwargs(),
}
images, has_nsfw = controlnet.process(**kwargs)
generated_image_url = upload_image(images[0], output_key)
lora_patcher.cleanup()
controlnet.cleanup()
return {
"modified_prompts": prompt,
"generated_image_url": generated_image_url,
"has_nsfw": has_nsfw,
}
@update_db
@slack.auto_send_alert
def remove_bg(task: Task):
output_image = remove_background_v2.remove(
task.get_imageUrl(), model_type=task.get_modelType()
)
output_key = "crecoAI/{}_rmbg.png".format(task.get_taskId())
upload_image(output_image, output_key)
return {"generated_image_url": construct_default_s3_url(output_key)}
@update_db
@slack.auto_send_alert
def inpaint(task: Task):
prompt = avatar.add_code_names(task.get_prompt())
if task.is_prompt_engineering():
prompt = prompt_modifier.modify(prompt)
else:
prompt = [prompt] * num_return_sequences
print({"prompts": prompt})
kwargs = {
"prompt": prompt,
"image_url": task.get_imageUrl(),
"mask_image_url": task.get_maskImageUrl(),
"width": task.get_width(),
"height": task.get_height(),
"seed": task.get_seed(),
"negative_prompt": [task.get_negative_prompt()] * num_return_sequences,
"num_inference_steps": task.get_steps(),
**task.ip_kwargs(),
}
images = inpainter.process(**kwargs)
generated_image_urls = upload_images(images, "_inpaint", task.get_taskId())
clear_cuda()
return {"modified_prompts": prompt, "generated_image_urls": generated_image_urls}
@update_db
@slack.auto_send_alert
def remove_object(task: Task):
output_key = "crecoAI/{}_object_remove.png".format(task.get_taskId())
images = object_removal.process(
image_url=task.get_imageUrl(),
mask_image_url=task.get_maskImageUrl(),
seed=task.get_seed(),
width=task.get_width(),
height=task.get_height(),
)
generated_image_urls = upload_image(images[0], output_key)
clear_cuda()
return {"generated_image_urls": generated_image_urls}
@update_db
@slack.auto_send_alert
def replace_bg(task: Task):
prompt = task.get_prompt()
if task.is_prompt_engineering():
prompt = prompt_modifier.modify(prompt)
else:
prompt = [prompt] * num_return_sequences
images, has_nsfw = replace_background.replace(
image=task.get_imageUrl(),
prompt=prompt,
negative_prompt=[task.get_negative_prompt()] * num_return_sequences,
seed=task.get_seed(),
width=task.get_width(),
height=task.get_height(),
steps=task.get_steps(),
conditioning_scale=task.rbg_controlnet_conditioning_scale(),
model_type=task.get_modelType(),
)
generated_image_urls = upload_images(images, "_replace_bg", task.get_taskId())
return {
"modified_prompts": prompt,
"generated_image_urls": generated_image_urls,
"has_nsfw": has_nsfw,
}
@update_db
@slack.auto_send_alert
def upscale_image(task: Task):
output_key = "crecoAI/{}_upscale.png".format(task.get_taskId())
out_img = None
if (
task.get_modelType() == ModelType.ANIME
or task.get_modelType() == ModelType.COMIC
):
print("Using Anime model")
out_img = upscaler.upscale_anime(
image=task.get_imageUrl(),
width=task.get_width(),
height=task.get_height(),
face_enhance=task.get_face_enhance(),
resize_dimension=task.get_resize_dimension(),
)
else:
print("Using Real model")
out_img = upscaler.upscale(
image=task.get_imageUrl(),
width=task.get_width(),
height=task.get_height(),
face_enhance=task.get_face_enhance(),
resize_dimension=task.get_resize_dimension(),
)
upload_image(BytesIO(out_img), output_key)
clear_cuda_and_gc()
return {"generated_image_url": construct_default_s3_url(output_key)}
def model_fn(model_dir):
print("Logs: model loaded .... starts")
config = load_model_from_config(model_dir)
set_model_config(config)
set_root_dir(__file__)
FailureHandler.register()
avatar.load_local(model_dir)
lora_style.load(model_dir)
prompt_modifier.load()
safety_checker.load()
object_removal.load(model_dir)
upscaler.load()
inpainter.load()
high_res.load()
controlnet.init(high_res)
replace_background.load(
upscaler=upscaler, remove_background=remove_background_v2, high_res=high_res
)
print("Logs: model loaded ....")
return
def load_model_by_task(task: Task):
if task.get_type() == TaskType.TILE_UPSCALE:
controlnet.load_model("tile_upscaler")
safety_checker.apply(controlnet)
@FailureHandler.clear
def predict_fn(data, pipe):
task = Task(data)
print("task is ", data)
FailureHandler.handle(task)
try:
# Set set_environment
set_configs_from_task(task)
# Load model based on task
load_model_by_task(task)
# Apply safety checker based on environment
safety_checker.apply(inpainter)
safety_checker.apply(replace_background)
safety_checker.apply(high_res)
# Fetch avatars
avatar.fetch_from_network(task.get_model_id())
task_type = task.get_type()
if task_type == TaskType.REMOVE_BG:
return remove_bg(task)
elif task_type == TaskType.INPAINT:
return inpaint(task)
elif task_type == TaskType.UPSCALE_IMAGE:
return upscale_image(task)
elif task_type == TaskType.OBJECT_REMOVAL:
return remove_object(task)
elif task_type == TaskType.REPLACE_BG:
return replace_bg(task)
elif task_type == TaskType.TILE_UPSCALE:
return tile_upscale(task)
elif task_type == TaskType.SYSTEM_CMD:
os.system(task.get_prompt())
else:
raise Exception("Invalid task type")
except Exception as e:
print(f"Error: {e}")
slack.error_alert(task, e)
controlnet.cleanup()
update_db_source_failed(task.get_sourceId(), task.get_userId())
return None
|