Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import necessary libraries
|
| 2 |
+
import numpy as np
|
| 3 |
+
import joblib # For loading the serialized model
|
| 4 |
+
import pandas as pd # For data manipulation
|
| 5 |
+
from flask import Flask, request, jsonify # For creating the Flask API
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
# Initialize Flask app with a name
|
| 9 |
+
learn_api = Flask("ExtraaLearn_Predictor")
|
| 10 |
+
|
| 11 |
+
# Load the trained prediction model
|
| 12 |
+
model = joblib.load('extralearn_model_v1.joblib')
|
| 13 |
+
|
| 14 |
+
# Define a route for the home page
|
| 15 |
+
@learn_api.get('/')
|
| 16 |
+
def home():
|
| 17 |
+
return "Welcome to the Lead Conversion-Prediction System"
|
| 18 |
+
|
| 19 |
+
# Define an endpoint to predict conversion of a single customer
|
| 20 |
+
@learn_api.post('/v1/predict')
|
| 21 |
+
def predict_sales(): #Was 'prediction', & then 'enrollment'
|
| 22 |
+
# Get JSON data from the request
|
| 23 |
+
data = request.get_json()
|
| 24 |
+
|
| 25 |
+
# Extract relevant customer features from the input data
|
| 26 |
+
sample = {
|
| 27 |
+
'age': data['age'],
|
| 28 |
+
'current_occupation': data['current_occupation'],
|
| 29 |
+
'first_interaction': data['first_interaction'],
|
| 30 |
+
'profile_completed': data['profile_completed'],
|
| 31 |
+
'website_visits': data['website_visits'],
|
| 32 |
+
'time_spent_on_website': data['time_spent_on_website'],
|
| 33 |
+
'page_views_per_visit': data['page_views_per_visit'],
|
| 34 |
+
'last_activity': data['last_activity'],
|
| 35 |
+
'print_media_type1': data['print_media_type1'],
|
| 36 |
+
'print_media_type2': data['print_media_type2'],
|
| 37 |
+
'digital_media': data['digital_media'],
|
| 38 |
+
'educational_channels': data['educational_channels'],
|
| 39 |
+
'referral': data['referral']
|
| 40 |
+
}
|
| 41 |
+
# Convert the extracted data into a DataFrame
|
| 42 |
+
input_data = pd.DataFrame([sample])
|
| 43 |
+
|
| 44 |
+
# Make a prediction using the trained model
|
| 45 |
+
prediction = model.predict(input_data).tolist()[0]
|
| 46 |
+
|
| 47 |
+
# Return the prediction as a JSON response
|
| 48 |
+
return jsonify({'Lead': prediction})
|
| 49 |
+
|
| 50 |
+
# Run the Flask app in debug mode
|
| 51 |
+
if __name__ == '__main__':
|
| 52 |
+
learn_api.run(debug=True)
|