flask_ml_api.py 744 B

123456789101112131415161718192021222324
  1. #!flask/bin/python
  2. from flask import Flask, jsonify, request
  3. import joblib
  4. import pandas as pd
  5. from cassandra_rw import CassandraReadWriteDb
  6. from PredictTxInfo import PredictTxInfoModel
  7. app = Flask(__name__)
  8. cwd = CassandraReadWriteDb(ip_addrs=['172.17.0.2'], keyspace="emp")
  9. cwd.sync_class_table(PredictTxInfoModel)
  10. @app.route('/predict/tx', methods=['POST'])
  11. def create_task():
  12. tx_data = request.json
  13. df = pd.DataFrame.from_records([tx_data])
  14. df = df.drop(['Time'],axis=1)
  15. model = joblib.load('model3.pipeline')
  16. tx_data['P'] = model.best_estimator_.predict(df)[0]
  17. cwd.write_json_table(tx_data)
  18. tx_data['P'] = str(tx_data['P'])
  19. return jsonify(tx_data), 201
  20. if __name__ == '__main__':
  21. app.run(debug=True)