predict_revenue / app.py
affanthinks's picture
Upload folder using huggingface_hub
12e570a verified
from flask import Flask, request, jsonify
import joblib
import numpy as np
import os
from huggingface_hub import hf_hub_download
import joblib
model_path = hf_hub_download(
repo_id="affanthinks/superkart",
filename="AGreatLearning/tuned_bagging_model.pkl", # include directory
revision="main", # ensures correct branch
token=os.getenv("HF_TOKEN") # authentication
)
model = joblib.load(model_path)
print("✅ Model loaded successfully from", model_path)
# Initialize app
app = Flask("predict_revenue")
@app.route("/")
def home():
return jsonify({"message": "Supermarket Revenue Prediction API is running!"})
@app.route("/predict", methods=["POST"])
def predict():
try:
# Get JSON input
data = request.get_json(force=True)
features = np.array(data["features"]).reshape(1, -1)
# Predict
prediction = model.predict(features)[0]
return jsonify({"predicted_revenue": float(prediction)})
except Exception as e:
return jsonify({"error": str(e)})
if "predict_revenue" == "__main__":
app.run(host="0.0.0.0", port=7860, debug=True)