File size: 5,053 Bytes
3c45d64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

import pandas as pd
import numpy as np
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
import matplotlib.pyplot as plt
import seaborn as sns
import pickle
import gradio as gr
import os

# Load the model
model_path = 'career_prediction_model.pkl'
with open(model_path, 'rb') as f:
    saved_data = pickle.load(f)

model = saved_data['model']
label_encoders = saved_data['label_encoders']
target_encoder = saved_data['target_encoder']
features = saved_data['features']
target = 'What would you like to become when you grow up'

# Function for individual prediction
def predict_career(work_env, academic_perf, motivation, leadership, tech_savvy, risk_taking=5, financial_stability=5, work_exp="No Experience"):
    # Prepare input data
    input_data = pd.DataFrame({
        'Preferred Work Environment': [work_env],
        'Academic Performance (CGPA/Percentage)': [float(academic_perf)],
        'Motivation for Career Choice ': [motivation],  # Note the space at the end
        'Leadership Experience': [leadership],
        'Tech-Savviness': [tech_savvy],
        'Risk-Taking Ability ': [float(risk_taking)],  # Note the space at the end
        'Financial Stability - self/family (1 is low income and 10 is high income)': [float(financial_stability)],
        'Previous Work Experience (If Any)': [work_exp]
    })
    
    # Encode categorical features
    for feature in features:
        if feature in input_data.columns:
            if feature in label_encoders and input_data[feature].dtype == 'object':
                try:
                    input_data[feature] = label_encoders[feature].transform(input_data[feature])
                except ValueError:
                    # Handle unknown categories
                    print(f"Warning: Unknown category in {feature}. Using most frequent category.")
                    input_data[feature] = 0  # Default to first category
        else:
            print(f"Warning: Feature {feature} not found in input data.")
    
    # Make prediction
    prediction = model.predict(input_data)[0]
    predicted_career = target_encoder.inverse_transform([int(prediction)])[0]
    
    # Get probabilities for all classes
    if hasattr(model, 'predict_proba'):
        probabilities = model.predict_proba(input_data)[0]
        class_probs = {target_encoder.inverse_transform([i])[0]: prob 
                      for i, prob in enumerate(probabilities)}
        sorted_probs = dict(sorted(class_probs.items(), key=lambda x: x[1], reverse=True))
        
        result = f"Predicted career: {predicted_career}\n\nProbabilities:\n"
        for career, prob in sorted_probs.items():
            result += f"{career}: {prob:.2f}\n"
        return result
    else:
        return f"Predicted career: {predicted_career}"

# Get unique values for dropdowns
work_env_options = list(label_encoders['Preferred Work Environment'].classes_)
motivation_options = list(label_encoders['Motivation for Career Choice '].classes_)
leadership_options = list(label_encoders['Leadership Experience'].classes_)
tech_savvy_options = list(label_encoders['Tech-Savviness'].classes_)

# Get work experience options if available
work_exp_options = []
if 'Previous Work Experience (If Any)' in label_encoders:
    work_exp_options = list(label_encoders['Previous Work Experience (If Any)'].classes_)
else:
    work_exp_options = ["No Experience", "Internship", "Part Time", "Full Time"]

# Create the Gradio interface
iface = gr.Interface(
    fn=predict_career,
    inputs=[
        gr.Dropdown(work_env_options, label="Preferred Work Environment"),
        gr.Number(label="Academic Performance (CGPA/Percentage)", minimum=0, maximum=10),
        gr.Dropdown(motivation_options, label="Motivation for Career Choice"),
        gr.Dropdown(leadership_options, label="Leadership Experience"),
        gr.Dropdown(tech_savvy_options, label="Tech-Savviness"),
        gr.Slider(minimum=1, maximum=10, step=1, value=5, label="Risk-Taking Ability"),
        gr.Slider(minimum=1, maximum=10, step=1, value=5, label="Financial Stability"),
        gr.Dropdown(work_exp_options, label="Previous Work Experience")
    ],
    outputs="text",
    title="Career Prediction Model",
    description="Enter your details to predict your future career path",
    theme="huggingface"
)
# Create a separate interface for model evaluation
eval_iface = gr.Interface(
    fn=evaluate_model_with_csv,
    inputs=gr.File(label="Upload Test CSV File"),
    outputs=[
        gr.Textbox(label="Evaluation Results"),
        gr.Image(label="Confusion Matrix")
    ],
    title="Career Prediction Model Evaluation",
    description="Upload a CSV file with test data to evaluate the model's performance",
    theme="huggingface"
)

# Create a tabbed interface
demo = gr.TabbedInterface(
    [iface, eval_iface],
    ["Individual Prediction", "Batch Evaluation"]
)

# Launch the interface
demo.launch()