Spaces:
Build error
Build error
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -3,13 +3,23 @@ import joblib
|
|
| 3 |
import pandas as pd
|
| 4 |
from flask import Flask, request, jsonify
|
| 5 |
|
| 6 |
-
# Create the Flask app
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
-
# Load the model
|
| 10 |
-
# Note: backend_files contains Extraalearn.joblib, so in the container it's just 'Extraalearn.joblib'
|
| 11 |
model = joblib.load('Extraalearn.joblib')
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
@app.get('/')
|
| 14 |
def home():
|
| 15 |
return 'ExtraaLearn API is Running'
|
|
@@ -18,8 +28,25 @@ def home():
|
|
| 18 |
def predict_sales():
|
| 19 |
try:
|
| 20 |
data = request.get_json()
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
return jsonify({'Sales': float(prediction)})
|
| 24 |
except Exception as e:
|
| 25 |
return jsonify({'error': str(e)}), 400
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
from flask import Flask, request, jsonify
|
| 5 |
|
| 6 |
+
# Create the Flask app
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
+
# Load the model
|
|
|
|
| 10 |
model = joblib.load('Extraalearn.joblib')
|
| 11 |
|
| 12 |
+
# Define the expected feature columns from training (excluding 'status')
|
| 13 |
+
EXPECTED_FEATURES = [
|
| 14 |
+
'age', 'website_visits', 'time_spent_on_website', 'page_views_per_visit',
|
| 15 |
+
'current_occupation_Student', 'current_occupation_Unemployed',
|
| 16 |
+
'first_interaction_Website', 'profile_completed_Low',
|
| 17 |
+
'profile_completed_Medium', 'last_activity_Phone Activity',
|
| 18 |
+
'last_activity_Website Activity', 'print_media_type1_Yes',
|
| 19 |
+
'print_media_type2_Yes', 'digital_media_Yes',
|
| 20 |
+
'educational_channels_Yes', 'referral_Yes'
|
| 21 |
+
]
|
| 22 |
+
|
| 23 |
@app.get('/')
|
| 24 |
def home():
|
| 25 |
return 'ExtraaLearn API is Running'
|
|
|
|
| 28 |
def predict_sales():
|
| 29 |
try:
|
| 30 |
data = request.get_json()
|
| 31 |
+
# Convert input to DataFrame
|
| 32 |
+
df_input = pd.DataFrame([data])
|
| 33 |
+
|
| 34 |
+
# Apply One-Hot Encoding to match training preprocessing
|
| 35 |
+
categorical_cols = ['current_occupation', 'first_interaction', 'profile_completed',
|
| 36 |
+
'last_activity', 'print_media_type1', 'print_media_type2',
|
| 37 |
+
'digital_media', 'educational_channels', 'referral']
|
| 38 |
+
|
| 39 |
+
df_encoded = pd.get_dummies(df_input, columns=categorical_cols, dtype=int)
|
| 40 |
+
|
| 41 |
+
# Ensure all columns from training are present (add missing as 0)
|
| 42 |
+
for col in EXPECTED_FEATURES:
|
| 43 |
+
if col not in df_encoded.columns:
|
| 44 |
+
df_encoded[col] = 0
|
| 45 |
+
|
| 46 |
+
# Reorder columns to match training set exactly
|
| 47 |
+
df_encoded = df_encoded[EXPECTED_FEATURES]
|
| 48 |
+
|
| 49 |
+
prediction = model.predict(df_encoded).tolist()[0]
|
| 50 |
return jsonify({'Sales': float(prediction)})
|
| 51 |
except Exception as e:
|
| 52 |
return jsonify({'error': str(e)}), 400
|