Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -6,49 +6,58 @@ import pandas as pd # For data manipulation
|
|
| 6 |
from flask import Flask, request, jsonify # For creating the Flask API
|
| 7 |
|
| 8 |
# Initialize Flask app with a name
|
| 9 |
-
extraalearn_api = Flask("ExtraaLearn")
|
| 10 |
|
| 11 |
-
# Load the trained
|
| 12 |
model = joblib.load("extraalearn_model.joblib")
|
| 13 |
|
| 14 |
# Define a route for the home page
|
| 15 |
@extraalearn_api.get('/')
|
| 16 |
def home():
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
# Define an endpoint to predict
|
| 20 |
@extraalearn_api.post('/v1/predict')
|
| 21 |
def predict_sales():
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
data = request.get_json()
|
| 24 |
|
| 25 |
# Extract relevant lead features from the input data
|
| 26 |
sample = {
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
}
|
| 41 |
-
|
| 42 |
-
# Convert the extracted data into a DataFrame
|
| 43 |
input_data = pd.DataFrame([sample])
|
| 44 |
|
| 45 |
-
# Make
|
| 46 |
prediction = model.predict(input_data)[0]
|
| 47 |
|
| 48 |
# Return the prediction as a JSON response
|
| 49 |
return jsonify({'Sales': prediction})
|
| 50 |
|
| 51 |
|
| 52 |
-
# Run the Flask app in debug mode
|
| 53 |
if __name__ == '__main__':
|
| 54 |
extraalearn_api.run(debug=True)
|
|
|
|
| 6 |
from flask import Flask, request, jsonify # For creating the Flask API
|
| 7 |
|
| 8 |
# Initialize Flask app with a name
|
| 9 |
+
extraalearn_api = Flask("ExtraaLearn Leads Predictor")
|
| 10 |
|
| 11 |
+
# Load the trained machine learning model extraalearn_model.joblib
|
| 12 |
model = joblib.load("extraalearn_model.joblib")
|
| 13 |
|
| 14 |
# Define a route for the home page
|
| 15 |
@extraalearn_api.get('/')
|
| 16 |
def home():
|
| 17 |
+
"""
|
| 18 |
+
This function handles GET requests to the root URL ('/') of the API.
|
| 19 |
+
It returns a simple welcome message.
|
| 20 |
+
"""
|
| 21 |
+
return "Welcome to the ExtraaLearn Prediction API!"
|
| 22 |
|
| 23 |
+
# Define an endpoint to predict a single lead
|
| 24 |
@extraalearn_api.post('/v1/predict')
|
| 25 |
def predict_sales():
|
| 26 |
+
"""
|
| 27 |
+
This function handles POST requests to the '/v1/predict' endpoint.
|
| 28 |
+
It expects a JSON payload containing product details and returns
|
| 29 |
+
the predicted lead's outcome status as a JSON response.
|
| 30 |
+
"""
|
| 31 |
+
# Get the JSON data from the request body
|
| 32 |
data = request.get_json()
|
| 33 |
|
| 34 |
# Extract relevant lead features from the input data
|
| 35 |
sample = {
|
| 36 |
+
'Age': data['age'],
|
| 37 |
+
'Current_Occupation': data['current_occupation'],
|
| 38 |
+
'First_Interaction': data['first_interaction'],
|
| 39 |
+
'Profile_Completed': data['profile_completed'],
|
| 40 |
+
'Website_Visits': data['website_visits'],
|
| 41 |
+
'Time_Spent_on_Website': data['time_spent_on_website'],
|
| 42 |
+
'Page_Views_Per_Visit': data['page_views_per_visit'],
|
| 43 |
+
'Last_Activity': data['last_activity'],
|
| 44 |
+
'Print_Media_Type1': data['print_media_type1'],
|
| 45 |
+
'Print_Media_Type2': data['print_media_type2'],
|
| 46 |
+
'Digital_Media': data['digital_media'],
|
| 47 |
+
'Educational_Channels': data['educational_channels'],
|
| 48 |
+
'Referral': data['referral']
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
# Convert the extracted data into a Pandas DataFrame
|
| 52 |
input_data = pd.DataFrame([sample])
|
| 53 |
|
| 54 |
+
# Make prediction using the trained model
|
| 55 |
prediction = model.predict(input_data)[0]
|
| 56 |
|
| 57 |
# Return the prediction as a JSON response
|
| 58 |
return jsonify({'Sales': prediction})
|
| 59 |
|
| 60 |
|
| 61 |
+
# Run the Flask app in debug mode if this script is executed directly
|
| 62 |
if __name__ == '__main__':
|
| 63 |
extraalearn_api.run(debug=True)
|