File size: 2,612 Bytes
d9fec98
 
f1caa30
d9fec98
f1caa30
90b2a8a
 
 
d9fec98
f1caa30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9fec98
f1caa30
 
d9fec98
 
f1caa30
d9fec98
 
f1caa30
 
 
 
 
 
 
 
 
 
 
d9fec98
f1caa30
 
 
d9fec98
f1caa30
d9fec98
 
 
 
f1caa30
 
 
 
 
 
 
 
d9fec98
 
f1caa30
d9fec98
 
f1caa30
 
 
 
 
 
 
d9fec98
 
 
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
import gradio as gr
import pandas as pd
from joblib import load

# Load the saved model and encoders
model = load("random_forest_model.pkl")
encoder = load("one_hot_encoder.pkl")
field_encoder = load("label_encoder.pkl")

def preprocess_input(high_school_stream, physics, math, chemistry, biology, economics, geography, history, literature):
    """
    Preprocess user input for prediction.
    """
    # Create a dictionary for the input data
    user_data = {
        "High_School_Stream": high_school_stream,
        "Physics": physics,
        "Math": math,
        "Chemistry": chemistry,
        "Biology": biology,
        "Economics": economics,
        "Geography": geography,
        "History": history,
        "Literature": literature,
    }

    # Convert to DataFrame
    user_df = pd.DataFrame([user_data])

    # One-hot encode High_School_Stream
    stream_encoded = encoder.transform(user_df[["High_School_Stream"]])
    stream_encoded_df = pd.DataFrame(stream_encoded, columns=encoder.get_feature_names_out(["High_School_Stream"]))

    # Combine encoded features with subject scores
    user_processed = pd.concat([stream_encoded_df, user_df.iloc[:, 1:]], axis=1)

    return user_processed

def predict(high_school_stream, physics, math, chemistry, biology, economics, geography, history, literature):
    """
    Make a prediction using the trained model.
    """
    # Preprocess input
    user_processed = preprocess_input(high_school_stream, physics, math, chemistry, biology, economics, geography, history, literature)

    # Make prediction
    y_pred_encoded = model.predict(user_processed)
    y_pred = field_encoder.inverse_transform(y_pred_encoded)

    return y_pred[0]

# Define Gradio interface
inputs = [
    gr.Dropdown(label="High School Stream", choices=["PCM", "PCB", "MEG", "MPG", "HGL"]),
    gr.Number(label="Physics Score (0 if not applicable)"),
    gr.Number(label="Math Score (0 if not applicable)"),
    gr.Number(label="Chemistry Score (0 if not applicable)"),
    gr.Number(label="Biology Score (0 if not applicable)"),
    gr.Number(label="Economics Score (0 if not applicable)"),
    gr.Number(label="Geography Score (0 if not applicable)"),
    gr.Number(label="History Score (0 if not applicable)"),
    gr.Number(label="Literature Score (0 if not applicable)"),
]

output = gr.Textbox(label="Predicted Field")

# Create Gradio app
app = gr.Interface(
    fn=predict,
    inputs=inputs,
    outputs=output,
    title="Student Field Prediction",
    description="Enter your details to predict the recommended field of study.",
)

# Launch the app
app.launch()