Upload folder using huggingface_hub
Browse files- Dockerfile +17 -0
- app.py +56 -0
- requirements.txt +5 -0
Dockerfile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use an official Python runtime as a parent image
|
| 2 |
+
FROM python:3.9-slim
|
| 3 |
+
|
| 4 |
+
# Set the working directory in the container
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Copy the current directory contents into the container at /app
|
| 8 |
+
COPY . /app
|
| 9 |
+
|
| 10 |
+
# Install any needed packages specified in requirements.txt
|
| 11 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 12 |
+
|
| 13 |
+
# Make port 5000 available to the world outside this container
|
| 14 |
+
EXPOSE 5000
|
| 15 |
+
|
| 16 |
+
# Run the application using gunicorn
|
| 17 |
+
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5000", "app:app"]
|
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from flask import Flask, request, jsonify
|
| 3 |
+
import joblib
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
# Create a Flask application instance
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
+
# Define model path
|
| 10 |
+
MODEL_DIR = "model_artifacts"
|
| 11 |
+
MODEL_FILENAME = "best_sales_forecast_model.joblib"
|
| 12 |
+
MODEL_PATH = os.path.join(MODEL_DIR, MODEL_FILENAME)
|
| 13 |
+
|
| 14 |
+
# Load model at startup
|
| 15 |
+
try:
|
| 16 |
+
model = joblib.load(MODEL_PATH)
|
| 17 |
+
print(" Model loaded successfully!")
|
| 18 |
+
except Exception as e:
|
| 19 |
+
print(f" Error loading model: {e}")
|
| 20 |
+
model = None
|
| 21 |
+
|
| 22 |
+
# Health check route
|
| 23 |
+
@app.route("/", methods=["GET"])
|
| 24 |
+
def index():
|
| 25 |
+
return jsonify({"status": "Backend is running!"})
|
| 26 |
+
|
| 27 |
+
# Prediction route
|
| 28 |
+
@app.route("/predict", methods=["POST"])
|
| 29 |
+
def predict():
|
| 30 |
+
if model is None:
|
| 31 |
+
return jsonify({"error": "Model not loaded"}), 500
|
| 32 |
+
|
| 33 |
+
try:
|
| 34 |
+
# Get request JSON
|
| 35 |
+
data = request.get_json(force=True)
|
| 36 |
+
if not data:
|
| 37 |
+
return jsonify({"error": "No input data provided"}), 400
|
| 38 |
+
|
| 39 |
+
# Convert to DataFrame
|
| 40 |
+
df = pd.DataFrame(data)
|
| 41 |
+
|
| 42 |
+
# Drop ID column if present, as it's not used in prediction
|
| 43 |
+
if "Product_Id" in df.columns:
|
| 44 |
+
df = df.drop("Product_Id", axis=1)
|
| 45 |
+
|
| 46 |
+
# Predict
|
| 47 |
+
predictions = model.predict(df)
|
| 48 |
+
|
| 49 |
+
return jsonify({"predictions": predictions.tolist()})
|
| 50 |
+
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return jsonify({"error": str(e)}), 400
|
| 53 |
+
|
| 54 |
+
# Entry point
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
app.run(host="0.0.0.0", port=5000)
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Flask==3.0.3
|
| 2 |
+
joblib==1.4.2
|
| 3 |
+
pandas==2.2.2
|
| 4 |
+
scikit-learn==1.6.1
|
| 5 |
+
gunicorn==22.0.0
|