Spaces:
Sleeping
Sleeping
# Install necessary libraries | |
#!pip install gradio pandas scikit-learn joblib | |
import pandas as pd | |
import joblib | |
import gradio as gr | |
# Load the model and scaler from the binary files | |
with open('model.bin', 'rb') as file: | |
model = joblib.load(file) | |
with open('scaler.bin', 'rb') as file: | |
scaler = joblib.load(file) | |
# Define ordinal mappings and columns | |
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' | |
] | |
# Define the prediction function | |
def predict_depression_level(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 = model.predict(input_data_scaled)[0] | |
return "Your depression severity may be " + str(prediction) | |
# Define Gradio interface inputs | |
inputs = [ | |
gr.Slider(minimum=0, maximum=100, label="Age"), | |
gr.Dropdown(choices=["Male", "Female"], label="Gender"), | |
gr.Slider(minimum=0.0, maximum=4.0, 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) | |
output = gr.Textbox(label="Predicted Depression Level") | |
# Create Gradio interface | |
iface = gr.Interface(fn=predict_depression_level, inputs=inputs, outputs=output, title="SAD: Self Assessment of Depression", | |
description="A questionnaire to determine potential depression severity.") | |
iface.launch(debug=True, share=True) |