Skip to content

Commit

Permalink
feat: validation & error handling
Browse files Browse the repository at this point in the history
 Refs: #4
  • Loading branch information
ssaru committed Aug 6, 2020
1 parent c22d24e commit 0036d84
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions serving/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import torch
import torchvision

from fastapi import HTTPException
from pydantic import BaseModel
from pathlib import Path
from typing import Dict
Expand All @@ -25,7 +26,7 @@ def __init__(self):
"""
instantiation deep learning model
1. declare weight path
1. declare weight path variable
2. instantiation deep learning model
3. load weight and
"""
Expand All @@ -40,7 +41,7 @@ def __call__(self, request: Request) -> str:
inference
Args:
request (Request): 딥러닝 모델 추론을 위한 입력 데이터
request (Request): 딥러닝 모델 추론을 위한 입력 데이터(single image)
Returns:
(Response): 딥러닝 모델 추론 결과
Expand All @@ -55,15 +56,22 @@ def __call__(self, request: Request) -> str:
@staticmethod
def _preprocessing(base64image: str) -> torch.Tensor:
"""
base64로 encoding된 image를 torch.tensor로 변환
base64로 encoding된 single image를 torch.tensor로 변환
Args:
base64image (str): base64로 encoding된 이미지
Returns:
(torch.Tensor)
(torch.Tensor): torch tensor 이미지
"""
image = Image.open(BytesIO(base64.b64decode(base64image)))

try:
image = Image.open(BytesIO(base64.b64decode(base64image,
validate=True)))
except Exception as e:
raise HTTPException(status_code=400,
detail=f"Invalid base64 image string: {e}")

inputs = torchvision.transforms.ToTensor()(image).unsqueeze(0)

return inputs
Expand Down

0 comments on commit 0036d84

Please sign in to comment.