Spaces:
Configuration error
Configuration error
Download app.py from Bakalo/Sales_forecasting_api: direct link, hf CLI and curl.
- Browser
- Download file 1.29 kB
-
https://huggingface.co/spaces/Bakalo/Sales_forecasting_api/resolve/main/app.py
- Command line
-
hf download hf://spaces/Bakalo/Sales_forecasting_api/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Bakalo/Sales_forecasting_api/resolve/main/app.py
1.29 kB
| import pandas as pd | |
| from flask import Flask, request, jsonify | |
| import joblib | |
| import os | |
| import numpy as np | |
| import sklearn # Required for deserializing scikit-learn objects in the pipeline | |
| from flask_cors import CORS # To handle Cross-Origin Resource Sharing | |
| from dotenv import load_dotenv # To load environment variables | |
| # Load environment variables (if any) | |
| load_dotenv() | |
| #Initialize Flask app with a name | |
| Sales_forecasting_api = Flask("Sales Forecasting API") | |
| CORS(Sales_forecasting_api) # Initialize CORS for the Flask app | |
| #Load of the Saved Model Pipeline | |
| pipeline = joblib.load(os.path.join(os.path.dirname(__file__), "superkart_pipeline.pkl")) | |
| #Home Route | |
| def home(): | |
| return"Welcome to the SuperKart Sales Prediction API!" | |
| # Define an endpoint to predict Sales | |
| def predict(): | |
| try: | |
| # Get JSON data from the request | |
| data = request.get_json() | |
| # Convert JSON to DataFrame | |
| input_df = pd.DataFrame([data]) | |
| # Make predictions | |
| predictions = pipeline.predict(input_df) | |
| # Return predictions as JSON | |
| return jsonify({"predictions": predictions.tolist()}) | |
| except Exception as e: | |
| return jsonify({"error": str(e)}) | |