Spaces:
Sleeping
Sleeping
Create App.py
Browse files
App.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
import joblib
|
| 3 |
+
import numpy as np
|
| 4 |
+
import traceback
|
| 5 |
+
|
| 6 |
+
# Initialize Flask app
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
+
# Load model
|
| 10 |
+
model = joblib.load("tuned_gradient_boosting_model.pkl")
|
| 11 |
+
|
| 12 |
+
# Home route
|
| 13 |
+
@app.route('/')
|
| 14 |
+
def home():
|
| 15 |
+
return "Sales Prediction API is running."
|
| 16 |
+
|
| 17 |
+
# Prediction route
|
| 18 |
+
@app.route('/predict', methods=['POST'])
|
| 19 |
+
def predict():
|
| 20 |
+
try:
|
| 21 |
+
# Parse JSON input
|
| 22 |
+
data = request.get_json(force=True)
|
| 23 |
+
|
| 24 |
+
# Extract features as a 2D array (expecting list or single sample dict)
|
| 25 |
+
features = np.array(data['features']).reshape(1, -1)
|
| 26 |
+
|
| 27 |
+
# Make prediction
|
| 28 |
+
prediction = model.predict(features)
|
| 29 |
+
|
| 30 |
+
# Return prediction as JSON
|
| 31 |
+
return jsonify({"predicted_sales": prediction[0]})
|
| 32 |
+
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return jsonify({"error": str(e), "trace": traceback.format_exc()})
|
| 35 |
+
|
| 36 |
+
if __name__ == '__main__':
|
| 37 |
+
app.run(debug=True, host='0.0.0.0', port=5000)
|