SuperKart / app.py
SagarAtHf's picture
Upload folder using huggingface_hub
4b98b11 verified
import joblib
from flask import Flask, request, jsonify
import pandas as pd
import numpy as np
# Load the trained model and preprocessor
model = joblib.load('final_model.joblib')
preprocessor = joblib.load('preprocessor.joblib')
superkart_revenue_forecaster_api = Flask("SuperKart Sales Revenue Forecaster")
# Define a route for the home page
@superkart_revenue_forecaster_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 Revenue Forecaster - By Vidyasagar Chitchula'
# Define the prediction route
#@superkart_revenue_forecaster_api.route('/forecast_revenue', methods=['POST'])
# Define an endpoint to predict the sales revenue
@superkart_revenue_forecaster_api.post('/v1/forecastrevenue')
def forecast_Revenue():
try:
data = request.get_json()
# Convert input data to a Pandas DataFrame
input_df = pd.DataFrame([data])
# Recreate the 'Store_Age' feature if 'Store_Establishment_Year' is provided
if 'Store_Establishment_Year' in input_df.columns:
input_df['Store_Age'] = 2025 - input_df['Store_Establishment_Year']
input_df = input_df.drop('Store_Establishment_Year', axis=1)
# Drop 'Product_Id' if it exists in the input
if 'Product_Id' in input_df.columns:
input_df = input_df.drop('Product_Id', axis=1)
# Preprocess the input data
processed_data = preprocessor.transform(input_df)
# Make prediction
prediction = model.predict(processed_data)
# Convert numpy.float32 to standard float before JSON serialization
return jsonify({'predicted_sales': float(prediction[0])})
except Exception as e:
return jsonify({'error': str(e)}), 400
#if __name__ == '__main__':
# superkart_revenue_forecaster_api.run(host='0.0.0.0', port=5000)