Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- backend_files/app.py +38 -27
- 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 |
-
|
| 9 |
-
CORS(
|
| 10 |
|
| 11 |
-
# Load the trained model pipeline
|
| 12 |
-
model = joblib.load("Extraa_Learn_model_v1_0.joblib")
|
| 13 |
|
| 14 |
# Health check route
|
| 15 |
-
@
|
| 16 |
def home():
|
| 17 |
-
return "β
|
| 18 |
|
| 19 |
# Prediction route
|
| 20 |
-
@
|
| 21 |
-
def predict_status():
|
| 22 |
try:
|
| 23 |
# Parse JSON payload
|
| 24 |
data = request.get_json()
|
|
|
|
| 25 |
|
| 26 |
# Validate expected fields
|
| 27 |
required_fields = [
|
| 28 |
-
'age',
|
| 29 |
-
'
|
| 30 |
-
'
|
| 31 |
-
'
|
| 32 |
-
'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 41 |
sample = {
|
| 42 |
-
'age': data['age'],
|
| 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':
|
| 48 |
-
'page_views_per_visit': data['page_views_per_visit'],
|
| 49 |
'last_activity': data['last_activity'],
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
'digital_media':data['digital_media'],
|
| 53 |
-
'educational_channels':data['educational_channels'],
|
| 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})
|
| 63 |
|
| 64 |
except Exception as e:
|
|
|
|
| 65 |
return jsonify({'error': f"Prediction failed: {str(e)}"}), 500
|
| 66 |
|
|
|
|
| 67 |
if __name__ == '__main__':
|
| 68 |
-
|
|
|
|
| 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("
|
| 9 |
|
| 10 |
-
# User Inputs
|
| 11 |
-
Age = st.number_input("
|
| 12 |
-
current_occupation = st.selectbox("
|
| 13 |
-
first_interaction = st.selectbox("
|
| 14 |
-
profile_completed = st.selectbox("
|
| 15 |
-
website_visits = st.
|
| 16 |
-
time_spent_on_website = st.
|
| 17 |
-
page_views_per_visit = st.
|
| 18 |
-
last_activity = st.selectbox
|
| 19 |
-
print_media_type1 = st.selectbox("
|
| 20 |
-
print_media_type2 = st.selectbox("
|
| 21 |
-
digital_media = st.
|
| 22 |
-
educational_channels = st.selectbox("
|
| 23 |
-
referral= st.selectbox("
|
| 24 |
|
| 25 |
|
| 26 |
# Prepare JSON payload for the backend
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
"current_occupation
|
| 30 |
-
"first_interaction":
|
| 31 |
-
"profile_completed":
|
| 32 |
-
"website_visits":
|
| 33 |
-
"time_spent_on_website":
|
| 34 |
-
"page_views_per_visit":
|
| 35 |
-
"last_activity":
|
| 36 |
"print_media_type1": print_media_type1,
|
| 37 |
"print_media_type2": print_media_type2,
|
| 38 |
-
"digital_media":
|
| 39 |
"educational_channels": educational_channels,
|
| 40 |
"referral": referral
|
| 41 |
}
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
if st.button("status", type='primary'):
|
| 46 |
try:
|
|
|
|
| 47 |
response = requests.post(
|
| 48 |
-
"https://HSSHETTY01-
|
| 49 |
json=product_data
|
| 50 |
)
|
|
|
|
|
|
|
| 51 |
if response.status_code == 200:
|
|
|
|
| 52 |
result = response.json()
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
else:
|
| 56 |
-
st.error("β API Error:
|
|
|
|
| 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 |
+
|