SK_backend / app.py
kuldeeptaiml's picture
Upload folder using huggingface_hub
599de66 verified
import numpy as np
import pandas as pd
import joblib
from flask import Flask, request, jsonify
# Initialize Flask app
superkart_app = Flask("superkart_sales_api")
# Load the trained model pipeline (preprocessing + model)
model = joblib.load("superkart.joblib")
# Health check route
@superkart_app.get('/')
def home():
return "Welcome to the SuperKart Sales Prediction API"
# Prediction route
@superkart_app.post('/v1/predict')
def predict_sales():
try:
# Parse JSON payload
data = request.get_json()
print("Raw incoming data:", data)
# Convert and transform input
sample = {
'Product_Weight': float(data['Product_Weight']),
'Product_Sugar_Content': data['Product_Sugar_Content'],
'Product_Allocated_Area': np.log1p(float(data['Product_Allocated_Area'])), # transform here
'Product_Type': data['Product_Type'],
'Product_MRP': float(data['Product_MRP']),
'Store_Size': data['Store_Size'],
'Store_Location_City_Type': data['Store_Location_City_Type'],
'Store_Type': data['Store_Type'],
'Store_Current_Age': int(data['Store_Current_Age'])
}
input_df = pd.DataFrame([sample])
print("Transformed input for model:\n", input_df)
# Make prediction
prediction = model.predict(input_df).tolist()[0]
return jsonify({'Predicted_Sales': prediction})
except Exception as e:
print("Error during prediction:", str(e))
return jsonify({'error': f"Prediction failed: {str(e)}"}), 500
# Run the Flask development server (for local testing/setup)
if __name__ == '__main__':
# Ensure the backend_files directory exists (important if running this file directly)
superkart_app.run(debug=True)