File size: 1,516 Bytes
d2ce771 16d8b1f be0362b 16d8b1f 23c180c d2ce771 16d8b1f be0362b 16d8b1f cb17ba0 be0362b 16d8b1f be0362b 16d8b1f cb17ba0 16d8b1f cb17ba0 16d8b1f be0362b 16d8b1f be0362b 16d8b1f be0362b 16d8b1f be0362b 16d8b1f 43ff6d3 16d8b1f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
import os
import joblib
from flask import Flask, request, jsonify
import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings("ignore")
# Define the path to the serialised model
MODEL_PATH = "/content/Backend_files/SuperKart_Sales_Prediction_Model.joblib"
# Load the trained model pipeline
try:
model_pipeline = joblib.load(MODEL_PATH)
print(f"Model loaded successfully from {MODEL_PATH}")
except Exception as e:
model_pipeline = None
print(f"Error loading model: {e}")
# Initialize the Flask application
app = Flask(__name__)
# Define a route for the home page
@app.route("/", methods=["GET"])
def home():
return "Welcome to the SuperKart Sales Prediction App!"
# Define an endpoint for making predictions
@app.route("/predict", methods=["POST"])
def predict():
if model_pipeline is None:
return jsonify({"error": "Model not loaded"}), 500
try:
# Get JSON data from the request
data = request.get_json()
if not data:
return jsonify({"error": "No data provided"}), 400
# Extract features from the JSON data
input_df = pd.DataFrame([data])
prediction = model_pipeline.predict(input_df)
return jsonify({"prediction": prediction.tolist()})
except Exception as e:
return jsonify({"error": f'Error during prediction: {e}'}), 500
if __name__ == "__main__": # Correct indentation
port = int(os.environ.get("PORT", 5000))
app.run(host="0.0.0.0", port=5000, debug=True)
|