에코프로.AI

[Python 모델 서빙] FastApi - 써보기(기본) 본문

AI Tutorial

[Python 모델 서빙] FastApi - 써보기(기본)

AI_HitchHiker 2024. 12. 15. 16:52

https://github.com/fastapi/fastapi

 

FastAPI 설치

vscode 의 터미널에서 아래의 명령어를 실행하여, FastAPI 를 설치해 줍니다.

pip install "fastapi[standard]"

 

FastAPI 기본 실행 해보기

main.py 파일을 생성 후, 아래와 같이 작성해 줍니다.

# FastAPI 패키지 가져오기
from fastapi import FastAPI

# 인스턴스 생성
app = FastAPI()

# "/" 경로 설정
# GET: 데이터를 읽습니다.
@app.get("/")
# "/" 경로에 호출 함수
async def root():
    return {"message" : "Hello World"}

 

vscode의 터미널에 아래의 명령을 실행하여, 웹서버를 구동해 줍니다.

fastapi dev main.py

 

브라우저에서 아래의 URL접속

http://127.0.0.1:8000/

JSON 응답은 다음과 같습니다.

{"message": "Hello World"}

 

 

브라우저에서 아래의 URL 접속 ( 자동 대화형 API 문서가 제공됩니다)

http://127.0.0.1:8000/docs

 

 

[참고사이트] https://fastapi.tiangolo.com/tutorial/first-steps/#step-5-return-the-content

 

끝~