Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +25 -0
- app.py +69 -0
- requirements.txt +10 -0
- superkart_sales_pipeline.joblib +3 -0
Dockerfile
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
Use a stable and slim Python base image
|
| 3 |
+
FROM python:3.9-slim
|
| 4 |
+
|
| 5 |
+
Set the working directory inside the container to /app
|
| 6 |
+
WORKDIR /app
|
| 7 |
+
|
| 8 |
+
Copy the requirements file first to leverage Docker's layer caching.
|
| 9 |
+
This way, dependencies are only re-installed if requirements.txt changes.
|
| 10 |
+
COPY requirements.txt .
|
| 11 |
+
|
| 12 |
+
Install the Python dependencies specified in the requirements file
|
| 13 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 14 |
+
|
| 15 |
+
Copy all other application files (app.py, model .joblib file, etc.) into the container
|
| 16 |
+
COPY . .
|
| 17 |
+
|
| 18 |
+
Expose the port that Hugging Face Spaces uses to run the application
|
| 19 |
+
EXPOSE 7860
|
| 20 |
+
|
| 21 |
+
Define the command to run the application using Gunicorn, a production-ready web server.
|
| 22 |
+
-w 2: Starts 2 worker processes to handle incoming requests.
|
| 23 |
+
-b 0.0.0.0:7860: Binds the server to port 7860, making it accessible from outside the container.
|
| 24 |
+
app:app: Specifies to run the 'app' Flask instance found in the 'app.py' file.
|
| 25 |
+
CMD ["gunicorn", "-w", "2", "-b", "0.0.0.0
|
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import os
|
| 3 |
+
import joblib
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from flask import Flask, request, jsonify
|
| 6 |
+
|
| 7 |
+
# Initialize Flask app
|
| 8 |
+
app = Flask("SuperKart Sales Predictor")
|
| 9 |
+
|
| 10 |
+
# Load the trained model pipeline from the file
|
| 11 |
+
# This is loaded only once when the application starts
|
| 12 |
+
try:
|
| 13 |
+
model = joblib.load("superkart_sales_pipeline.joblib")
|
| 14 |
+
print("Model loaded successfully.")
|
| 15 |
+
except FileNotFoundError:
|
| 16 |
+
print("Model file not found. Make sure 'superkart_sales_pipeline.joblib' is in the same directory.")
|
| 17 |
+
model = None
|
| 18 |
+
except Exception as e:
|
| 19 |
+
print(f"An error occurred while loading the model: {e}")
|
| 20 |
+
model = None
|
| 21 |
+
|
| 22 |
+
# Define a root endpoint for a health check
|
| 23 |
+
@app.route('/', methods=['GET'])
|
| 24 |
+
def home():
|
| 25 |
+
"""A simple endpoint to confirm the API is running."""
|
| 26 |
+
return "Welcome to the SuperKart Sales Prediction API!"
|
| 27 |
+
|
| 28 |
+
# Define the main endpoint for making sales predictions
|
| 29 |
+
@app.route('/predict', methods=['POST'])
|
| 30 |
+
def predict_sales():
|
| 31 |
+
"""
|
| 32 |
+
Receives a JSON object with features and returns a sales prediction.
|
| 33 |
+
"""
|
| 34 |
+
if model is None:
|
| 35 |
+
return jsonify({'error': 'Model is not loaded or failed to load.'}), 500
|
| 36 |
+
|
| 37 |
+
# Get the JSON data from the request body
|
| 38 |
+
input_data = request.get_json()
|
| 39 |
+
if not input_data:
|
| 40 |
+
return jsonify({'error': 'No input data provided.'}), 400
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
# Convert the JSON data into a pandas DataFrame
|
| 44 |
+
# The `index=[0]` is crucial for creating a single-row DataFrame
|
| 45 |
+
features_df = pd.DataFrame(input_data, index=[0])
|
| 46 |
+
|
| 47 |
+
# Make a prediction using the full pipeline
|
| 48 |
+
# The pipeline handles all preprocessing steps (scaling, encoding, etc.)
|
| 49 |
+
prediction = model.predict(features_df)
|
| 50 |
+
|
| 51 |
+
# The prediction is a numpy array, so we extract the single value
|
| 52 |
+
predicted_value = prediction[0]
|
| 53 |
+
|
| 54 |
+
# Return the prediction in a JSON response, rounded to 2 decimal places
|
| 55 |
+
return jsonify({'predicted_sales': round(predicted_value, 2)})
|
| 56 |
+
|
| 57 |
+
except (KeyError, TypeError) as e:
|
| 58 |
+
# This catches errors if the input JSON is missing keys or malformed
|
| 59 |
+
return jsonify({'error': f'Invalid input data format: {str(e)}'}), 400
|
| 60 |
+
except Exception as e:
|
| 61 |
+
# A general catch-all for any other unexpected errors
|
| 62 |
+
return jsonify({'error': f'An unexpected error occurred: {str(e)}'}), 500
|
| 63 |
+
|
| 64 |
+
# This block is for deployment environments like Hugging Face Spaces
|
| 65 |
+
if __name__ == '__main__':
|
| 66 |
+
# The port is determined by the environment variable, defaulting to 7860
|
| 67 |
+
port = int(os.environ.get("PORT", 7860))
|
| 68 |
+
# Running on 0.0.0.0 makes the app accessible from outside the container
|
| 69 |
+
app.run(host='0.0.0.0', port=port)
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Flask Web Framework and Gunicorn Server
|
| 2 |
+
Flask==2.2.2
|
| 3 |
+
gunicorn==20.1.0
|
| 4 |
+
|
| 5 |
+
# Core Data Science and ML Libraries
|
| 6 |
+
pandas==2.2.2
|
| 7 |
+
numpy==2.0.2
|
| 8 |
+
scikit-learn==1.6.1
|
| 9 |
+
xgboost==2.1.4
|
| 10 |
+
joblib==1.4.2
|
superkart_sales_pipeline.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4576bc43ad42b7c3ece2fbe8c3f19293f9d2914e3f5f36e67a1abbc795086ff8
|
| 3 |
+
size 24069091
|