sathishaiuse's picture
Update app.py
a3b7b9f verified
raw
history blame contribute delete
943 Bytes
import joblib
import pandas as pd
import numpy as np
from flask import Flask, request, jsonify
# Load model
model = joblib.load("tuned_random_forest.joblib")
# Preprocessing pipeline must match training
# For now, assume incoming JSON already matches feature schema
app = Flask(__name__)
# Define a route for the home page
@app.get('/')
def home():
return "Welcome to the SuperKart Prediction API!"
@app.route("/health", methods=["GET"])
def health():
return jsonify({"status": "ok"}), 200
@app.post('/predict')
def predict():
try:
data = request.get_json()
df = pd.DataFrame([data]) # single row input
y_pred_log = model.predict(df)
y_pred = np.expm1(y_pred_log) # inverse log1p
return jsonify({"prediction": float(y_pred[0])})
except Exception as e:
return jsonify({"error": str(e)}), 400
if __name__ == '__main__':
app.run(host="0.0.0.0", port=7860, debug=True)