main.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import pandas as pd
  2. from pycaret.classification import load_model, predict_model
  3. from fastapi import FastAPI
  4. import uvicorn
  5. # Create the app
  6. app = FastAPI()
  7. # Load trained Pipeline
  8. model = load_model('my_api')
  9. # Define predict function
  10. @app.post('/predict')
  11. def predict(hum, temp, door, motion, illum, dayofweek, month, day, hour, minute):
  12. data = pd.DataFrame([[hum, temp, door, motion, illum, dayofweek, month, day, hour, minute]])
  13. data.columns = ['hum', 'temp', 'door', 'motion', 'illum', 'dayofweek', 'month', 'day', 'hour', 'minute']
  14. predictions = predict_model(model, data=data)
  15. return {'prediction': list(predictions['Label'])}
  16. @app.get('/predict')
  17. def predict(hum, temp, door, motion, illum, dayofweek, month, day, hour, minute):
  18. data = pd.DataFrame([[hum, temp, door, motion, illum, dayofweek, month, day, hour, minute]])
  19. data.columns = ['hum', 'temp', 'door', 'motion', 'illum', 'dayofweek', 'month', 'day', 'hour', 'minute']
  20. predictions = predict_model(model, data=data)
  21. return {'prediction': list(predictions['Label'])}
  22. # # Define predict function
  23. # @app.get('/predict')
  24. # def predict(hum, d_hum, dd_hum, temp, d_temp, dd_temp, door, motion, illum, dayofweek, month, hour):
  25. # data = pd.DataFrame([[hum, d_hum, dd_hum, temp, d_temp, dd_temp, door, motion, illum, dayofweek, month, hour]])
  26. # data.columns = ['hum', 'd_hum', 'dd_hum', 'temp', 'd_temp', 'dd_temp', 'door', 'motion', 'illum', 'dayofweek', 'month', 'hour']
  27. # predictions = predict_model(model, data=data)
  28. # return {'prediction': list(predictions['Label'])}
  29. # if __name__ == '__main__':
  30. # uvicorn.run(app, host='127.0.0.1', port=8000)
  31. '''
  32. uvicorn main:app --host 0.0.0.0 --port 8000
  33. curl -X 'GET' \
  34. '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' \
  35. -H 'accept: application/json'
  36. Request URL
  37. 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
  38. Response body
  39. Download
  40. {"prediction": ["0.0"]}
  41. Response headers
  42. content-length: 22
  43. content-type: application/json
  44. date: Fri,18 Mar 2022 05:25:05 GMT
  45. server: uvicorn
  46. 인증 토큰을 검색하고 레지스트리에 대해 Docker 클라이언트를 인증합니다.
  47. Use the AWS CLI:
  48. aws ecr get-login-password --region ap-northeast-2 | docker login --username AWS --password-stdin 314854109580.dkr.ecr.ap-northeast-2.amazonaws.com
  49. 참고: {AWSCLI}을(를) 사용하는 중 오류가 발생하면 최신 버전의 {AWSCLI} 및 Docker가 설치되어 있는지 확인하세요.
  50. 다음 명령을 사용하여 도커 이미지를 빌드합니다. 도커 파일을 처음부터 새로 빌드하는 방법에 대한 자세한 내용은 여기 지침을 참조하십시오. 이미지를 이미 빌드한 경우에는 이 단계를 건너뛸 수 있습니다.
  51. docker build -t iot-02 .
  52. 빌드가 완료되면 이미지에 태그를 지정하여 이 리포지토리에 푸시할 수 있습니다.
  53. docker tag iot-02:latest 314854109580.dkr.ecr.ap-northeast-2.amazonaws.com/iot-02:latest
  54. 다음 명령을 실행하여 이 이미지를 새로 생성한 AWS 리포지토리로 푸시합니다.
  55. docker push 314854109580.dkr.ecr.ap-northeast-2.amazonaws.com/iot-02:latest
  56. '''