Spaces:
Sleeping
Sleeping
app.py
Browse filesapp.py file uploaded
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import joblib
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
+
from sklearn.preprocessing import StandardScaler, OneHotEncoder, LabelEncoder
|
| 7 |
+
|
| 8 |
+
# Load the trained model and scaler objects from file
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
REPO_ID = "Hemg/studentApp" # hugging face repo ID
|
| 12 |
+
MoDEL_FILENAME = "stx.joblib" # model file name
|
| 13 |
+
SCALER_FILENAME ="scaler.joblib" # scaler file name
|
| 14 |
+
|
| 15 |
+
model = joblib.load(hf_hub_download(repo_id=REPO_ID, filename=MoDEL_FILENAME))
|
| 16 |
+
|
| 17 |
+
scaler = joblib.load(hf_hub_download(repo_id=REPO_ID, filename=SCALER_FILENAME))
|
| 18 |
+
|
| 19 |
+
def encode_categorical_columns(df):
|
| 20 |
+
label_encoder = LabelEncoder()
|
| 21 |
+
ordinal_columns = df.select_dtypes(include=['object']).columns
|
| 22 |
+
|
| 23 |
+
for col in ordinal_columns:
|
| 24 |
+
df[col] = label_encoder.fit_transform(df[col])
|
| 25 |
+
|
| 26 |
+
nominal_columns = df.select_dtypes(include=['object']).columns.difference(ordinal_columns)
|
| 27 |
+
df = pd.get_dummies(df, columns=nominal_columns, drop_first=True)
|
| 28 |
+
|
| 29 |
+
return df
|
| 30 |
+
|
| 31 |
+
# Define the prediction function
|
| 32 |
+
def predict_performance(Hours_Studied,Previous_Scores,Extracurricular_Activities,Sleep_Hours,SampleQuestions_Practiced):
|
| 33 |
+
# Prepare input data represents independent variables for house prediction
|
| 34 |
+
input_data = [[Hours_Studied,Previous_Scores,Extracurricular_Activities,Sleep_Hours,SampleQuestions_Practiced]]
|
| 35 |
+
|
| 36 |
+
# Get the feature names from the Gradio interface inputs
|
| 37 |
+
feature_names = ['Hours_Studied','Previous_Scores','Extracurricular_Activities','Sleep_Hours','SampleQuestions_Practiced']
|
| 38 |
+
# Create a Pandas DataFrame with the input data and feature names
|
| 39 |
+
input_df = pd.DataFrame(input_data, columns=feature_names)
|
| 40 |
+
|
| 41 |
+
input_df = pd.DataFrame(input_data, columns=feature_names)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
df = encode_categorical_columns(input_df)
|
| 45 |
+
|
| 46 |
+
# Scale the input data using the loaded scaler
|
| 47 |
+
scaled_input = scaler.transform(df)
|
| 48 |
+
|
| 49 |
+
# Make predictions using the loaded model
|
| 50 |
+
prediction = model.predict(scaled_input)[0]
|
| 51 |
+
|
| 52 |
+
return f"Performance_Index: {prediction:,.2f}"
|
| 53 |
+
|
| 54 |
+
# Create the Gradio app
|
| 55 |
+
iface = gr.Interface(
|
| 56 |
+
fn=predict_performance,
|
| 57 |
+
inputs=[
|
| 58 |
+
gr.Slider(minimum=1, maximum=10, step=1, label="Hours_Studied",info="How many hours did you study last week?"),
|
| 59 |
+
gr.Slider(minimum=40, maximum=100, step=1, label="Previous_Scores",info="What were your previous academic scores in your most recent exams or assessments?"),
|
| 60 |
+
#gr.Slider(minimum=1, maximum=100, step=1, label="Extracurricular_Activities",info="Are you involved in any extracurricular activities outside of your regular coursework?"),
|
| 61 |
+
gr.Radio(["Yes", "No"], label="Extracurricular_Activities",info="Are you involved in any extracurricular activities outside of your regular coursework?"),
|
| 62 |
+
gr.Slider(minimum=3, maximum=10, step=1, label="Sleep_Hours",info="On average, how many hours of sleep do you get per night?"),
|
| 63 |
+
gr.Slider(minimum=1, maximum=10, step=1, label="SampleQuestions_Practiced",info="Have you been practicing any sample questions or past exam papers as part of your study routine?")
|
| 64 |
+
],
|
| 65 |
+
outputs="text",
|
| 66 |
+
title="Student_performance_prediction",
|
| 67 |
+
description="Predict Performance_Index"
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
# Run the app
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
iface.launch(share=True)
|