File size: 8,578 Bytes
37d9988 8fde1ac a7bcef8 37d9988 a7bcef8 37d9988 a7bcef8 37d9988 a7bcef8 37d9988 a7bcef8 37d9988 a7bcef8 37d9988 a7bcef8 37d9988 a7bcef8 37d9988 a7bcef8 37d9988 a7bcef8 37d9988 a7bcef8 37d9988 a7bcef8 37d9988 a7bcef8 37d9988 a7bcef8 37d9988 a7bcef8 37d9988 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# 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="Understand your Depression Levels",
description="A questionnaire to determine potential depression severity using the questions below - ")
iface.launch(debug=True, share=True) |