Bibek Mukherjee commited on
Commit
3c45d64
·
verified ·
1 Parent(s): 57205fc

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +118 -0
app.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, 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
+ 'Risk-Taking Ability ': [float(risk_taking)], # Note the space at the end
32
+ 'Financial Stability - self/family (1 is low income and 10 is high income)': [float(financial_stability)],
33
+ 'Previous Work Experience (If Any)': [work_exp]
34
+ })
35
+
36
+ # Encode categorical features
37
+ for feature in features:
38
+ if feature in input_data.columns:
39
+ if feature in label_encoders and input_data[feature].dtype == 'object':
40
+ try:
41
+ input_data[feature] = label_encoders[feature].transform(input_data[feature])
42
+ except ValueError:
43
+ # Handle unknown categories
44
+ print(f"Warning: Unknown category in {feature}. Using most frequent category.")
45
+ input_data[feature] = 0 # Default to first category
46
+ else:
47
+ print(f"Warning: Feature {feature} not found in input data.")
48
+
49
+ # Make prediction
50
+ prediction = model.predict(input_data)[0]
51
+ predicted_career = target_encoder.inverse_transform([int(prediction)])[0]
52
+
53
+ # Get probabilities for all classes
54
+ if hasattr(model, 'predict_proba'):
55
+ probabilities = model.predict_proba(input_data)[0]
56
+ class_probs = {target_encoder.inverse_transform([i])[0]: prob
57
+ for i, prob in enumerate(probabilities)}
58
+ sorted_probs = dict(sorted(class_probs.items(), key=lambda x: x[1], reverse=True))
59
+
60
+ result = f"Predicted career: {predicted_career}\n\nProbabilities:\n"
61
+ for career, prob in sorted_probs.items():
62
+ result += f"{career}: {prob:.2f}\n"
63
+ return result
64
+ else:
65
+ return f"Predicted career: {predicted_career}"
66
+
67
+ # Get unique values for dropdowns
68
+ work_env_options = list(label_encoders['Preferred Work Environment'].classes_)
69
+ motivation_options = list(label_encoders['Motivation for Career Choice '].classes_)
70
+ leadership_options = list(label_encoders['Leadership Experience'].classes_)
71
+ tech_savvy_options = list(label_encoders['Tech-Savviness'].classes_)
72
+
73
+ # Get work experience options if available
74
+ work_exp_options = []
75
+ if 'Previous Work Experience (If Any)' in label_encoders:
76
+ work_exp_options = list(label_encoders['Previous Work Experience (If Any)'].classes_)
77
+ else:
78
+ work_exp_options = ["No Experience", "Internship", "Part Time", "Full Time"]
79
+
80
+ # Create the Gradio interface
81
+ iface = gr.Interface(
82
+ fn=predict_career,
83
+ inputs=[
84
+ gr.Dropdown(work_env_options, label="Preferred Work Environment"),
85
+ gr.Number(label="Academic Performance (CGPA/Percentage)", minimum=0, maximum=10),
86
+ gr.Dropdown(motivation_options, label="Motivation for Career Choice"),
87
+ gr.Dropdown(leadership_options, label="Leadership Experience"),
88
+ gr.Dropdown(tech_savvy_options, label="Tech-Savviness"),
89
+ gr.Slider(minimum=1, maximum=10, step=1, value=5, label="Risk-Taking Ability"),
90
+ gr.Slider(minimum=1, maximum=10, step=1, value=5, label="Financial Stability"),
91
+ gr.Dropdown(work_exp_options, label="Previous Work Experience")
92
+ ],
93
+ outputs="text",
94
+ title="Career Prediction Model",
95
+ description="Enter your details to predict your future career path",
96
+ theme="huggingface"
97
+ )
98
+ # Create a separate interface for model evaluation
99
+ eval_iface = gr.Interface(
100
+ fn=evaluate_model_with_csv,
101
+ inputs=gr.File(label="Upload Test CSV File"),
102
+ outputs=[
103
+ gr.Textbox(label="Evaluation Results"),
104
+ gr.Image(label="Confusion Matrix")
105
+ ],
106
+ title="Career Prediction Model Evaluation",
107
+ description="Upload a CSV file with test data to evaluate the model's performance",
108
+ theme="huggingface"
109
+ )
110
+
111
+ # Create a tabbed interface
112
+ demo = gr.TabbedInterface(
113
+ [iface, eval_iface],
114
+ ["Individual Prediction", "Batch Evaluation"]
115
+ )
116
+
117
+ # Launch the interface
118
+ demo.launch()