Spaces:
Sleeping
Sleeping
| 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() |