Shortheadband commited on
Commit
5daf30d
·
verified ·
1 Parent(s): 9e6aa83

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -10
app.py CHANGED
@@ -1,12 +1,11 @@
1
  import pandas as pd
2
- from sklearn.model_selection import train_test_split
3
  from sklearn.ensemble import RandomForestRegressor
 
4
  from sklearn.metrics import r2_score
 
5
 
6
- # ----------------------------
7
  # Load dataset
8
- CSV_PATH = "test_score_prediction_dataset.csv"
9
- df = pd.read_csv(CSV_PATH)
10
 
11
  # Features and target
12
  X = df[["AP", "Honors", "GPA_Points", "Credits_Earned"]]
@@ -14,12 +13,38 @@ y = df["Predicted_Test_Score"]
14
 
15
  # Train/test split
16
  X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
17
-
18
- # ----------------------------
19
- # Train model (Random Forest)
20
  model = RandomForestRegressor(n_estimators=100, random_state=42)
21
  model.fit(X_train, y_train)
22
 
23
- # Predict and evaluate
24
- y_pred = model.predict(X_test)
25
- print("R² Score:", r2_score(y_test, y_pred))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import pandas as pd
 
2
  from sklearn.ensemble import RandomForestRegressor
3
+ from sklearn.model_selection import train_test_split
4
  from sklearn.metrics import r2_score
5
+ import gradio as gr
6
 
 
7
  # Load dataset
8
+ df = pd.read_csv("test_score_prediction_dataset.csv")
 
9
 
10
  # Features and target
11
  X = df[["AP", "Honors", "GPA_Points", "Credits_Earned"]]
 
13
 
14
  # Train/test split
15
  X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
 
 
 
16
  model = RandomForestRegressor(n_estimators=100, random_state=42)
17
  model.fit(X_train, y_train)
18
 
19
+ # Accuracy
20
+ train_r2 = r2_score(y_train, model.predict(X_train))
21
+ test_r2 = r2_score(y_test, model.predict(X_test))
22
+
23
+ # Prediction function
24
+ def predict_test_score(ap, honors, gpa_points, credits_earned):
25
+ features = [[ap, honors, gpa_points, credits_earned]]
26
+ prediction = model.predict(features)[0]
27
+ return round(prediction, 2), round(train_r2, 3), round(test_r2, 3)
28
+
29
+ # Gradio UI
30
+ with gr.Blocks() as demo:
31
+ gr.Markdown("# 📊 Test Score Predictor")
32
+
33
+ with gr.Row():
34
+ ap = gr.Checkbox(label="AP")
35
+ honors = gr.Checkbox(label="Honors")
36
+ gpa_points = gr.Number(label="GPA Points")
37
+ credits_earned = gr.Number(label="Credits Earned")
38
+
39
+ output_score = gr.Number(label="Predicted Test Score")
40
+ output_train = gr.Number(label="Training R² Score")
41
+ output_test = gr.Number(label="Testing R² Score")
42
+
43
+ btn = gr.Button("Predict")
44
+ btn.click(
45
+ predict_test_score,
46
+ inputs=[ap, honors, gpa_points, credits_earned],
47
+ outputs=[output_score, output_train, output_test]
48
+ )
49
+
50
+ demo.launch()