Pratik26Dec commited on
Commit
4820019
·
verified ·
1 Parent(s): f7edc53

requirement.txt

Browse files

Flask==2.2.2
pandas==1.5.3
scikit-learn==1.2.2
joblib==1.2.0
gunicorn==20.1.0

Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import joblib
2
+ import pandas as pd
3
+ from flask import Flask, request, jsonify
4
+
5
+ app = Flask(__name__)
6
+
7
+ # Load the serialized model and its components
8
+ try:
9
+ model = joblib.load('extraalearn_best_model.joblib')
10
+ except FileNotFoundError:
11
+ print("Error: 'extraalearn_best_model.joblib' not found. Ensure it's in the same directory.")
12
+ model = None
13
+
14
+ @app.route('/predict', methods=['POST'])
15
+ def predict():
16
+ """
17
+ Predicts lead conversion based on input data.
18
+ Input data should be a JSON object with lead features.
19
+ """
20
+ if not model:
21
+ return jsonify({'error': 'Model not loaded. Check server logs.'}), 500
22
+
23
+ try:
24
+ # Get the JSON data from the request
25
+ data = request.get_json(force=True)
26
+
27
+ # Convert the dictionary to a DataFrame. The feature names must match the training data.
28
+ input_df = pd.DataFrame([data])
29
+
30
+ # Ensure the columns are in the correct order for the pipeline
31
+ required_columns = ['age', 'current_occupation', 'first_interaction',
32
+ 'profile_completed', 'website_visits', 'time_spent_on_website',
33
+ 'page_views_per_visit', 'last_activity', 'print_media_type1',
34
+ 'print_media_type2', 'digital_media', 'educational_channels',
35
+ 'referral']
36
+ input_df = input_df[required_columns]
37
+
38
+ # Make prediction
39
+ prediction = model.predict(input_df)[0]
40
+ prediction_proba = model.predict_proba(input_df)[0].tolist()
41
+
42
+ # Return the result
43
+ result = {
44
+ 'prediction': int(prediction),
45
+ 'prediction_label': 'Converted' if prediction == 1 else 'Not Converted',
46
+ 'probabilities': {
47
+ 'Not Converted': prediction_proba[0],
48
+ 'Converted': prediction_proba[1]
49
+ }
50
+ }
51
+ return jsonify(result)
52
+
53
+ except Exception as e:
54
+ return jsonify({'error': str(e)}), 400
55
+
56
+ if __name__ == '__main__':
57
+ app.run(host='0.0.0.0', port=5000)