Spaces:
Sleeping
Sleeping
Bibek Mukherjee commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,120 +1,120 @@
|
|
| 1 |
-
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import numpy as np
|
| 4 |
-
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
|
| 5 |
-
import matplotlib.pyplot as plt
|
| 6 |
-
import seaborn as sns
|
| 7 |
-
import pickle
|
| 8 |
-
import gradio as gr
|
| 9 |
-
import os
|
| 10 |
-
|
| 11 |
-
# Load the model
|
| 12 |
-
model_path = 'career_prediction_model.pkl'
|
| 13 |
-
with open(model_path, 'rb') as f:
|
| 14 |
-
saved_data = pickle.load(f)
|
| 15 |
-
|
| 16 |
-
model = saved_data['model']
|
| 17 |
-
label_encoders = saved_data['label_encoders']
|
| 18 |
-
target_encoder = saved_data['target_encoder']
|
| 19 |
-
features = saved_data['features']
|
| 20 |
-
target = 'What would you like to become when you grow up'
|
| 21 |
-
|
| 22 |
-
# Function for individual prediction
|
| 23 |
-
def predict_career(work_env, academic_perf, motivation, leadership, tech_savvy, preferred_subjects, gender, risk_taking=5, financial_stability=5, work_exp="No Experience"):
|
| 24 |
-
# Prepare input data
|
| 25 |
-
input_data = pd.DataFrame({
|
| 26 |
-
'Preferred Work Environment': [work_env],
|
| 27 |
-
'Academic Performance (CGPA/Percentage)': [float(academic_perf)],
|
| 28 |
-
'Motivation for Career Choice ': [motivation], # Note the space at the end
|
| 29 |
-
'Leadership Experience': [leadership],
|
| 30 |
-
'Tech-Savviness': [tech_savvy],
|
| 31 |
-
'Preferred Subjects in Highschool/College': [preferred_subjects], # New feature
|
| 32 |
-
'Gender': [gender], # New feature
|
| 33 |
-
'Risk-Taking Ability ': [float(risk_taking)], # Note the space at the end
|
| 34 |
-
'Financial Stability - self/family (1 is low income and 10 is high income)': [float(financial_stability)],
|
| 35 |
-
'Previous Work Experience (If Any)': [work_exp]
|
| 36 |
-
})
|
| 37 |
-
|
| 38 |
-
# Encode categorical features
|
| 39 |
-
for feature in features:
|
| 40 |
-
if feature in input_data.columns:
|
| 41 |
-
if feature in label_encoders and input_data[feature].dtype == 'object':
|
| 42 |
-
try:
|
| 43 |
-
input_data[feature] = label_encoders[feature].transform(input_data[feature])
|
| 44 |
-
except ValueError:
|
| 45 |
-
# Handle unknown categories
|
| 46 |
-
print(f"Warning: Unknown category in {feature}. Using most frequent category.")
|
| 47 |
-
input_data[feature] = 0 # Default to first category
|
| 48 |
-
else:
|
| 49 |
-
print(f"Warning: Feature {feature} not found in input data.")
|
| 50 |
-
|
| 51 |
-
# Make prediction
|
| 52 |
-
prediction = model.predict(input_data)[0]
|
| 53 |
-
predicted_career = target_encoder.inverse_transform([int(prediction)])[0]
|
| 54 |
-
|
| 55 |
-
# Get probabilities for all classes
|
| 56 |
-
if hasattr(model, 'predict_proba'):
|
| 57 |
-
probabilities = model.predict_proba(input_data)[0]
|
| 58 |
-
class_probs = {target_encoder.inverse_transform([i])[0]: prob
|
| 59 |
-
for i, prob in enumerate(probabilities)}
|
| 60 |
-
sorted_probs = dict(sorted(class_probs.items(), key=lambda x: x[1], reverse=True))
|
| 61 |
-
|
| 62 |
-
result = f"Predicted career: {predicted_career}\n\nProbabilities:\n"
|
| 63 |
-
for career, prob in sorted_probs.items():
|
| 64 |
-
result += f"{career}: {prob:.2f}\n"
|
| 65 |
-
return result
|
| 66 |
-
else:
|
| 67 |
-
return f"Predicted career: {predicted_career}"
|
| 68 |
-
|
| 69 |
-
# Get unique values for dropdowns
|
| 70 |
-
work_env_options = list(label_encoders['Preferred Work Environment'].classes_)
|
| 71 |
-
motivation_options = list(label_encoders['Motivation for Career Choice '].classes_)
|
| 72 |
-
leadership_options = list(label_encoders['Leadership Experience'].classes_)
|
| 73 |
-
tech_savvy_options = list(label_encoders['Tech-Savviness'].classes_)
|
| 74 |
-
|
| 75 |
-
# Get options for new features with error handling
|
| 76 |
-
subject_options = []
|
| 77 |
-
if 'Preferred Subjects in Highschool/College' in label_encoders:
|
| 78 |
-
subject_options = list(label_encoders['Preferred Subjects in Highschool/College'].classes_)
|
| 79 |
-
else:
|
| 80 |
-
# Default options if not in the model
|
| 81 |
-
subject_options = ["Science", "Commerce", "Arts", "Unknown"]
|
| 82 |
-
|
| 83 |
-
gender_options = []
|
| 84 |
-
if 'Gender' in label_encoders:
|
| 85 |
-
gender_options = list(label_encoders['Gender'].classes_)
|
| 86 |
-
else:
|
| 87 |
-
# Default options if not in the model
|
| 88 |
-
gender_options = ["Male", "Female", "Other"]
|
| 89 |
-
|
| 90 |
-
# Get work experience options if available
|
| 91 |
-
work_exp_options = []
|
| 92 |
-
if 'Previous Work Experience (If Any)' in label_encoders:
|
| 93 |
-
work_exp_options = list(label_encoders['Previous Work Experience (If Any)'].classes_)
|
| 94 |
-
else:
|
| 95 |
-
work_exp_options = ["No Experience", "Internship", "Part Time", "Full Time"]
|
| 96 |
-
|
| 97 |
-
# Create the Gradio interface
|
| 98 |
-
iface = gr.Interface(
|
| 99 |
-
fn=predict_career,
|
| 100 |
-
inputs=[
|
| 101 |
-
gr.Dropdown(work_env_options, label="Preferred Work Environment"),
|
| 102 |
-
gr.
|
| 103 |
-
gr.Dropdown(motivation_options, label="Motivation for Career Choice"),
|
| 104 |
-
gr.Dropdown(leadership_options, label="Leadership Experience"),
|
| 105 |
-
gr.Dropdown(tech_savvy_options, label="Tech-Savviness"),
|
| 106 |
-
gr.Dropdown(subject_options, label="Preferred Subjects"),
|
| 107 |
-
gr.Dropdown(gender_options, label="Gender"),
|
| 108 |
-
gr.Slider(minimum=1, maximum=10, step=1, value=5, label="Risk-Taking Ability"),
|
| 109 |
-
gr.Slider(minimum=1, maximum=10, step=1, value=5, label="Financial Stability"),
|
| 110 |
-
gr.Dropdown(work_exp_options, label="Previous Work Experience")
|
| 111 |
-
],
|
| 112 |
-
outputs="text",
|
| 113 |
-
title="Career Prediction Model",
|
| 114 |
-
description="Enter your details to predict your future career path",
|
| 115 |
-
theme="huggingface"
|
| 116 |
-
)
|
| 117 |
-
|
| 118 |
-
# Launch the interface
|
| 119 |
-
if __name__ == "__main__":
|
| 120 |
-
iface.launch()
|
|
|
|
| 1 |
+
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
import seaborn as sns
|
| 7 |
+
import pickle
|
| 8 |
+
import gradio as gr
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
# Load the model
|
| 12 |
+
model_path = 'career_prediction_model.pkl'
|
| 13 |
+
with open(model_path, 'rb') as f:
|
| 14 |
+
saved_data = pickle.load(f)
|
| 15 |
+
|
| 16 |
+
model = saved_data['model']
|
| 17 |
+
label_encoders = saved_data['label_encoders']
|
| 18 |
+
target_encoder = saved_data['target_encoder']
|
| 19 |
+
features = saved_data['features']
|
| 20 |
+
target = 'What would you like to become when you grow up'
|
| 21 |
+
|
| 22 |
+
# Function for individual prediction
|
| 23 |
+
def predict_career(work_env, academic_perf, motivation, leadership, tech_savvy, preferred_subjects, gender, risk_taking=5, financial_stability=5, work_exp="No Experience"):
|
| 24 |
+
# Prepare input data
|
| 25 |
+
input_data = pd.DataFrame({
|
| 26 |
+
'Preferred Work Environment': [work_env],
|
| 27 |
+
'Academic Performance (CGPA/Percentage)': [float(academic_perf)],
|
| 28 |
+
'Motivation for Career Choice ': [motivation], # Note the space at the end
|
| 29 |
+
'Leadership Experience': [leadership],
|
| 30 |
+
'Tech-Savviness': [tech_savvy],
|
| 31 |
+
'Preferred Subjects in Highschool/College': [preferred_subjects], # New feature
|
| 32 |
+
'Gender': [gender], # New feature
|
| 33 |
+
'Risk-Taking Ability ': [float(risk_taking)], # Note the space at the end
|
| 34 |
+
'Financial Stability - self/family (1 is low income and 10 is high income)': [float(financial_stability)],
|
| 35 |
+
'Previous Work Experience (If Any)': [work_exp]
|
| 36 |
+
})
|
| 37 |
+
|
| 38 |
+
# Encode categorical features
|
| 39 |
+
for feature in features:
|
| 40 |
+
if feature in input_data.columns:
|
| 41 |
+
if feature in label_encoders and input_data[feature].dtype == 'object':
|
| 42 |
+
try:
|
| 43 |
+
input_data[feature] = label_encoders[feature].transform(input_data[feature])
|
| 44 |
+
except ValueError:
|
| 45 |
+
# Handle unknown categories
|
| 46 |
+
print(f"Warning: Unknown category in {feature}. Using most frequent category.")
|
| 47 |
+
input_data[feature] = 0 # Default to first category
|
| 48 |
+
else:
|
| 49 |
+
print(f"Warning: Feature {feature} not found in input data.")
|
| 50 |
+
|
| 51 |
+
# Make prediction
|
| 52 |
+
prediction = model.predict(input_data)[0]
|
| 53 |
+
predicted_career = target_encoder.inverse_transform([int(prediction)])[0]
|
| 54 |
+
|
| 55 |
+
# Get probabilities for all classes
|
| 56 |
+
if hasattr(model, 'predict_proba'):
|
| 57 |
+
probabilities = model.predict_proba(input_data)[0]
|
| 58 |
+
class_probs = {target_encoder.inverse_transform([i])[0]: prob
|
| 59 |
+
for i, prob in enumerate(probabilities)}
|
| 60 |
+
sorted_probs = dict(sorted(class_probs.items(), key=lambda x: x[1], reverse=True))
|
| 61 |
+
|
| 62 |
+
result = f"Predicted career: {predicted_career}\n\nProbabilities:\n"
|
| 63 |
+
for career, prob in sorted_probs.items():
|
| 64 |
+
result += f"{career}: {prob:.2f}\n"
|
| 65 |
+
return result
|
| 66 |
+
else:
|
| 67 |
+
return f"Predicted career: {predicted_career}"
|
| 68 |
+
|
| 69 |
+
# Get unique values for dropdowns
|
| 70 |
+
work_env_options = list(label_encoders['Preferred Work Environment'].classes_)
|
| 71 |
+
motivation_options = list(label_encoders['Motivation for Career Choice '].classes_)
|
| 72 |
+
leadership_options = list(label_encoders['Leadership Experience'].classes_)
|
| 73 |
+
tech_savvy_options = list(label_encoders['Tech-Savviness'].classes_)
|
| 74 |
+
|
| 75 |
+
# Get options for new features with error handling
|
| 76 |
+
subject_options = []
|
| 77 |
+
if 'Preferred Subjects in Highschool/College' in label_encoders:
|
| 78 |
+
subject_options = list(label_encoders['Preferred Subjects in Highschool/College'].classes_)
|
| 79 |
+
else:
|
| 80 |
+
# Default options if not in the model
|
| 81 |
+
subject_options = ["Science", "Commerce", "Arts", "Unknown"]
|
| 82 |
+
|
| 83 |
+
gender_options = []
|
| 84 |
+
if 'Gender' in label_encoders:
|
| 85 |
+
gender_options = list(label_encoders['Gender'].classes_)
|
| 86 |
+
else:
|
| 87 |
+
# Default options if not in the model
|
| 88 |
+
gender_options = ["Male", "Female", "Other"]
|
| 89 |
+
|
| 90 |
+
# Get work experience options if available
|
| 91 |
+
work_exp_options = []
|
| 92 |
+
if 'Previous Work Experience (If Any)' in label_encoders:
|
| 93 |
+
work_exp_options = list(label_encoders['Previous Work Experience (If Any)'].classes_)
|
| 94 |
+
else:
|
| 95 |
+
work_exp_options = ["No Experience", "Internship", "Part Time", "Full Time"]
|
| 96 |
+
|
| 97 |
+
# Create the Gradio interface
|
| 98 |
+
iface = gr.Interface(
|
| 99 |
+
fn=predict_career,
|
| 100 |
+
inputs=[
|
| 101 |
+
gr.Dropdown(work_env_options, label="Preferred Work Environment"),
|
| 102 |
+
gr.Textbox(label="Academic Performance (CGPA/Percentage)", placeholder="Enter your CGPA (e.g. 8.75)"),
|
| 103 |
+
gr.Dropdown(motivation_options, label="Motivation for Career Choice"),
|
| 104 |
+
gr.Dropdown(leadership_options, label="Leadership Experience"),
|
| 105 |
+
gr.Dropdown(tech_savvy_options, label="Tech-Savviness"),
|
| 106 |
+
gr.Dropdown(subject_options, label="Preferred Subjects"),
|
| 107 |
+
gr.Dropdown(gender_options, label="Gender"),
|
| 108 |
+
gr.Slider(minimum=1, maximum=10, step=1, value=5, label="Risk-Taking Ability"),
|
| 109 |
+
gr.Slider(minimum=1, maximum=10, step=1, value=5, label="Financial Stability"),
|
| 110 |
+
gr.Dropdown(work_exp_options, label="Previous Work Experience")
|
| 111 |
+
],
|
| 112 |
+
outputs="text",
|
| 113 |
+
title="Career Prediction Model",
|
| 114 |
+
description="Enter your details to predict your future career path",
|
| 115 |
+
theme="huggingface"
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
# Launch the interface
|
| 119 |
+
if __name__ == "__main__":
|
| 120 |
+
iface.launch()
|