vijayendras's picture
Upload folder using huggingface_hub
456ba7b verified
# 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)