Update src/streamlit_app.py
Browse files- src/streamlit_app.py +92 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,94 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
""
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import joblib
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# --- Configuration ---
|
| 7 |
+
MODEL_PATH = 'src/kn_perormance_prediction.joblib'
|
| 8 |
+
ENCODERS_PATH = 'src/label_encoders.joblib'
|
| 9 |
+
|
| 10 |
+
FEATURES = [
|
| 11 |
+
'gender', 'race/ethnicity', 'parental level of education',
|
| 12 |
+
'lunch', 'test preparation course', 'math score',
|
| 13 |
+
'reading score', 'writing score'
|
| 14 |
+
]
|
| 15 |
+
|
| 16 |
+
RACE_MAP = {
|
| 17 |
+
'group A': 1, 'group B': 2, 'group C': 3,
|
| 18 |
+
'group D': 4, 'group E': 5
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
@st.cache_resource
|
| 22 |
+
def load_assets():
|
| 23 |
+
try:
|
| 24 |
+
model = joblib.load(MODEL_PATH)
|
| 25 |
+
encoders = joblib.load(ENCODERS_PATH) # Load the dict of encoders
|
| 26 |
+
return model, encoders
|
| 27 |
+
except Exception as e:
|
| 28 |
+
st.error(f"Error loading assets. Ensure model and encoders are uploaded. Error: {e}")
|
| 29 |
+
return None, None
|
| 30 |
+
|
| 31 |
+
def preprocess_and_predict(model, encoders, input_data):
|
| 32 |
+
|
| 33 |
+
# 1. Create a DataFrame for processing
|
| 34 |
+
df_input = pd.DataFrame([input_data])
|
| 35 |
+
|
| 36 |
+
# 2. Apply Custom Race/Ethnicity Mapping (as done in the original notebook)
|
| 37 |
+
df_input['race/ethnicity'] = df_input['race/ethnicity'].map(RACE_MAP)
|
| 38 |
+
|
| 39 |
+
# 3. Apply Label Encoding for other Categorical Features
|
| 40 |
+
for col, le in encoders.items():
|
| 41 |
+
# Use the loaded LabelEncoder objects to transform the input strings
|
| 42 |
+
df_input[col] = le.transform(df_input[col])
|
| 43 |
+
|
| 44 |
+
# 4. Prepare the final input array
|
| 45 |
+
final_input_array = df_input[FEATURES].values
|
| 46 |
+
|
| 47 |
+
# 5. Predict the grade (O, A, B, C, D, E)
|
| 48 |
+
prediction_label = model.predict(final_input_array)
|
| 49 |
+
|
| 50 |
+
return prediction_label[0]
|
| 51 |
+
|
| 52 |
+
# --- Streamlit Interface ---
|
| 53 |
+
st.set_page_config(page_title="Student Performance", layout="centered")
|
| 54 |
+
st.title("📚 Student Final Grade Predictor (A-E)")
|
| 55 |
+
st.markdown("Enter student characteristics and scores to predict the final letter grade.")
|
| 56 |
+
|
| 57 |
+
model, encoders = load_assets()
|
| 58 |
+
|
| 59 |
+
if model is not None and encoders is not None:
|
| 60 |
+
st.sidebar.header("Student Information")
|
| 61 |
+
|
| 62 |
+
# --- INPUT WIDGETS ---
|
| 63 |
+
gender = st.sidebar.selectbox("Gender:", options=['male', 'female'])
|
| 64 |
+
race = st.sidebar.selectbox("Race/Ethnicity:", options=list(RACE_MAP.keys()))
|
| 65 |
+
parental_education = st.sidebar.selectbox("Parental Education:", options=['some high school', 'high school', 'some college', 'associate\'s degree', 'bachelor\'s degree', 'master\'s degree'])
|
| 66 |
+
lunch = st.sidebar.selectbox("Lunch:", options=['standard', 'free/reduced'])
|
| 67 |
+
prep_course = st.sidebar.selectbox("Test Prep Course:", options=['none', 'completed'])
|
| 68 |
+
|
| 69 |
+
st.sidebar.header("Scores (0-100)")
|
| 70 |
+
math = st.sidebar.slider("Math Score:", min_value=0, max_value=100, value=70)
|
| 71 |
+
reading = st.sidebar.slider("Reading Score:", min_value=0, max_value=100, value=75)
|
| 72 |
+
writing = st.sidebar.slider("Writing Score:", min_value=0, max_value=100, value=72)
|
| 73 |
+
|
| 74 |
+
# Collect inputs
|
| 75 |
+
input_data = {
|
| 76 |
+
'gender': gender,
|
| 77 |
+
'race/ethnicity': race,
|
| 78 |
+
'parental level of education': parental_education,
|
| 79 |
+
'lunch': lunch,
|
| 80 |
+
'test preparation course': prep_course,
|
| 81 |
+
'math score': math,
|
| 82 |
+
'reading score': reading,
|
| 83 |
+
'writing score': writing
|
| 84 |
+
}
|
| 85 |
|
| 86 |
+
if st.button("Predict Final Grade"):
|
| 87 |
+
with st.spinner('Calculating prediction...'):
|
| 88 |
+
predicted_grade = preprocess_and_predict(model, encoders, input_data)
|
| 89 |
+
|
| 90 |
+
st.success("Prediction Successful!")
|
| 91 |
+
st.markdown("### Predicted Letter Grade:")
|
| 92 |
+
st.markdown(f"**{predicted_grade}**")
|
| 93 |
+
|
| 94 |
+
st.info("The prediction is the most likely letter grade (O, A, B, C, D, E) based on the input features.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|