Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- Dockerfile +11 -15
- app.py +86 -0
- requirements.txt +10 -3
Dockerfile
CHANGED
|
@@ -1,20 +1,16 @@
|
|
| 1 |
-
FROM python:3.
|
| 2 |
|
|
|
|
| 3 |
WORKDIR /app
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
curl \
|
| 8 |
-
git \
|
| 9 |
-
&& rm -rf /var/lib/apt/lists/*
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
ENTRYPOINT ["streamlit", "run", "src/streamlit_app.py", "--server.port=8501", "--server.address=0.0.0.0"]
|
|
|
|
| 1 |
+
FROM python:3.9-slim
|
| 2 |
|
| 3 |
+
# Set the working directory inside the container
|
| 4 |
WORKDIR /app
|
| 5 |
|
| 6 |
+
# Copy all files from the current directory to the container's working directory
|
| 7 |
+
COPY . .
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Install dependencies from the requirements file without using cache to reduce image size
|
| 10 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 11 |
|
| 12 |
+
# Define the command to start the application using Gunicorn with 4 worker processes
|
| 13 |
+
# - `-w 4`: Uses 4 worker processes for handling requests
|
| 14 |
+
# - `-b 0.0.0.0:7860`: Binds the server to port 7860 on all network interfaces
|
| 15 |
+
# - `app:app`: Runs the Flask app (assuming `app.py` contains the Flask instance named `app`)
|
| 16 |
+
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:app"]
|
|
|
|
|
|
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# backend_files/app.py
|
| 3 |
+
import joblib
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from flask import Flask, request, jsonify
|
| 6 |
+
|
| 7 |
+
# Initialize Flask app
|
| 8 |
+
app = Flask("ExtraaLearn Lead Conversion Predictor")
|
| 9 |
+
|
| 10 |
+
# Load the trained lead-conversion model
|
| 11 |
+
model = joblib.load("my_model_v1_0.joblib")
|
| 12 |
+
|
| 13 |
+
@app.get("/")
|
| 14 |
+
def home():
|
| 15 |
+
return (
|
| 16 |
+
"Welcome to the ExtraaLearn Lead Conversion Prediction API - "
|
| 17 |
+
"Use POST /v1/lead for single prediction or POST /v1/leadbatch to upload CSV."
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
@app.post("/v1/lead")
|
| 21 |
+
def predict_lead():
|
| 22 |
+
"""
|
| 23 |
+
Expects JSON body with the lead features only.
|
| 24 |
+
"""
|
| 25 |
+
try:
|
| 26 |
+
lead_json = request.get_json(force=True)
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return jsonify({"error": "Invalid or missing JSON body", "details": str(e)}), 400
|
| 29 |
+
|
| 30 |
+
# Features used during model training
|
| 31 |
+
expected_features = [
|
| 32 |
+
"age", "current_occupation", "first_interaction", "profile_completed",
|
| 33 |
+
"website_visits", "time_spent_on_website", "page_views_per_visit",
|
| 34 |
+
"last_activity", "EmailActivity", "PhoneActivity", "WebsiteActivity",
|
| 35 |
+
"print_media_type1", "print_media_type2", "digital_media",
|
| 36 |
+
"educational_channels", "referral"
|
| 37 |
+
]
|
| 38 |
+
|
| 39 |
+
# Build sample
|
| 40 |
+
sample = {f: lead_json.get(f, None) for f in expected_features}
|
| 41 |
+
input_df = pd.DataFrame([sample])
|
| 42 |
+
|
| 43 |
+
try:
|
| 44 |
+
raw_pred = model.predict(input_df)[0] # returns 0 or 1
|
| 45 |
+
label = "Converted" if raw_pred == 1 else "Not Converted"
|
| 46 |
+
|
| 47 |
+
return jsonify({"Prediction": label})
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return jsonify({
|
| 50 |
+
"error": "Model prediction failed.",
|
| 51 |
+
"details": str(e),
|
| 52 |
+
"sample_input": sample
|
| 53 |
+
}), 500
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
@app.post("/v1/leadbatch")
|
| 57 |
+
def predict_lead_batch():
|
| 58 |
+
"""
|
| 59 |
+
Expects a 'file' in form-data (CSV).
|
| 60 |
+
CSV must contain only model features.
|
| 61 |
+
"""
|
| 62 |
+
if "file" not in request.files:
|
| 63 |
+
return jsonify({"error": "No file uploaded. Please attach a CSV with key 'file'."}), 400
|
| 64 |
+
|
| 65 |
+
file = request.files["file"]
|
| 66 |
+
if file.filename == "":
|
| 67 |
+
return jsonify({"error": "Empty filename. Please upload a valid CSV file."}), 400
|
| 68 |
+
|
| 69 |
+
try:
|
| 70 |
+
df = pd.read_csv(file)
|
| 71 |
+
except Exception as e:
|
| 72 |
+
return jsonify({"error": "Failed to read CSV file.", "details": str(e)}), 400
|
| 73 |
+
|
| 74 |
+
try:
|
| 75 |
+
raw_preds = model.predict(df).tolist()
|
| 76 |
+
results = ["Converted" if int(pred) == 1 else "Not Converted" for pred in raw_preds]
|
| 77 |
+
return jsonify({"Predictions": results})
|
| 78 |
+
except Exception as e:
|
| 79 |
+
return jsonify({
|
| 80 |
+
"error": "Batch prediction failed.",
|
| 81 |
+
"details": str(e)
|
| 82 |
+
}), 500
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
if __name__ == "__main__":
|
| 86 |
+
app.run(debug=True, host="0.0.0.0", port=5000)
|
requirements.txt
CHANGED
|
@@ -1,3 +1,10 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas==2.2.2
|
| 2 |
+
numpy==2.0.2
|
| 3 |
+
scikit-learn==1.6.1
|
| 4 |
+
xgboost==2.1.4
|
| 5 |
+
joblib==1.4.2
|
| 6 |
+
Werkzeug==2.2.2
|
| 7 |
+
flask==2.2.2
|
| 8 |
+
gunicorn==20.1.0
|
| 9 |
+
requests==2.28.1
|
| 10 |
+
uvicorn[standard]
|