ishworrsubedii commited on
Commit
2252bb5
·
1 Parent(s): bfb697d

add: supabase upload url return necklacetryon

Browse files
Files changed (2) hide show
  1. app.py +4 -1
  2. src/api/nto_api.py +44 -10
app.py CHANGED
@@ -22,4 +22,7 @@ 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
+ )
26
+
27
+ import uvicorn
28
+ uvicorn.run(app)
src/api/nto_api.py CHANGED
@@ -3,6 +3,8 @@ project @ NTO-TCP-HF
3
  created @ 2024-10-28
4
  author @ github/ishworrsubedii
5
  """
 
 
6
  import time
7
  import cv2
8
  import numpy as np
@@ -23,6 +25,9 @@ from pydantic import BaseModel
23
  import replicate
24
  import requests
25
  from src.utils.logger import logger
 
 
 
26
 
27
  pipeline = Pipeline()
28
 
@@ -36,8 +41,8 @@ supabase_storage: str = os.getenv("SUPABASE_STORAGE")
36
  cto_replicate: str = os.getenv(
37
  "CTO")
38
 
39
- supabase: Client = create_client(supabase_key=key, supabase_url=url)
40
- bucket = supabase.storage.from_(supabase_storage)
41
 
42
 
43
  def replicate_run_cto(input):
@@ -357,6 +362,33 @@ async def parse_necklace_try_on_id(necklaceImageId: str = Form(...),
357
  )
358
 
359
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
360
  @nto_cto_router.post("/necklaceTryOnID")
361
  async def necklace_try_on_id(necklace_try_on_id: NecklaceTryOnIDEntity = Depends(parse_necklace_try_on_id),
362
  image: UploadFile = File(...)):
@@ -403,12 +435,14 @@ async def necklace_try_on_id(necklace_try_on_id: NecklaceTryOnIDEntity = Depends
403
  status_code=500)
404
 
405
  try:
406
- inMemFile = BytesIO()
407
- inMemFileMask = BytesIO()
408
- result.save(inMemFile, format="WEBP", quality=85)
409
- mask.save(inMemFileMask, format="WEBP", quality=85)
410
- outputBytes = inMemFile.getvalue()
411
- maskBytes = inMemFileMask.getvalue()
 
 
412
  logger.info(">>> RESULT IMAGES SAVED <<<")
413
  except Exception as e:
414
  logger.error(f">>> RESULT SAVING ERROR: {str(e)} <<<")
@@ -419,8 +453,8 @@ async def necklace_try_on_id(necklace_try_on_id: NecklaceTryOnIDEntity = Depends
419
  total_backend_time = round((time.time() - start_time), 2)
420
  response = {
421
  "code": 200,
422
- "output": f"data:image/WEBP;base64,{base64.b64encode(outputBytes).decode('utf-8')}",
423
- "mask": f"data:image/WEBP;base64,{base64.b64encode(maskBytes).decode('utf-8')}",
424
  "inference_time": total_backend_time
425
  }
426
  if creditResponse == "No Credits Available":
 
3
  created @ 2024-10-28
4
  author @ github/ishworrsubedii
5
  """
6
+ import secrets
7
+ import tempfile
8
  import time
9
  import cv2
10
  import numpy as np
 
25
  import replicate
26
  import requests
27
  from src.utils.logger import logger
28
+ import secrets
29
+
30
+ from batch_half_model_generation.supabase_image_fetch import supabase_client
31
 
32
  pipeline = Pipeline()
33
 
 
41
  cto_replicate: str = os.getenv(
42
  "CTO")
43
 
44
+ supabase = create_client(supabase_url=url, supabase_key=key)
45
+ bucket = supabase.storage.from_("JewelMirrorOutputs")
46
 
47
 
48
  def replicate_run_cto(input):
 
362
  )
363
 
364
 
365
+ def supabase_upload_and_return_url(prefix: str, image: Image.Image,quality: int = 85) :
366
+
367
+ try:
368
+ filename = f"{prefix}_{secrets.token_hex(16)}.webp"
369
+
370
+
371
+
372
+
373
+ buffer = BytesIO()
374
+ image.save(buffer, format='WEBP', quality=quality, optimize=True)
375
+ image_bytes = buffer.getvalue()
376
+
377
+ bucket.upload(
378
+ path=filename,
379
+ file=image_bytes,
380
+ )
381
+
382
+ return bucket.get_public_url(filename)
383
+
384
+ except Exception as e:
385
+ print(f"Failed to upload image: {str(e)}")
386
+ return None
387
+ finally:
388
+ if 'buffer' in locals():
389
+ buffer.close()
390
+
391
+
392
  @nto_cto_router.post("/necklaceTryOnID")
393
  async def necklace_try_on_id(necklace_try_on_id: NecklaceTryOnIDEntity = Depends(parse_necklace_try_on_id),
394
  image: UploadFile = File(...)):
 
435
  status_code=500)
436
 
437
  try:
438
+ logger.info(">>> SAVING RESULT IMAGES <<<")
439
+ start_time_saving = time.time()
440
+
441
+ result_url = supabase_upload_and_return_url(prefix="necklace_try_on", image=result)
442
+ mask_url = supabase_upload_and_return_url(prefix="necklace_try_on_mask", image=mask)
443
+
444
+ logger.info(f">>> RESULT IMAGES SAVED IN {round((time.time() - start_time_saving), 2)}s <<<")
445
+
446
  logger.info(">>> RESULT IMAGES SAVED <<<")
447
  except Exception as e:
448
  logger.error(f">>> RESULT SAVING ERROR: {str(e)} <<<")
 
453
  total_backend_time = round((time.time() - start_time), 2)
454
  response = {
455
  "code": 200,
456
+ "output": f"{result_url}",
457
+ "mask": f"{mask_url}",
458
  "inference_time": total_backend_time
459
  }
460
  if creditResponse == "No Credits Available":