nlauchande's picture
Upload folder using huggingface_hub
f826441 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 the Flask application
forecast_predictor_api = Flask("Forecast Predictor")
# Load the trained machine learning model
model = joblib.load("SuperKartbest_xgb.joblib")
@forecast_predictor_api.get('/')
def home():
return "Welcome to the Sales Prediction API!"
@forecast_predictor_api.post('/v1/predict')
def predict_single():
# Get the JSON data from the request body
product_data = request.get_json()
# Extract relevant features from the JSON data
sample = {
'Product_Weight': product_data['Product_Weight'],
'Product_Allocated_Area': product_data['Product_Allocated_Area'],
'Product_MRP': product_data['Product_MRP'],
'Store_Establishment_Year': product_data['Store_Establishment_Year'],
'Product_Sugar_Content_No Sugar': product_data['Product_Sugar_Content_No Sugar'],
'Product_Sugar_Content_Regular': product_data['Product_Sugar_Content_Regular'],
'Product_Sugar_Content_reg': product_data['Product_Sugar_Content_reg'],
'Product_Type_Breads': product_data['Product_Type_Breads'],
'Product_Type_Breakfast': product_data['Product_Type_Breakfast'],
'Product_Type_Canned': product_data['Product_Type_Canned'],
'Product_Type_Dairy': product_data['Product_Type_Dairy'],
'Product_Type_Frozen Foods': product_data['Product_Type_Frozen Foods'],
'Product_Type_Fruits and Vegetables': product_data['Product_Type_Fruits and Vegetables'],
'Product_Type_Hard Drinks': product_data['Product_Type_Hard Drinks'],
'Product_Type_Health and Hygiene': product_data['Product_Type_Health and Hygiene'],
'Product_Type_Household': product_data['Product_Type_Household'],
'Product_Type_Meat': product_data['Product_Type_Meat'],
'Product_Type_Others': product_data['Product_Type_Others'],
'Product_Type_Seafood': product_data['Product_Type_Seafood'],
'Product_Type_Snack Foods': product_data['Product_Type_Snack Foods'],
'Product_Type_Soft Drinks': product_data['Product_Type_Soft Drinks'],
'Product_Type_Starchy Foods': product_data['Product_Type_Starchy Foods'],
'Store_Size_Medium': product_data['Store_Size_Medium'],
'Store_Size_Small': product_data['Store_Size_Small'],
'Store_Location_City_Type_Tier 2': product_data['Store_Location_City_Type_Tier 2'],
'Store_Location_City_Type_Tier 3': product_data['Store_Location_City_Type_Tier 3'],
'Store_Type_Food Mart': product_data['Store_Type_Food Mart'],
'Store_Type_Supermarket Type1': product_data['Store_Type_Supermarket Type1'],
'Store_Type_Supermarket Type2': product_data['Store_Type_Supermarket Type2']
}
# Convert the extracted data into a Pandas DataFrame
input_data = pd.DataFrame([sample])
# Make prediction (get log_price)
predicted_price = model.predict(input_data)[0]
# Convert predicted_price to Python float
predicted_price = round(float(predicted_price), 2)
# The conversion above is needed as we convert the model prediction (log price) to actual price using np.exp, which returns predictions as NumPy float32 values.
# When we send this value directly within a JSON response, Flask's jsonify function encounters a datatype error
# Return the actual price
return jsonify({'Predicted Price (in dollars)': predicted_price})
# Run the Flask application in debug mode if this script is executed directly
if __name__ == '__main__':
forecast_predictor_api.run(debug=True)