Skip to content

클라우드에 저장된 감성 분류 모델을 FastAPI 기반 Kubernetes 앱으로 배포하는 프로젝트입니다.

Notifications You must be signed in to change notification settings

jihoahn9303/fastapi-model-serving

Repository files navigation

Realtime Sentiment Classification with FastAPI

FastAPI 기반 Kubernetes 앱 배포를 바탕으로, GCP에 존재하는 ML 모델을 서빙하는 프로젝트입니다.

모델 학습 및 아티팩트 배포와 관련된 소스 및 블로그 설명은 다음 링크를 참고해주시기 바랍니다.

Code source

Blog

감성 분석 모델: Scikit-learn의 Logistic Regression

성능: 0.897(F1-score)

성과: 동시 접속자가 300명인 환경에서, 평균 모델 추론 시간이 28ms인 FastAPI 웹 서버 구축

Machine Learning Task

머신러닝 태스크의 경우, 영문 리뷰를 기반으로 한 이진 감성 분류 문제를 선택하였습니다. (positive/negative)

모델 학습 및 평가를 위한 데이터로서, Kaggle에서 제공한 'IMDB Dataset of 50K Movie Reviews'를 사용하였습니다.

자세한 내용은 링크를 참고해주세요 :)

Project process

serving project

  1. 모델 서빙에 필요한 코드를 Github에 push할 경우, Github Actions을 통해 자동으로 코드를 컨테이너로 build합니다.

  2. 빌드 된 컨테이너는 GCP Artifact Registry에 자동으로 배포됩니다.

  3. Github Actions의 워크플로우에 의하여, 컨테이너는 GCP Kubernetes에 Kubernetes 애플리케이션 형태로 배포됩니다.

  4. ArgoCD Server를 통해 Kubernetes 애플리케이션에 대한 Health check를 자동으로 수행합니다. (이때, ArgoCD 서버는 GCP Kubernetes 클러스터에서 미리 설치한 상태입니다)

  5. Health check가 완료된 애플리케이션은 GCP Cloud SQL에 쿼리를 수행하여, 성능이 가장 좋은 모델의 URL을 가져옵니다.

  6. 애플리케이션은 GCP Cloud Storage에서 URL에 대응하는 모델을 가져옵니다. (FastAPI Lifespan Event)

  7. FastAPI 웹 애플리케이션은 가져온 모델을 통해, 추론(inference)을 수행할 준비를 마칩니다.

Project structure

본 프로젝트의 파일 구성요소는 다음과 같습니다.

.
├── Dockerfile
├── README.md
├── chart
│   ├── Chart.yaml
│   ├── charts
│   ├── sentiment-helm-0.0.1.tgz
│   ├── templates
│   │   ├── NOTES.txt
│   │   ├── _helpers.tpl
│   │   ├── deployment.yaml
│   │   ├── hpa.yaml
│   │   ├── ingress.yaml
│   │   ├── service.yaml
│   │   ├── serviceaccount.yaml
│   │   └── tests
│   │       └── test-connection.yaml
│   └── values.yaml
├── database
│   ├── __init__.py
│   ├── database_setting.py
│   └── model.py
├── main.py
├── nlp-mlops-service-account.json
├── poetry.lock
├── pyproject.toml
├── requirements.txt
├── scripts
│   └── create_env.sh
└── src
    ├── __init__.py
    └── model.py

Develop & Experiment environment

개발 환경에 대한 주요 사항은 아래와 같습니다.

Source Version
OS(Host) Host: Microsoft Windows 10 Pro build 19045 / NVIDIA GeForce RTX 3060 12GB
Remote controller WSL2 / Ubuntu version: 20.04
Python 3.9.10
IDLE Visual Studio code 1.75.1
Docker Docker desktop 4.26.1 / Engine version: 24.0.7

Usage

Curl

터미널에서 curl을 이용하여, 다음과 같이 추론 서비스를 호출할 수 있습니다.

curl -X 'POST' \
  'http://34.47.86.49/inference' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "sentence": "This story is really good!"
}'

Python

Requests 라이브러리를 이용하여, 다음과 같이 추론 서비스를 호출할 수 있습니다.

import requests


if __name__ == '__main__':
    url = 'http://34.47.86.49/inference'
    result = requests.post(
        url=url, 
        json={
            "sentence": "This story is really good!"
        }
    )

    print(result.text)

About

클라우드에 저장된 감성 분류 모델을 FastAPI 기반 Kubernetes 앱으로 배포하는 프로젝트입니다.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published