Spaces:
No application file
No application file
File size: 3,824 Bytes
cb10ae9 2f301d0 cb10ae9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | # Import necessary libraries
import numpy as np
import joblib # For loading the serialized model
import pandas as pd # For data manipulation
from flask import Flask, request, jsonify # For creating the Flask API
# Initialize the Flask application
cust_predictor_api = Flask("ExtraaLearn Customer Predictor")
# Load the trained machine learning model
model = joblib.load("customer_prediction_model_v1_0.joblib")
# Define a route for the home page (GET request)
@cust_predictor_api.get('/')
def home():
"""
This function handles GET requests to the root URL ('/') of the API.
It returns a simple welcome message.
"""
return "Welcome to the ExtraaLearn Customer Prediction API!"
classification_threshold = 0.45
# Define an endpoint for customer prediction (POST request)
@cust_predictor_api.post('/v1/cust_lead')
def predict_cust_lead():
"""
This function handles POST requests to the '/v1/cust_lead' endpoint.
It expects a JSON payload containing customer details and returns
the predicted customer probability as a JSON response.
"""
# Get the JSON data from the request body
cust_data = request.get_json()
# Extract relevant features from the JSON data
sample = {
'age' : cust_data['age'],
'current_occupation' : cust_data['current_occupation'],
'first_interaction' : cust_data['first_interaction'],
'profile_completed' : cust_data['profile_completed'],
'website_visits' : cust_data['website_visits'],
'time_spent_on_website' : cust_data['time_spent_on_website'],
'page_views_per_visit' : cust_data['page_views_per_visit'],
'last_activity' : cust_data['last_activity'],
'print_media_type1' : cust_data['print_media_type1'],
'print_media_type2' : cust_data['print_media_type2'],
'digital_media' : cust_data['digital_media'],
'educational_channels' : cust_data['educational_channels'],
'referral' : cust_data['referral']
}
# Convert the extracted data into a Pandas DataFrame
input_data = pd.DataFrame([sample])
# Make prediction
predicted_cust = model.predict_proba(input_data)[0][1]
# convert continuous prob as 0/1
predicted_cust = (predicted_cust >= classification_threshold).astype(int)
# Return the actual prediction status
return jsonify({'Predicted customer status': predicted_cust})
# Define an endpoint for batch prediction (POST request)
@cust_predictor_api.post('/v1/cust_lead_batch')
def predict_cust_lead_batch():
"""
This function handles POST requests to the '/v1/cust_lead_batch' endpoint.
It expects a CSV file containing property details for multiple properties
and returns the predicted status as a dictionary in the JSON response.
"""
# Get the uploaded CSV file from the request
file = request.files['file']
# Read the CSV file into a Pandas DataFrame
input_data = pd.read_csv(file)
# Make predictions for all properties in the DataFrame (get log_prices)
predicted_cust_list = model.predict_proba(input_data)[0][1]
predicted_cust_list = predicted_cust_list.tolist()
# Calculate actual prices
predicted_cust_list = [round(float(np.exp(log_price)), 2) for log_price in predicted_log_prices]
predicted_cust_list = [(predicted_cust >= classification_threshold).astype(int) for predicted_cust in predicted_cust_list]
# Create a dictionary of predictions with customer IDs as keys
ids = input_data['ID'].tolist()
output_dict = dict(zip(ids, predicted_cust_list))
# Return the predictions dictionary as a JSON response
return output_dict
# Run the Flask application in debug mode if this script is executed directly
if __name__ == '__main__':
cust_predictor_api.run(debug=True)
|