Spaces:
Runtime error
Runtime error
Commit ·
c699f9b
1
Parent(s): 9064a97
Add K-Means customer segmentation application
Browse files- Dockerfile +28 -0
- app.py +51 -0
- kmeans_model.joblib +0 -0
- requirements.txt +4 -0
- scaler_model.joblib +0 -0
Dockerfile
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
| 2 |
+
# you will also find guides on how best to write your Dockerfile
|
| 3 |
+
FROM python:3.9
|
| 4 |
+
|
| 5 |
+
# Set up a non-root user for security
|
| 6 |
+
RUN useradd -m -u 1000 user
|
| 7 |
+
USER user
|
| 8 |
+
|
| 9 |
+
# Set the PATH for user's local installations
|
| 10 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 11 |
+
|
| 12 |
+
# Set the working directory inside the container
|
| 13 |
+
WORKDIR /app
|
| 14 |
+
|
| 15 |
+
# Copy requirements.txt and install dependencies
|
| 16 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
| 17 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 18 |
+
|
| 19 |
+
# Copy all application files (including app.py, kmeans_model.joblib, scaler_model.joblib)
|
| 20 |
+
COPY --chown=user . /app
|
| 21 |
+
|
| 22 |
+
# Command to run your Flask app
|
| 23 |
+
# Flask typically uses a WSGI server like Gunicorn or directly app.run() for simple cases.
|
| 24 |
+
# For a simple Flask app to run, you can use:
|
| 25 |
+
CMD ["python", "app.py"] # This assumes app.py has app.run(host="0.0.0.0", port=7860)
|
| 26 |
+
|
| 27 |
+
# OR, if you want a more robust setup with Gunicorn (add gunicorn to requirements.txt):
|
| 28 |
+
# CMD ["gunicorn", "app:app", "--bind", "0.0.0.0:7860"]
|
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
import joblib
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
|
| 8 |
+
# Load the pre-trained model and scaler
|
| 9 |
+
try:
|
| 10 |
+
kmeans_model = joblib.load("kmeans_model.joblib")
|
| 11 |
+
scaler_model = joblib.load("scaler_model.joblib")
|
| 12 |
+
print("Models loaded successfully!")
|
| 13 |
+
except Exception as e:
|
| 14 |
+
print(f"Error loading models: {e}")
|
| 15 |
+
kmeans_model = None
|
| 16 |
+
scaler_model = None
|
| 17 |
+
|
| 18 |
+
@app.route("/predict", methods=["POST"])
|
| 19 |
+
def predict():
|
| 20 |
+
if kmeans_model is None or scaler_model is None:
|
| 21 |
+
return jsonify({"error": "Model not loaded. Please check deployment logs."}), 500
|
| 22 |
+
|
| 23 |
+
data = request.get_json(force=True)
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
# Extract features and ensure order
|
| 27 |
+
age = data.get("age")
|
| 28 |
+
annual_income = data.get("annual_income")
|
| 29 |
+
spending_score = data.get("spending_score")
|
| 30 |
+
|
| 31 |
+
if age is None or annual_income is None or spending_score is None:
|
| 32 |
+
return jsonify({"error": "Missing required input features (age, annual_income, spending_score)."}), 400
|
| 33 |
+
|
| 34 |
+
# Create a DataFrame for scaling
|
| 35 |
+
features_df = pd.DataFrame([[age, annual_income, spending_score]],
|
| 36 |
+
columns=['Age', 'Annual Income (k$)', 'Spending Score (1-100)'])
|
| 37 |
+
|
| 38 |
+
# Scale the input features using the loaded scaler
|
| 39 |
+
scaled_features = scaler_model.transform(features_df)
|
| 40 |
+
|
| 41 |
+
# Predict the cluster
|
| 42 |
+
prediction = kmeans_model.predict(scaled_features)
|
| 43 |
+
cluster_id = int(prediction[0]) # Convert numpy int to Python int
|
| 44 |
+
|
| 45 |
+
return jsonify({"cluster_id": cluster_id})
|
| 46 |
+
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return jsonify({"error": str(e)}), 500
|
| 49 |
+
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
app.run(debug=True) # debug=True for local testing, set to False for deployment
|
kmeans_model.joblib
ADDED
|
Binary file (222 Bytes). View file
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas
|
| 2 |
+
scikit-learn
|
| 3 |
+
joblib
|
| 4 |
+
flask # or fastapi, uvicorn
|
scaler_model.joblib
ADDED
|
Binary file (129 Bytes). View file
|
|
|