sastrysagi's picture
Upload folder using huggingface_hub
25dbcef verified
from flask import Flask, request, jsonify
import joblib
import pandas as pd
import numpy as np
app = Flask(__name__)
# Load the serialized model
try:
model = joblib.load('sales_forecast_model_v1.joblib')
except Exception as e:
print(f"Error loading model: {e}")
@app.route('/v1/sales', methods=['POST'])
def predict_single():
"""Endpoint for single prediction based on JSON input."""
try:
data = request.get_json()
df = pd.DataFrame([data])
prediction = model.predict(df)[0]
return jsonify({'Predicted_Sales': round(float(prediction), 2)})
except Exception as e:
return jsonify({'error': str(e)}), 400
@app.route('/v1/salesbatch', methods=['POST'])
def predict_batch():
"""Endpoint for batch predictions from a CSV file."""
try:
file = request.files['file']
df = pd.read_csv(file)
predictions = model.predict(df)
return jsonify({str(idx): round(float(pred), 2) for idx, pred in enumerate(predictions)})
except Exception as e:
return jsonify({'error': str(e)}), 400
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860, debug=False)