Spaces:
Sleeping
Sleeping
Commit
·
f33f113
1
Parent(s):
d187e5b
add: detail logging and authorization header
Browse files- app.py +33 -3
- src/api/batch_api.py +3 -0
- src/api/nto_api.py +26 -7
app.py
CHANGED
@@ -4,8 +4,10 @@ created @ 2024-10-28
|
|
4 |
author @ github.com/ishworrsubedii
|
5 |
"""
|
6 |
import time
|
|
|
7 |
|
8 |
-
from fastapi import FastAPI
|
|
|
9 |
from starlette.middleware.cors import CORSMiddleware
|
10 |
|
11 |
from src.api.image_prep_api import preprocessing_router
|
@@ -13,8 +15,36 @@ from src.api.nto_api import nto_cto_router
|
|
13 |
from src.api.image_regeneration_api import image_regeneration_router
|
14 |
from src.api.batch_api import batch_router
|
15 |
from src.api.mannequin_to_model_api import mto_router
|
|
|
16 |
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
app.include_router(nto_cto_router, tags=["NTO-CTO"])
|
19 |
app.include_router(preprocessing_router, tags=["Image-Preprocessing"])
|
20 |
app.include_router(image_regeneration_router, tags=["Image-Regeneration"])
|
@@ -29,4 +59,4 @@ app.add_middleware(
|
|
29 |
allow_credentials=True,
|
30 |
allow_methods=["*"],
|
31 |
allow_headers=["*"],
|
32 |
-
)
|
|
|
4 |
author @ github.com/ishworrsubedii
|
5 |
"""
|
6 |
import time
|
7 |
+
from datetime import datetime, timedelta, timezone
|
8 |
|
9 |
+
from fastapi import FastAPI, Depends, HTTPException
|
10 |
+
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
11 |
from starlette.middleware.cors import CORSMiddleware
|
12 |
|
13 |
from src.api.image_prep_api import preprocessing_router
|
|
|
15 |
from src.api.image_regeneration_api import image_regeneration_router
|
16 |
from src.api.batch_api import batch_router
|
17 |
from src.api.mannequin_to_model_api import mto_router
|
18 |
+
from src.api.nto_api import supabase
|
19 |
|
20 |
+
security = HTTPBearer()
|
21 |
+
|
22 |
+
|
23 |
+
async def verify_login_token(credentials: HTTPAuthorizationCredentials = Depends(security)):
|
24 |
+
try:
|
25 |
+
response = supabase.table("JewelMirrorUserLogins").select("*").eq("LoginToken",
|
26 |
+
credentials.credentials).execute()
|
27 |
+
|
28 |
+
if not response.data:
|
29 |
+
raise HTTPException(status_code=401, detail="Unauthorized: Token not found")
|
30 |
+
|
31 |
+
token_data = response.data[0]
|
32 |
+
|
33 |
+
created_at = datetime.fromisoformat(token_data["UpdatedAt"].replace("Z", "+00:00"))
|
34 |
+
current_time = datetime.now(timezone.utc)
|
35 |
+
time_difference = current_time - created_at
|
36 |
+
|
37 |
+
if time_difference <= timedelta(minutes=30):
|
38 |
+
return {"status": "Authorized", "message": "Token is valid"}
|
39 |
+
|
40 |
+
raise HTTPException(status_code=401, detail="Unauthorized: Token expired")
|
41 |
+
|
42 |
+
except Exception as e:
|
43 |
+
print(f"Token verification error: {e}")
|
44 |
+
raise HTTPException(status_code=401, detail="Invalid token")
|
45 |
+
|
46 |
+
|
47 |
+
app = FastAPI(dependencies=[Depends(verify_login_token)])
|
48 |
app.include_router(nto_cto_router, tags=["NTO-CTO"])
|
49 |
app.include_router(preprocessing_router, tags=["Image-Preprocessing"])
|
50 |
app.include_router(image_regeneration_router, tags=["Image-Regeneration"])
|
|
|
59 |
allow_credentials=True,
|
60 |
allow_methods=["*"],
|
61 |
allow_headers=["*"],
|
62 |
+
)
|
src/api/batch_api.py
CHANGED
@@ -30,6 +30,9 @@ async def rt_cto(
|
|
30 |
image: UploadFile = File(...),
|
31 |
c_list: str = Form(...)
|
32 |
):
|
|
|
|
|
|
|
33 |
try:
|
34 |
clothing_list = [item.strip() for item in c_list.split(",")]
|
35 |
logger.info(f">>> CLOTHING LIST: {clothing_list} <<<")
|
|
|
30 |
image: UploadFile = File(...),
|
31 |
c_list: str = Form(...)
|
32 |
):
|
33 |
+
logger.info("-" * 50)
|
34 |
+
logger.info(">>> REAL-TIME CTO STARTED <<<")
|
35 |
+
logger.info(f"Parameters: clothing_list={c_list}")
|
36 |
try:
|
37 |
clothing_list = [item.strip() for item in c_list.split(",")]
|
38 |
logger.info(f">>> CLOTHING LIST: {clothing_list} <<<")
|
src/api/nto_api.py
CHANGED
@@ -65,6 +65,7 @@ class NecklaceTryOnIDEntity(BaseModel):
|
|
65 |
async def clothing_try_on_v2(image: UploadFile = File(...), clothing_type: str = Form(...)):
|
66 |
logger.info("-" * 50)
|
67 |
logger.info(">>> CLOTHING TRY ON V2 STARTED <<<")
|
|
|
68 |
start_time = time.time()
|
69 |
|
70 |
try:
|
@@ -132,6 +133,7 @@ async def clothing_try_on(image: UploadFile = File(...),
|
|
132 |
mask: UploadFile = File(...), clothing_type: str = Form(...)):
|
133 |
logger.info("-" * 50)
|
134 |
logger.info(">>> CLOTHING TRY ON STARTED <<<")
|
|
|
135 |
start_time = time.time()
|
136 |
|
137 |
try:
|
@@ -428,7 +430,10 @@ def process_image(image: Image.Image, quality: int) -> bytes:
|
|
428 |
async def necklace_try_on_id(necklace_try_on_id: NecklaceTryOnIDEntity = Depends(parse_necklace_try_on_id),
|
429 |
image: UploadFile = File(...)):
|
430 |
logger.info("-" * 50)
|
431 |
-
logger.info(
|
|
|
|
|
|
|
432 |
start_time = time.time()
|
433 |
|
434 |
try:
|
@@ -493,7 +498,7 @@ async def necklace_try_on_id(necklace_try_on_id: NecklaceTryOnIDEntity = Depends
|
|
493 |
return JSONResponse(content={"error": f"Error generating response", "code": 500}, status_code=500)
|
494 |
|
495 |
logger.info(f">>> TOTAL INFERENCE TIME: {total_backend_time}s <<<")
|
496 |
-
logger.info(f">>> NECKLACE TRY ON COMPLETED
|
497 |
logger.info("-" * 50)
|
498 |
|
499 |
return JSONResponse(content=response, status_code=200)
|
@@ -508,7 +513,10 @@ async def necklace_try_on_id(necklace_try_on_id: NecklaceTryOnIDEntity = Depends
|
|
508 |
async def canvas_points(necklace_try_on_id: NecklaceTryOnIDEntity = Depends(parse_necklace_try_on_id),
|
509 |
image: UploadFile = File(...)):
|
510 |
logger.info("-" * 50)
|
511 |
-
logger.info(
|
|
|
|
|
|
|
512 |
start_time = time.time()
|
513 |
|
514 |
try:
|
@@ -543,7 +551,7 @@ async def canvas_points(necklace_try_on_id: NecklaceTryOnIDEntity = Depends(pars
|
|
543 |
|
544 |
total_inference_time = round((time.time() - start_time), 2)
|
545 |
logger.info(f">>> TOTAL INFERENCE TIME: {total_inference_time}s <<<")
|
546 |
-
logger.info(f">>> CANVAS POINTS COMPLETED
|
547 |
logger.info("-" * 50)
|
548 |
|
549 |
return JSONResponse(status_code=200, content=response)
|
@@ -557,7 +565,11 @@ async def necklace_try_on_with_points(necklace_try_on_id: NecklaceTryOnIDEntity
|
|
557 |
right_x: int = Form(...),
|
558 |
right_y: int = Form(...)):
|
559 |
logger.info("-" * 50)
|
560 |
-
logger.info(
|
|
|
|
|
|
|
|
|
561 |
start_time = time.time()
|
562 |
|
563 |
try:
|
@@ -613,7 +625,7 @@ async def necklace_try_on_with_points(necklace_try_on_id: NecklaceTryOnIDEntity
|
|
613 |
return JSONResponse(content={"error": f"Error deducting credits", "code": 500}, status_code=500)
|
614 |
|
615 |
logger.info(f">>> TOTAL INFERENCE TIME: {total_inference_time}s <<<")
|
616 |
-
logger.info(f">>> NECKLACE TRY ON WITH POINTS COMPLETED
|
617 |
logger.info("-" * 50)
|
618 |
|
619 |
return JSONResponse(content=response, status_code=200)
|
@@ -629,6 +641,10 @@ async def clothing_and_necklace_try_on(
|
|
629 |
):
|
630 |
logger.info("-" * 50)
|
631 |
logger.info(">>> CLOTHING AND NECKLACE TRY ON STARTED <<<")
|
|
|
|
|
|
|
|
|
632 |
start_time = time.time()
|
633 |
|
634 |
def image_to_base64(img: Image.Image) -> str:
|
@@ -715,7 +731,10 @@ async def clothing_and_necklace_try_on(
|
|
715 |
async def mannequin_nto(necklace_try_on_id: NecklaceTryOnIDEntity = Depends(parse_necklace_try_on_id),
|
716 |
image: UploadFile = File(...)):
|
717 |
logger.info("-" * 50)
|
718 |
-
logger.info(
|
|
|
|
|
|
|
719 |
start_time = time.time()
|
720 |
|
721 |
try:
|
|
|
65 |
async def clothing_try_on_v2(image: UploadFile = File(...), clothing_type: str = Form(...)):
|
66 |
logger.info("-" * 50)
|
67 |
logger.info(">>> CLOTHING TRY ON V2 STARTED <<<")
|
68 |
+
logger.info(f"Parameters: clothing_type={clothing_type}")
|
69 |
start_time = time.time()
|
70 |
|
71 |
try:
|
|
|
133 |
mask: UploadFile = File(...), clothing_type: str = Form(...)):
|
134 |
logger.info("-" * 50)
|
135 |
logger.info(">>> CLOTHING TRY ON STARTED <<<")
|
136 |
+
logger.info(f"Parameters: clothing_type={clothing_type}")
|
137 |
start_time = time.time()
|
138 |
|
139 |
try:
|
|
|
430 |
async def necklace_try_on_id(necklace_try_on_id: NecklaceTryOnIDEntity = Depends(parse_necklace_try_on_id),
|
431 |
image: UploadFile = File(...)):
|
432 |
logger.info("-" * 50)
|
433 |
+
logger.info(">>> NECKLACE TRY ON ID STARTED <<<")
|
434 |
+
logger.info(f"Parameters: storename={necklace_try_on_id.storename}, "
|
435 |
+
f"necklaceCategory={necklace_try_on_id.necklaceCategory}, "
|
436 |
+
f"necklaceImageId={necklace_try_on_id.necklaceImageId}")
|
437 |
start_time = time.time()
|
438 |
|
439 |
try:
|
|
|
498 |
return JSONResponse(content={"error": f"Error generating response", "code": 500}, status_code=500)
|
499 |
|
500 |
logger.info(f">>> TOTAL INFERENCE TIME: {total_backend_time}s <<<")
|
501 |
+
logger.info(f">>> NECKLACE TRY ON COMPLETED <<<")
|
502 |
logger.info("-" * 50)
|
503 |
|
504 |
return JSONResponse(content=response, status_code=200)
|
|
|
513 |
async def canvas_points(necklace_try_on_id: NecklaceTryOnIDEntity = Depends(parse_necklace_try_on_id),
|
514 |
image: UploadFile = File(...)):
|
515 |
logger.info("-" * 50)
|
516 |
+
logger.info(">>> CANVAS POINTS STARTED <<<")
|
517 |
+
logger.info(f"Parameters: storename={necklace_try_on_id.storename}, "
|
518 |
+
f"necklaceCategory={necklace_try_on_id.necklaceCategory}, "
|
519 |
+
f"necklaceImageId={necklace_try_on_id.necklaceImageId}")
|
520 |
start_time = time.time()
|
521 |
|
522 |
try:
|
|
|
551 |
|
552 |
total_inference_time = round((time.time() - start_time), 2)
|
553 |
logger.info(f">>> TOTAL INFERENCE TIME: {total_inference_time}s <<<")
|
554 |
+
logger.info(f">>> CANVAS POINTS COMPLETED <<<")
|
555 |
logger.info("-" * 50)
|
556 |
|
557 |
return JSONResponse(status_code=200, content=response)
|
|
|
565 |
right_x: int = Form(...),
|
566 |
right_y: int = Form(...)):
|
567 |
logger.info("-" * 50)
|
568 |
+
logger.info(">>> NECKLACE TRY ON WITH POINTS STARTED <<<")
|
569 |
+
logger.info(f"Parameters: storename={necklace_try_on_id.storename}, "
|
570 |
+
f"necklaceCategory={necklace_try_on_id.necklaceCategory}, "
|
571 |
+
f"necklaceImageId={necklace_try_on_id.necklaceImageId}, "
|
572 |
+
f"left_point=({left_x}, {left_y}), right_point=({right_x}, {right_y})")
|
573 |
start_time = time.time()
|
574 |
|
575 |
try:
|
|
|
625 |
return JSONResponse(content={"error": f"Error deducting credits", "code": 500}, status_code=500)
|
626 |
|
627 |
logger.info(f">>> TOTAL INFERENCE TIME: {total_inference_time}s <<<")
|
628 |
+
logger.info(f">>> NECKLACE TRY ON WITH POINTS COMPLETED <<<")
|
629 |
logger.info("-" * 50)
|
630 |
|
631 |
return JSONResponse(content=response, status_code=200)
|
|
|
641 |
):
|
642 |
logger.info("-" * 50)
|
643 |
logger.info(">>> CLOTHING AND NECKLACE TRY ON STARTED <<<")
|
644 |
+
logger.info(f"Parameters: storename={storename}, "
|
645 |
+
f"necklaceCategory={necklaceCategory}, "
|
646 |
+
f"necklaceImageId={necklaceImageId}, "
|
647 |
+
f"clothing_type={clothing_type}")
|
648 |
start_time = time.time()
|
649 |
|
650 |
def image_to_base64(img: Image.Image) -> str:
|
|
|
731 |
async def mannequin_nto(necklace_try_on_id: NecklaceTryOnIDEntity = Depends(parse_necklace_try_on_id),
|
732 |
image: UploadFile = File(...)):
|
733 |
logger.info("-" * 50)
|
734 |
+
logger.info(">>> MANNEQUIN NTO STARTED <<<")
|
735 |
+
logger.info(f"Parameters: storename={necklace_try_on_id.storename}, "
|
736 |
+
f"necklaceCategory={necklace_try_on_id.necklaceCategory}, "
|
737 |
+
f"necklaceImageId={necklace_try_on_id.necklaceImageId}")
|
738 |
start_time = time.time()
|
739 |
|
740 |
try:
|