Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 35 additions & 20 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,48 @@
import os
from logging import getLogger
from fastapi import FastAPI, Response, status
from vectorizer import ImageVectorizer, VectorImagePayload
import os


app = FastAPI()
imgVec : ImageVectorizer
logger = getLogger('uvicorn')


cuda_env = os.getenv("ENABLE_CUDA")
cuda_support=False
cuda_core=""
if cuda_env is not None and cuda_env == "true" or cuda_env == "1":
cuda_support=True
cuda_core = os.getenv("CUDA_CORE")
if cuda_core is None or cuda_core == "":
cuda_core = "cuda:0"
print("[INFO] cuda core set to {}".format(cuda_core))
else:
print("[INFO] running on CPU")
@app.on_event("startup")
def startup_event():
global imgVec

cuda_env = os.getenv("ENABLE_CUDA")
cuda_support = False
cuda_core = ""

if cuda_env is not None and cuda_env == "true" or cuda_env == "1":
cuda_support = True
cuda_core = os.getenv("CUDA_CORE")
if cuda_core is None or cuda_core == "":
cuda_core = "cuda:0"
logger.info(f"CUDA_CORE set to {cuda_core}")
else:
logger.info("Running on CPU")

imgVec = ImageVectorizer(cuda_support, cuda_core)

imgVec = ImageVectorizer(cuda_support, cuda_core)

@app.get("/.well-known/live", response_class=Response)
@app.get("/.well-known/ready", response_class=Response)
def live_and_ready(response: Response):
response.status_code = status.HTTP_204_NO_CONTENT
response.status_code = status.HTTP_204_NO_CONTENT


@app.post("/vectors")
def read_item(item: VectorImagePayload, response: Response):
try:
vector = imgVec.vectorize(item.id, item.image)
return {"id": item.id, "vector": vector.tolist(), "dim": len(vector)}
except Exception as e:
response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
return {"error": str(e)}
try:
vector = imgVec.vectorize(item.id, item.image)
return {"id": item.id, "vector": vector.tolist(), "dim": len(vector)}
except Exception as e:
logger.exception(
'Something went wrong while vectorizing data.'
)
response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
return {"error": str(e)}
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ fastapi==0.63.0
torch==1.8.1
torchvision==0.9.1
uvicorn==0.13.3
Pillow==8.1.2
Pillow==8.1.2