superkart-api / app.py
romanus-iii's picture
Upload folder using huggingface_hub
7c5425e verified
import joblib
import numpy as np
import pandas as pd
from flask import Flask, request, jsonify
superkart_api = Flask(__name__)
print("πŸ”„ Loading model...")
model = joblib.load("superkart_model.joblib")
print("βœ… Model loaded.")
@superkart_api.route("/", methods=["GET", "POST"])
def index():
print(f"πŸ“‘ Incoming {request.method} request to /")
if request.method == "POST":
try:
data = request.get_json(force=True)
print(f"πŸ“₯ Received JSON: {data}")
input_dict = {
"Product_Weight": data.get("Product_Weight", 0.0),
"Product_Sugar_Content": data.get("Product_Sugar_Content", "Regular"),
"Product_Allocated_Area": data.get("Product_Allocated_Area", 0.0),
"Product_Type": data.get("Product_Type", "Other"),
"Product_MRP": data.get("Product_MRP", 0.0),
"Store_Establishment_Year": data.get("Store_Establishment_Year", 2000),
"Store_Size": data.get("Store_Size", "Medium"),
"Store_Location_City_Type": data.get("Store_Location_City_Type", "Tier 2"),
"Store_Type": data.get("Store_Type", "Supermarket Type1")
}
input_df = pd.DataFrame([input_dict])
prediction = model.predict(input_df)
return jsonify({"prediction": float(prediction[0])})
except Exception as e:
print(f"❌ Error: {str(e)}")
return jsonify({"error": str(e)}), 400
else:
return "βœ… SuperKart API root: ready."
if __name__ == "__main__":
print("πŸš€ Starting Flask server...")
superkart_api.run(host="0.0.0.0", port=7860)