my_api.py 793 B

12345678910111213141516171819202122
  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, d_hum, dd_hum, temp, d_temp, dd_temp, door, motion, illum, dayofweek, month, hour):
  12. data = pd.DataFrame([[hum, d_hum, dd_hum, temp, d_temp, dd_temp, door, motion, illum, dayofweek, month, hour]])
  13. data.columns = ['hum', 'd_hum', 'dd_hum', 'temp', 'd_temp', 'dd_temp', 'door', 'motion', 'illum', 'dayofweek', 'month', 'hour']
  14. predictions = predict_model(model, data=data)
  15. return {'prediction': list(predictions['Label'])}
  16. if __name__ == '__main__':
  17. uvicorn.run(app, host='127.0.0.1', port=8000)