Sirisha335 commited on
Commit
cef9ca2
·
verified ·
1 Parent(s): 540ec30

Add deployment file: app.py

Browse files
Files changed (1) hide show
  1. deployment/app.py +35 -0
deployment/app.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import joblib
2
+ import pandas as pd
3
+ from flask import Flask, request, jsonify
4
+
5
+ app = Flask(__name__)
6
+
7
+ # Load the trained model
8
+ model = joblib.load('best_random_forest_model.joblib')
9
+
10
+ @app.route('/predict', methods=['POST'])
11
+ def predict():
12
+ try:
13
+ # Get JSON data from the request
14
+ data = request.get_json(force=True)
15
+
16
+ # Convert input data to DataFrame
17
+ # Ensure the order of columns matches the training data
18
+ # Expected features: Engine rpm, Lub oil pressure, Fuel pressure, Coolant pressure, Coolant temp
19
+ input_df = pd.DataFrame([data])
20
+
21
+ # Make prediction
22
+ prediction = model.predict(input_df)
23
+ prediction_proba = model.predict_proba(input_df)
24
+
25
+ # Return prediction as JSON
26
+ return jsonify({
27
+ 'engine_condition_prediction': int(prediction[0]),
28
+ 'probability_normal': prediction_proba[0][0],
29
+ 'probability_faulty': prediction_proba[0][1]
30
+ })
31
+ except Exception as e:
32
+ return jsonify({'error': str(e)}), 400
33
+
34
+ if __name__ == '__main__':
35
+ app.run(host='0.0.0.0', port=5000, debug=True)