Commit
·
03bc5ff
0
Parent(s):
Initial commit with model and application files
Browse files- .gitattributes +1 -0
- app.py +223 -0
- model.keras +3 -0
- requirements.txt +4 -0
.gitattributes
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
*.keras filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,223 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import tensorflow as tf
|
3 |
+
import pandas as pd
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
# Load the model
|
7 |
+
# Load the model dynamically from the current directory
|
8 |
+
model_path = "model.keras"
|
9 |
+
|
10 |
+
try:
|
11 |
+
model = tf.keras.models.load_model(model_path)
|
12 |
+
print(f"Model loaded successfully from: {model_path}")
|
13 |
+
except Exception as e:
|
14 |
+
raise RuntimeError(f"Failed to load the model from {model_path}: {e}")
|
15 |
+
|
16 |
+
|
17 |
+
# Define the risk classes
|
18 |
+
classes = ['No Immediate Risk', 'Medium Risk of Harm - Needs Monitoring', 'Immediate Concern - Needs Urgent Support']
|
19 |
+
|
20 |
+
# Yes/No mapping for questions
|
21 |
+
yes_no_mapping = {
|
22 |
+
'q1': {'yes': 0.1, 'no': 0}, 'q2': {'yes': 0.1, 'no': 0}, 'q3': {'yes': 0.1, 'no': 0},
|
23 |
+
'q4': {'yes': 0.1, 'no': 0}, 'q5': {'yes': 0.1, 'no': 0}, 'q6': {'yes': 0.1, 'no': 0},
|
24 |
+
'q7': {'yes': 0.1, 'no': 0}, 'q8': {'yes': 0.1, 'no': 0}, 'q9': {'yes': 0.1, 'no': 0},
|
25 |
+
'q10': {'yes': 0.1, 'no': 0}, 'q11': {'yes': 0.5, 'no': 0}, 'q12': {'yes': 0.7, 'no': 0},
|
26 |
+
'q13': {'yes': 0.5, 'no': 0}, 'q14': {'yes': 0.5, 'no': 0}, 'q15': {'yes': 0.5, 'no': 0},
|
27 |
+
'q16': {'yes': 0.5, 'no': 0}, 'q17': {'yes': 0.7, 'no': 0}, 'q18': {'yes': 0.7, 'no': 0},
|
28 |
+
'q19': {'yes': 0.7, 'no': 0}, 'q20': {'yes': 0.7, 'no': 0}, 'q21': {'yes': 0.7, 'no': 0},
|
29 |
+
'q22': {'yes': 0.7, 'no': 0}, 'q23': {'yes': 0.7, 'no': 0}, 'q24': {'yes': 0.7, 'no': 0},
|
30 |
+
'q25': {'yes': 0.7, 'no': 0}, 'q26': {'yes': 0.7, 'no': 0}, 'q27': {'yes': 0.9, 'no': 0},
|
31 |
+
'q28': {'yes': 0.9, 'no': 0}, 'q29': {'yes': 0.9, 'no': 0}, 'q30': {'yes': 0.9, 'no': 0},
|
32 |
+
'q31': {'yes': 1, 'no': 0}, 'q32': {'yes': 1, 'no': 0}, 'q33': {'yes': 1, 'no': 0}
|
33 |
+
}
|
34 |
+
|
35 |
+
# Store responses in a DataFrame
|
36 |
+
responses_df = pd.DataFrame(columns=["Name", "ID", "Predicted Risk Level"])
|
37 |
+
|
38 |
+
# Function to process form input
|
39 |
+
def predict_from_form(name, id_number, *responses):
|
40 |
+
global responses_df
|
41 |
+
try:
|
42 |
+
# Map Yes/No to numeric
|
43 |
+
processed_data = [yes_no_mapping[f'q{i+1}'][response.lower()] for i, response in enumerate(responses)]
|
44 |
+
input_data = pd.DataFrame([processed_data], columns=[f'q{i+1}' for i in range(len(responses))])
|
45 |
+
|
46 |
+
# Predict using model
|
47 |
+
predictions = model.predict(input_data)
|
48 |
+
predicted_class = predictions.argmax(axis=1)[0]
|
49 |
+
|
50 |
+
# Store the result
|
51 |
+
new_entry = {"Name": name or "N/A", "ID": id_number or "N/A", "Predicted Risk Level": classes[predicted_class]}
|
52 |
+
responses_df = pd.concat([responses_df, pd.DataFrame([new_entry])], ignore_index=True)
|
53 |
+
|
54 |
+
return f"Prediction for {name or 'Anonymous'}: {classes[predicted_class]}"
|
55 |
+
except Exception as e:
|
56 |
+
return f"Error: {e}"
|
57 |
+
|
58 |
+
# Function to process uploaded CSV file
|
59 |
+
def predict_risk_from_csv(file):
|
60 |
+
try:
|
61 |
+
# Load the CSV file
|
62 |
+
form_data = pd.read_csv(file.name)
|
63 |
+
|
64 |
+
# Clean column names: Remove trailing spaces, parentheses, etc.
|
65 |
+
cleaned_columns = [col.split('(')[0].strip() for col in form_data.columns]
|
66 |
+
form_data.columns = cleaned_columns
|
67 |
+
|
68 |
+
# Standardize "Yes"/"No" answers: Strip spaces and standardize case
|
69 |
+
form_data_cleaned = form_data.apply(
|
70 |
+
lambda x: x.str.strip().str.lower() if x.dtype == "object" else x
|
71 |
+
)
|
72 |
+
|
73 |
+
# Remove unnecessary columns (optional, can be adjusted as per the CSV format)
|
74 |
+
form_data_cleaned.drop(columns=["Timestamp", "Full Name", "Preparing for"], inplace=True, errors='ignore')
|
75 |
+
|
76 |
+
# Rename questions to match model input: Mapping column names to q1, q2, ..., q33
|
77 |
+
simplified_columns = {
|
78 |
+
'Is your age is less than 18 years?': 'q1',
|
79 |
+
'Are you Male': 'q2',
|
80 |
+
'Have financial concerns been a source of stress for you or your family?': 'q3',
|
81 |
+
'Are you currently living alone, and unsatisfied with your living situation?': 'q4',
|
82 |
+
'Are you living in Hostel and unsatisfied with your living situation?': 'q5',
|
83 |
+
'Do you feel like you are studying here mainly because of your family’s expectations?': 'q6',
|
84 |
+
'Have you been feeling disappointed with your academic performance recently?': 'q7',
|
85 |
+
'Do you feel like your parents are unhappy with your academic performance and you are letting them down?': 'q8',
|
86 |
+
'Are there are several problems in your family or your family environment is very stressful?': 'q9',
|
87 |
+
'Your family does not support your decisions or your choices?': 'q10',
|
88 |
+
'Have you recently experienced a breakup': 'q11',
|
89 |
+
'Have you ever suffered from any mental illness in past or are currently suffering': 'q12',
|
90 |
+
'Is there any history of psychiatric illness in your family?': 'q13',
|
91 |
+
'Suicide committed by any close family member recently or in past': 'q14',
|
92 |
+
'Suicide committed by any close friend recently': 'q15',
|
93 |
+
'Suffering from any medical illness like Diabetes or Asthma or Hypertension or any other chronic illness': 'q16',
|
94 |
+
'Have you found yourself using alcohol, drugs, or online gaming to cope with stress or emotions recently?': 'q17',
|
95 |
+
'Have you ever experienced any form of abuse': 'q18',
|
96 |
+
'Have you been feeling emotionally numb or disconnected over the past few days?': 'q19',
|
97 |
+
'Are you feeling very sad or very anxious since last few days': 'q20',
|
98 |
+
'Have you been feeling hurt or embarrassed by things others have said or done recently?': 'q21',
|
99 |
+
'Have you been feeling trapped in your current situation, and do you need support to find a way out?': 'q22',
|
100 |
+
'Have you been feeling particularly lonely over the past few days?': 'q23',
|
101 |
+
'Have you been feeling uncertain or hopeless about your future?': 'q24',
|
102 |
+
'Have you lost interest in things you usually enjoy over the past few days?': 'q25',
|
103 |
+
'Have you been struggling with sleep recently, either finding it hard to fall asleep or stay asleep?': 'q26',
|
104 |
+
'Have thoughts about hurting yourself crossed your mind?': 'q27',
|
105 |
+
'Have you felt like life has been overwhelming recently?': 'q28',
|
106 |
+
'Have you been feeling a lack of motivation or desire to keep going in life ?': 'q29',
|
107 |
+
'Have you been feeling like hurting yourself recently?': 'q30',
|
108 |
+
'Have you been searching online for information related to suicide?': 'q31',
|
109 |
+
'Have you been feeling so overwhelmed or distressed that you’ve considered or planned how to end your life?': 'q32',
|
110 |
+
'Have you previously had moments where you tried to harm yourself or end your life?': 'q33'
|
111 |
+
}
|
112 |
+
|
113 |
+
form_data_cleaned.rename(columns=simplified_columns, inplace=True)
|
114 |
+
|
115 |
+
# Apply Yes/No mapping
|
116 |
+
for col, mapping in yes_no_mapping.items():
|
117 |
+
if col in form_data_cleaned.columns:
|
118 |
+
form_data_cleaned[col] = form_data_cleaned[col].map(mapping)
|
119 |
+
|
120 |
+
# Convert any non-numeric values to NaN
|
121 |
+
form_data_cleaned = form_data_cleaned.apply(pd.to_numeric, errors='coerce')
|
122 |
+
|
123 |
+
# Drop rows with missing data
|
124 |
+
form_data_cleaned.dropna(inplace=True)
|
125 |
+
|
126 |
+
# Ensure valid rows exist after cleaning
|
127 |
+
if not form_data_cleaned.empty:
|
128 |
+
predictions = model.predict(form_data_cleaned)
|
129 |
+
predicted_classes = predictions.argmax(axis=1)
|
130 |
+
|
131 |
+
# Add predicted risk category back to the original data
|
132 |
+
form_data['Predicted Risk Category'] = [classes[i] for i in predicted_classes]
|
133 |
+
|
134 |
+
# Return relevant columns: Full Name (if exists) and Predictions
|
135 |
+
if 'Full Name' in form_data.columns:
|
136 |
+
return form_data[['Full Name', 'Predicted Risk Category']]
|
137 |
+
else:
|
138 |
+
return form_data[['Predicted Risk Category']]
|
139 |
+
else:
|
140 |
+
return "No valid data to process after cleaning. Please check the file format."
|
141 |
+
|
142 |
+
except Exception as e:
|
143 |
+
return f"Error processing the file: {str(e)}"
|
144 |
+
|
145 |
+
|
146 |
+
# Function to view all recorded responses
|
147 |
+
def view_responses():
|
148 |
+
if responses_df.empty:
|
149 |
+
return "No responses recorded yet."
|
150 |
+
return responses_df
|
151 |
+
|
152 |
+
# Define Gradio Inputs
|
153 |
+
form_inputs = [
|
154 |
+
gr.Textbox(label="Name (Optional)"), gr.Textbox(label="ID (Optional)")
|
155 |
+
]
|
156 |
+
form_inputs.extend([
|
157 |
+
gr.Radio(["Yes", "No"], label="Is your age less than 18 years?"), # q1
|
158 |
+
gr.Radio(["Yes", "No"], label="Are you Male?"), # q2
|
159 |
+
gr.Radio(["Yes", "No"], label="Have financial concerns been a source of stress for you or your family?"), # q3
|
160 |
+
gr.Radio(["Yes", "No"], label="Are you currently living alone, and unsatisfied with your living situation?"), # q4
|
161 |
+
gr.Radio(["Yes", "No"], label="Are you living in Hostel and unsatisfied with your living situation?"), # q5
|
162 |
+
gr.Radio(["Yes", "No"], label="Do you feel like you are studying here mainly because of your family’s expectations?"), # q6
|
163 |
+
gr.Radio(["Yes", "No"], label="Have you been feeling disappointed with your academic performance recently?"), # q7
|
164 |
+
gr.Radio(["Yes", "No"], label="Do you feel like your parents are unhappy with your academic performance and you are letting them down?"), # q8
|
165 |
+
gr.Radio(["Yes", "No"], label="Are there are several problems in your family or your family environment is very stressful?"), # q9
|
166 |
+
gr.Radio(["Yes", "No"], label="Your family does not support your decisions or your choices?"), # q10
|
167 |
+
gr.Radio(["Yes", "No"], label="Have you recently experienced a breakup?"), # q11
|
168 |
+
gr.Radio(["Yes", "No"], label="Have you ever suffered from any mental illness in past or are currently suffering?"), # q12
|
169 |
+
gr.Radio(["Yes", "No"], label="Is there any history of psychiatric illness in your family?"), # q13
|
170 |
+
gr.Radio(["Yes", "No"], label="Has any close family member recently or in the past committed suicide?"), # q14
|
171 |
+
gr.Radio(["Yes", "No"], label="Has any close friend recently committed suicide?"), # q15
|
172 |
+
gr.Radio(["Yes", "No"], label="Are you suffering from any medical illness like Diabetes, Asthma, Hypertension, or other chronic illness?"), # q16
|
173 |
+
gr.Radio(["Yes", "No"], label="Have you found yourself using alcohol, drugs, or online gaming to cope with stress recently?"), # q17
|
174 |
+
gr.Radio(["Yes", "No"], label="Have you ever experienced any form of abuse?"), # q18
|
175 |
+
gr.Radio(["Yes", "No"], label="Have you been feeling emotionally numb or disconnected over the past few days?"), # q19
|
176 |
+
gr.Radio(["Yes", "No"], label="Are you feeling very sad or anxious since the last few days?"), # q20
|
177 |
+
gr.Radio(["Yes", "No"], label="Have you been feeling hurt or embarrassed by things others have said recently?"), # q21
|
178 |
+
gr.Radio(["Yes", "No"], label="Have you been feeling trapped in your current situation and need support to find a way out?"), # q22
|
179 |
+
gr.Radio(["Yes", "No"], label="Have you been feeling particularly lonely over the past few days?"), # q23
|
180 |
+
gr.Radio(["Yes", "No"], label="Have you been feeling uncertain or hopeless about your future?"), # q24
|
181 |
+
gr.Radio(["Yes", "No"], label="Have you lost interest in things you usually enjoy over the past few days?"), # q25
|
182 |
+
gr.Radio(["Yes", "No"], label="Have you been struggling with sleep recently, either finding it hard to fall asleep or stay asleep?"), # q26
|
183 |
+
gr.Radio(["Yes", "No"], label="Have thoughts about hurting yourself crossed your mind?"), # q27
|
184 |
+
gr.Radio(["Yes", "No"], label="Have you felt like life has been overwhelming recently?"), # q28
|
185 |
+
gr.Radio(["Yes", "No"], label="Have you been feeling a lack of motivation or desire to keep going in life?"), # q29
|
186 |
+
gr.Radio(["Yes", "No"], label="Have you been feeling like hurting yourself recently?"), # q30
|
187 |
+
gr.Radio(["Yes", "No"], label="Have you been searching online for information related to suicide?"), # q31
|
188 |
+
gr.Radio(["Yes", "No"], label="Have you been feeling so distressed that you’ve considered or planned how to end your life?"), # q32
|
189 |
+
gr.Radio(["Yes", "No"], label="Have you previously had moments where you tried to harm yourself or end your life?") # q33
|
190 |
+
])
|
191 |
+
|
192 |
+
|
193 |
+
# Gradio Interface
|
194 |
+
interface = gr.TabbedInterface(
|
195 |
+
[
|
196 |
+
gr.Interface(
|
197 |
+
fn=predict_from_form,
|
198 |
+
inputs=form_inputs,
|
199 |
+
outputs="text",
|
200 |
+
title="Manual Form Submission",
|
201 |
+
description="Submit your responses manually."
|
202 |
+
),
|
203 |
+
gr.Interface(
|
204 |
+
fn=predict_risk_from_csv,
|
205 |
+
inputs=gr.File(label="Upload CSV File"),
|
206 |
+
outputs="dataframe",
|
207 |
+
title="Upload CSV File",
|
208 |
+
description="Upload a Google Form CSV file for bulk predictions."
|
209 |
+
),
|
210 |
+
gr.Interface(
|
211 |
+
fn=view_responses,
|
212 |
+
inputs=None,
|
213 |
+
outputs="dataframe",
|
214 |
+
title="View All Responses",
|
215 |
+
description="View all the recorded responses so far."
|
216 |
+
)
|
217 |
+
],
|
218 |
+
["Manual Form Submission", "Upload CSV File", "View All Responses"]
|
219 |
+
)
|
220 |
+
|
221 |
+
# Launch the Gradio app
|
222 |
+
if __name__ == "__main__":
|
223 |
+
interface.launch(share=False, inbrowser=True)
|
model.keras
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:23fcfce21eeb6f07d4a9e48417cec80da9b823a320a2fb06c713cd3e207fef18
|
3 |
+
size 265377
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
tensorflow==2.18.0
|
2 |
+
pandas
|
3 |
+
gradio
|
4 |
+
numpy
|