[ProductServinng] Day76_FastAPI(feat.poetry)

윰진·2023년 1월 9일
0

NaverAIBoostCamp정리

목록 보기
24/30

✨ 일반적으로 머신러닝 서버가 존재하거나 커다란 서버 안에 머신러닝 파트가 존재한다.

  • Monolithic Architecture : 하나의 큰 서버에서 파트 별로 처리하는 형식
    • Build 가 무거워지고 부분 Side Effect 발생 시 전체 서비스가 영향 받는다.

1. 기초 지식

  • RESTful API : Representational State Transfer API
  • HTTP(Hyper Text Transfer Protocol) :
  • URI 와 URL
    • Uniform Resource Identifier 로 인터넷 상의 자원을 식별하기 위한 문자열의 구성
    • Uniform Resource Locator 로 인터넷 상 자원의 위치
    • URI 는 URL 을 포함하며, URI가 더 포괄적인 범위다.
  • HTTP Method
    • GET, POST, PUT, PATCH, DELETE
  • GET,POST 차이
    • 참고 자료 - MDN Docs HTTP 캐싱
    • GET 은 어떤 정보를 가져와서 조회하기 위해 사용되는 방식으로, URL 에 변수(데이터)를 포함시켜 요청하는 것이다.
    • GET 은 데이터를 Header 에 포함하여 전송하고, URL 에 데이터가 노출되어 보안에 취약할 수 있고 캐싱할 수 있다.
    • POST 는 데이터를 서버로 제출해 추가 또는 수정하기 위해 사용하는 방식으로, URL 에 변수(데이터)를 노출하지 않고 요청한다.
    • POST 는 데이터를 Body 에 포함하여 전송하고, URL 에 데이터가 노출되지 않아 기본 보안은 되어 있다. 캐싱할 수 없다.
  • Header 와 Body
  • 동기와 비동기
  • IP
  • Port

✨ 참고 : 프로젝트 구조 (v1)

  • 프로젝트의 코드가 들어갈 모듈 설정(ap)
  • __main__.py : 간단하게 애플리케이션을 실행할 수 있는 entry point
  • main.py 또는 app.py : FastAPI 의 애플리케이션과 Router 설청
  • model.py 는 ML model 에 대한 클래스와 함수 정의

2. Poetry

virtualenv 는 순차적 설치라 의존성이 깨지기 쉽다. Poetry 는 이 점을 보완한다.

  • poetry init
  • poetry shell
    • poetry 활성화
  • poetry install
    • .toml 에 저장된 내용을 바탕으로 라이브러리 설치
  • poetry add
    • 필요한 패키지를 추가하고 싶은 경우 사용

1 ) 설치

curl -sSL https://install.python-poetry.org | python3 -

설치 후 나타나는 메시지

  • 아래 내용에 때라 shell configuration file 에 export PATH="/opt/ml/.local/bin:$PATH" 를 추가 해주어야 한다.
Retrieving Poetry metadata

# Welcome to Poetry!

This will download and install the latest version of Poetry,
a dependency and package manager for Python.

It will add the `poetry` command to Poetry's bin directory, located at:

/opt/ml/.local/bin

You can uninstall at any time by executing this script with the --uninstall option,
and these changes will be reverted.

Installing Poetry (1.3.1): Done

Poetry (1.3.1) is installed now. Great!

To get started you need Poetry's bin directory (/opt/ml/.local/bin) in your `PATH`
environment variable.

Add `export PATH="/opt/ml/.local/bin:$PATH"` to your shell configuration file.

Alternatively, you can call Poetry explicitly with `/opt/ml/.local/bin/poetry`.

You can test that everything is set up by executing:

`poetry --version`

shell configuration file 에 환경 변수 추가

shell configuration file 에 추가
export PATH="/opt/ml/.local/bin:$PATH"

열려있는 terminal 전부 끄고 poetry --version

2 ) poetry init

[tool.poetry]
name = "test-fastapi"
version = "0.1.0"
description = "FastAPI Test"
authors = ["hobbang2 <cattie@pukyong.ac.kr>"]
license = "MIT"
readme = "README.md"
packages = [{include = "test_fastapi"}]

[tool.poetry.dependencies]
python = ">=3.6.2,<3.10"
fastapi = "^0.70.0"
uvicorn = "^0.15.0"
pandas = "1.0.0"

[tool.poetry.group.dev.dependencies]
pytest = "^6.2.5"
black = "^21.9b0"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

3 ) 기본 코드 실행하기

from fastapi import FastAPI
app = FastAPI()

# "/" 로 접근하면 return message 를 보여준다. 
@app.get("/")
def read_root():
    return {"Hello": "World"}
CLI 또는 터미널에서 실행
uvicorn 01_simple_webserver:app --reload --host 0.0.0.0 --port 30001

또는

from fastapi import FastAPI
import uvicorn

app = FastAPI()

# "/" 로 접근하면 return message 를 보여준다. 
@app.get("/")
def read_root():
    return {"Hello": "World"}

if __name__ == '__main__':
    uvicorn.run(app, host="0.0.0.0",port=30001)

99.Windows poetry 설정

  • PowerShell 에서 (Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | python -
  • 환경 변수 추가 To get started you need Poetry's bin directory ($USERPROFILE$\AppData\Roaming\Python\Scripts) in your <PATH> environment variable.
  • PowerShell 에서 poetry --version

이 글은 커넥트 재단 Naver AI Boost Camp 교육자료를 참고했습니다.

0개의 댓글