diff --git a/bird_cnn/__pycache__/server.cpython-314.pyc b/bird_cnn/__pycache__/server.cpython-314.pyc index 0cd2d06..9ae87ea 100644 Binary files a/bird_cnn/__pycache__/server.cpython-314.pyc and b/bird_cnn/__pycache__/server.cpython-314.pyc differ diff --git a/bird_cnn/server.py b/bird_cnn/server.py index 96fa479..3d82a5d 100644 --- a/bird_cnn/server.py +++ b/bird_cnn/server.py @@ -11,9 +11,13 @@ import torch.nn.functional as F from bird_cnn import Bird_CNN +import threading + BUILD_PATH = "./build_models" IMAGE_SIZE = 256 +sem = threading.Semaphore(1) #adjust to performance + class bird_species(Enum): Common_Kingfisher = 0 CommonMyna = 1 @@ -53,17 +57,18 @@ app.add_middleware( @app.post("/predict") async def predict(file: UploadFile = File(...)): - image_bytes = await file.read() + with sem: + image_bytes = await file.read() - image = Image.open(io.BytesIO(image_bytes)).convert("RGB") - image = transform(image).unsqueeze(0).to(device) + image = Image.open(io.BytesIO(image_bytes)).convert("RGB") + image = transform(image).unsqueeze(0).to(device) - with torch.no_grad(): - pred = model(image) - probs = F.softmax(pred, dim=1) - confidence, cls = torch.max(probs, dim=1) + with torch.no_grad(): + pred = model(image) + probs = F.softmax(pred, dim=1) + confidence, cls = torch.max(probs, dim=1) - return { - "class": bird_species(cls.item()).name, - "confidence": confidence.item() - } \ No newline at end of file + return { + "class": bird_species(cls.item()).name, + "confidence": confidence.item() + } \ No newline at end of file