Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +16 -0
- app.py +64 -0
- extlearn_model.joblib +3 -0
- requirements.txt +13 -0
Dockerfile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 --upgrade -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:extraalearn_api"]
|
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# Import necessary libraries
|
| 3 |
+
import numpy as np
|
| 4 |
+
import joblib # For loading the serialized model
|
| 5 |
+
import pandas as pd # For data manipulation
|
| 6 |
+
from flask import Flask, request, jsonify # For creating the Flask API
|
| 7 |
+
|
| 8 |
+
# Initialize Flask app with a name reflective of the project
|
| 9 |
+
extraalearn_api = Flask("ExtLearn")
|
| 10 |
+
|
| 11 |
+
# Load the trained lead conversion model (ensure the file name matches your saved model)
|
| 12 |
+
model = joblib.load("extlearn_model.joblib")
|
| 13 |
+
|
| 14 |
+
# Define a route for the home page
|
| 15 |
+
@extraalearn_api.get('/')
|
| 16 |
+
def home():
|
| 17 |
+
return "Welcome to the ExtraaLearn Lead Conversion Prediction System"
|
| 18 |
+
|
| 19 |
+
# Define an endpoint to predict status (converted/not converted) for a lead
|
| 20 |
+
@extraalearn_api.post('/v1/predict')
|
| 21 |
+
def predict_conversion():
|
| 22 |
+
# Get JSON data from the request body
|
| 23 |
+
data = request.get_json()
|
| 24 |
+
|
| 25 |
+
# Extract relevant features based on the ExtraaLearn dataset
|
| 26 |
+
# These must match the exact feature names used during model training
|
| 27 |
+
sample = {
|
| 28 |
+
'age': data['age'],
|
| 29 |
+
'current_occupation': data['current_occupation'],
|
| 30 |
+
'first_interaction': data['first_interaction'],
|
| 31 |
+
'profile_completed': data['profile_completed'],
|
| 32 |
+
'website_visits': data['website_visits'],
|
| 33 |
+
'time_spent_on_website': data['time_spent_on_website'],
|
| 34 |
+
'page_views_per_visit': data['page_views_per_visit'],
|
| 35 |
+
'last_activity': data['last_activity'],
|
| 36 |
+
'print_media_type1': data['print_media_type1'],
|
| 37 |
+
'print_media_type2': data['print_media_type2'],
|
| 38 |
+
'digital_media': data['digital_media'],
|
| 39 |
+
'educational_channels': data['educational_channels'],
|
| 40 |
+
'referral': data['referral']
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
# Convert the extracted data into a DataFrame for the model pipeline
|
| 44 |
+
input_data = pd.DataFrame([sample])
|
| 45 |
+
|
| 46 |
+
# Calculate the engineered feature 'age_time_interaction'
|
| 47 |
+
input_data['age_time_interaction'] = input_data['age'] * input_data['time_spent_on_website']
|
| 48 |
+
|
| 49 |
+
# Make a prediction (1 for converted, 0 for not converted)
|
| 50 |
+
prediction = int(model.predict(input_data)[0])
|
| 51 |
+
|
| 52 |
+
# Optional: Get the probability of conversion
|
| 53 |
+
probability = model.predict_proba(input_data)[0][1]
|
| 54 |
+
|
| 55 |
+
# Return the prediction and probability as a JSON response
|
| 56 |
+
return jsonify({
|
| 57 |
+
'Status_Prediction': prediction,
|
| 58 |
+
'Conversion_Probability': round(float(probability), 4),
|
| 59 |
+
'Message': 'High Potential Lead' if prediction == 1 else 'Low Potential Lead'
|
| 60 |
+
})
|
| 61 |
+
|
| 62 |
+
# Run the Flask app
|
| 63 |
+
if __name__ == '__main__':
|
| 64 |
+
extraalearn_api.run(debug=True)
|
extlearn_model.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e7f34511ea039b78cd2ab7c73c51dc8bf750612223b90c1f2c7245cacd5718cb
|
| 3 |
+
size 230374
|
requirements.txt
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas==2.2.2
|
| 2 |
+
numpy==2.0.2
|
| 3 |
+
scikit-learn==1.6.1
|
| 4 |
+
seaborn==0.13.2
|
| 5 |
+
joblib==1.4.2
|
| 6 |
+
xgboost==2.1.4
|
| 7 |
+
joblib==1.4.2
|
| 8 |
+
Werkzeug==2.2.2
|
| 9 |
+
flask==2.2.2
|
| 10 |
+
gunicorn==20.1.0
|
| 11 |
+
requests==2.32.3
|
| 12 |
+
uvicorn[standard]
|
| 13 |
+
streamlit==1.43.2
|