Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,29 +4,40 @@ from sklearn.model_selection import train_test_split
|
|
| 4 |
from sklearn.metrics import r2_score
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
X
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 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 |
-
#
|
| 20 |
-
train_r2 =
|
| 21 |
-
test_r2 = r2_score(y_test, model.predict(X_test))
|
| 22 |
|
| 23 |
-
# Prediction
|
| 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),
|
| 28 |
|
| 29 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
with gr.Blocks() as demo:
|
| 31 |
gr.Markdown("# 📊 Test Score Predictor")
|
| 32 |
|
|
@@ -47,4 +58,10 @@ with gr.Blocks() as demo:
|
|
| 47 |
outputs=[output_score, output_train, output_test]
|
| 48 |
)
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
demo.launch()
|
|
|
|
|
|
| 4 |
from sklearn.metrics import r2_score
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
+
# -------- Initial Training --------
|
| 8 |
+
def train_model(csv_path="test_score_prediction_dataset.csv"):
|
| 9 |
+
df = pd.read_csv(csv_path)
|
| 10 |
+
X = df[["AP", "Honors", "GPA_Points", "Credits_Earned"]]
|
| 11 |
+
y = df["Predicted_Test_Score"]
|
| 12 |
|
| 13 |
+
X_train, X_test, y_train, y_test = train_test_split(
|
| 14 |
+
X, y, test_size=0.2, random_state=42
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
model = RandomForestRegressor(n_estimators=100, random_state=42)
|
| 18 |
+
model.fit(X_train, y_train)
|
| 19 |
+
|
| 20 |
+
train_r2 = r2_score(y_train, model.predict(X_train))
|
| 21 |
+
test_r2 = r2_score(y_test, model.predict(X_test))
|
| 22 |
|
| 23 |
+
return model, round(train_r2, 3), round(test_r2, 3)
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# Global model
|
| 26 |
+
model, train_r2, test_r2 = train_model()
|
|
|
|
| 27 |
|
| 28 |
+
# -------- Prediction Function --------
|
| 29 |
def predict_test_score(ap, honors, gpa_points, credits_earned):
|
| 30 |
features = [[ap, honors, gpa_points, credits_earned]]
|
| 31 |
prediction = model.predict(features)[0]
|
| 32 |
+
return round(prediction, 2), train_r2, test_r2
|
| 33 |
|
| 34 |
+
# -------- Retraining with Uploaded CSV --------
|
| 35 |
+
def retrain_with_csv(file):
|
| 36 |
+
global model, train_r2, test_r2
|
| 37 |
+
model, train_r2, test_r2 = train_model(file.name)
|
| 38 |
+
return f"✅ Model retrained successfully!\nTraining R²: {train_r2}, Testing R²: {test_r2}"
|
| 39 |
+
|
| 40 |
+
# -------- Gradio UI --------
|
| 41 |
with gr.Blocks() as demo:
|
| 42 |
gr.Markdown("# 📊 Test Score Predictor")
|
| 43 |
|
|
|
|
| 58 |
outputs=[output_score, output_train, output_test]
|
| 59 |
)
|
| 60 |
|
| 61 |
+
gr.Markdown("### 🔄 Upload New Dataset to Retrain")
|
| 62 |
+
file_upload = gr.File(file_types=[".csv"], label="Upload CSV")
|
| 63 |
+
retrain_output = gr.Textbox(label="Retrain Status")
|
| 64 |
+
file_upload.upload(retrain_with_csv, inputs=file_upload, outputs=retrain_output)
|
| 65 |
+
|
| 66 |
demo.launch()
|
| 67 |
+
|