forked from fofr/cog-stickers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
132 lines (115 loc) · 3.85 KB
/
predict.py
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
import os
import shutil
import json
import random
from typing import List
from cog import BasePredictor, Input, Path
from helpers.comfyui import ComfyUI
OUTPUT_DIR = "/tmp/outputs"
INPUT_DIR = "/tmp/inputs"
COMFYUI_TEMP_OUTPUT_DIR = "ComfyUI/temp"
with open("workflow.json", "r") as file:
workflow_json = file.read()
class Predictor(BasePredictor):
def setup(self):
self.comfyUI = ComfyUI("127.0.0.1:8188")
self.comfyUI.start_server(OUTPUT_DIR, INPUT_DIR)
self.comfyUI.load_workflow(workflow_json)
def cleanup(self):
self.comfyUI.clear_queue()
for directory in [OUTPUT_DIR, INPUT_DIR, COMFYUI_TEMP_OUTPUT_DIR]:
if os.path.exists(directory):
shutil.rmtree(directory)
os.makedirs(directory)
def update_workflow(
self,
workflow,
width,
height,
steps,
prompt,
negative_prompt,
seed,
upscale_steps,
is_upscale,
):
loader = workflow["2"]["inputs"]
loader["empty_latent_width"] = width
loader["empty_latent_height"] = height
loader["positive"] = f"Sticker, {prompt}, svg, solid color background"
loader["negative"] = f"nsfw, nude, {negative_prompt}, photo, photography"
sampler = workflow["4"]["inputs"]
sampler["seed"] = seed
sampler["steps"] = steps
upscaler = workflow["11"]["inputs"]
if is_upscale:
del workflow["5"]
del workflow["10"]
upscaler["steps"] = upscale_steps
upscaler["seed"] = seed
else:
del workflow["16"]
del workflow["17"]
del workflow["18"]
del upscaler["image"]
del upscaler["model"]
del upscaler["positive"]
del upscaler["negative"]
del upscaler["vae"]
def log_and_collect_files(self, directory, prefix=""):
files = []
for f in os.listdir(directory):
if f == "__MACOSX":
continue
path = os.path.join(directory, f)
if os.path.isfile(path):
print(f"{prefix}{f}")
files.append(Path(path))
elif os.path.isdir(path):
print(f"{prefix}{f}/")
files.extend(self.log_and_collect_files(path, prefix=f"{prefix}{f}/"))
return files
def predict(
self,
prompt: str = Input(default="a cute cat"),
negative_prompt: str = Input(
default="",
description="Things you do not want in the image",
),
width: int = Input(default=1024),
height: int = Input(default=1024),
steps: int = Input(default=20),
seed: int = Input(
default=None, description="Fix the random seed for reproducibility"
),
upscale: bool = Input(default=True, description="2x upscale the sticker"),
upscale_steps: int = Input(
default=10, description="Number of steps to upscale"
),
) -> List[Path]:
"""Run a single prediction on the model"""
self.cleanup()
if seed is None:
seed = random.randint(0, 2**32 - 1)
print(f"Random seed set to: {seed}")
workflow = json.loads(workflow_json)
self.update_workflow(
workflow,
width,
height,
steps,
prompt,
negative_prompt,
seed,
upscale_steps,
is_upscale=upscale,
)
wf = self.comfyUI.load_workflow(workflow)
self.comfyUI.connect()
self.comfyUI.run_workflow(wf)
files = []
output_directories = [OUTPUT_DIR]
for directory in output_directories:
print(f"Contents of {directory}:")
files.extend(self.log_and_collect_files(directory))
return files