simnid's picture
Upload folder using huggingface_hub
3642fc8 verified
# Import necessary libraries
import numpy as np # For numerical operations
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
from flask_cors import CORS # Enable Cross-Origin Resource Sharing (CORS) for the API
# Initialize the Flask application
superkart_sales_api = Flask("SuperKart Sales Predictor")
CORS(superkart_sales_api)
# Load the trained machine learning model
model = joblib.load("superkart_sales_model.pkl")
# Define a route for the home page (GET request)
@superkart_sales_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 SuperKart Sales Prediction API!"
# Define an endpoint for single product-store prediction (POST request)
@superkart_sales_api.post('/v1/predict')
def predict_sales():
"""
This function handles POST requests to the '/v1/predict' endpoint.
It expects a JSON payload containing product and store details and returns
the predicted sales total as a JSON response.
"""
try:
# Get the JSON data from the request body
input_data = request.get_json()
# Check if data is provided
if not input_data:
return jsonify({'error': 'No data provided'}), 400
# DataFrame with ALL expected features, filling missing ones with defaults
expected_features = {
'Product_Weight': 0.0,
'Product_Allocated_Area': 0.0,
'Product_MRP': 0.0,
'Product_Sugar_Content': 'Missing',
'Product_Type': 'Missing',
'Store_Size': 'Missing',
'Store_Location_City_Type': 'Missing',
'Store_Type': 'Missing',
'Store_Age': 0,
'Product_Category_Code': 0,
'Store_Age_Group': 'Missing' # This was the missing feature
}
# Update with the provided values, keep defaults for missing ones
for key in expected_features:
if key in input_data:
expected_features[key] = input_data[key]
# Convert to DataFrame
input_df = pd.DataFrame([expected_features])
# Make prediction
predicted_sales = model.predict(input_df)[0]
# Convert predicted_sales to Python float and round
predicted_sales = round(float(predicted_sales), 2)
# Return the predicted sales
return jsonify({'Predicted Sales Total': predicted_sales})
except Exception as e:
return jsonify({'error': str(e)}), 500
# Define an endpoint for batch prediction (POST request)
@superkart_sales_api.post('/v1/predictbatch')
def predict_sales_batch():
"""
This function handles POST requests to the '/v1/predictbatch' endpoint.
It expects a CSV file containing product-store details for multiple items
and returns the predicted sales totals as a dictionary in the JSON response.
"""
try:
# Get the uploaded CSV file from the request
file = request.files['file']
# Read the CSV file into a Pandas DataFrame
input_data = pd.read_csv(file)
# Make predictions for all items in the DataFrame
predicted_sales = model.predict(input_data)
# Round the predictions
predicted_sales = [round(float(sale), 2) for sale in predicted_sales]
# Create a dictionary of predictions with item IDs as keys
# Assuming there's an 'item_id' column in your CSV
if 'item_id' in input_data.columns:
item_ids = input_data['item_id'].tolist()
output_dict = dict(zip(item_ids, predicted_sales))
else:
# If no ID column, use index as keys
output_dict = {f"item_{i}": sale for i, sale in enumerate(predicted_sales)}
# Return the predictions dictionary as a JSON response
return output_dict
except Exception as e:
return jsonify({'error': str(e)}), 500
# Run the Flask application
if __name__ == '__main__':
superkart_sales_api.run(debug=True, host='0.0.0.0', port=5000)