myfirsthftoken / app.py
sbpkoundinya's picture
Upload app.py with huggingface_hub
e49c81a verified
from flask import Flask, request, jsonify
import joblib
import numpy as np
import pandas as pd # Import pandas
# Load model
model = joblib.load('superkart_model_v1.0_joblib')
app = Flask(__name__)
@app.route("/")
def welcome():
return "SuperKart Sales Forecasting API is running."
@app.route("/predict", methods=["POST"])
def predict():
# Expect JSON with keys matching model input feature order
data = request.json
# The input data from the request needs to be in the correct order and format
# as expected by the preprocessor and model pipeline.
# Assuming the input JSON keys match the original feature names and order:
# Create a pandas DataFrame from the input data
# Ensure the column names match the features used during training
feature_names = [
'Product_Weight', 'Product_Sugar_Content','Product_Allocated_Area', 'Product_Type',
'Product_MRP', 'Store_Establishment_Year', 'Store_Size',
'Store_Location_City_Type', 'Store_Type'
]
input_df = pd.DataFrame([data.values()], columns=feature_names) # Convert data to DataFrame
# Note: The loaded model includes the preprocessor, so we don't need to
# manually preprocess the input here. The pipeline handles it.
prediction = model.predict(input_df) # Pass DataFrame to predict
return jsonify({'prediction': float(prediction[0])})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860, debug=True)