forked from Jack000/glid-3-xl-stable
-
Notifications
You must be signed in to change notification settings - Fork 2
/
predict.py
127 lines (111 loc) · 3.66 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
# Prediction interface for Cog ⚙️
# https://github.com/replicate/cog/blob/main/docs/python.md
import gc
import os
import shutil
import typing
from cog import BasePredictor, Input, Path
import sample
os.environ["TRANSFORMERS_CACHE"] = "transformers_cache"
class Predictor(BasePredictor):
inpaint_model_args = None
diffusion_model_args = None
def setup(self):
self.inpaint_model_args = sample.do_load(
sample.parser.parse_args(["--model_path", "inpaint.pt"])
)
self.diffusion_model_args = sample.do_load(
sample.parser.parse_args(["--model_path", "diffusion.pt"])
)
def predict(
self,
prompt: str = Input(description="Input prompt"),
num_inference_steps: int = Input(
description="Number of denoising steps.",
ge=1,
le=500,
default=50,
),
init_image: Path = Input(
description="init image to use",
default=None,
),
edit_image: Path = Input(
description="The Image you want to edit. If this is provided, then mask is required.",
default=None,
),
mask: Path = Input(
description="Black and white image to use as mask for inpainting over init_image. "
"White pixels = keep, Black pixels = discard",
default=None,
),
num_outputs: int = Input(
description="Number of images to output",
ge=1,
le=4,
default=1,
),
negative_prompt: str = Input(
description="Negative text prompt",
default=None,
),
outpaint: str = Input(
choices=["expand", "wider", "taller", "left", "right", "top", "bottom"],
default=None,
),
skip_timesteps: int = Input(
description="How many diffusion steps to skip",
default=0,
),
width: int = Input(
description="Output image width (multiple of 8)",
default=512,
),
height: int = Input(
description="Output image height (multiple of 8)",
default=512,
),
) -> typing.List[Path]:
if edit_image:
assert mask, "mask must be provided if edit_image is being used"
args = [
"--model_path",
"inpaint.pt",
"--edit",
str(edit_image),
"--mask",
str(mask),
]
model_args = self.inpaint_model_args
else:
args = ["--model_path", "diffusion.pt"]
model_args = self.diffusion_model_args
if outpaint:
assert (
edit_image
), "edit_image and mask must be provided if outpaint is being used"
args += ["--outpaint", outpaint]
if init_image:
args += ["--init_image", str(init_image)]
if negative_prompt:
args += ["--negative", negative_prompt]
args += [
"--steps",
str(num_inference_steps),
"--text",
prompt,
"--num_batches",
str(num_outputs),
"--skip_timesteps",
str(skip_timesteps),
"--width",
str(width),
"--height",
str(height),
]
print("-> args:", " ".join(args))
shutil.rmtree("output", ignore_errors=True)
shutil.rmtree("output_npy", ignore_errors=True)
gc.collect()
sample.do_run(sample.parser.parse_args(args), *model_args)
return [Path(outfile) for outfile in Path("output").glob("*.png")]