Rizwan9's picture
Upload app.py with huggingface_hub
3af9bf8 verified
from flask import Flask, request, jsonify
import joblib
import pandas as pd
app = Flask(__name__)
# Load the serialized model
model = joblib.load('best_sales_forecasting_model.pkl')
@app.route('/')
def home():
return "Sales Forecasting Backend is running!"
@app.route('/predict', methods=['POST'])
def predict():
try:
data = request.get_json(force=True)
# Convert the incoming data to a pandas DataFrame
# Assuming the incoming data is a list of dictionaries, where each dictionary is a data point
input_data = pd.DataFrame(data)
# Ensure the columns are in the same order as the training data
# This assumes you have access to the columns from your training data (X_train)
# You might need to adjust this part based on how you handle feature ordering
# For demonstration, let's assume the input data has the same columns in the same order
# In a real application, you might need to reorder columns or handle missing ones
# Make predictions
predictions = model.predict(input_data)
# Return predictions as a JSON response
return jsonify(predictions.tolist())
except Exception as e:
return jsonify({'error': str(e)})
if __name__ == '__main__':
# Running on 0.0.0.0 makes it accessible externally, useful for deployment
app.run(host='0.0.0.0', port=5000)