Download app.py from StudentDepressionPrediction/Student_Depression_Prediction: direct link, hf CLI and curl.
- Browser
- Download file 4.78 kB
-
https://huggingface.co/StudentDepressionPrediction/Student_Depression_Prediction/resolve/main/app.py
- Command line
-
hf download hf://StudentDepressionPrediction/Student_Depression_Prediction/app.py
-
curl -L -o app.py https://huggingface.co/StudentDepressionPrediction/Student_Depression_Prediction/resolve/main/app.py
4.78 kB
| import streamlit as st | |
| import pandas as pd | |
| import matplotlib.pyplot as plt | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.preprocessing import LabelEncoder | |
| from sklearn.ensemble import RandomForestClassifier | |
| from sklearn.metrics import accuracy_score, classification_report | |
| def load_data(): | |
| df = pd.read_csv(r"C:\Users\pavilion\Desktop\INNOMATICS_PROJECTS(SL)\Student_Depresion_Prediction(Machine_Learning)\student_depression_dataset.csv") | |
| df.drop(columns=['id', 'City', 'Work Pressure', 'Job Satisfaction'], inplace=True, errors='ignore') | |
| if 'Profession' in df.columns: | |
| df = df[df['Profession'].str.lower() == 'student'] | |
| df.drop(columns=['Profession'], inplace=True, errors='ignore') | |
| return df | |
| def preprocess_data(df): | |
| label_encoders = {} | |
| categorical_cols = df.select_dtypes(include=['object']).columns.tolist() | |
| for col in categorical_cols: | |
| le = LabelEncoder() | |
| df[col] = le.fit_transform(df[col]) | |
| label_encoders[col] = le | |
| return df, label_encoders | |
| def train_model(X_train, y_train): | |
| model = RandomForestClassifier(n_estimators=100, random_state=42) | |
| model.fit(X_train, y_train) | |
| return model | |
| def main(): | |
| st.set_page_config(page_title="Student Depression Prediction", layout="wide") | |
| st.markdown("<h1 style='text-align: center;'>๐ Student Depression Prediction</h1>", unsafe_allow_html=True) | |
| st.write("This app predicts whether a student is likely to experience depression based on various factors.") | |
| df = load_data() | |
| df, label_encoders = preprocess_data(df) | |
| X = df.drop(columns=['Depression']) | |
| y = df['Depression'] | |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y) | |
| model = train_model(X_train, y_train) | |
| y_pred = model.predict(X_test) | |
| accuracy = accuracy_score(y_test, y_pred) | |
| classification_rep = classification_report(y_test, y_pred) | |
| if st.button("๐ Train Model"): | |
| st.subheader("โ Model Trained Successfully!") | |
| st.write(f"**Accuracy:** {accuracy:.2f}") | |
| st.text("Classification Report:") | |
| st.text(classification_rep) | |
| feature_importances = model.feature_importances_ | |
| feature_importance_df = pd.DataFrame({'Feature': X.columns, 'Importance': feature_importances}).sort_values(by='Importance', ascending=False) | |
| st.subheader("๐ Feature Importance") | |
| fig, ax = plt.subplots() | |
| ax.barh(feature_importance_df['Feature'], feature_importance_df['Importance'], color='skyblue') | |
| ax.set_xlabel("Importance") | |
| ax.set_ylabel("Feature") | |
| ax.set_title("Feature Importance in Random Forest") | |
| st.pyplot(fig) | |
| st.subheader("๐ Predict Depression Risk") | |
| input_data = {} | |
| for col in X.columns: | |
| if col in label_encoders: | |
| input_data[col] = st.selectbox(f"Select {col}", label_encoders[col].classes_) | |
| input_data[col] = label_encoders[col].transform([input_data[col]])[0] | |
| else: | |
| input_data[col] = st.number_input(f"Enter {col}", min_value=int(X[col].min()), max_value=int(X[col].max()), value=int(X[col].mean())) | |
| if st.button("๐ฎ Predict"): | |
| input_df = pd.DataFrame([input_data]) | |
| prediction = model.predict(input_df)[0] | |
| if prediction == 1: | |
| st.error("๐จ **The student is at risk of depression.**") | |
| st.subheader("๐ Recommended Actions:") | |
| st.markdown(""" | |
| 1. **Seek Professional Help** ๐ฉโโ๏ธ - Consult a therapist or counselor. | |
| 2. **Stay Connected with Loved Ones** ๐ก - Talk to trusted friends or family. | |
| 3. **Engage in Physical Activities** ๐โโ๏ธ - Exercise regularly to improve mood. | |
| 4. **Practice Mindfulness & Relaxation** ๐งโโ๏ธ - Try meditation, yoga, or deep breathing. | |
| 5. **Manage Academic Stress** ๐ - Break tasks into smaller steps and seek support. | |
| """) | |
| else: | |
| st.success("๐ฅณ **The student is not at risk of depression.**") | |
| st.subheader("โ Wellness Tips:") | |
| st.markdown(""" | |
| 1. **Maintain a Balanced Lifestyle** โ๏ธ - Manage studies, social life, and self-care. | |
| 2. **Get Enough Sleep** ๐ด - Ensure 7โ9 hours of quality sleep for better mental health. | |
| 3. **Engage in Hobbies & Creative Activities** ๐จ - Pursue music, reading, or painting. | |
| 4. **Stay Active & Exercise** ๐ดโโ๏ธ - Physical activity boosts energy and reduces anxiety. | |
| 5. **Support Others & Build Strong Friendships** ๐ค - Encourage open discussions and check on friends. | |
| """) | |
| if __name__ == '__main__': | |
| main() | |