Sirisha335 commited on
Commit
e4c0391
·
verified ·
1 Parent(s): a8cde64

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +0 -1
  2. app.py +19 -42
  3. requirements.txt +1 -0
Dockerfile CHANGED
@@ -2,5 +2,4 @@ FROM python:3.9-slim
2
  WORKDIR /app
3
  COPY . .
4
  RUN pip install --no-cache-dir -r requirements.txt
5
- # Important: app:app refers to app.py and the Flask instance named 'app'
6
  CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:app"]
 
2
  WORKDIR /app
3
  COPY . .
4
  RUN pip install --no-cache-dir -r requirements.txt
 
5
  CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:app"]
app.py CHANGED
@@ -3,13 +3,9 @@ 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
  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',
@@ -25,46 +21,27 @@ def home():
25
  return 'ExtraaLearn API is Running'
26
 
27
  @app.post('/v1/predict')
28
- def predict_conversion_probability(): # Renamed endpoint for clarity
29
  try:
30
  data = request.get_json()
31
-
32
- # Create a dictionary to hold the processed features
33
- processed_data = {}
34
-
35
- # 1. Map Numerical Features
36
- processed_data['age'] = float(data.get('age', 0))
37
- processed_data['website_visits'] = float(data.get('website_visits', 0))
38
- processed_data['time_spent_on_website'] = float(data.get('time_spent_on_website', 0))
39
- processed_data['page_views_per_visit'] = float(data.get('page_views_per_visit', 0))
40
-
41
- # 2. Map Categorical Features (One-Hot Encoding logic)
42
- # Note: 'Professional', 'Mobile App', 'High', 'Email Activity', and 'No' are the baselines (drop_first)
43
-
44
- occ = data.get('current_occupation', '')
45
- processed_data['current_occupation_Student'] = 1 if occ == 'Student' else 0
46
- processed_data['current_occupation_Unemployed'] = 1 if occ == 'Unemployed' else 0
47
-
48
- processed_data['first_interaction_Website'] = 1 if data.get('first_interaction', '') == 'Website' else 0
49
-
50
- prof = data.get('profile_completed', '')
51
- processed_data['profile_completed_Low'] = 1 if prof == 'Low' else 0
52
- processed_data['profile_completed_Medium'] = 1 if prof == 'Medium' else 0
53
-
54
- last = data.get('last_activity', '')
55
- processed_data['last_activity_Phone Activity'] = 1 if last == 'Phone Activity' else 0
56
- processed_data['last_activity_Website Activity'] = 1 if last == 'Website Activity' else 0
57
-
58
- processed_data['print_media_type1_Yes'] = 1 if data.get('print_media_type1', '') == 'Yes' else 0
59
- processed_data['print_media_type2_Yes'] = 1 if data.get('print_media_type2', '') == 'Yes' else 0
60
- processed_data['digital_media_Yes'] = 1 if data.get('digital_media', '') == 'Yes' else 0
61
- processed_data['educational_channels_Yes'] = 1 if data.get('educational_channels', '') == 'Yes' else 0
62
- processed_data['referral_Yes'] = 1 if data.get('referral', '') == 'Yes' else 0
63
-
64
- # Convert dictionary to DataFrame with the exact training order
65
- df_final = pd.DataFrame([processed_data])[EXPECTED_FEATURES]
66
-
67
- # Predict probability of conversion (positive class is 1)
68
  prediction_proba = model.predict_proba(df_final)[:, 1].tolist()[0] # Use predict_proba
69
  return jsonify({'Conversion_Probability': float(prediction_proba)}) # Changed key to Conversion_Probability
70
  except Exception as e:
 
3
  import pandas as pd
4
  from flask import Flask, request, jsonify
5
 
 
6
  app = Flask(__name__)
 
 
7
  model = joblib.load('Extraalearn.joblib')
8
 
 
9
  EXPECTED_FEATURES = [
10
  'age', 'website_visits', 'time_spent_on_website', 'page_views_per_visit',
11
  'current_occupation_Student', 'current_occupation_Unemployed',
 
21
  return 'ExtraaLearn API is Running'
22
 
23
  @app.post('/v1/predict')
24
+ def predict():
25
  try:
26
  data = request.get_json()
27
+ p = {}
28
+ p['age'] = float(data.get('age', 0))
29
+ p['website_visits'] = float(data.get('website_visits', 0))
30
+ p['time_spent_on_website'] = float(data.get('time_spent_on_website', 0))
31
+ p['page_views_per_visit'] = float(data.get('page_views_per_visit', 0))
32
+ p['current_occupation_Student'] = 1 if data.get('current_occupation') == 'Student' else 0
33
+ p['current_occupation_Unemployed'] = 1 if data.get('current_occupation') == 'Unemployed' else 0
34
+ p['first_interaction_Website'] = 1 if data.get('first_interaction') == 'Website' else 0
35
+ p['profile_completed_Low'] = 1 if data.get('profile_completed') == 'Low' else 0
36
+ p['profile_completed_Medium'] = 1 if data.get('profile_completed') == 'Medium' else 0
37
+ p['last_activity_Phone Activity'] = 1 if data.get('last_activity') == 'Phone Activity' else 0
38
+ p['last_activity_Website Activity'] = 1 if data.get('last_activity') == 'Website Activity' else 0
39
+ p['print_media_type1_Yes'] = 1 if data.get('print_media_type1') == 'Yes' else 0
40
+ p['print_media_type2_Yes'] = 1 if data.get('print_media_type2') == 'Yes' else 0
41
+ p['digital_media_Yes'] = 1 if data.get('digital_media') == 'Yes' else 0
42
+ p['educational_channels_Yes'] = 1 if data.get('educational_channels') == 'Yes' else 0
43
+ p['referral_Yes'] = 1 if data.get('referral') == 'Yes' else 0
44
+ df_final = pd.DataFrame([p])[EXPECTED_FEATURES]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  prediction_proba = model.predict_proba(df_final)[:, 1].tolist()[0] # Use predict_proba
46
  return jsonify({'Conversion_Probability': float(prediction_proba)}) # Changed key to Conversion_Probability
47
  except Exception as e:
requirements.txt CHANGED
@@ -1,3 +1,4 @@
 
1
  pandas==2.2.2
2
  numpy==2.0.2
3
  scikit-learn==1.6.1
 
1
+ writefile backend_files/requirements.txt
2
  pandas==2.2.2
3
  numpy==2.0.2
4
  scikit-learn==1.6.1