본문 바로가기
융복합 프로젝트 PZONE

<PZONE 프로젝트> 3.FastAPI로 Model 환경 구성 및 Container 생성

by 앙팡공기팡 2023. 2. 20.

 

 

 

지난 글

 

EC2 대여 및 Docker 환경 구축하기 : https://cooookieeee.tistory.com/5

 

<PZONE 프로젝트> 2. EC2 대여 및 Docker 환경 구축

지난 글 Django 환경 구성하기: https://cooookieeee.tistory.com/2 1. Django 환경 구성하기 기본 requirement 1. Python 설치(Path 환경변수 지정 필*) 2. Pycharm 설치 진행사항 1. 새 프로젝트 생성( Python 가상환경 같이

cooookieeee.tistory.com

 

 

 

 

 

진행사항

 

팀원과의 협업 및 지속적 통합을 위해 git을 사용하였다.

Django로부터 request가 Image로 온다는것을 가정하였다.

uviconrn을 통해 127.0.0.1/docs/로 Image Request를 시험하였다.

Response는 Dictionary형태로 Key는 Image 내의 마커 개수, 마커 종류로 구성하였다. 

 

 

 

1. Pycharm을 통해  새 Python 프로젝트 생성(가상환경 설치)

 

2. pip install을 통하여 FastAPI Module 설치

 

3. uvicorn 설치 

 

4. 빅데이터 팀원으로부터 Model git clone으로 받아오기 

 

5. post method로 API생성 및 post,multipart-formdata로 설정

 

6. Model과 API 연동

 

async def save_file(file: IO):
    with NamedTemporaryFile("wb", delete=False) as tempfile:
        tempfile.write(file.read())
        return tempfile.name


@app.post("/file/store")
async def store_file(file: UploadFile = File(...)):
    path = await save_file(file.file)

    return Score(path)

 

7. python 설치 패키지 requirements 파일로 freeze

 

8.  git commit push 및 Docker Image  Build

 

9. Build 한 Image로 Docker Container 배포 (port는 8080 : 8000 portforwarding 으로 구성)

 

10. EC2 SG에서 Inbound 8080 허용 설정

 

 

문제점 및 개선점

 

 

 

 

1. "vars() argument must have_dict_attribute" Error 문제 :

 

     -Numpy개체를 Int로 함수로 덧씌워 생기는 문제였다.

 

 

2. 이미지 파일 request를 못받는 문제:

         

    -tempfile library사용해 임시 파일로 저장하고 다시 불러오는 방법으로 해결하였다.

 

 

3.경로를 잘 못 찾는 문제

 

   -BASE_DIR 변수와 os.path.join을 통하여 패스를 재 지정하였다. 

 

 

4. "uploadfile' object has no attribute" Error 문제

 

    -file: UploadFile = File(...) 구문 사용(UploadFIle은 await필요), await save_file(file.file)로 구문을 바꾸었더니 해결되었다.

 

 

5. 임시 배포때 gunicorn 사용시 container stop되는 문제

 

   - uvicorn을 사용하였더니 해결되었다. 

  

 

6. pywin32 linux에서 안돌아가는 문제

   

    -requirements 에서 삭제

 

 

7. Local판과 배포판과의 환경 차이 문제

 

    -container exec으로 들어가서 수정 후 docker image commit으로 업데이트

  

8. "ImportError: libGL.so.1" Error 문제

 

    - DockerFile에 apt-get install libgl1-mesa-glx 구문 추가

 

 

9.  cors Error 문제

         

     -cors exempt로 예외 처리

 

 

 

 

 

REF

 

 

 

https://www.youtube.com/watch?v=KGlr8Adw858

 

-fastapi와 django 비교

 

 

https://choiseokwon.tistory.com/257

 

-fastapi 기본 

 

 

https://velog.io/@lottocomeon/FastAPI-%ED%8C%8C%EC%9D%BC%EC%B2%98

%EB%A6%AC

https://kkminseok.github.io/posts/fastapi20/

 

-fast api file 처리

 

https://fastapi.tiangolo.com/tutorial/request-files/

 

-fast api 공식 document

 

https://ko.javascript.info/async-await

 

-async와 await

 

https://facerain.club/fastapi-nginx/

 

-fastapi 내 gunicorn 사용