File size: 2,311 Bytes
b6d6830
 
 
 
 
 
 
 
4ba7573
b6d6830
4ba7573
b6d6830
 
 
 
 
4ba7573
 
 
 
 
b6d6830
4ba7573
b6d6830
 
4ba7573
 
456ba7b
4ba7573
 
 
319b613
b6d6830
 
 
264fe94
 
 
 
 
 
 
 
 
 
 
 
 
4ba7573
 
 
b6d6830
 
4ba7573
b6d6830
 
 
2ac3ac1
456ba7b
b6d6830
4ba7573
b6d6830
 
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

# 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
extraalearn_api = Flask("ExtraaLearn Leads Predictor")

# Load the trained machine learning model extraalearn_model.joblib
model = joblib.load("extraalearn_model.joblib")

# Define a route for the home page
@extraalearn_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 Prediction API!"

# Define an endpoint to predict a single lead
@extraalearn_api.post('/v1/predict')
def predict_sales():
    """
    This function handles POST requests to the '/v1/predict' endpoint.
    It expects a JSON payload containing lead details and returns
    the predicted lead's outcome status as a JSON response.
    """
    # Get the JSON data from the request body
    lead_data = request.get_json()

    # Extract relevant lead features from the input data
    sample = {
      'age': lead_data['age'],
      'current_occupation': lead_data['current_occupation'],
      'first_interaction': lead_data['first_interaction'],
      'profile_completed': lead_data['profile_completed'],
      'website_visits': lead_data['website_visits'],
      'time_spent_on_website': lead_data['time_spent_on_website'],
      'page_views_per_Visit': lead_data['page_views_per_visit'],
      'last_activity': lead_data['last_activity'],
      'print_media_type1': lead_data['print_media_type1'],
      'print_media_type2': lead_data['print_media_type2'],
      'digital_media': lead_data['digital_media'],
      'educational_channels': lead_data['educational_channels'],
      'referral': lead_data['referral']
    }

    # Convert the extracted data into a Pandas DataFrame
    input_data = pd.DataFrame([sample])

    # Make prediction using the trained model
    prediction = model.predict(input_data)[0]

    # Return the prediction as a JSON response
    return jsonify({'Status': int(prediction)})
        

# Run the Flask app in debug mode if this script is executed directly
if __name__ == '__main__':
    extraalearn_api.run(debug=True)