Spaces:
Sleeping
Sleeping
Commit
·
692f363
1
Parent(s):
60493a2
add: custom error hadling
Browse files- app.py +1 -1
- src/api/image_prep_api.py +162 -91
- src/api/image_regeneration_api.py +28 -10
app.py
CHANGED
@@ -22,4 +22,4 @@ app.add_middleware(
|
|
22 |
allow_credentials=True,
|
23 |
allow_methods=["*"],
|
24 |
allow_headers=["*"],
|
25 |
-
)
|
|
|
22 |
allow_credentials=True,
|
23 |
allow_methods=["*"],
|
24 |
allow_headers=["*"],
|
25 |
+
)
|
src/api/image_prep_api.py
CHANGED
@@ -49,173 +49,244 @@ def replicate_enhancer(input):
|
|
49 |
|
50 |
@preprocessing_router.post("/rem_bg")
|
51 |
async def remove_background(image: UploadFile = File(...)):
|
52 |
-
|
53 |
-
image = Image.open(BytesIO(image_bytes)).convert("RGB")
|
54 |
-
act_img_base_64 = BytesIO()
|
55 |
-
image.save(act_img_base_64, format="WEBP")
|
56 |
-
image_bytes_ = base64.b64encode(act_img_base_64.getvalue()).decode("utf-8")
|
57 |
|
58 |
-
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
68 |
|
69 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
response = {
|
71 |
"output": f"{base64_prefix}{base_64}",
|
72 |
-
|
|
|
73 |
}
|
74 |
-
|
75 |
return JSONResponse(content=response, status_code=200)
|
76 |
|
77 |
except Exception as e:
|
78 |
-
|
|
|
79 |
|
80 |
|
81 |
@preprocessing_router.post("/upscale_image")
|
82 |
async def upscale_image(image: UploadFile = File(...), scale: int = 1):
|
83 |
-
|
84 |
-
image = Image.open(BytesIO(image_bytes)).convert("RGBA")
|
85 |
-
act_img_base_64 = BytesIO()
|
86 |
-
image.save(act_img_base_64, format="PNG")
|
87 |
-
image_bytes_ = base64.b64encode(act_img_base_64.getvalue()).decode("utf-8")
|
88 |
-
|
89 |
-
image_data_uri = f"data:image/png;base64,{image_bytes_}"
|
90 |
|
91 |
-
|
92 |
-
|
93 |
-
"
|
94 |
-
|
|
|
95 |
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
response = {
|
105 |
"output": f"{base64_prefix}{base_64}",
|
106 |
-
|
|
|
107 |
}
|
108 |
-
|
109 |
return JSONResponse(content=response, status_code=200)
|
110 |
|
111 |
except Exception as e:
|
112 |
-
|
|
|
113 |
|
114 |
|
115 |
@preprocessing_router.post("/crop_transparent")
|
116 |
async def crop_transparent(image: UploadFile):
|
117 |
-
|
118 |
-
raise HTTPException(status_code=400, detail="Only PNG files are supported")
|
119 |
|
120 |
try:
|
121 |
-
|
|
|
|
|
|
|
|
|
|
|
122 |
|
|
|
|
|
123 |
cropped_image_bytes, metadata = crop_transparent_image(contents)
|
|
|
|
|
|
|
124 |
|
|
|
125 |
base64_image = base64.b64encode(cropped_image_bytes).decode('utf-8')
|
126 |
base64_prefix = "data:image/png;base64,"
|
127 |
|
128 |
-
|
|
|
|
|
129 |
"status": "success",
|
|
|
130 |
"data": {
|
131 |
"image": f"{base64_prefix}{base64_image}",
|
132 |
-
"metadata": metadata
|
|
|
133 |
}
|
134 |
-
}
|
135 |
-
|
136 |
-
except ValueError as e:
|
137 |
-
raise HTTPException(status_code=400, detail=str(e))
|
138 |
except Exception as e:
|
139 |
-
|
|
|
140 |
|
141 |
|
142 |
@preprocessing_router.post("/background_replace")
|
143 |
async def bg_replace(image: UploadFile = File(...), bg_image: UploadFile = File(...)):
|
144 |
-
|
145 |
-
bg_image = await bg_image.read()
|
146 |
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
|
156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
|
158 |
try:
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
}
|
163 |
|
164 |
-
|
165 |
|
|
|
|
|
|
|
|
|
|
|
166 |
except Exception as e:
|
167 |
-
|
|
|
168 |
|
169 |
|
170 |
@preprocessing_router.post("/rem_bg_color_extraction")
|
171 |
-
async def remove_background_color_extraction(image: UploadFile = File(...),
|
|
|
172 |
threshold: int = 30):
|
173 |
-
|
174 |
-
image = Image.open(BytesIO(image_bytes)).convert("RGBA")
|
175 |
-
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
176 |
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
|
|
|
|
182 |
|
183 |
-
|
|
|
|
|
|
|
|
|
|
|
184 |
|
185 |
try:
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
}
|
190 |
|
191 |
-
|
192 |
|
|
|
|
|
|
|
|
|
|
|
193 |
except Exception as e:
|
194 |
-
|
|
|
195 |
|
196 |
|
197 |
@preprocessing_router.post("/title_description_generator")
|
198 |
async def product_title_description_generator(image: UploadFile = File(...)):
|
199 |
start_time = time.time()
|
200 |
-
|
201 |
-
image = Image.open(BytesIO(image_bytes)).convert("RGB")
|
202 |
try:
|
203 |
-
|
204 |
-
|
|
|
|
|
|
|
205 |
|
|
|
|
|
206 |
title = result.split("Title:")[1].split("Description:")[0]
|
207 |
description = result.split("Description:")[1]
|
208 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
209 |
|
210 |
-
|
211 |
"code": 200,
|
212 |
"title": title,
|
213 |
"description": description,
|
214 |
-
"inference_time":
|
215 |
-
}
|
216 |
-
|
217 |
-
return JSONResponse(content=result, status_code=200)
|
218 |
-
|
219 |
except Exception as e:
|
220 |
-
|
221 |
-
|
|
|
49 |
|
50 |
@preprocessing_router.post("/rem_bg")
|
51 |
async def remove_background(image: UploadFile = File(...)):
|
52 |
+
start_time = time.time()
|
|
|
|
|
|
|
|
|
53 |
|
54 |
+
try:
|
55 |
+
image_bytes = await image.read()
|
56 |
+
image = Image.open(BytesIO(image_bytes)).convert("RGB")
|
57 |
+
except Exception as e:
|
58 |
+
return JSONResponse(status_code=500, content={"error": f"Error reading image: {str(e)}", "code": 500})
|
59 |
|
60 |
+
try:
|
61 |
+
act_img_base_64 = BytesIO()
|
62 |
+
image.save(act_img_base_64, format="WEBP")
|
63 |
+
image_bytes_ = base64.b64encode(act_img_base_64.getvalue()).decode("utf-8")
|
64 |
+
image_data_uri = f"data:image/WEBP;base64,{image_bytes_}"
|
65 |
+
except Exception as e:
|
66 |
+
return JSONResponse(status_code=500,
|
67 |
+
content={"error": f"Error converting image to base64: {str(e)}", "code": 500})
|
68 |
|
69 |
+
try:
|
70 |
+
output = replicate_bg({"image": image_data_uri})
|
71 |
+
except Exception as e:
|
72 |
+
return JSONResponse(status_code=500,
|
73 |
+
content={"error": f"Error running background removal: {str(e)}", "code": 500})
|
74 |
|
75 |
try:
|
76 |
+
response = requests.get(output)
|
77 |
+
base_64 = base64.b64encode(response.content).decode('utf-8')
|
78 |
+
base64_prefix = "data:image/WEBP;base64,"
|
79 |
+
|
80 |
+
total_inference_time = round((time.time() - start_time), 2)
|
81 |
+
|
82 |
response = {
|
83 |
"output": f"{base64_prefix}{base_64}",
|
84 |
+
"inference_time": total_inference_time,
|
85 |
+
"code": 200
|
86 |
}
|
|
|
87 |
return JSONResponse(content=response, status_code=200)
|
88 |
|
89 |
except Exception as e:
|
90 |
+
return JSONResponse(status_code=500,
|
91 |
+
content={"error": f"Error processing response: {str(e)}", "code": 500})
|
92 |
|
93 |
|
94 |
@preprocessing_router.post("/upscale_image")
|
95 |
async def upscale_image(image: UploadFile = File(...), scale: int = 1):
|
96 |
+
start_time = time.time()
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
+
try:
|
99 |
+
image_bytes = await image.read()
|
100 |
+
image = Image.open(BytesIO(image_bytes)).convert("RGBA")
|
101 |
+
except Exception as e:
|
102 |
+
return JSONResponse(status_code=500, content={"error": f"Error reading image: {str(e)}", "code": 500})
|
103 |
|
104 |
+
try:
|
105 |
+
act_img_base_64 = BytesIO()
|
106 |
+
image.save(act_img_base_64, format="PNG")
|
107 |
+
image_bytes_ = base64.b64encode(act_img_base_64.getvalue()).decode("utf-8")
|
108 |
+
image_data_uri = f"data:image/png;base64,{image_bytes_}"
|
109 |
+
except Exception as e:
|
110 |
+
return JSONResponse(status_code=500,
|
111 |
+
content={"error": f"Error converting image to base64: {str(e)}", "code": 500})
|
112 |
|
113 |
+
try:
|
114 |
+
input = {
|
115 |
+
"image": image_data_uri,
|
116 |
+
"scale": scale,
|
117 |
+
"face_enhance": False
|
118 |
+
}
|
119 |
+
output = replicate_enhancer(input)
|
120 |
+
except Exception as e:
|
121 |
+
return JSONResponse(status_code=500,
|
122 |
+
content={"error": f"Error running image enhancement: {str(e)}", "code": 500})
|
123 |
|
124 |
try:
|
125 |
+
response = requests.get(output)
|
126 |
+
base_64 = base64.b64encode(response.content).decode('utf-8')
|
127 |
+
base64_prefix = image_data_uri.split(",")[0] + ","
|
128 |
+
|
129 |
+
total_inference_time = round((time.time() - start_time), 2)
|
130 |
+
|
131 |
response = {
|
132 |
"output": f"{base64_prefix}{base_64}",
|
133 |
+
"inference_time": total_inference_time,
|
134 |
+
"code": 200
|
135 |
}
|
|
|
136 |
return JSONResponse(content=response, status_code=200)
|
137 |
|
138 |
except Exception as e:
|
139 |
+
return JSONResponse(status_code=500,
|
140 |
+
content={"error": f"Error processing response: {str(e)}", "code": 500})
|
141 |
|
142 |
|
143 |
@preprocessing_router.post("/crop_transparent")
|
144 |
async def crop_transparent(image: UploadFile):
|
145 |
+
start_time = time.time()
|
|
|
146 |
|
147 |
try:
|
148 |
+
if not image.content_type == "image/png":
|
149 |
+
return JSONResponse(status_code=400,
|
150 |
+
content={"error": "Only PNG files are supported", "code": 400})
|
151 |
+
except Exception as e:
|
152 |
+
return JSONResponse(status_code=500,
|
153 |
+
content={"error": f"Error checking file type: {str(e)}", "code": 500})
|
154 |
|
155 |
+
try:
|
156 |
+
contents = await image.read()
|
157 |
cropped_image_bytes, metadata = crop_transparent_image(contents)
|
158 |
+
except Exception as e:
|
159 |
+
return JSONResponse(status_code=500,
|
160 |
+
content={"error": f"Error cropping image: {str(e)}", "code": 500})
|
161 |
|
162 |
+
try:
|
163 |
base64_image = base64.b64encode(cropped_image_bytes).decode('utf-8')
|
164 |
base64_prefix = "data:image/png;base64,"
|
165 |
|
166 |
+
total_inference_time = round((time.time() - start_time), 2)
|
167 |
+
|
168 |
+
return JSONResponse(content={
|
169 |
"status": "success",
|
170 |
+
"code": 200,
|
171 |
"data": {
|
172 |
"image": f"{base64_prefix}{base64_image}",
|
173 |
+
"metadata": metadata,
|
174 |
+
"inference_time": total_inference_time
|
175 |
}
|
176 |
+
}, status_code=200)
|
|
|
|
|
|
|
177 |
except Exception as e:
|
178 |
+
return JSONResponse(status_code=500,
|
179 |
+
content={"error": f"Error processing response: {str(e)}", "code": 500})
|
180 |
|
181 |
|
182 |
@preprocessing_router.post("/background_replace")
|
183 |
async def bg_replace(image: UploadFile = File(...), bg_image: UploadFile = File(...)):
|
184 |
+
start_time = time.time()
|
|
|
185 |
|
186 |
+
try:
|
187 |
+
image_bytes = await image.read()
|
188 |
+
bg_bytes = await bg_image.read()
|
189 |
+
image = Image.open(BytesIO(image_bytes)).convert("RGBA")
|
190 |
+
bg_image = Image.open(BytesIO(bg_bytes)).convert("RGB")
|
191 |
+
except Exception as e:
|
192 |
+
return JSONResponse(status_code=500,
|
193 |
+
content={"error": f"Error reading images: {str(e)}", "code": 500})
|
194 |
|
195 |
+
try:
|
196 |
+
width, height = bg_image.size
|
197 |
+
background = Image.fromarray(np.array(bg_image)).resize((width, height))
|
198 |
+
orig_img = Image.fromarray(np.array(image)).resize((width, height))
|
199 |
+
background.paste(orig_img, (0, 0), mask=orig_img)
|
200 |
+
except Exception as e:
|
201 |
+
return JSONResponse(status_code=500,
|
202 |
+
content={"error": f"Error processing images: {str(e)}", "code": 500})
|
203 |
|
204 |
try:
|
205 |
+
act_img_base_64 = BytesIO()
|
206 |
+
background.save(act_img_base_64, format="WEBP")
|
207 |
+
image_bytes_ = base64.b64encode(act_img_base_64.getvalue()).decode("utf-8")
|
208 |
+
image_data_uri = f"data:image/webp;base64,{image_bytes_}"
|
209 |
|
210 |
+
total_inference_time = round((time.time() - start_time), 2)
|
211 |
|
212 |
+
return JSONResponse(content={
|
213 |
+
"output": image_data_uri,
|
214 |
+
"code": 200,
|
215 |
+
"inference_time": total_inference_time
|
216 |
+
}, status_code=200)
|
217 |
except Exception as e:
|
218 |
+
return JSONResponse(status_code=500,
|
219 |
+
content={"error": f"Error creating response: {str(e)}", "code": 500})
|
220 |
|
221 |
|
222 |
@preprocessing_router.post("/rem_bg_color_extraction")
|
223 |
+
async def remove_background_color_extraction(image: UploadFile = File(...),
|
224 |
+
hex_color: str = "#FFFFFF",
|
225 |
threshold: int = 30):
|
226 |
+
start_time = time.time()
|
|
|
|
|
227 |
|
228 |
+
try:
|
229 |
+
image_bytes = await image.read()
|
230 |
+
image = Image.open(BytesIO(image_bytes)).convert("RGBA")
|
231 |
+
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
232 |
+
except Exception as e:
|
233 |
+
return JSONResponse(status_code=500,
|
234 |
+
content={"error": f"Error reading image: {str(e)}", "code": 500})
|
235 |
|
236 |
+
try:
|
237 |
+
result = color_extraction_rmbg.extract_color(image, hex_color, threshold)
|
238 |
+
result = Image.fromarray(cv2.cvtColor(result, cv2.COLOR_RGB2BGRA)).convert("RGBA")
|
239 |
+
except Exception as e:
|
240 |
+
return JSONResponse(status_code=500,
|
241 |
+
content={"error": f"Error extracting colors: {str(e)}", "code": 500})
|
242 |
|
243 |
try:
|
244 |
+
act_img_base_64 = BytesIO()
|
245 |
+
result.save(act_img_base_64, format="PNG")
|
246 |
+
image_bytes_ = base64.b64encode(act_img_base_64.getvalue()).decode("utf-8")
|
247 |
+
image_data_uri = f"data:image/png;base64,{image_bytes_}"
|
248 |
|
249 |
+
total_inference_time = round((time.time() - start_time), 2)
|
250 |
|
251 |
+
return JSONResponse(content={
|
252 |
+
"output": image_data_uri,
|
253 |
+
"code": 200,
|
254 |
+
"inference_time": total_inference_time
|
255 |
+
}, status_code=200)
|
256 |
except Exception as e:
|
257 |
+
return JSONResponse(status_code=500,
|
258 |
+
content={"error": f"Error creating response: {str(e)}", "code": 500})
|
259 |
|
260 |
|
261 |
@preprocessing_router.post("/title_description_generator")
|
262 |
async def product_title_description_generator(image: UploadFile = File(...)):
|
263 |
start_time = time.time()
|
264 |
+
|
|
|
265 |
try:
|
266 |
+
image_bytes = await image.read()
|
267 |
+
image = Image.open(BytesIO(image_bytes)).convert("RGB")
|
268 |
+
except Exception as e:
|
269 |
+
return JSONResponse(status_code=500,
|
270 |
+
content={"error": f"Error reading image: {str(e)}", "code": 500})
|
271 |
|
272 |
+
try:
|
273 |
+
result = product_listing_obj.gen_title_desc(image=image)
|
274 |
title = result.split("Title:")[1].split("Description:")[0]
|
275 |
description = result.split("Description:")[1]
|
276 |
+
except Exception as e:
|
277 |
+
return JSONResponse(status_code=500,
|
278 |
+
content={"error": "Please make sure the image is clear and necklaces are visible",
|
279 |
+
"code": 500})
|
280 |
+
|
281 |
+
try:
|
282 |
+
total_inference_time = round((time.time() - start_time), 2)
|
283 |
|
284 |
+
return JSONResponse(content={
|
285 |
"code": 200,
|
286 |
"title": title,
|
287 |
"description": description,
|
288 |
+
"inference_time": total_inference_time
|
289 |
+
}, status_code=200)
|
|
|
|
|
|
|
290 |
except Exception as e:
|
291 |
+
return JSONResponse(status_code=500,
|
292 |
+
content={"error": f"Error creating response: {str(e)}", "code": 500})
|
src/api/image_regeneration_api.py
CHANGED
@@ -31,30 +31,26 @@ async def image_re_gen(
|
|
31 |
negative_prompt: str = Form(""),
|
32 |
image: UploadFile = File(...),
|
33 |
mask_image: Optional[UploadFile] = File(default=None),
|
34 |
-
|
35 |
reference_image_c1: Optional[UploadFile] = File(default=None),
|
36 |
reference_image_c1_type: Optional[str] = Form(default=""),
|
37 |
reference_image_c1_weight: Optional[float] = Form(default=0.0),
|
38 |
reference_image_c1_stop: Optional[float] = Form(default=0.0),
|
39 |
-
|
40 |
reference_image_c2: Optional[UploadFile] = File(default=None),
|
41 |
reference_image_c2_type: Optional[str] = Form(default=""),
|
42 |
reference_image_c2_weight: Optional[float] = Form(default=0.0),
|
43 |
reference_image_c2_stop: Optional[float] = Form(default=0.0),
|
44 |
-
|
45 |
reference_image_c3: Optional[UploadFile] = File(default=None),
|
46 |
reference_image_c3_type: Optional[str] = Form(default=""),
|
47 |
reference_image_c3_weight: Optional[float] = Form(default=0.0),
|
48 |
reference_image_c3_stop: Optional[float] = Form(default=0.0),
|
49 |
-
|
50 |
reference_image_c4: Optional[UploadFile] = File(default=None),
|
51 |
reference_image_c4_type: Optional[str] = Form(default=""),
|
52 |
reference_image_c4_weight: Optional[float] = Form(default=0.0),
|
53 |
reference_image_c4_stop: Optional[float] = Form(default=0.0),
|
54 |
):
|
55 |
start_time = time.time()
|
56 |
-
try:
|
57 |
|
|
|
58 |
async def process_reference_image(reference_image: Optional[UploadFile]) -> Optional[str]:
|
59 |
if reference_image is not None:
|
60 |
reference_image_bytes = await reference_image.read()
|
@@ -64,22 +60,32 @@ async def image_re_gen(
|
|
64 |
reference_image_b64 = base64.b64encode(ref_img_base64.getvalue()).decode("utf-8")
|
65 |
return f"data:image/WEBP;base64,{reference_image_b64}"
|
66 |
return None
|
|
|
|
|
|
|
67 |
|
68 |
-
|
69 |
image_bytes = await image.read()
|
70 |
image = Image.open(BytesIO(image_bytes)).convert("RGB")
|
71 |
img_base64 = BytesIO()
|
72 |
image.save(img_base64, format="WEBP")
|
73 |
image_data_uri = f"data:image/WEBP;base64,{base64.b64encode(img_base64.getvalue()).decode('utf-8')}"
|
|
|
|
|
|
|
74 |
|
|
|
75 |
reference_images = {
|
76 |
'c1': await process_reference_image(reference_image_c1),
|
77 |
'c2': await process_reference_image(reference_image_c2),
|
78 |
'c3': await process_reference_image(reference_image_c3),
|
79 |
'c4': await process_reference_image(reference_image_c4)
|
80 |
}
|
|
|
|
|
|
|
81 |
|
82 |
-
|
83 |
input_data = {
|
84 |
"prompt": prompt,
|
85 |
"inpaint_input_image": image_data_uri,
|
@@ -100,7 +106,11 @@ async def image_re_gen(
|
|
100 |
mask_image.save(mask_base64, format="WEBP")
|
101 |
mask_image_data_uri = f"data:image/WEBP;base64,{base64.b64encode(mask_base64.getvalue()).decode('utf-8')}"
|
102 |
input_data["inpaint_input_mask"] = mask_image_data_uri
|
|
|
|
|
|
|
103 |
|
|
|
104 |
for i in range(1, 5):
|
105 |
c = f'c{i}'
|
106 |
if reference_images[c] is not None:
|
@@ -117,19 +127,27 @@ async def image_re_gen(
|
|
117 |
stop_value = locals()[f'reference_image_{c}_stop']
|
118 |
if stop_value != 0.0 or stop_value != 0:
|
119 |
input_data[f"cn_stop{i}"] = stop_value
|
|
|
|
|
|
|
120 |
|
|
|
121 |
output = image_regeneration_replicate(input_data)
|
122 |
response = requests.get(output[0])
|
123 |
output_base64 = base64.b64encode(response.content).decode('utf-8')
|
124 |
base64_prefix = image_data_uri.split(",")[0] + ","
|
|
|
|
|
|
|
|
|
|
|
125 |
inference_time = time.time() - start_time
|
126 |
response = {
|
127 |
"output": f"{base64_prefix}{output_base64}",
|
128 |
"inference_time": inference_time,
|
129 |
"code": 200,
|
130 |
}
|
131 |
-
|
132 |
return JSONResponse(content=response, status_code=200)
|
133 |
except Exception as e:
|
134 |
-
return JSONResponse(
|
135 |
-
|
|
|
31 |
negative_prompt: str = Form(""),
|
32 |
image: UploadFile = File(...),
|
33 |
mask_image: Optional[UploadFile] = File(default=None),
|
|
|
34 |
reference_image_c1: Optional[UploadFile] = File(default=None),
|
35 |
reference_image_c1_type: Optional[str] = Form(default=""),
|
36 |
reference_image_c1_weight: Optional[float] = Form(default=0.0),
|
37 |
reference_image_c1_stop: Optional[float] = Form(default=0.0),
|
|
|
38 |
reference_image_c2: Optional[UploadFile] = File(default=None),
|
39 |
reference_image_c2_type: Optional[str] = Form(default=""),
|
40 |
reference_image_c2_weight: Optional[float] = Form(default=0.0),
|
41 |
reference_image_c2_stop: Optional[float] = Form(default=0.0),
|
|
|
42 |
reference_image_c3: Optional[UploadFile] = File(default=None),
|
43 |
reference_image_c3_type: Optional[str] = Form(default=""),
|
44 |
reference_image_c3_weight: Optional[float] = Form(default=0.0),
|
45 |
reference_image_c3_stop: Optional[float] = Form(default=0.0),
|
|
|
46 |
reference_image_c4: Optional[UploadFile] = File(default=None),
|
47 |
reference_image_c4_type: Optional[str] = Form(default=""),
|
48 |
reference_image_c4_weight: Optional[float] = Form(default=0.0),
|
49 |
reference_image_c4_stop: Optional[float] = Form(default=0.0),
|
50 |
):
|
51 |
start_time = time.time()
|
|
|
52 |
|
53 |
+
try:
|
54 |
async def process_reference_image(reference_image: Optional[UploadFile]) -> Optional[str]:
|
55 |
if reference_image is not None:
|
56 |
reference_image_bytes = await reference_image.read()
|
|
|
60 |
reference_image_b64 = base64.b64encode(ref_img_base64.getvalue()).decode("utf-8")
|
61 |
return f"data:image/WEBP;base64,{reference_image_b64}"
|
62 |
return None
|
63 |
+
except Exception as e:
|
64 |
+
return JSONResponse(status_code=500,
|
65 |
+
content={"error": f"Error processing reference image: {str(e)}", "code": 500})
|
66 |
|
67 |
+
try:
|
68 |
image_bytes = await image.read()
|
69 |
image = Image.open(BytesIO(image_bytes)).convert("RGB")
|
70 |
img_base64 = BytesIO()
|
71 |
image.save(img_base64, format="WEBP")
|
72 |
image_data_uri = f"data:image/WEBP;base64,{base64.b64encode(img_base64.getvalue()).decode('utf-8')}"
|
73 |
+
except Exception as e:
|
74 |
+
return JSONResponse(status_code=500,
|
75 |
+
content={"error": f"Error processing main image: {str(e)}", "code": 500})
|
76 |
|
77 |
+
try:
|
78 |
reference_images = {
|
79 |
'c1': await process_reference_image(reference_image_c1),
|
80 |
'c2': await process_reference_image(reference_image_c2),
|
81 |
'c3': await process_reference_image(reference_image_c3),
|
82 |
'c4': await process_reference_image(reference_image_c4)
|
83 |
}
|
84 |
+
except Exception as e:
|
85 |
+
return JSONResponse(status_code=500,
|
86 |
+
content={"error": f"Error processing reference images: {str(e)}", "code": 500})
|
87 |
|
88 |
+
try:
|
89 |
input_data = {
|
90 |
"prompt": prompt,
|
91 |
"inpaint_input_image": image_data_uri,
|
|
|
106 |
mask_image.save(mask_base64, format="WEBP")
|
107 |
mask_image_data_uri = f"data:image/WEBP;base64,{base64.b64encode(mask_base64.getvalue()).decode('utf-8')}"
|
108 |
input_data["inpaint_input_mask"] = mask_image_data_uri
|
109 |
+
except Exception as e:
|
110 |
+
return JSONResponse(status_code=500,
|
111 |
+
content={"error": f"Error preparing input data: {str(e)}", "code": 500})
|
112 |
|
113 |
+
try:
|
114 |
for i in range(1, 5):
|
115 |
c = f'c{i}'
|
116 |
if reference_images[c] is not None:
|
|
|
127 |
stop_value = locals()[f'reference_image_{c}_stop']
|
128 |
if stop_value != 0.0 or stop_value != 0:
|
129 |
input_data[f"cn_stop{i}"] = stop_value
|
130 |
+
except Exception as e:
|
131 |
+
return JSONResponse(status_code=500,
|
132 |
+
content={"error": f"Error processing reference image parameters: {str(e)}", "code": 500})
|
133 |
|
134 |
+
try:
|
135 |
output = image_regeneration_replicate(input_data)
|
136 |
response = requests.get(output[0])
|
137 |
output_base64 = base64.b64encode(response.content).decode('utf-8')
|
138 |
base64_prefix = image_data_uri.split(",")[0] + ","
|
139 |
+
except Exception as e:
|
140 |
+
return JSONResponse(status_code=500,
|
141 |
+
content={"error": f"Error generating image: {str(e)}", "code": 500})
|
142 |
+
|
143 |
+
try:
|
144 |
inference_time = time.time() - start_time
|
145 |
response = {
|
146 |
"output": f"{base64_prefix}{output_base64}",
|
147 |
"inference_time": inference_time,
|
148 |
"code": 200,
|
149 |
}
|
|
|
150 |
return JSONResponse(content=response, status_code=200)
|
151 |
except Exception as e:
|
152 |
+
return JSONResponse(status_code=500,
|
153 |
+
content={"error": f"Error creating response: {str(e)}", "code": 500})
|