backend-space / app.py
kutkarsh200's picture
Upload folder using huggingface_hub
46b9a4f verified
import joblib
import pandas as pd
from flask import Flask, request, jsonify
# Initialize Flask app with a name
future_sale_predictor_api = Flask("SuperKart Sales Predictor")
# Load the trained sales prediction model
model = joblib.load("xgb_tuned_model.joblib")
# Define a route for the home page
@future_sale_predictor_api.get('/')
def home():
return "Welcome to the SuperKart Sales Prediction API!, Created by Kumar Utkarsh"
# Define an endpoint to predict sales for a single product-store combination
@future_sale_predictor_api.post('/v1/predict_sale')
def predict_sale():
# Get JSON data from the request
sale_data = request.get_json()
# Extract relevant product-store information from the input data
# Ensure these keys match the expected input features for your trained model
sample = {
'Product_Weight': sale_data['Product_Weight'],
'Product_Sugar_Content': sale_data['Product_Sugar_Content'],
'Product_Allocated_Area': sale_data['Product_Allocated_Area'],
'Product_Type': sale_data['Product_Type'],
'Product_MRP': sale_data['Product_MRP'],
'Store_Id': sale_data['Store_Id'],
'Store_Establishment_Year': sale_data['Store_Establishment_Year'],
'Store_Size': sale_data['Store_Size'],
'Store_Location_City_Type': sale_data['Store_Location_City_Type'],
'Store_Type': sale_data['Store_Type']
}
# Convert the extracted data into a DataFrame
input_data = pd.DataFrame([sample])
# Make a sales prediction using the trained model
prediction = model.predict(input_data).tolist()[0]
# Return the prediction as a JSON response
return jsonify({'Predicted_Sales': prediction})
# Define an endpoint to predict sales for a batch of product-store combinations
@future_sale_predictor_api.post('/v1/predict_sales_batch')
def predict_sales_batch():
# Get the uploaded CSV file from the request
file = request.files['file']
# Read the file into a DataFrame
input_data = pd.read_csv(file)
# Make predictions for the batch data
# Assuming the input CSV for batch prediction has the same columns as the training data
predictions = model.predict(input_data).tolist()
# You might want to return predictions linked to an identifier if available in the batch input
# For simplicity, returning a list of predictions
return jsonify({'Predicted_Sales_Batch': predictions})
# Run the Flask app in debug mode
if __name__ == '__main__':
# Port 7860 is used
app.run(debug=True, host='0.0.0.0', port=7860)