# 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 Flask app with a name reflective of the project extraalearn_api = Flask("ExtLearn") # Load the trained lead conversion model (ensure the file name matches your saved model) model = joblib.load("extlearn_model.joblib") # Define a route for the home page @extraalearn_api.get('/') def home(): return "Welcome to the ExtraaLearn Lead Conversion Prediction System" # Define an endpoint to predict status (converted/not converted) for a lead @extraalearn_api.post('/v1/predict') def predict_conversion(): # Get JSON data from the request body data = request.get_json() # Extract relevant features based on the ExtraaLearn dataset # These must match the exact feature names used during model training sample = { 'age': data['age'], 'current_occupation': data['current_occupation'], 'first_interaction': data['first_interaction'], 'profile_completed': data['profile_completed'], 'website_visits': data['website_visits'], 'time_spent_on_website': data['time_spent_on_website'], 'page_views_per_visit': data['page_views_per_visit'], 'last_activity': data['last_activity'], 'print_media_type1': data['print_media_type1'], 'print_media_type2': data['print_media_type2'], 'digital_media': data['digital_media'], 'educational_channels': data['educational_channels'], 'referral': data['referral'] } # Convert the extracted data into a DataFrame for the model pipeline input_data = pd.DataFrame([sample]) # Calculate the engineered feature 'age_time_interaction' input_data['age_time_interaction'] = input_data['age'] * input_data['time_spent_on_website'] # Make a prediction (1 for converted, 0 for not converted) prediction = int(model.predict(input_data)[0]) # Optional: Get the probability of conversion probability = model.predict_proba(input_data)[0][1] # Return the prediction and probability as a JSON response return jsonify({ 'Status_Prediction': prediction, 'Conversion_Probability': round(float(probability), 4), 'Message': 'High Potential Lead' if prediction == 1 else 'Low Potential Lead' }) # Run the Flask app if __name__ == '__main__': extraalearn_api.run(debug=True)