File size: 5,703 Bytes
19b3da3
0eec7f4
19b3da3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86248f3
 
a3d6c18
1bc457e
19b3da3
 
 
 
 
 
 
a3f5c82
 
 
 
19b3da3
 
 
 
 
b71808f
19b3da3
 
 
469e0ba
 
19b3da3
 
 
 
 
 
 
 
 
 
42ef134
 
 
19b3da3
 
 
86248f3
 
 
 
 
 
19b3da3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f70725b
19b3da3
 
 
 
 
 
 
 
 
 
a3d6c18
 
 
f256b62
 
 
19b3da3
 
 
 
 
 
a3d6c18
 
 
19b3da3
 
 
 
 
 
8aeb9e5
 
 
1bc457e
 
 
049a85c
 
 
19b3da3
 
 
 
 
 
 
 
 
1bc457e
 
 
a3f5c82
 
 
19b3da3
 
0eec7f4
f70725b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0eec7f4
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from enum import Enum
from functools import lru_cache
from typing import Union

import numpy as np


class TaskType(Enum):
    TEXT_TO_IMAGE = "GENERATE_AI_IMAGE"
    IMAGE_TO_IMAGE = "IMAGE_TO_IMAGE"
    POSE = "POSE"
    CANNY = "CANNY"
    REMOVE_BG = "REMOVE_BG"
    INPAINT = "INPAINT"
    UPSCALE_IMAGE = "UPSCALE_IMAGE"
    TILE_UPSCALE = "TILE_UPSCALE"
    OBJECT_REMOVAL = "OBJECT_REMOVAL"
    SCRIBBLE = "SCRIBBLE"
    LINEARART = "LINEARART"
    REPLACE_BG = "REPLACE_BG"
    SYSTEM_CMD = "SYSTEM_CMD"


class ModelType(Enum):
    REAL = 10000
    ANIME = 10001
    COMIC = 10002

    @classmethod
    def _missing_(cls, value):
        return cls.REAL


class Task:
    def __init__(self, data):
        self.__data = data
        if data.get("seed", -1) == None or self.get_seed() == -1:
            self.__data["seed"] = np.random.randint(0, np.iinfo(np.int32).max)
        prompt = data.get("prompt", "")
        if prompt is None:
            self.__data["prompt"] = ""
        elif len(prompt) > 200:
            self.__data["prompt"] = data.get("prompt", "")[:200] + ", "

    def get_taskId(self) -> str:
        return self.__data.get("task_id")

    def get_sourceId(self) -> str:
        return self.__data.get("source_id")

    def get_imageUrl(self) -> str:
        return self.__data.get("imageUrl", None)

    def get_auxilary_imageUrl(self) -> str:
        return self.__data.get("aux_imageUrl", None)

    def get_prompt(self) -> str:
        return self.__data.get("prompt", "")

    def get_prompt_left(self) -> str:
        return self.__data.get("prompt_left", "")

    def get_prompt_right(self) -> str:
        return self.__data.get("prompt_right", "")

    def get_userId(self) -> str:
        return self.__data.get("userId", "")

    def get_email(self) -> str:
        return self.__data.get("email", "")

    def get_style(self) -> str:
        return self.__data.get("style", None)

    def get_iteration(self) -> float:
        return float(self.__data.get("iteration", 3.0))

    def get_modelType(self) -> ModelType:
        id = self.get_model_id()
        return ModelType(id)

    def get_model_id(self) -> int:
        return int(self.__data.get("modelId", 10000))

    def get_width(self) -> int:
        return int(self.__data.get("width", 512))

    def get_height(self) -> int:
        return int(self.__data.get("height", 512))

    def get_seed(self) -> int:
        return int(self.__data.get("seed", -1))

    def get_steps(self) -> int:
        return int(self.__data.get("steps", 30))

    def get_type(self) -> Union[TaskType, None]:
        try:
            return TaskType(self.__data.get("task_type"))
        except ValueError:
            return None

    def get_maskImageUrl(self) -> str:
        return self.__data.get("maskImageUrl")

    def get_pose_coordinates(self) -> dict:
        return self.__data.get("pose_coordinates", None)

    def get_pose_estimation(self) -> bool:
        return self.__data.get("pose_estimation", True)

    def get_negative_prompt(self) -> str:
        return self.__data.get("negative_prompt", "")

    def is_prompt_engineering(self) -> bool:
        return self.__data.get("auto_mode", True)

    def get_image_scale(self) -> float:
        return self.__data.get("image_scale", 1.0)

    def get_queue_name(self) -> str:
        return self.__data.get("queue_name", "")

    def get_resize_dimension(self) -> int:
        return self.__data.get("resize_dimension", 1024)

    def get_face_enhance(self) -> bool:
        return self.__data.get("up_face_enhance", False)

    def rbg_controlnet_conditioning_scale(self) -> float:
        return self.__data.get("rbg_conditioning_scale", 0.5)

    def rbg_extend_object(self) -> bool:
        return self.__data.get("rbg_extend_object", False)

    def get_nsfw_threshold(self) -> float:
        return self.__data.get("nsfw_threshold", 0.03)

    def can_access_nsfw(self) -> bool:
        return self.__data.get("can_access_nsfw", False)

    def get_access_token(self) -> str:
        return self.__data.get("access_token", "")

    def get_high_res_fix(self) -> bool:
        return self.__data.get("high_res_fix", False)

    def get_base_dimension(self) -> int:
        return self.__data.get("base_dimension", 512)

    def get_raw(self) -> dict:
        return self.__data.copy()

    def t2i_kwargs(self) -> dict:
        return dict(self.__get_kwargs("t2i_"))

    def i2i_kwargs(self) -> dict:
        return dict(self.__get_kwargs("i2i_"))

    def ip_kwargs(self) -> dict:
        return dict(self.__get_kwargs("ip_"))

    def cnc_kwargs(self) -> dict:
        return dict(self.__get_kwargs("cnc_"))

    def cnp_kwargs(self) -> dict:
        return dict(self.__get_kwargs("cnp_"))

    def cns_kwargs(self) -> dict:
        return dict(self.__get_kwargs("cns_"))

    def cnl_kwargs(self) -> dict:
        return dict(self.__get_kwargs("cnl_"))

    def cnt_kwargs(self) -> dict:
        return dict(self.__get_kwargs("cnt_"))

    def high_res_kwargs(self) -> dict:
        return dict(self.__get_kwargs("hrf_"))

    def __get_kwargs(self, prefix: str):
        for k, v in self.__data.items():
            if k.startswith(prefix):
                yield k[len(prefix) :], v

    @property
    @lru_cache(1)
    def PROMPT(self):
        class PromptMethods:
            def __init__(self, task: Task):
                self.__task = task

            def has_placeholder_blip_merge(self) -> bool:
                return "<blip:[merge]>" in self.__task.get_prompt()

            def merge_blip(self, text: str) -> str:
                return self.__task.get_prompt().replace("<blip:[merge]>", text)

        return PromptMethods(self)