SAD / app.py
Shakirul360's picture
Create app.py
a7bcef8 verified
raw
history blame
13.7 kB
!pip install gradio pandas scikit-learn joblib
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
import gradio as gr
import joblib
# Load your dataset (assuming 'AIDA-PHQ-Updated.csv' is in the same directory as your script)
df = pd.read_csv("AIDA-PHQ-Updated.csv")
df.head()
# Ordinal mappings (if not already applied)
ordinal_mappings = {
'Not Applicable' : 0,
'Strongly disagree': 1,
'Disagree': 2,
'Neutral': 3,
'Agree': 4,
'Strongly agree': 5
}
ordinal_columns = [
'PoorAcademicPerformanceSelfPerception', 'AcademicCriticismSelfPerception',
'UnsatisfiedAcademicWorkloadSelfPerception', 'NonInterestSubjectOpinion',
'UnhappySubjectOpinion', 'NonInterestInstitutionOpinion',
'UnhappyInstitutionOpinion', 'ParentalStrictness', 'ParentalAcademicPressure',
'ParentalMarriagePressure', 'ParentalCareerPressure',
'ParentalStudyAbroadPressure', 'ParentalUnderstanding', 'SiblingBonding',
'ParentalRelationshipStability', 'PeerRelationship', 'TeacherSupport',
'PartnerRelationshipImpact', 'PhysicalViolenceExperience',
'SexualViolenceExperience', 'VerbalViolenceExperience',
'EmotionalViolenceExperience'
]
for col in ordinal_columns:
df[col] = df[col].map(ordinal_mappings)
# Encode the Gender column
from sklearn.preprocessing import LabelEncoder
encoder = LabelEncoder()
df['Gender'] = encoder.fit_transform(df['Gender'])
df.head()
# Split into X and y
X = df.drop('DepressionLevel', axis=1)
y = df['DepressionLevel']
X.columns
X.head()
# Split into train and test sets
random_state = 2024
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=random_state)
# Standardize features
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Train Logistic Regression model
logreg = LogisticRegression(C=10.0, solver='newton-cg')
logreg.fit(X_train_scaled, y_train)
# Save the model as a .bin file
with open('model.bin', 'wb') as file:
joblib.dump(logreg, file)
# Save the scaler as a .bin file
with open('scaler.bin', 'wb') as file:
joblib.dump(scaler, file)
def predict_depression_level_logreg(age, gender, cgpa, poor_academic_performance,
academic_criticism, unsatisfied_workload,
non_interest_subject, unhappy_subject,
non_interest_institution, unhappy_institution,
parental_strictness, parental_academic_pressure,
parental_marriage_pressure, parental_career_pressure,
parental_study_abroad, parental_understanding,
sibling_bonding, parental_relationship_stability,
peer_relationship, teacher_support,
partner_relationship_impact, physical_violence,
sexual_violence, verbal_violence, emotional_violence,
little_interest, feeling_down, sleeping_issue,
feeling_tired, poor_appetite, feeling_bad,
trouble_concentrating, slowness, self_harm):
# Convert gender to numeric
gender = 1 if gender == 'Female' else 0
# Define feature names matching those used during fitting
feature_names = [
'Age', 'Gender', 'CGPA', 'PoorAcademicPerformanceSelfPerception', 'AcademicCriticismSelfPerception',
'UnsatisfiedAcademicWorkloadSelfPerception', 'NonInterestSubjectOpinion', 'UnhappySubjectOpinion',
'NonInterestInstitutionOpinion', 'UnhappyInstitutionOpinion', 'ParentalStrictness', 'ParentalAcademicPressure',
'ParentalMarriagePressure', 'ParentalCareerPressure', 'ParentalStudyAbroadPressure', 'ParentalUnderstanding',
'SiblingBonding', 'ParentalRelationshipStability', 'PeerRelationship', 'TeacherSupport',
'PartnerRelationshipImpact', 'PhysicalViolenceExperience', 'SexualViolenceExperience', 'VerbalViolenceExperience',
'EmotionalViolenceExperience', 'little interest', 'feeling down', 'Sleeping issue', 'feeling tired',
'poor appetite', 'feeling bad', 'trouble concertrating', 'slowness', 'self harm'
]
# Map ordinal columns to numerical values using ordinal_mappings
input_data = pd.DataFrame([[age, gender, cgpa, ordinal_mappings[poor_academic_performance],
ordinal_mappings[academic_criticism], ordinal_mappings[unsatisfied_workload],
ordinal_mappings[non_interest_subject], ordinal_mappings[unhappy_subject],
ordinal_mappings[non_interest_institution], ordinal_mappings[unhappy_institution],
ordinal_mappings[parental_strictness], ordinal_mappings[parental_academic_pressure],
ordinal_mappings[parental_marriage_pressure], ordinal_mappings[parental_career_pressure],
ordinal_mappings[parental_study_abroad], ordinal_mappings[parental_understanding],
ordinal_mappings[sibling_bonding], ordinal_mappings[parental_relationship_stability],
ordinal_mappings[peer_relationship], ordinal_mappings[teacher_support],
ordinal_mappings[partner_relationship_impact], ordinal_mappings[physical_violence],
ordinal_mappings[sexual_violence], ordinal_mappings[verbal_violence], ordinal_mappings[emotional_violence],
little_interest, feeling_down, sleeping_issue, feeling_tired,
poor_appetite, feeling_bad, trouble_concentrating, slowness, self_harm]],
columns=feature_names)
input_data_scaled = scaler.transform(input_data)
prediction = logreg.predict(input_data_scaled)[0]
return str(prediction)
# Define Gradio interface with proper input labels and output type
inputs = [
gr.Slider(minimum=int(df['Age'].min()), maximum=int(df['Age'].max()), label="Age"),
gr.Dropdown(choices=["Male", "Female"], label="Gender"),
gr.Slider(minimum=float(df['CGPA'].min()), maximum=float(df['CGPA'].max()), label="CGPA")
]
# Updated labels for ordinal columns
ordinal_labels = {
'PoorAcademicPerformanceSelfPerception': 'Your Academic Performance is poor.',
'AcademicCriticismSelfPerception': 'You experience Academic Criticism.',
'UnsatisfiedAcademicWorkloadSelfPerception': 'You are unsatisfied with your academic workload.',
'NonInterestSubjectOpinion': 'The subject you are studying is of non-interest to you.',
'UnhappySubjectOpinion': 'You are unhappy with the subject you are studying.',
'NonInterestInstitutionOpinion': 'You study at an institution of your non-interest.',
'UnhappyInstitutionOpinion': 'You are Unhappy with your institution.',
'ParentalStrictness': 'Your parents are strict.',
'ParentalAcademicPressure': 'You experience academic pressure from your parents.',
'ParentalMarriagePressure': 'You experience pressure to get married from your parents.',
'ParentalCareerPressure': 'You experience career pressure from your parents.',
'ParentalStudyAbroadPressure': 'You experience pressure to study abroad from your parents.',
'ParentalUnderstanding': 'Your have poor understanding with your parents.',
'SiblingBonding': 'You have poor bonding with your siblings.',
'ParentalRelationshipStability': 'Your parents have unstable relationship.',
'PeerRelationship': 'You have poor relationship with your peers.',
'TeacherSupport': 'Teachers do not support you.',
'PartnerRelationshipImpact': 'You have poor relationship with your partner.',
'PhysicalViolenceExperience': 'You have experience physical violence.',
'SexualViolenceExperience': 'You have experience sexual violence.',
'VerbalViolenceExperience': 'You have experience verbal violence.',
'EmotionalViolenceExperience': 'You have experienced emotional violence.',
}
# Add radio buttons for ordinal columns with updated labels
for col in ordinal_columns:
inputs.append(gr.Radio(choices=list(ordinal_mappings.keys()), label=ordinal_labels[col]))
# Add sliders for the remaining inputs
additional_inputs = [
gr.Slider(minimum=0, maximum=5, step=1, label="How has your interest changed over work and activities? (0= No change)"),
gr.Slider(minimum=0, maximum=5, step=1, label="How often do you feel down?"),
gr.Slider(minimum=0, maximum=5, step=1, label="Do you struggle to sleep?"),
gr.Slider(minimum=0, maximum=5, step=1, label="How often do you feel tired?"),
gr.Slider(minimum=0, maximum=5, step=1, label="How has your appetite changed?"),
gr.Slider(minimum=0, maximum=5, step=1, label="How often do you feel bad about yourself?"),
gr.Slider(minimum=0, maximum=5, step=1, label="How has your concentration levels changed?"),
gr.Slider(minimum=0, maximum=5, step=1, label="Do you feel slow?"),
gr.Slider(minimum=0, maximum=5, step=1, label="Have you had suicidal thoughts?")
]
inputs.extend(additional_inputs)
# # Sample input values (these should match the features and their order in your function)
# sample_input = {
# "Age": 25,
# "Gender": "Female", # Assuming string value for Gender
# "CGPA": 3.5,
# "PoorAcademicPerformanceSelfPerception": "Disagree", # String value, needs mapping
# "AcademicCriticismSelfPerception": "Neutral", # String value, needs mapping
# "UnsatisfiedAcademicWorkloadSelfPerception": "Strongly disagree", # String value, needs mapping
# "NonInterestSubjectOpinion": "Disagree", # String value, needs mapping
# "UnhappySubjectOpinion": "Neutral", # String value, needs mapping
# "NonInterestInstitutionOpinion": "Strongly disagree", # String value, needs mapping
# "UnhappyInstitutionOpinion": "Disagree", # String value, needs mapping
# "ParentalStrictness": "Strongly agree", # String value, needs mapping
# "ParentalAcademicPressure": "Agree", # String value, needs mapping
# "ParentalMarriagePressure": "Neutral", # String value, needs mapping
# "ParentalCareerPressure": "Strongly disagree", # String value, needs mapping
# "ParentalStudyAbroadPressure": "Neutral", # String value, needs mapping
# "ParentalUnderstanding": "Strongly agree", # String value, needs mapping
# "SiblingBonding": "Strongly agree", # String value, needs mapping
# "ParentalRelationshipStability": "Neutral", # String value, needs mapping
# "PeerRelationship": "Agree", # String value, needs mapping
# "TeacherSupport": "Strongly agree", # String value, needs mapping
# "PartnerRelationshipImpact": "Neutral", # String value, needs mapping
# "PhysicalViolenceExperience": 0,
# "SexualViolenceExperience": 1,
# "VerbalViolenceExperience": 0,
# "EmotionalViolenceExperience": 2,
# "little interest": 1,
# "feeling down": 3,
# "Sleeping issue": 2,
# "feeling tired": 4,
# "poor appetite": 2,
# "feeling bad": 3,
# "trouble concertrating": 1,
# "slowness": 2,
# "self harm": 0
# }
# # Convert the sample input dictionary to a DataFrame
# sample_df = pd.DataFrame([sample_input])
# # Map ordinal columns to numerical values using ordinal_mappings
# for col in ordinal_columns:
# sample_df[col] = sample_df[col].map(ordinal_mappings)
# # Convert gender to numeric
# sample_df['Gender'] = 1 if sample_df['Gender'].iloc[0] == 'Female' else 0
# # Scale the input data using the scaler from your model
# sample_df_scaled = scaler.transform(sample_df)
# # Predict using your function
# prediction = predict_depression_level_logreg(*sample_df_scaled[0])
# # Print or use the prediction as needed
# print("Predicted Depression Level:", prediction)
output = gr.Textbox(label="Predicted Depression Level")
# Create Gradio interface
iface = gr.Interface(fn=predict_depression_level_logreg, inputs=inputs, outputs=output, title="Understand your Depression Levels",
description="A questionnaire to determine potential depression severity using the questions below - ")
iface.launch(debug=True, share=True)
# #importing libraries
# import numpy as np
# import lime
# # import lime.lime_tabular
# from lime import lime_tabular
# # Training Logistic Regression classifier
# model = LogisticRegression()
# model.fit(X_train, y_train)
# # Defining the lime explainer
# explainer = lime_tabular.LimeTabularExplainer(
# training_data=np.array(X_train),
# feature_names=X_train.columns,
# class_names=['MINIMAL','MILD','Moderate','ModeratelySevere','Severe'],
# mode='classification'
# )
# #importing libraries
# import numpy as np
# import lime
# # import lime.lime_tabular
# from lime import lime_tabular
# # Training Logistic Regression classifier
# model = LogisticRegression()
# model.fit(X_train, y_train)
# # Defining the lime explainer
# explainer = lime_tabular.LimeTabularExplainer(
# training_data=np.array(X_train),
# feature_names=X_train.columns,
# class_names=['MINIMAL','MILD','Moderate','ModeratelySevere','Severe'],
# mode='classification'
# )
# exp = explainer.explain_instance(
# data_row=X_test.iloc[5],
# predict_fn=model.predict_proba, num_features = 12
# )
# exp.show_in_notebook(show_table=True)