Spaces:
Build error
Build error
Commit ·
03dc3d7
1
Parent(s): 1dcfbb3
add: inference time
Browse files- src/api/nto_api.py +21 -7
src/api/nto_api.py
CHANGED
|
@@ -3,6 +3,7 @@ project @ NTO-TCP-HF
|
|
| 3 |
created @ 2024-10-28
|
| 4 |
author @ github/ishworrsubedii
|
| 5 |
"""
|
|
|
|
| 6 |
import cv2
|
| 7 |
import numpy as np
|
| 8 |
from PIL.ImageOps import grayscale
|
|
@@ -53,6 +54,7 @@ class NecklaceTryOnIDEntity(BaseModel):
|
|
| 53 |
|
| 54 |
@nto_cto_router.post("/clothingTryOnV2")
|
| 55 |
async def clothing_try_on_v2(image: UploadFile = File(...), clothing_type: str = Form(...)):
|
|
|
|
| 56 |
image_bytes = await image.read()
|
| 57 |
image = Image.open(BytesIO(image_bytes)).convert("RGB")
|
| 58 |
|
|
@@ -76,10 +78,12 @@ async def clothing_try_on_v2(image: UploadFile = File(...), clothing_type: str =
|
|
| 76 |
}
|
| 77 |
|
| 78 |
output = replicate_run_cto(input)
|
|
|
|
| 79 |
|
| 80 |
response = {
|
| 81 |
"output": f"{output[0]}",
|
| 82 |
-
'code': 200
|
|
|
|
| 83 |
}
|
| 84 |
|
| 85 |
return JSONResponse(content=response, status_code=200)
|
|
@@ -88,6 +92,7 @@ async def clothing_try_on_v2(image: UploadFile = File(...), clothing_type: str =
|
|
| 88 |
@nto_cto_router.post("/clothingTryOn")
|
| 89 |
async def clothing_try_on(image: UploadFile = File(...),
|
| 90 |
mask: UploadFile = File(...), clothing_type: str = Form(...)):
|
|
|
|
| 91 |
image_bytes = await image.read()
|
| 92 |
mask_bytes = await mask.read()
|
| 93 |
image, mask = Image.open(BytesIO(image_bytes)).convert("RGB"), Image.open(
|
|
@@ -142,10 +147,12 @@ async def clothing_try_on(image: UploadFile = File(...),
|
|
| 142 |
in_mem_file = BytesIO()
|
| 143 |
result.save(in_mem_file, format="WEBP", quality=85)
|
| 144 |
base_64_output = base64.b64encode(in_mem_file.getvalue()).decode('utf-8')
|
|
|
|
| 145 |
|
| 146 |
response = {
|
| 147 |
"output": f"data:image/WEBP;base64,{base_64_output}",
|
| 148 |
-
|
|
|
|
| 149 |
}
|
| 150 |
|
| 151 |
return JSONResponse(content=response, status_code=200)
|
|
@@ -297,6 +304,7 @@ async def parse_necklace_try_on_id(necklaceImageId: str = Form(...),
|
|
| 297 |
@nto_cto_router.post("/necklaceTryOnID")
|
| 298 |
async def necklace_try_on_id(necklace_try_on_id: NecklaceTryOnIDEntity = Depends(parse_necklace_try_on_id),
|
| 299 |
image: UploadFile = File(...)):
|
|
|
|
| 300 |
data, _ = supabase.table("APIKeyList").select("*").filter("API_KEY", "eq",
|
| 301 |
necklace_try_on_id.api_token).execute()
|
| 302 |
|
|
@@ -328,11 +336,14 @@ async def necklace_try_on_id(necklace_try_on_id: NecklaceTryOnIDEntity = Depends
|
|
| 328 |
mask.save(inMemFileMask, format="WEBP", quality=85)
|
| 329 |
outputBytes = inMemFile.getvalue()
|
| 330 |
maskBytes = inMemFileMask.getvalue()
|
|
|
|
|
|
|
| 331 |
response = {
|
| 332 |
"output": f"data:image/WEBP;base64,{base64.b64encode(outputBytes).decode('utf-8')}",
|
| 333 |
-
"mask": f"data:image/WEBP;base64,{base64.b64encode(maskBytes).decode('utf-8')}"
|
|
|
|
| 334 |
}
|
| 335 |
-
|
| 336 |
if creditResponse == "No Credits Available":
|
| 337 |
response = {
|
| 338 |
"error": "No Credits Remaining"
|
|
@@ -384,6 +395,7 @@ async def necklace_try_on_with_points(necklace_try_on_id: NecklaceTryOnIDEntity
|
|
| 384 |
left_y: int = Form(...),
|
| 385 |
right_x: int = Form(...),
|
| 386 |
right_y: int = Form(...)):
|
|
|
|
| 387 |
imageBytes = await image.read()
|
| 388 |
|
| 389 |
jewellery_url = f"https://lvuhhlrkcuexzqtsbqyu.supabase.co/storage/v1/object/public/Stores/{necklace_try_on_id.storename}/{necklace_try_on_id.necklaceCategory}/image/{necklace_try_on_id.necklaceImageId}.png"
|
|
@@ -409,12 +421,14 @@ async def necklace_try_on_with_points(necklace_try_on_id: NecklaceTryOnIDEntity
|
|
| 409 |
mask.save(inMemFileMask, format="WEBP", quality=85)
|
| 410 |
outputBytes = inMemFile.getvalue()
|
| 411 |
maskBytes = inMemFileMask.getvalue()
|
|
|
|
|
|
|
|
|
|
| 412 |
response = {
|
| 413 |
"output": f"data:image/WEBP;base64,{base64.b64encode(outputBytes).decode('utf-8')}",
|
| 414 |
-
"mask": f"data:image/WEBP;base64,{base64.b64encode(maskBytes).decode('utf-8')}"
|
|
|
|
| 415 |
}
|
| 416 |
-
|
| 417 |
-
creditResponse = deductAndTrackCredit(storename=necklace_try_on_id.storename, endpoint="/necklaceTryOnID")
|
| 418 |
if creditResponse == "No Credits Available":
|
| 419 |
response = {
|
| 420 |
"error": "No Credits Remaining"
|
|
|
|
| 3 |
created @ 2024-10-28
|
| 4 |
author @ github/ishworrsubedii
|
| 5 |
"""
|
| 6 |
+
import time
|
| 7 |
import cv2
|
| 8 |
import numpy as np
|
| 9 |
from PIL.ImageOps import grayscale
|
|
|
|
| 54 |
|
| 55 |
@nto_cto_router.post("/clothingTryOnV2")
|
| 56 |
async def clothing_try_on_v2(image: UploadFile = File(...), clothing_type: str = Form(...)):
|
| 57 |
+
start_time = time.time()
|
| 58 |
image_bytes = await image.read()
|
| 59 |
image = Image.open(BytesIO(image_bytes)).convert("RGB")
|
| 60 |
|
|
|
|
| 78 |
}
|
| 79 |
|
| 80 |
output = replicate_run_cto(input)
|
| 81 |
+
total_inference_time = round((time.time() - start_time), 2)
|
| 82 |
|
| 83 |
response = {
|
| 84 |
"output": f"{output[0]}",
|
| 85 |
+
'code': 200,
|
| 86 |
+
"inference_time": total_inference_time
|
| 87 |
}
|
| 88 |
|
| 89 |
return JSONResponse(content=response, status_code=200)
|
|
|
|
| 92 |
@nto_cto_router.post("/clothingTryOn")
|
| 93 |
async def clothing_try_on(image: UploadFile = File(...),
|
| 94 |
mask: UploadFile = File(...), clothing_type: str = Form(...)):
|
| 95 |
+
start_time = time.time()
|
| 96 |
image_bytes = await image.read()
|
| 97 |
mask_bytes = await mask.read()
|
| 98 |
image, mask = Image.open(BytesIO(image_bytes)).convert("RGB"), Image.open(
|
|
|
|
| 147 |
in_mem_file = BytesIO()
|
| 148 |
result.save(in_mem_file, format="WEBP", quality=85)
|
| 149 |
base_64_output = base64.b64encode(in_mem_file.getvalue()).decode('utf-8')
|
| 150 |
+
total_inference_time = round((time.time() - start_time), 2)
|
| 151 |
|
| 152 |
response = {
|
| 153 |
"output": f"data:image/WEBP;base64,{base_64_output}",
|
| 154 |
+
"code": 200,
|
| 155 |
+
"inference_time": total_inference_time
|
| 156 |
}
|
| 157 |
|
| 158 |
return JSONResponse(content=response, status_code=200)
|
|
|
|
| 304 |
@nto_cto_router.post("/necklaceTryOnID")
|
| 305 |
async def necklace_try_on_id(necklace_try_on_id: NecklaceTryOnIDEntity = Depends(parse_necklace_try_on_id),
|
| 306 |
image: UploadFile = File(...)):
|
| 307 |
+
start_time = time.time()
|
| 308 |
data, _ = supabase.table("APIKeyList").select("*").filter("API_KEY", "eq",
|
| 309 |
necklace_try_on_id.api_token).execute()
|
| 310 |
|
|
|
|
| 336 |
mask.save(inMemFileMask, format="WEBP", quality=85)
|
| 337 |
outputBytes = inMemFile.getvalue()
|
| 338 |
maskBytes = inMemFileMask.getvalue()
|
| 339 |
+
creditResponse = deductAndTrackCredit(storename=necklace_try_on_id.storename, endpoint="/necklaceTryOnID")
|
| 340 |
+
total_backend_time = round((time.time() - start_time), 2)
|
| 341 |
response = {
|
| 342 |
"output": f"data:image/WEBP;base64,{base64.b64encode(outputBytes).decode('utf-8')}",
|
| 343 |
+
"mask": f"data:image/WEBP;base64,{base64.b64encode(maskBytes).decode('utf-8')}",
|
| 344 |
+
"inference_time": total_backend_time
|
| 345 |
}
|
| 346 |
+
|
| 347 |
if creditResponse == "No Credits Available":
|
| 348 |
response = {
|
| 349 |
"error": "No Credits Remaining"
|
|
|
|
| 395 |
left_y: int = Form(...),
|
| 396 |
right_x: int = Form(...),
|
| 397 |
right_y: int = Form(...)):
|
| 398 |
+
start_time = time.time()
|
| 399 |
imageBytes = await image.read()
|
| 400 |
|
| 401 |
jewellery_url = f"https://lvuhhlrkcuexzqtsbqyu.supabase.co/storage/v1/object/public/Stores/{necklace_try_on_id.storename}/{necklace_try_on_id.necklaceCategory}/image/{necklace_try_on_id.necklaceImageId}.png"
|
|
|
|
| 421 |
mask.save(inMemFileMask, format="WEBP", quality=85)
|
| 422 |
outputBytes = inMemFile.getvalue()
|
| 423 |
maskBytes = inMemFileMask.getvalue()
|
| 424 |
+
|
| 425 |
+
creditResponse = deductAndTrackCredit(storename=necklace_try_on_id.storename, endpoint="/necklaceTryOnID")
|
| 426 |
+
total_inference_time = round((time.time() - start_time), 2)
|
| 427 |
response = {
|
| 428 |
"output": f"data:image/WEBP;base64,{base64.b64encode(outputBytes).decode('utf-8')}",
|
| 429 |
+
"mask": f"data:image/WEBP;base64,{base64.b64encode(maskBytes).decode('utf-8')}",
|
| 430 |
+
"inference_time": total_inference_time
|
| 431 |
}
|
|
|
|
|
|
|
| 432 |
if creditResponse == "No Credits Available":
|
| 433 |
response = {
|
| 434 |
"error": "No Credits Remaining"
|