Spaces:
Sleeping
Sleeping
| # Import necessary libraries | |
| import joblib | |
| import pandas as pd | |
| from flask import Flask, request, jsonify | |
| # Initialize the Flask application | |
| sales_predictor_api = Flask("Sales Predictor") | |
| # Load the trained machine learning model | |
| model = joblib.load("sales_prediction_model_v1_0.joblib") | |
| # Home route | |
| def home(): | |
| return "Welcome to the Sales Prediction API!" | |
| # Single sales prediction | |
| def predict_sales(): | |
| sales_data = request.get_json() | |
| input_df = pd.DataFrame([{ | |
| "Product_Weight": sales_data["Product_Weight"], | |
| "Product_Sugar_Content": sales_data["Product_Sugar_Content"], | |
| "Product_Allocated_Area": sales_data["Product_Allocated_Area"], | |
| "Product_Type": sales_data["Product_Type"], | |
| "Product_MRP": sales_data["Product_MRP"], | |
| "Store_Establishment_Year": sales_data["Store_Establishment_Year"], | |
| "Store_Size": sales_data["Store_Size"], | |
| "Store_Location_City_Type": sales_data["Store_Location_City_Type"], | |
| "Store_Type": sales_data["Store_Type"] | |
| }]) | |
| prediction = model.predict(input_df)[0] | |
| return jsonify({"predicted_sales": float(prediction)}) | |
| # Batch prediction | |
| def predict_sales_batch(): | |
| file = request.files["file"] | |
| input_df = pd.read_csv(file) | |
| predictions = model.predict(input_df) | |
| return jsonify({"predicted_sales": predictions.tolist()}) | |
| if __name__ == "__main__": | |
| sales_predictor_api.run(debug=True) | |