WickedFaith commited on
Commit
83478b3
·
verified ·
1 Parent(s): 6e950ab

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -0
app.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
76
+ subject_options = list(label_encoders['Preferred Subjects in Highschool/College'].classes_)
77
+ gender_options = list(label_encoders['Gender'].classes_)
78
+
79
+ # Get work experience options if available
80
+ work_exp_options = []
81
+ if 'Previous Work Experience (If Any)' in label_encoders:
82
+ work_exp_options = list(label_encoders['Previous Work Experience (If Any)'].classes_)
83
+ else:
84
+ work_exp_options = ["No Experience", "Internship", "Part Time", "Full Time"]
85
+
86
+ # Create the Gradio interface
87
+ iface = gr.Interface(
88
+ fn=predict_career,
89
+ inputs=[
90
+ gr.Dropdown(work_env_options, label="Preferred Work Environment"),
91
+ gr.Number(label="Academic Performance (CGPA/Percentage)", minimum=0, maximum=10),
92
+ gr.Dropdown(motivation_options, label="Motivation for Career Choice"),
93
+ gr.Dropdown(leadership_options, label="Leadership Experience"),
94
+ gr.Dropdown(tech_savvy_options, label="Tech-Savviness"),
95
+ gr.Dropdown(subject_options, label="Preferred Subjects"),
96
+ gr.Dropdown(gender_options, label="Gender"),
97
+ gr.Slider(minimum=1, maximum=10, step=1, value=5, label="Risk-Taking Ability"),
98
+ gr.Slider(minimum=1, maximum=10, step=1, value=5, label="Financial Stability"),
99
+ gr.Dropdown(work_exp_options, label="Previous Work Experience")
100
+ ],
101
+ outputs="text",
102
+ title="Career Prediction Model",
103
+ description="Enter your details to predict your future career path",
104
+ theme="huggingface"
105
+ )
106
+
107
+ # Launch the interface
108
+ if __name__ == "__main__":
109
+ iface.launch()