superkart / app.py
entorr's picture
Upload backend files for SuperKart (#4)
1a1bdd7 verified
import joblib
import pandas as pd
from flask import Flask, request, jsonify
# Initialize Flask app with a clear name
app = Flask("SuperKart Sales Forecaster")
# Load the trained model
model = joblib.load('model.joblib')
# Define a route for the home page (Health Check)
@app.route('/', methods=['GET'])
def home():
return "Welcome to the SuperKart Sales Forecasting API!"
# Define the prediction endpoint
@app.route('/predict', methods=['POST'])
def predict():
try:
# Get JSON data from the request
data = request.get_json()
# Convert input to pandas DataFrame
if isinstance(data, dict):
df = pd.DataFrame([data])
else:
df = pd.DataFrame(data)
# Make prediction
prediction = model.predict(df)
# Return the result as JSON
return jsonify({
'status': 'success',
'prediction': prediction.tolist()
})
except Exception as e:
return jsonify({
'status': 'error',
'message': str(e)
})
# Run the app on port 7860 for Hugging Face Spaces
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860)