Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,77 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
-
import
|
| 4 |
-
import numpy as np
|
| 5 |
|
| 6 |
-
# Load the model and encoders
|
| 7 |
-
model =
|
| 8 |
-
encoder =
|
| 9 |
-
field_encoder =
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
"
|
| 18 |
-
"
|
| 19 |
-
"
|
| 20 |
-
"
|
| 21 |
-
"
|
| 22 |
-
"
|
| 23 |
-
"
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
# Compute Performance_Score
|
| 30 |
-
input_data["Performance_Score"] = input_data.iloc[:, 1:9].sum(axis=1) / 100
|
| 31 |
|
| 32 |
# One-hot encode High_School_Stream
|
| 33 |
-
stream_encoded = encoder.transform(
|
| 34 |
stream_encoded_df = pd.DataFrame(stream_encoded, columns=encoder.get_feature_names_out(["High_School_Stream"]))
|
| 35 |
|
| 36 |
-
# Combine encoded features with
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
|
| 42 |
|
| 43 |
-
return
|
| 44 |
|
| 45 |
# Define Gradio interface
|
| 46 |
inputs = [
|
| 47 |
gr.Dropdown(label="High School Stream", choices=["PCM", "PCB", "MEG", "MPG", "HGL"]),
|
| 48 |
-
gr.Number(label="Physics Score"),
|
| 49 |
-
gr.Number(label="Math Score"),
|
| 50 |
-
gr.Number(label="Chemistry Score"),
|
| 51 |
-
gr.Number(label="Biology Score"),
|
| 52 |
-
gr.Number(label="Economics Score"),
|
| 53 |
-
gr.Number(label="Geography Score"),
|
| 54 |
-
gr.Number(label="History Score"),
|
| 55 |
-
gr.Number(label="Literature Score")
|
| 56 |
]
|
| 57 |
|
| 58 |
-
|
| 59 |
|
| 60 |
# Create Gradio app
|
| 61 |
-
app = gr.Interface(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
# Launch the app
|
| 64 |
app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
+
from joblib import load
|
|
|
|
| 4 |
|
| 5 |
+
# Load the saved model and encoders
|
| 6 |
+
model = load("trained_model.joblib")
|
| 7 |
+
encoder = load("one_hot_encoder.joblib")
|
| 8 |
+
field_encoder = load("label_encoder.joblib")
|
| 9 |
|
| 10 |
+
def preprocess_input(high_school_stream, physics, math, chemistry, biology, economics, geography, history, literature):
|
| 11 |
+
"""
|
| 12 |
+
Preprocess user input for prediction.
|
| 13 |
+
"""
|
| 14 |
+
# Create a dictionary for the input data
|
| 15 |
+
user_data = {
|
| 16 |
+
"High_School_Stream": high_school_stream,
|
| 17 |
+
"Physics": physics,
|
| 18 |
+
"Math": math,
|
| 19 |
+
"Chemistry": chemistry,
|
| 20 |
+
"Biology": biology,
|
| 21 |
+
"Economics": economics,
|
| 22 |
+
"Geography": geography,
|
| 23 |
+
"History": history,
|
| 24 |
+
"Literature": literature,
|
| 25 |
+
}
|
| 26 |
|
| 27 |
+
# Convert to DataFrame
|
| 28 |
+
user_df = pd.DataFrame([user_data])
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# One-hot encode High_School_Stream
|
| 31 |
+
stream_encoded = encoder.transform(user_df[["High_School_Stream"]])
|
| 32 |
stream_encoded_df = pd.DataFrame(stream_encoded, columns=encoder.get_feature_names_out(["High_School_Stream"]))
|
| 33 |
|
| 34 |
+
# Combine encoded features with subject scores
|
| 35 |
+
user_processed = pd.concat([stream_encoded_df, user_df.iloc[:, 1:]], axis=1)
|
| 36 |
+
|
| 37 |
+
return user_processed
|
| 38 |
+
|
| 39 |
+
def predict(high_school_stream, physics, math, chemistry, biology, economics, geography, history, literature):
|
| 40 |
+
"""
|
| 41 |
+
Make a prediction using the trained model.
|
| 42 |
+
"""
|
| 43 |
+
# Preprocess input
|
| 44 |
+
user_processed = preprocess_input(high_school_stream, physics, math, chemistry, biology, economics, geography, history, literature)
|
| 45 |
|
| 46 |
+
# Make prediction
|
| 47 |
+
y_pred_encoded = model.predict(user_processed)
|
| 48 |
+
y_pred = field_encoder.inverse_transform(y_pred_encoded)
|
| 49 |
|
| 50 |
+
return y_pred[0]
|
| 51 |
|
| 52 |
# Define Gradio interface
|
| 53 |
inputs = [
|
| 54 |
gr.Dropdown(label="High School Stream", choices=["PCM", "PCB", "MEG", "MPG", "HGL"]),
|
| 55 |
+
gr.Number(label="Physics Score (0 if not applicable)"),
|
| 56 |
+
gr.Number(label="Math Score (0 if not applicable)"),
|
| 57 |
+
gr.Number(label="Chemistry Score (0 if not applicable)"),
|
| 58 |
+
gr.Number(label="Biology Score (0 if not applicable)"),
|
| 59 |
+
gr.Number(label="Economics Score (0 if not applicable)"),
|
| 60 |
+
gr.Number(label="Geography Score (0 if not applicable)"),
|
| 61 |
+
gr.Number(label="History Score (0 if not applicable)"),
|
| 62 |
+
gr.Number(label="Literature Score (0 if not applicable)"),
|
| 63 |
]
|
| 64 |
|
| 65 |
+
output = gr.Textbox(label="Predicted Field")
|
| 66 |
|
| 67 |
# Create Gradio app
|
| 68 |
+
app = gr.Interface(
|
| 69 |
+
fn=predict,
|
| 70 |
+
inputs=inputs,
|
| 71 |
+
outputs=output,
|
| 72 |
+
title="Student Field Prediction",
|
| 73 |
+
description="Enter your details to predict the recommended field of study.",
|
| 74 |
+
)
|
| 75 |
|
| 76 |
# Launch the app
|
| 77 |
app.launch()
|