vaishaliagarwal's picture
Upload folder using huggingface_hub
e457145 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
import io
# Initialize the Flask application
product_sales_price_predictor_api = Flask("SuperKart Product Sales Predictor")
# Load the trained machine learning model
# Make sure the model file 'product_sales_prediction_model_v1_0.joblib' is in the same directory.
try:
model = joblib.load("product_sales_prediction_model_v1_0.joblib")
except FileNotFoundError:
model = None # Handle case where model is not found
# Define a route for the home page (GET request)
@product_sales_price_predictor_api.route('/', methods=['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 SuperKart Product Sales Prediction API!"
# Define an endpoint for single sales prediction (POST request)
@product_sales_price_predictor_api.route('/v1/predict_sales', methods=['POST'])
def predict_product_sales():
"""
This function handles POST requests to the '/v1/predict_sales' endpoint.
It expects a JSON payload containing product and store details and returns
the predicted sales total as a JSON response.
"""
if model is None:
return jsonify({'error': 'Model not loaded. Please check the server configuration.'}), 500
# Get the JSON data from the request body
data = request.get_json()
# Extract relevant features from the JSON data, matching the frontend
try:
sample = {
'Product_Weight': data['Product_Weight'],
'Product_Sugar_Content': data['Product_Sugar_Content'],
'Product_Allocated_Area': data['Product_Allocated_Area'],
'Product_Type': data['Product_Type'],
'Product_MRP': data['Product_MRP'],
'Store_Establishment_Year': data['Store_Establishment_Year'],
'Store_Size': data['Store_Size'],
'Store_Location_City_Type': data['Store_Location_City_Type'],
'Store_Type': data['Store_Type']
}
except KeyError as e:
return jsonify({'error': f'Missing key in input data: {e}'}), 400
# Convert the extracted data into a Pandas DataFrame
input_data = pd.DataFrame([sample])
# Make prediction
# Assuming the model predicts the actual sales value directly.
# If it predicts log value, you would need to convert it back (e.g., np.exp).
predicted_sales = model.predict(input_data)[0]
# Convert predicted_sales to a standard Python float for JSON serialization
predicted_sales = round(float(predicted_sales), 2)
# Return the predicted sales
return jsonify({'Predicted_Sales': predicted_sales})
# Define an endpoint for batch prediction (POST request)
@product_sales_price_predictor_api.route('/v1/predict_sales_batch', methods=['POST'])
def predict_product_sales_batch():
"""
This function handles POST requests to the '/v1/predict_sales_batch' endpoint.
It expects a CSV file containing details for multiple products
and returns the predicted sales as a JSON response.
"""
if model is None:
return jsonify({'error': 'Model not loaded. Please check the server configuration.'}), 500
# Check if a file was posted
if 'file' not in request.files:
return jsonify({'error': 'No file part in the request'}), 400
file = request.files['file']
# If the user does not select a file, the browser submits an empty file without a filename.
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
if file:
# Read the CSV file into a Pandas DataFrame
try:
input_data = pd.read_csv(file)
except Exception as e:
return jsonify({'error': f'Could not process CSV file: {e}'}), 400
# Ensure the required columns exist
required_columns = [
'Product_Weight', 'Product_Sugar_Content', 'Product_Allocated_Area',
'Product_Type', 'Product_MRP', 'Store_Establishment_Year',
'Store_Size', 'Store_Location_City_Type', 'Store_Type'
]
if not all(col in input_data.columns for col in required_columns):
return jsonify({'error': f'CSV must contain the following columns: {required_columns}'}), 400
# Make predictions for all products in the DataFrame
predictions = model.predict(input_data).tolist()
# Round the predictions
rounded_predictions = [round(float(p), 2) for p in predictions]
# Add predictions as a new column to the original dataframe
input_data['Predicted_Sales'] = rounded_predictions
# Return the DataFrame with predictions as a JSON object
return input_data.to_json(orient='records')
return jsonify({'error': 'An unknown error occurred'}), 500
# Run the Flask application in debug mode if this script is executed directly
if __name__ == '__main__':
# Use 0.0.0.0 to make it accessible from the network
product_sales_price_predictor_api.run(host='0.0.0.0', port=5000, debug=True)