Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from sklearn.model_selection import train_test_split
|
| 4 |
+
from sklearn.ensemble import RandomForestRegressor
|
| 5 |
+
import joblib
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# --- Load Dataset ---
|
| 9 |
+
# Make sure you upload your CSV into the Space with the same name
|
| 10 |
+
df = pd.read_csv("course_history_with_difficulty.csv")
|
| 11 |
+
|
| 12 |
+
# Convert True/False into integers for modeling
|
| 13 |
+
df["AP"] = df["AP"].astype(int)
|
| 14 |
+
df["Honors"] = df["Honors"].astype(int)
|
| 15 |
+
|
| 16 |
+
# --- Features & Target ---
|
| 17 |
+
X = df[["AP", "Honors", "Credits_Attempted", "Credits_Earned"]]
|
| 18 |
+
y = df["Weighted_GPA_Points"]
|
| 19 |
+
|
| 20 |
+
# Train-test split
|
| 21 |
+
X_train, X_test, y_train, y_test = train_test_split(
|
| 22 |
+
X, y, test_size=0.2, random_state=42
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# --- Train Model (Random Forest as best baseline) ---
|
| 26 |
+
model = RandomForestRegressor(n_estimators=100, random_state=42)
|
| 27 |
+
model.fit(X_train, y_train)
|
| 28 |
+
|
| 29 |
+
# Save model for persistence
|
| 30 |
+
joblib.dump(model, "gpa_model.pkl")
|
| 31 |
+
|
| 32 |
+
# Reload model (useful when restarting Space)
|
| 33 |
+
model = joblib.load("gpa_model.pkl")
|
| 34 |
+
|
| 35 |
+
# --- Prediction Function ---
|
| 36 |
+
def predict(ap, honors, credits_attempted, credits_earned):
|
| 37 |
+
features = [[int(ap), int(honors), float(credits_attempted), float(credits_earned)]]
|
| 38 |
+
prediction = model.predict(features)[0]
|
| 39 |
+
return round(prediction, 2)
|
| 40 |
+
|
| 41 |
+
# --- Gradio UI ---
|
| 42 |
+
demo = gr.Interface(
|
| 43 |
+
fn=predict,
|
| 44 |
+
inputs=[
|
| 45 |
+
gr.Checkbox(label="AP"),
|
| 46 |
+
gr.Checkbox(label="Honors"),
|
| 47 |
+
gr.Number(label="Credits Attempted"),
|
| 48 |
+
gr.Number(label="Credits Earned")
|
| 49 |
+
],
|
| 50 |
+
outputs=gr.Number(label="Predicted GPA Points"),
|
| 51 |
+
title="GPA Prediction Model",
|
| 52 |
+
description="Enter course details to predict GPA points (weighted)."
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
demo.launch()
|