HSSHETTY01 commited on
Commit
7be39ad
Β·
verified Β·
1 Parent(s): 99c6153

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. backend_files/app.py +38 -27
  2. frontend_files/app.py +44 -33
backend_files/app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import numpy as np
2
  import pandas as pd
3
  import joblib
@@ -5,64 +6,74 @@ from flask import Flask, request, jsonify
5
  from flask_cors import CORS
6
 
7
  # Initialize Flask app
8
- superkart_api = Flask("Extraa_Learn_api")
9
- CORS(superkart_api)
10
 
11
- # Load the trained model pipeline inside the container
12
- model = joblib.load("Extraa_Learn_model_v1_0.joblib")
13
 
14
  # Health check route
15
- @superkart_api.get('/')
16
  def home():
17
- return "βœ… Extraa_Learn Backend is Running"
18
 
19
  # Prediction route
20
- @superkart_api.post('/v1/Status')
21
- def predict_status(): # Renamed function for clarity
22
  try:
23
  # Parse JSON payload
24
  data = request.get_json()
 
25
 
26
  # Validate expected fields
27
  required_fields = [
28
- 'age', 'current_occupation', 'first_interaction',
29
- 'profile_completed', 'website_visits','time_spent_on_website',
30
- 'page_views_per_visit', # Added missing field
31
- 'last_activity', 'print_media_type1','print_media_type2',
32
- 'digital_media', 'educational_channels', 'referral'
 
 
 
 
 
 
 
 
33
  ]
34
-
35
- # Check for missing fields
36
  missing_fields = [f for f in required_fields if f not in data]
37
  if missing_fields:
38
  return jsonify({'error': f"Missing fields: {missing_fields}"}), 400
39
 
40
- # Convert and transform input exactly to match X_train structure
41
  sample = {
42
- 'age': data['age'], # Removed extra closing parenthesis
43
  'current_occupation': data['current_occupation'],
44
  'first_interaction': data['first_interaction'],
45
  'profile_completed': data['profile_completed'],
46
  'website_visits': data['website_visits'],
47
- 'time_spent_on_website': data['time_spent_on_website'],
48
- 'page_views_per_visit': data['page_views_per_visit'], # Added missing field
49
  'last_activity': data['last_activity'],
50
- 'print_media_type1':data['print_media_type1'], # Added missing comma
51
- 'print_media_type2': data['print_media_type2'],
52
- 'digital_media':data['digital_media'],
53
- 'educational_channels':data['educational_channels'], # Added missing comma
54
- 'referral':data['referral']
55
  }
56
 
57
- # Create DataFrame and ensure column order doesn't cause issues
58
  input_df = pd.DataFrame([sample])
 
 
59
 
60
  # Make prediction
61
  prediction = model.predict(input_df).tolist()[0]
62
- return jsonify({'Status': prediction}) # Changed key from 'Predicted_Sales' to 'Status' for consistency with frontend
63
 
64
  except Exception as e:
 
65
  return jsonify({'error': f"Prediction failed: {str(e)}"}), 500
66
 
 
67
  if __name__ == '__main__':
68
- superkart_api.run(debug=True)
 
1
+
2
  import numpy as np
3
  import pandas as pd
4
  import joblib
 
6
  from flask_cors import CORS
7
 
8
  # Initialize Flask app
9
+ Extraa_Learn_api = Flask("Extraa_Learn_api")
10
+ CORS(Extraa_Learn_api)
11
 
12
+ # Load the trained model pipeline (preprocessing + model)
13
+ model = joblib.load("/content/drive/My Drive/Colab Notebooks/deployment_files/Extraa_Learn_model_v1_0.joblib")
14
 
15
  # Health check route
16
+ @Extraa_Learn_api.get('/')
17
  def home():
18
+ return "βœ… Welcome to the Extraa_Learn_API"
19
 
20
  # Prediction route
21
+ @Extraa_Learn_api.post('/v1/status')
22
+ def predict_status():
23
  try:
24
  # Parse JSON payload
25
  data = request.get_json()
26
+ print("Raw incoming data:", data)
27
 
28
  # Validate expected fields
29
  required_fields = [
30
+ 'age',
31
+ 'current_occupation',
32
+ 'first_interaction',
33
+ 'profile_completed',
34
+ 'website_visits',
35
+ 'time_spent_on_website',
36
+ 'page_views_per_visit',
37
+ 'last_activity',
38
+ 'print_media_type1',
39
+ 'print_media_type2',
40
+ 'digital_media',
41
+ 'educational_channels',
42
+ 'referral'
43
  ]
 
 
44
  missing_fields = [f for f in required_fields if f not in data]
45
  if missing_fields:
46
  return jsonify({'error': f"Missing fields: {missing_fields}"}), 400
47
 
48
+ # Convert and transform input
49
  sample = {
50
+ 'age': float(data['age']),
51
  'current_occupation': data['current_occupation'],
52
  'first_interaction': data['first_interaction'],
53
  'profile_completed': data['profile_completed'],
54
  'website_visits': data['website_visits'],
55
+ 'time_spent_on_website': ['time_spent_on_website']),
56
+ 'page_views_per_visit': data['page_views_per_visit'],
57
  'last_activity': data['last_activity'],
58
+ 'print_media_type1': data['print_media_type1'],
59
+ 'print_media_type2': data['print_media_type2']),
60
+ 'digital_media': data['digital_media'],
61
+ 'educational_channels': data['educational_channels'],
62
+ 'referral': data['referral']
63
  }
64
 
 
65
  input_df = pd.DataFrame([sample])
66
+ print("Transformed input for model:
67
+ ", input_df)
68
 
69
  # Make prediction
70
  prediction = model.predict(input_df).tolist()[0]
71
+ return jsonify({'Status': prediction})
72
 
73
  except Exception as e:
74
+ print("❌ Error during prediction:", str(e))
75
  return jsonify({'error': f"Prediction failed: {str(e)}"}), 500
76
 
77
+ # Run the app (for local testing only)
78
  if __name__ == '__main__':
79
+ Extraa_Learn_api.run(debug=True)
frontend_files/app.py CHANGED
@@ -5,54 +5,65 @@ import requests
5
  import numpy as np
6
 
7
  # App Title
8
- st.title("Extraa_Learn_Prediction")
9
 
10
- # User Inputs
11
- Age = st.number_input("age)", min_value=0.0, value=100)
12
- current_occupation = st.selectbox("current_occupation", ["Professional ", "Unemployed ", "Student "])
13
- first_interaction = st.selectbox("first_interaction",["Website","Mobile App"])
14
- profile_completed = st.selectbox("profile_completed)",["High","Medium", "Low"])
15
- website_visits = st.selectbox("website_visits"),
16
- time_spent_on_website = st.selectbox("time_spent_on_website"),
17
- page_views_per_visit = st.selectbox("page_views_per_visit"),
18
- last_activity = st.selectbox ("last_activity",
19
- print_media_type1 = st.selectbox("print_media_type1"),
20
- print_media_type2 = st.selectbox("print_media_type2"),
21
- digital_media = st._selectbox("digital_media)",
22
- educational_channels = st.selectbox("educational_channels"),
23
- referral= st.selectbox("referral")
24
 
25
 
26
  # Prepare JSON payload for the backend
27
- product_data = {
28
- "Age": str(Age),
29
- "current_occupation ":str( Current_occupation),
30
- "first_interaction": str(first_interaction),
31
- "profile_completed": str(profile_completed),
32
- "website_visits": str(website_visits),
33
- "time_spent_on_website": str(time_spent_on_website),
34
- "page_views_per_visit": str(page_views_per_visit),
35
- "last_activity": str(last_activity),
36
  "print_media_type1": print_media_type1,
37
  "print_media_type2": print_media_type2,
38
- "digital_media": str(digital_media),
39
  "educational_channels": educational_channels,
40
  "referral": referral
41
  }
42
-
43
-
44
- # Trigger Prediction
45
- if st.button("status", type='primary'):
46
  try:
 
47
  response = requests.post(
48
- "https://HSSHETTY01-Extraa_Learns-backend2.hf.space/v1/predict",
49
  json=product_data
50
  )
 
 
51
  if response.status_code == 200:
 
52
  result = response.json()
53
- predicted_sales = result["Status"]
54
- st.success(f"πŸ“ˆ Ststus: **${status:,.2f} USD**")
 
 
 
 
55
  else:
56
- st.error("❌ API Error: Please verify input values or try again later.")
 
57
  except Exception as e:
 
58
  st.error(f"⚠️ Connection error: {e}")
 
 
 
5
  import numpy as np
6
 
7
  # App Title
8
+ st.title("Extraa Learn Lead Conversion Prediction")
9
 
10
+ # User Inputs
11
+ Age = st.number_input("Age", min_value=18, max_value=100, value=35)
12
+ current_occupation = st.selectbox("Current Occupation", ["Professional", "Unemployed", "Student"])
13
+ first_interaction = st.selectbox("First Interaction", ["Website", "Mobile App"])
14
+ profile_completed = st.selectbox("Profile Completed", ["High", "Medium", "Low"])
15
+ website_visits = st.number_input("Website Visits", min_value=0, value=3)
16
+ time_spent_on_website = st.number_input("Time Spent on Website (seconds)", min_value=0, value=500)
17
+ page_views_per_visit = st.number_input("Page Views Per Visit", min_value=0.0, value=3.0)
18
+ last_activity = st.selectbox("Last Activity", ["Email Activity", "Phone Activity", "Website Activity"])
19
+ print_media_type1 = st.selectbox("Seen Ad in Newspaper?", ["Yes", "No"])
20
+ print_media_type2 = st.selectbox("Seen Ad in Magazine?", ["Yes", "No"])
21
+ digital_media = st.selectbox("Seen Ad on Digital Platforms?", ["Yes", "No"])
22
+ educational_channels = st.selectbox("Heard via Educational Channels?", ["Yes", "No"])
23
+ referral = st.selectbox("Heard via Referral?", ["Yes", "No"])
24
 
25
 
26
  # Prepare JSON payload for the backend
27
+ leaddata = {
28
+ "age": int(Age),
29
+ "current_occupation": current_occupation.strip(),
30
+ "first_interaction": first_interaction.strip(),
31
+ "profile_completed": profile_completed.strip(),
32
+ "website_visits": int(website_visits),
33
+ "time_spent_on_website": float(time_spent_on_website),
34
+ "page_views_per_visit": float(page_views_per_visit),
35
+ "last_activity": last_activity.strip(),
36
  "print_media_type1": print_media_type1,
37
  "print_media_type2": print_media_type2,
38
+ "digital_media": digital_media,
39
  "educational_channels": educational_channels,
40
  "referral": referral
41
  }
42
+
43
+ # Trigger Prediction API Call
44
+ if st.button("Predict Status", type='primary'):
 
45
  try:
46
+ # This is level 1 indentation (4 spaces)
47
  response = requests.post(
48
+ "https://HSSHETTY01-Extraa_Learn-backend2.hf.space/v1/Status",
49
  json=product_data
50
  )
51
+
52
+ # This must stay at level 1 indentation (4 spaces)
53
  if response.status_code == 200:
54
+ # This is level 2 indentation (8 spaces)
55
  result = response.json()
56
+ status_pred = result.get("Status", result.get("prediction", 0))
57
+
58
+ if status_pred == 1:
59
+ st.success("πŸ“ˆ **Status:** Lead is likely to convert!")
60
+ else:
61
+ st.warning("πŸ“‰ **Status:** Lead is not likely to convert.")
62
  else:
63
+ st.error(f"❌ API Error {response.status_code}: {response.text}")
64
+
65
  except Exception as e:
66
+ # Aligns perfectly with the 'try:' statement
67
  st.error(f"⚠️ Connection error: {e}")
68
+
69
+