Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +24 -0
- app.py +88 -0
- requirements.txt +14 -0
Dockerfile
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ================================
|
| 2 |
+
# Dockerfile for SuperKart Price Prediction API
|
| 3 |
+
# ================================
|
| 4 |
+
|
| 5 |
+
# Use a lightweight official Python image
|
| 6 |
+
FROM python:3.9-slim
|
| 7 |
+
|
| 8 |
+
# Set the working directory inside the container
|
| 9 |
+
WORKDIR /app
|
| 10 |
+
|
| 11 |
+
# Copy all backend and model files into the container
|
| 12 |
+
COPY . .
|
| 13 |
+
|
| 14 |
+
# Install dependencies from requirements.txt
|
| 15 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 16 |
+
|
| 17 |
+
# Expose port 7860 (Hugging Face Spaces default for Docker)
|
| 18 |
+
EXPOSE 7860
|
| 19 |
+
|
| 20 |
+
# Run the Flask app using Gunicorn with 4 workers
|
| 21 |
+
# -w 4 : 4 worker processes for handling multiple requests
|
| 22 |
+
# -b 0.0.0.0:7860 : bind to all network interfaces on port 7860
|
| 23 |
+
# app:superkart_api : points to "superkart_api" instance in app.py
|
| 24 |
+
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:superkart_api"]
|
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ==============================
|
| 2 |
+
# SuperKart Price Prediction API (Flask Backend)
|
| 3 |
+
# ==============================
|
| 4 |
+
|
| 5 |
+
# Import necessary libraries
|
| 6 |
+
from flask import Flask, request, jsonify # For creating the Flask API
|
| 7 |
+
|
| 8 |
+
# Initialize the Flask application
|
| 9 |
+
superkart_api = Flask("SuperKart Price Predictor")
|
| 10 |
+
|
| 11 |
+
# Load the trained SuperKart model pipeline (includes preprocessing + tuned XGBoost model)
|
| 12 |
+
model = joblib.load("deployment_files/xgb_tuned_superkart_model_v1_0.joblib")
|
| 13 |
+
|
| 14 |
+
# ---------------------------------
|
| 15 |
+
# Root Endpoint (GET request)
|
| 16 |
+
# ---------------------------------
|
| 17 |
+
@superkart_api.get('/')
|
| 18 |
+
def home():
|
| 19 |
+
"""
|
| 20 |
+
This function handles GET requests to the root URL ('/').
|
| 21 |
+
It simply returns a welcome message confirming the API is running.
|
| 22 |
+
"""
|
| 23 |
+
return "Welcome to the SuperKart Price Prediction API!"
|
| 24 |
+
|
| 25 |
+
# ---------------------------------
|
| 26 |
+
# Single Prediction Endpoint (POST request)
|
| 27 |
+
# ---------------------------------
|
| 28 |
+
@superkart_api.post('/v1/predict')
|
| 29 |
+
def predict_price():
|
| 30 |
+
"""
|
| 31 |
+
Handles POST requests to '/v1/predict'.
|
| 32 |
+
Expects JSON input with product features and returns
|
| 33 |
+
the predicted selling price as JSON.
|
| 34 |
+
"""
|
| 35 |
+
# Get JSON data from request body
|
| 36 |
+
product_data = request.get_json()
|
| 37 |
+
|
| 38 |
+
# Convert JSON to a DataFrame (single row)
|
| 39 |
+
input_data = pd.DataFrame([product_data])
|
| 40 |
+
|
| 41 |
+
# Make prediction using the trained pipeline
|
| 42 |
+
predicted_price = model.predict(input_data)[0]
|
| 43 |
+
|
| 44 |
+
# Ensure prediction is a standard Python float and round
|
| 45 |
+
predicted_price = round(float(predicted_price), 2)
|
| 46 |
+
|
| 47 |
+
# Return prediction as JSON response
|
| 48 |
+
return jsonify({'Predicted Price': predicted_price})
|
| 49 |
+
|
| 50 |
+
# ---------------------------------
|
| 51 |
+
# Batch Prediction Endpoint (POST request)
|
| 52 |
+
# ---------------------------------
|
| 53 |
+
@superkart_api.post('/v1/predictbatch')
|
| 54 |
+
def predict_price_batch():
|
| 55 |
+
"""
|
| 56 |
+
Handles POST requests to '/v1/predictbatch'.
|
| 57 |
+
Expects a CSV file upload with multiple product entries and
|
| 58 |
+
returns predicted prices for all rows in JSON format.
|
| 59 |
+
"""
|
| 60 |
+
# Get uploaded CSV file
|
| 61 |
+
file = request.files['file']
|
| 62 |
+
|
| 63 |
+
# Read CSV into DataFrame
|
| 64 |
+
input_data = pd.read_csv(file)
|
| 65 |
+
|
| 66 |
+
# Make predictions
|
| 67 |
+
predicted_prices = model.predict(input_data).tolist()
|
| 68 |
+
|
| 69 |
+
# Round predictions to 2 decimal places
|
| 70 |
+
predicted_prices = [round(float(p), 2) for p in predicted_prices]
|
| 71 |
+
|
| 72 |
+
# If 'id' column exists in data, map predictions to IDs
|
| 73 |
+
if 'id' in input_data.columns:
|
| 74 |
+
product_ids = input_data['id'].tolist()
|
| 75 |
+
output_dict = dict(zip(product_ids, predicted_prices))
|
| 76 |
+
else:
|
| 77 |
+
# Otherwise return as a simple list
|
| 78 |
+
output_dict = {"Predicted Prices": predicted_prices}
|
| 79 |
+
|
| 80 |
+
# Return JSON response
|
| 81 |
+
return jsonify(output_dict)
|
| 82 |
+
|
| 83 |
+
# ---------------------------------
|
| 84 |
+
# Run the Flask application
|
| 85 |
+
# ---------------------------------
|
| 86 |
+
if __name__ == '__main__':
|
| 87 |
+
# Run API in debug mode (for development)
|
| 88 |
+
superkart_api.run(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This file specifies all the dependencies required to run the SuperKart Price Prediction API
|
| 2 |
+
# It ensures that the environment is consistent across development, testing, and deployment.
|
| 3 |
+
|
| 4 |
+
pandas==2.2.2 # For handling tabular data and preprocessing
|
| 5 |
+
numpy==2.0.2 # For numerical computations
|
| 6 |
+
scikit-learn==1.6.1 # For preprocessing pipeline utilities
|
| 7 |
+
xgboost==2.1.4 # The core ML model (XGBoost Regressor)
|
| 8 |
+
joblib==1.4.2 # For loading/saving serialized models
|
| 9 |
+
Werkzeug==2.2.2 # Flask dependency (request/response utilities)
|
| 10 |
+
flask==2.2.2 # Flask framework for serving the API
|
| 11 |
+
gunicorn==20.1.0 # WSGI server for production deployment
|
| 12 |
+
requests==2.28.1 # For making HTTP requests (testing endpoints, etc.)
|
| 13 |
+
uvicorn[standard] # Optional: ASGI server (if switching to FastAPI later)
|
| 14 |
+
streamlit==1.43.2 # For interactive frontend (if we build a Streamlit dashboard)
|