forked from bakabtw/Fooocus-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
181 lines (160 loc) · 9.62 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
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
# Prediction interface for Cog ⚙️
# https://github.com/replicate/cog/blob/main/docs/python.md
import os
import sys
from typing import List
from cog import BasePredictor, Input, Path
from fooocusapi.parameters import GenerationFinishReason, ImageGenerationParams, available_aspect_ratios, uov_methods, outpaint_expansions, defualt_styles, default_base_model_name, default_refiner_model_name, default_loras, default_refiner_switch, default_cfg_scale, default_prompt_negative
from fooocusapi.task_queue import TaskType
from fooocusapi.worker import process_generate, task_queue
from fooocusapi.file_utils import output_dir
import numpy as np
from PIL import Image
class Predictor(BasePredictor):
def setup(self) -> None:
"""Load the model into memory to make running multiple predictions efficient"""
from main import pre_setup
pre_setup(disable_private_log=True, skip_pip=True, preload_pipeline=True, preset=None)
def predict(
self,
prompt: str = Input(
default='', description="Prompt for image generation"),
negative_prompt: str = Input(
default=default_prompt_negative, description="Negtive prompt for image generation"),
style_selections: str = Input(default=','.join(defualt_styles),
description="Fooocus styles applied for image generation, seperated by comma"),
performance_selection: str = Input(
default='Speed', description="Performance selection", choices=['Speed', 'Quality', 'Extreme Speed']),
aspect_ratios_selection: str = Input(
default='1152×896', description="The generated image's size", choices=available_aspect_ratios),
image_number: int = Input(
default=1, description="How many image to generate", ge=1, le=8),
image_seed: int = Input(
default=-1, description="Seed to generate image, -1 for random"),
sharpness: float = Input(default=2.0, ge=0.0, le=30.0),
guidance_scale: float = Input(default=default_cfg_scale, ge=1.0, le=30.0),
refiner_switch: float = Input(default=default_refiner_switch, ge=0.1, le=1.0),
uov_input_image: Path = Input(
default=None, description="Input image for upscale or variation, keep None for not upscale or variation"),
uov_method: str = Input(default='Disabled', choices=uov_methods),
inpaint_input_image: Path = Input(
default=None, description="Input image for inpaint or outpaint, keep None for not inpaint or outpaint. Please noticed, `uov_input_image` has bigger priority is not None."),
inpaint_input_mask: Path = Input(
default=None, description="Input mask for inpaint"),
outpaint_selections: str = Input(
default='', description="Outpaint expansion selections, literal 'Left', 'Right', 'Top', 'Bottom' seperated by comma"),
cn_img1: Path = Input(
default=None, description="Input image for image prompt. If all cn_img[n] are None, image prompt will not applied."),
cn_stop1: float = Input(
default=None, ge=0, le=1, description="Stop at for image prompt, None for default value"),
cn_weight1: float = Input(
default=None, ge=0, le=2, description="Weight for image prompt, None for default value"),
cn_type1: str = Input(default='ImagePrompt', description="ControlNet type for image prompt", choices=[
'ImagePrompt', 'FaceSwap', 'PyraCanny', 'CPDS']),
cn_img2: Path = Input(
default=None, description="Input image for image prompt. If all cn_img[n] are None, image prompt will not applied."),
cn_stop2: float = Input(
default=None, ge=0, le=1, description="Stop at for image prompt, None for default value"),
cn_weight2: float = Input(
default=None, ge=0, le=2, description="Weight for image prompt, None for default value"),
cn_type2: str = Input(default='ImagePrompt', description="ControlNet type for image prompt", choices=[
'ImagePrompt', 'FaceSwap', 'PyraCanny', 'CPDS']),
cn_img3: Path = Input(
default=None, description="Input image for image prompt. If all cn_img[n] are None, image prompt will not applied."),
cn_stop3: float = Input(
default=None, ge=0, le=1, description="Stop at for image prompt, None for default value"),
cn_weight3: float = Input(
default=None, ge=0, le=2, description="Weight for image prompt, None for default value"),
cn_type3: str = Input(default='ImagePrompt', description="ControlNet type for image prompt", choices=[
'ImagePrompt', 'FaceSwap', 'PyraCanny', 'CPDS']),
cn_img4: Path = Input(
default=None, description="Input image for image prompt. If all cn_img[n] are None, image prompt will not applied."),
cn_stop4: float = Input(
default=None, ge=0, le=1, description="Stop at for image prompt, None for default value"),
cn_weight4: float = Input(
default=None, ge=0, le=2, description="Weight for image prompt, None for default value"),
cn_type4: str = Input(default='ImagePrompt', description="ControlNet type for image prompt", choices=[
'ImagePrompt', 'FaceSwap', 'PyraCanny', 'CPDS']),
) -> List[Path]:
"""Run a single prediction on the model"""
import modules.flags as flags
from modules.sdxl_styles import legal_style_names
base_model_name = default_base_model_name
refiner_model_name = default_refiner_model_name
loras = default_loras
style_selections_arr = []
for s in style_selections.strip().split(','):
style = s.strip()
if style in legal_style_names:
style_selections_arr.append(style)
if uov_input_image is not None:
im = Image.open(str(uov_input_image))
uov_input_image = np.array(im)
inpaint_input_image_dict = None
if inpaint_input_image is not None:
im = Image.open(str(inpaint_input_image))
inpaint_input_image = np.array(im)
if inpaint_input_mask is not None:
im = Image.open(str(inpaint_input_mask))
inpaint_input_mask = np.array(im)
inpaint_input_image_dict = {
'image': inpaint_input_image,
'mask': inpaint_input_mask
}
outpaint_selections_arr = []
for e in outpaint_selections.strip().split(','):
expansion = e.strip()
if expansion in outpaint_expansions:
outpaint_selections_arr.append(expansion)
image_prompts = []
image_prompt_config = [(cn_img1, cn_stop1, cn_weight1, cn_type1), (cn_img2, cn_stop2, cn_weight2, cn_type2),
(cn_img3, cn_stop3, cn_weight3, cn_type3), (cn_img4, cn_stop4, cn_weight4, cn_type4)]
for config in image_prompt_config:
cn_img, cn_stop, cn_weight, cn_type = config
if cn_img is not None:
im = Image.open(str(cn_img))
cn_img = np.array(im)
if cn_stop is None:
cn_stop = flags.default_parameters[cn_type][0]
if cn_weight is None:
cn_weight = flags.default_parameters[cn_type][1]
image_prompts.append((cn_img, cn_stop, cn_weight, cn_type))
advanced_params = None
params = ImageGenerationParams(prompt=prompt,
negative_prompt=negative_prompt,
style_selections=style_selections_arr,
performance_selection=performance_selection,
aspect_ratios_selection=aspect_ratios_selection,
image_number=image_number,
image_seed=image_seed,
sharpness=sharpness,
guidance_scale=guidance_scale,
base_model_name=base_model_name,
refiner_model_name=refiner_model_name,
refiner_switch=refiner_switch,
loras=loras,
uov_input_image=uov_input_image,
uov_method=uov_method,
outpaint_selections=outpaint_selections_arr,
inpaint_input_image=inpaint_input_image_dict,
image_prompts=image_prompts,
advanced_params=advanced_params
)
print(f"[Predictor Predict] Params: {params.__dict__}")
queue_task = task_queue.add_task(TaskType.text_2_img, {'params': params.__dict__, 'require_base64': False})
if queue_task is None:
print("[Task Queue] The task queue has reached limit")
raise Exception(
f"The task queue has reached limit."
)
results = process_generate(queue_task, params)
output_paths: List[Path] = []
for r in results:
if r.finish_reason == GenerationFinishReason.success and r.im is not None:
output_paths.append(Path(os.path.join(output_dir, r.im)))
print(f"[Predictor Predict] Finished with {len(output_paths)} images")
if len(output_paths) == 0:
raise Exception(
f"Process failed."
)
return output_paths