Update main.py
Browse files
main.py
CHANGED
@@ -33,7 +33,7 @@ url_pre = "https://ap-east-1.tensorart.cloud/v1"
|
|
33 |
SAVE_DIR = "generated_images"
|
34 |
Path(SAVE_DIR).mkdir(exist_ok=True)
|
35 |
|
36 |
-
# Danh sách sản phẩm
|
37 |
PRODUCT_GROUPS = {
|
38 |
"Standard": {
|
39 |
"C1012 Glacier White": "817687427545199895",
|
@@ -48,30 +48,8 @@ PRODUCT_GROUPS = {
|
|
48 |
"C3105 Casla Cloudy": "828912225788997963",
|
49 |
"C3146 Casla Nova": "828013009961087650",
|
50 |
"C2240 Marquin": "828085015087780649",
|
51 |
-
"C2262 Concrete (Honed)": "822211862058871636",
|
52 |
-
"C3311 Calacatta Sky": "829984593223502930",
|
53 |
-
"C3346 Massimo": "827938741386607132",
|
54 |
-
},
|
55 |
-
"Luxury": {
|
56 |
-
"C4143 Mario": "829984593223502930",
|
57 |
-
"C4145 Marina": "828132560375742058",
|
58 |
-
"C4202 Calacatta Gold": "828167757632695310",
|
59 |
-
"C1205 Casla Everest": "828296778450463190",
|
60 |
-
"C4211 Calacatta Supreme": "828436321937882328",
|
61 |
-
"C4204 Calacatta Classic": "828422973179466146",
|
62 |
-
"C5240 Spring": "is coming",
|
63 |
-
"C1102 Super White": "828545723344775887",
|
64 |
-
"C4246 Casla Mystery": "828544778451950698",
|
65 |
-
"C4345 Oro": "828891068780182635",
|
66 |
-
"C4346 Luxe": "829436426547535131",
|
67 |
-
"C4342 Casla Eternal": "829190256201829181",
|
68 |
-
"C4221 Athena": "829644354504131520",
|
69 |
-
"C4222 Lagoon": "is coming",
|
70 |
-
"C5225 Amber": "is coming",
|
71 |
-
},
|
72 |
-
"Super Luxury": {
|
73 |
-
"C4255 Calacatta Extra": "829659013227537217",
|
74 |
},
|
|
|
75 |
}
|
76 |
|
77 |
PRODUCT_IMAGE_MAP = {
|
@@ -199,125 +177,31 @@ def txt2img(prompt, width, height, product_codes):
|
|
199 |
def generate_mask(image_resource_id, position, selected_product_code):
|
200 |
try:
|
201 |
if not image_resource_id:
|
202 |
-
raise Exception("
|
203 |
-
|
204 |
-
time.sleep(10) # Đợi đồng bộ tài nguyên
|
205 |
-
|
206 |
short_code = selected_product_code.split()[0]
|
207 |
texture_filepath = PRODUCT_IMAGE_MAP.get(selected_product_code)
|
208 |
-
print(f"Texture file: {texture_filepath}, exists: {os.path.exists(texture_filepath)}")
|
209 |
if not texture_filepath or not os.path.exists(texture_filepath):
|
210 |
-
raise Exception(f"
|
211 |
|
212 |
texture_resource_id = upload_image_to_tensorart(texture_filepath)
|
213 |
-
print(f"Texture resource_id: {texture_resource_id}")
|
214 |
if not texture_resource_id:
|
215 |
-
raise Exception(f"
|
216 |
-
time.sleep(10)
|
217 |
-
|
218 |
-
if isinstance(position, (set, list)):
|
219 |
-
position = position[0] if position else "default"
|
220 |
-
print(f"Position: {position}, type: {type(position)}")
|
221 |
|
222 |
-
# Dùng params đúng như mẫu TensorArt
|
223 |
workflow_params = {
|
224 |
-
"1": {
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
"detail_erode": 65,
|
230 |
-
"detail_method": "GuidedFilter",
|
231 |
-
"device": "cuda",
|
232 |
-
"image": ["2", 0],
|
233 |
-
"max_megapixels": 2,
|
234 |
-
"process_detail": True,
|
235 |
-
"prompt": ["4", 0],
|
236 |
-
"sam_models": ["3", 0],
|
237 |
-
"threshold": 0.3,
|
238 |
-
"white_point": 0.99
|
239 |
-
},
|
240 |
-
"properties": {"Node name for S&R": "LayerMask: SegmentAnythingUltra V3"}
|
241 |
-
},
|
242 |
-
"10": {
|
243 |
-
"classType": "Image Seamless Texture",
|
244 |
-
"inputs": {
|
245 |
-
"blending": 0.37,
|
246 |
-
"images": ["17", 0],
|
247 |
-
"tiled": "true",
|
248 |
-
"tiles": 2
|
249 |
-
},
|
250 |
-
"properties": {"Node name for S&R": "Image Seamless Texture"}
|
251 |
-
},
|
252 |
-
"13": {
|
253 |
-
"classType": "Paste By Mask",
|
254 |
-
"inputs": {
|
255 |
-
"image_base": ["2", 0],
|
256 |
-
"image_to_paste": ["10", 0],
|
257 |
-
"mask": ["8", 0],
|
258 |
-
"resize_behavior": "resize"
|
259 |
-
},
|
260 |
-
"properties": {"Node name for S&R": "Paste By Mask"}
|
261 |
-
},
|
262 |
-
"17": {
|
263 |
-
"classType": "TensorArt_LoadImage",
|
264 |
-
"inputs": {
|
265 |
-
"_height": 768,
|
266 |
-
"_width": 512,
|
267 |
-
"image": texture_resource_id,
|
268 |
-
"upload": "image"
|
269 |
-
},
|
270 |
-
"properties": {"Node name for S&R": "TensorArt_LoadImage"}
|
271 |
-
},
|
272 |
-
"2": {
|
273 |
-
"classType": "TensorArt_LoadImage",
|
274 |
-
"inputs": {
|
275 |
-
"_height": 1024,
|
276 |
-
"_width": 768,
|
277 |
-
"image": image_resource_id,
|
278 |
-
"upload": "image"
|
279 |
-
},
|
280 |
-
"properties": {"Node name for S&R": "TensorArt_LoadImage"}
|
281 |
-
},
|
282 |
-
"3": {
|
283 |
-
"classType": "LayerMask: LoadSegmentAnythingModels",
|
284 |
-
"inputs": {
|
285 |
-
"grounding_dino_model": "GroundingDINO_SwinB (938MB)",
|
286 |
-
"sam_model": "sam_vit_h (2.56GB)"
|
287 |
-
},
|
288 |
-
"properties": {"Node name for S&R": "LayerMask: LoadSegmentAnythingModels"}
|
289 |
-
},
|
290 |
-
"4": {
|
291 |
-
"classType": "TensorArt_PromptText",
|
292 |
-
"inputs": {"Text": position.lower()},
|
293 |
-
"properties": {"Node name for S&R": "TensorArt_PromptText"}
|
294 |
-
},
|
295 |
-
"7": {
|
296 |
-
"classType": "PreviewImage",
|
297 |
-
"inputs": {"images": ["13", 0]},
|
298 |
-
"properties": {"Node name for S&R": "PreviewImage"}
|
299 |
-
},
|
300 |
-
"8": {
|
301 |
-
"classType": "MaskToImage",
|
302 |
-
"inputs": {"mask": ["1", 1]},
|
303 |
-
"properties": {"Node name for S&R": "MaskToImage"}
|
304 |
-
}
|
305 |
-
}
|
306 |
-
|
307 |
-
payload = {
|
308 |
-
"requestId": f"workflow_{int(time.time())}",
|
309 |
-
"params": workflow_params,
|
310 |
-
"runningNotifyUrl": ""
|
311 |
}
|
312 |
-
|
313 |
-
|
314 |
-
return output_path
|
315 |
-
|
316 |
except Exception as e:
|
317 |
print(f"Mask generation error: {str(e)}")
|
318 |
return None
|
319 |
|
320 |
-
|
321 |
def run_workflow(payload, step_name):
|
322 |
headers = {
|
323 |
'Content-Type': 'application/json',
|
@@ -389,5 +273,5 @@ with gr.Blocks() as demo:
|
|
389 |
# Khởi chạy Gradio với API
|
390 |
demo.launch(
|
391 |
server_name="0.0.0.0",
|
392 |
-
server_port=7860
|
393 |
)
|
|
|
33 |
SAVE_DIR = "generated_images"
|
34 |
Path(SAVE_DIR).mkdir(exist_ok=True)
|
35 |
|
36 |
+
# Danh sách sản phẩm
|
37 |
PRODUCT_GROUPS = {
|
38 |
"Standard": {
|
39 |
"C1012 Glacier White": "817687427545199895",
|
|
|
48 |
"C3105 Casla Cloudy": "828912225788997963",
|
49 |
"C3146 Casla Nova": "828013009961087650",
|
50 |
"C2240 Marquin": "828085015087780649",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
},
|
52 |
+
# Thêm các nhóm khác nếu cần
|
53 |
}
|
54 |
|
55 |
PRODUCT_IMAGE_MAP = {
|
|
|
177 |
def generate_mask(image_resource_id, position, selected_product_code):
|
178 |
try:
|
179 |
if not image_resource_id:
|
180 |
+
raise Exception("Invalid image_resource_id")
|
181 |
+
time.sleep(10)
|
|
|
|
|
182 |
short_code = selected_product_code.split()[0]
|
183 |
texture_filepath = PRODUCT_IMAGE_MAP.get(selected_product_code)
|
|
|
184 |
if not texture_filepath or not os.path.exists(texture_filepath):
|
185 |
+
raise Exception(f"Texture file not found for {short_code}")
|
186 |
|
187 |
texture_resource_id = upload_image_to_tensorart(texture_filepath)
|
|
|
188 |
if not texture_resource_id:
|
189 |
+
raise Exception(f"Failed to upload texture for {short_code}")
|
190 |
+
time.sleep(10)
|
|
|
|
|
|
|
|
|
191 |
|
|
|
192 |
workflow_params = {
|
193 |
+
"1": { "classType": "LayerMask: SegmentAnythingUltra V3", "inputs": {"image": ["2", 0], "prompt": ["4", 0]}},
|
194 |
+
"2": {"classType": "TensorArt_LoadImage", "inputs": {"image": image_resource_id}},
|
195 |
+
"4": {"classType": "TensorArt_PromptText", "inputs": {"Text": position.lower()}},
|
196 |
+
"17": {"classType": "TensorArt_LoadImage", "inputs": {"image": texture_resource_id}},
|
197 |
+
# Thêm các node khác nếu cần từ mã gốc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
}
|
199 |
+
payload = {"requestId": f"workflow_{int(time.time())}", "params": workflow_params}
|
200 |
+
return run_workflow(payload, "full_workflow")
|
|
|
|
|
201 |
except Exception as e:
|
202 |
print(f"Mask generation error: {str(e)}")
|
203 |
return None
|
204 |
|
|
|
205 |
def run_workflow(payload, step_name):
|
206 |
headers = {
|
207 |
'Content-Type': 'application/json',
|
|
|
273 |
# Khởi chạy Gradio với API
|
274 |
demo.launch(
|
275 |
server_name="0.0.0.0",
|
276 |
+
server_port=7860,
|
277 |
)
|