Spaces:
No application file
No application file
| # 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) | |
| 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) | |
| 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) | |
| 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) | |