Spaces:
Build error
Build error
Commit ·
60493a2
1
Parent(s): f12d599
refactor: json response title and description generator
Browse files- app.py +0 -5
- src/api/image_prep_api.py +16 -1
app.py
CHANGED
|
@@ -23,8 +23,3 @@ app.add_middleware(
|
|
| 23 |
allow_methods=["*"],
|
| 24 |
allow_headers=["*"],
|
| 25 |
)
|
| 26 |
-
|
| 27 |
-
if __name__ == '__main__':
|
| 28 |
-
import uvicorn
|
| 29 |
-
|
| 30 |
-
uvicorn.run(app)
|
|
|
|
| 23 |
allow_methods=["*"],
|
| 24 |
allow_headers=["*"],
|
| 25 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/api/image_prep_api.py
CHANGED
|
@@ -5,6 +5,7 @@ author @ github.com/ishworrsubedii
|
|
| 5 |
"""
|
| 6 |
import base64
|
| 7 |
import os
|
|
|
|
| 8 |
from io import BytesIO
|
| 9 |
|
| 10 |
import cv2
|
|
@@ -195,12 +196,26 @@ async def remove_background_color_extraction(image: UploadFile = File(...), hex_
|
|
| 195 |
|
| 196 |
@preprocessing_router.post("/title_description_generator")
|
| 197 |
async def product_title_description_generator(image: UploadFile = File(...)):
|
|
|
|
| 198 |
image_bytes = await image.read()
|
| 199 |
image = Image.open(BytesIO(image_bytes)).convert("RGB")
|
| 200 |
try:
|
| 201 |
result = product_listing_obj.gen_title_desc(image=image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
return JSONResponse(content=result, status_code=200)
|
| 204 |
|
| 205 |
except Exception as e:
|
| 206 |
-
raise HTTPException(status_code=500,
|
|
|
|
|
|
| 5 |
"""
|
| 6 |
import base64
|
| 7 |
import os
|
| 8 |
+
import time
|
| 9 |
from io import BytesIO
|
| 10 |
|
| 11 |
import cv2
|
|
|
|
| 196 |
|
| 197 |
@preprocessing_router.post("/title_description_generator")
|
| 198 |
async def product_title_description_generator(image: UploadFile = File(...)):
|
| 199 |
+
start_time = time.time()
|
| 200 |
image_bytes = await image.read()
|
| 201 |
image = Image.open(BytesIO(image_bytes)).convert("RGB")
|
| 202 |
try:
|
| 203 |
result = product_listing_obj.gen_title_desc(image=image)
|
| 204 |
+
print(result)
|
| 205 |
+
|
| 206 |
+
title = result.split("Title:")[1].split("Description:")[0]
|
| 207 |
+
description = result.split("Description:")[1]
|
| 208 |
+
inference_time = time.time() - start_time
|
| 209 |
+
|
| 210 |
+
result = {
|
| 211 |
+
"code": 200,
|
| 212 |
+
"title": title,
|
| 213 |
+
"description": description,
|
| 214 |
+
"inference_time": inference_time
|
| 215 |
+
}
|
| 216 |
|
| 217 |
return JSONResponse(content=result, status_code=200)
|
| 218 |
|
| 219 |
except Exception as e:
|
| 220 |
+
raise HTTPException(status_code=500,
|
| 221 |
+
detail=f"Please make sure the image is clear and necklaces are visible{str(e)}")
|