ntam0001 commited on
Commit
f1caa30
·
verified ·
1 Parent(s): d68043c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -42
app.py CHANGED
@@ -1,64 +1,77 @@
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()
 
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()