Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
from sklearn.compose import ColumnTransformer
|
| 6 |
+
from sklearn.preprocessing import OneHotEncoder, StandardScaler
|
| 7 |
+
from sklearn.model_selection import train_test_split
|
| 8 |
+
from sklearn.metrics import mean_absolute_error, root_mean_squared_error
|
| 9 |
+
|
| 10 |
+
from tensorflow.keras.models import Sequential
|
| 11 |
+
from tensorflow.keras.layers import Dense, Input
|
| 12 |
+
from tensorflow.keras.callbacks import EarlyStopping
|
| 13 |
+
import keras_tuner as kt
|
| 14 |
+
|
| 15 |
+
# Load dataset
|
| 16 |
+
df = pd.read_csv("Dataset/Student_Performance.csv")
|
| 17 |
+
|
| 18 |
+
# Features
|
| 19 |
+
num_feature = ['Hours Studied', 'Previous Scores', 'Sleep Hours', 'Sample Question Papers Practiced']
|
| 20 |
+
cat_feature = ['Extracurricular Activities']
|
| 21 |
+
|
| 22 |
+
# Preprocessing
|
| 23 |
+
preprocess = ColumnTransformer(transformers=[
|
| 24 |
+
('num', StandardScaler(), num_feature),
|
| 25 |
+
('cat', OneHotEncoder(drop='first'), cat_feature)
|
| 26 |
+
])
|
| 27 |
+
|
| 28 |
+
x = df.drop(columns=['Performance Index'])
|
| 29 |
+
y = df['Performance Index']
|
| 30 |
+
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.4, random_state=42)
|
| 31 |
+
|
| 32 |
+
x_train_scaled = preprocess.fit_transform(x_train)
|
| 33 |
+
x_test_scaled = preprocess.transform(x_test)
|
| 34 |
+
|
| 35 |
+
# Build model
|
| 36 |
+
def build_model(hp):
|
| 37 |
+
model = Sequential([
|
| 38 |
+
Input(shape=(x_test_scaled.shape[1],)),
|
| 39 |
+
Dense(units=hp.Int('u1', 32, 256, step=32), activation='relu'),
|
| 40 |
+
Dense(units=hp.Int('u2', 16, 128, step=16), activation='relu'),
|
| 41 |
+
Dense(units=hp.Int('u3', 8, 64, step=8), activation='relu'),
|
| 42 |
+
Dense(1)
|
| 43 |
+
])
|
| 44 |
+
model.compile(optimizer="adam", loss="mse", metrics=["mae"])
|
| 45 |
+
return model
|
| 46 |
+
|
| 47 |
+
# Hyperparameter tuning
|
| 48 |
+
tuner = kt.RandomSearch(build_model, objective="val_loss", max_trials=3, overwrite=True)
|
| 49 |
+
early_stop = EarlyStopping(monitor="val_loss", patience=10, restore_best_weights=True)
|
| 50 |
+
|
| 51 |
+
tuner.search(x_train_scaled, y_train, epochs=50, batch_size=32, validation_split=0.2,
|
| 52 |
+
callbacks=[early_stop], verbose=0)
|
| 53 |
+
|
| 54 |
+
best_model = tuner.get_best_models(1)[0]
|
| 55 |
+
|
| 56 |
+
# Define prediction function for Gradio
|
| 57 |
+
def predict_performance(hours, prev_score, sleep, papers, activity):
|
| 58 |
+
user_input = pd.DataFrame([{
|
| 59 |
+
'Hours Studied': hours,
|
| 60 |
+
'Previous Scores': prev_score,
|
| 61 |
+
'Sleep Hours': sleep,
|
| 62 |
+
'Sample Question Papers Practiced': papers,
|
| 63 |
+
'Extracurricular Activities': activity
|
| 64 |
+
}])
|
| 65 |
+
X_user = preprocess.transform(user_input)
|
| 66 |
+
pred = best_model.predict(X_user).item()
|
| 67 |
+
return f"📊 Predicted Performance Index: {pred:.2f}"
|
| 68 |
+
|
| 69 |
+
# Gradio UI
|
| 70 |
+
inputs = [
|
| 71 |
+
gr.Number(label="Hours Studied"),
|
| 72 |
+
gr.Number(label="Previous Scores"),
|
| 73 |
+
gr.Number(label="Sleep Hours"),
|
| 74 |
+
gr.Number(label="Sample Question Papers Practiced"),
|
| 75 |
+
gr.Radio(choices=["Yes", "No"], label="Extracurricular Activities")
|
| 76 |
+
]
|
| 77 |
+
|
| 78 |
+
demo = gr.Interface(
|
| 79 |
+
fn=predict_performance,
|
| 80 |
+
inputs=inputs,
|
| 81 |
+
outputs="text",
|
| 82 |
+
title="🎓 Student Performance Prediction",
|
| 83 |
+
description="Enter student study habits and get predicted performance index."
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
if __name__ == "__main__":
|
| 87 |
+
demo.launch()
|