import pandas as pd from pycaret.classification import load_model, predict_model from fastapi import FastAPI import uvicorn # Create the app app = FastAPI() # Load trained Pipeline model = load_model('my_api') # Define predict function @app.post('/predict') def predict(hum, temp, door, motion, illum, dayofweek, month, day, hour, minute): data = pd.DataFrame([[hum, temp, door, motion, illum, dayofweek, month, day, hour, minute]]) data.columns = ['hum', 'temp', 'door', 'motion', 'illum', 'dayofweek', 'month', 'day', 'hour', 'minute'] predictions = predict_model(model, data=data) return {'prediction': list(predictions['Label'])} @app.get('/predict') def predict(hum, temp, door, motion, illum, dayofweek, month, day, hour, minute): data = pd.DataFrame([[hum, temp, door, motion, illum, dayofweek, month, day, hour, minute]]) data.columns = ['hum', 'temp', 'door', 'motion', 'illum', 'dayofweek', 'month', 'day', 'hour', 'minute'] predictions = predict_model(model, data=data) return {'prediction': list(predictions['Label'])} # # Define predict function # @app.get('/predict') # def predict(hum, d_hum, dd_hum, temp, d_temp, dd_temp, door, motion, illum, dayofweek, month, hour): # data = pd.DataFrame([[hum, d_hum, dd_hum, temp, d_temp, dd_temp, door, motion, illum, dayofweek, month, hour]]) # data.columns = ['hum', 'd_hum', 'dd_hum', 'temp', 'd_temp', 'dd_temp', 'door', 'motion', 'illum', 'dayofweek', 'month', 'hour'] # predictions = predict_model(model, data=data) # return {'prediction': list(predictions['Label'])} # if __name__ == '__main__': # uvicorn.run(app, host='127.0.0.1', port=8000) ''' uvicorn main:app --host 0.0.0.0 --port 8000 curl -X 'GET' \ 'http://localhost:8000/predict?hum=0&d_hum=0&dd_hum=0&temp=0&d_temp=0&dd_temp=0&door=0&motion=0&illum=0&dayofweek=0&month=0&hour=0' \ -H 'accept: application/json' Request URL http://localhost:8000/predict?hum=0&d_hum=0&dd_hum=0&temp=0&d_temp=0&dd_temp=0&door=0&motion=0&illum=0&dayofweek=0&month=0&hour=0 Response body Download {"prediction": ["0.0"]} Response headers content-length: 22 content-type: application/json date: Fri,18 Mar 2022 05:25:05 GMT server: uvicorn 인증 토큰을 검색하고 레지스트리에 대해 Docker 클라이언트를 인증합니다. Use the AWS CLI: aws ecr get-login-password --region ap-northeast-2 | docker login --username AWS --password-stdin 314854109580.dkr.ecr.ap-northeast-2.amazonaws.com 참고: {AWSCLI}을(를) 사용하는 중 오류가 발생하면 최신 버전의 {AWSCLI} 및 Docker가 설치되어 있는지 확인하세요. 다음 명령을 사용하여 도커 이미지를 빌드합니다. 도커 파일을 처음부터 새로 빌드하는 방법에 대한 자세한 내용은 여기 지침을 참조하십시오. 이미지를 이미 빌드한 경우에는 이 단계를 건너뛸 수 있습니다. docker build -t iot-02 . 빌드가 완료되면 이미지에 태그를 지정하여 이 리포지토리에 푸시할 수 있습니다. docker tag iot-02:latest 314854109580.dkr.ecr.ap-northeast-2.amazonaws.com/iot-02:latest 다음 명령을 실행하여 이 이미지를 새로 생성한 AWS 리포지토리로 푸시합니다. docker push 314854109580.dkr.ecr.ap-northeast-2.amazonaws.com/iot-02:latest '''