Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import joblib
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Load the model and encoders
|
| 7 |
+
model = joblib.load("decision_tree_model.pkl")
|
| 8 |
+
encoder = joblib.load("one_hot_encoder.pkl")
|
| 9 |
+
field_encoder = joblib.load("label_encoder.pkl")
|
| 10 |
+
|
| 11 |
+
# Define the prediction function
|
| 12 |
+
def predict_field(high_school_stream, physics, math, chemistry, biology, economics, geography, history, literature):
|
| 13 |
+
# Create a DataFrame from inputs
|
| 14 |
+
input_data = pd.DataFrame({
|
| 15 |
+
"High_School_Stream": [high_school_stream],
|
| 16 |
+
"Physics": [physics],
|
| 17 |
+
"Math": [math],
|
| 18 |
+
"Chemistry": [chemistry],
|
| 19 |
+
"Biology": [biology],
|
| 20 |
+
"Economics": [economics],
|
| 21 |
+
"Geography": [geography],
|
| 22 |
+
"History": [history],
|
| 23 |
+
"Literature": [literature]
|
| 24 |
+
})
|
| 25 |
+
|
| 26 |
+
# Fill NaN with 0
|
| 27 |
+
input_data.fillna(0, inplace=True)
|
| 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(input_data[["High_School_Stream"]])
|
| 34 |
+
stream_encoded_df = pd.DataFrame(stream_encoded, columns=encoder.get_feature_names_out(["High_School_Stream"]))
|
| 35 |
+
|
| 36 |
+
# Combine encoded features with Performance_Score
|
| 37 |
+
X = pd.concat([stream_encoded_df, input_data["Performance_Score"]], axis=1)
|
| 38 |
+
|
| 39 |
+
# Predict
|
| 40 |
+
predicted_field_encoded = model.predict(X)
|
| 41 |
+
predicted_field = field_encoder.inverse_transform(predicted_field_encoded)
|
| 42 |
+
|
| 43 |
+
return predicted_field[0]
|
| 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 |
+
outputs = gr.Textbox(label="Predicted Field")
|
| 59 |
+
|
| 60 |
+
# Create Gradio app
|
| 61 |
+
app = gr.Interface(fn=predict_field, inputs=inputs, outputs=outputs, title="Student Field Prediction")
|
| 62 |
+
|
| 63 |
+
# Launch the app
|
| 64 |
+
app.launch()
|