Sirisha335 commited on
Commit
8ccf4d5
·
verified ·
1 Parent(s): 9c7ed55

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +35 -17
app.py CHANGED
@@ -28,25 +28,43 @@ def home():
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
 
28
  def predict_sales():
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
+ prediction = model.predict(df_final).tolist()[0]
68
  return jsonify({'Sales': float(prediction)})
69
  except Exception as e:
70
  return jsonify({'error': str(e)}), 400