my_api.py 1.1 KB

1234567891011121314151617181920212223242526272829
  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. if __name__ == '__main__':
  23. uvicorn.run(app, host='127.0.0.1', port=8000)