jackfroooot's picture
Upload folder using huggingface_hub
5040877 verified
raw
history blame
2.79 kB
# 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
try:
model = joblib.load("customer_prediction_model_v1_0.joblib")
except Exception as e:
# If the model fails to load, print the error and continue, but the API will fail gracefully
print(f"ERROR: Failed to load model: {e}")
model = None # Set to None so the prediction route can check it
# 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})
# Run the Flask application in debug mode if this script is executed directly
if __name__ == '__main__':
cust_predictor_api.run(debug=True)