Upload folder using huggingface_hub
Browse files- README.md +14 -3
- app.py +92 -0
- index.html +224 -0
- le_gender_final.pkl +3 -0
- le_target_final.pkl +3 -0
- mental_health_model_final.pkl +3 -0
- requirements.txt +0 -0
- scaler_final.pkl +3 -0
- upload_to_hf.py +16 -0
README.md
CHANGED
@@ -1,3 +1,14 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Mental Health Prediction Model
|
2 |
+
## Overview
|
3 |
+
This project predicts mental health status (Low, Moderate, High) using a Flask app and an XGBoost model.
|
4 |
+
## Files
|
5 |
+
- app.py: Flask app for predictions
|
6 |
+
- index.html: Web interface
|
7 |
+
- mental_health_model_final.pkl: Trained XGBoost model
|
8 |
+
- scaler_final.pkl: Scaler for preprocessing
|
9 |
+
- le_gender_final.pkl: Label encoder for Gender
|
10 |
+
- le_target_final.pkl: Label encoder for target
|
11 |
+
## How to Use
|
12 |
+
1. Install dependencies: `pip install -r requirements.txt`
|
13 |
+
2. Run the app: `python app.py`
|
14 |
+
3. Access at `http://localhost:5000`
|
app.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify, send_from_directory
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import joblib
|
5 |
+
import os
|
6 |
+
|
7 |
+
app = Flask(__name__)
|
8 |
+
|
9 |
+
# Load the model and preprocessing objects
|
10 |
+
model = joblib.load('mental_health_model_final.pkl')
|
11 |
+
scaler = joblib.load('scaler_final.pkl')
|
12 |
+
le_gender = joblib.load('le_gender_final.pkl')
|
13 |
+
le_target = joblib.load('le_target_final.pkl')
|
14 |
+
|
15 |
+
# Print feature importance
|
16 |
+
columns = ['Sentiment_Score', 'HRV', 'Sleep_Hours', 'Activity', 'Age', 'Gender', 'Work_Study_Hours']
|
17 |
+
print("Feature Importance:")
|
18 |
+
for feature, importance in zip(columns, model.feature_importances_):
|
19 |
+
print(f"{feature}: {importance:.4f}")
|
20 |
+
|
21 |
+
# Serve the HTML file
|
22 |
+
@app.route('/')
|
23 |
+
def serve_html():
|
24 |
+
return send_from_directory('.', 'index.html')
|
25 |
+
|
26 |
+
@app.route('/predict', methods=['POST'])
|
27 |
+
def predict():
|
28 |
+
try:
|
29 |
+
# Get the input data from the request
|
30 |
+
data = request.get_json()
|
31 |
+
|
32 |
+
# Convert to DataFrame
|
33 |
+
columns = ['Sentiment_Score', 'HRV', 'Sleep_Hours', 'Activity', 'Age', 'Gender', 'Work_Study_Hours']
|
34 |
+
new_data = pd.DataFrame([data], columns=columns)
|
35 |
+
|
36 |
+
# Validate input
|
37 |
+
if new_data['Sleep_Hours'].iloc[0] < 0:
|
38 |
+
return jsonify({'error': 'Sleep_Hours cannot be negative'}), 400
|
39 |
+
if new_data['Work_Study_Hours'].iloc[0] < 0:
|
40 |
+
return jsonify({'error': 'Work_Study_Hours cannot be negative'}), 400
|
41 |
+
if new_data['Gender'].iloc[0] not in ['Male', 'Female']:
|
42 |
+
return jsonify({'error': 'Gender must be Male or Female'}), 400
|
43 |
+
|
44 |
+
# Preprocess the data
|
45 |
+
new_data['Gender'] = le_gender.transform(new_data['Gender'])
|
46 |
+
new_data_scaled = scaler.transform(new_data)
|
47 |
+
|
48 |
+
# Predict
|
49 |
+
prediction = model.predict(new_data_scaled)[0]
|
50 |
+
probs = model.predict_proba(new_data_scaled)[0]
|
51 |
+
health_status = {0: "Low", 1: "Moderate", 2: "High"}
|
52 |
+
result = health_status[prediction]
|
53 |
+
|
54 |
+
# Include probabilities in the response
|
55 |
+
prob_dict = {health_status[i]: float(prob) for i, prob in enumerate(probs)}
|
56 |
+
|
57 |
+
# Add a disclaimer for borderline predictions
|
58 |
+
max_prob = probs.max()
|
59 |
+
disclaimer = ""
|
60 |
+
if max_prob < 0.7:
|
61 |
+
disclaimer = "This prediction is uncertain (confidence below 70%). Please consult a professional for an accurate assessment."
|
62 |
+
|
63 |
+
# Add a simple chatbot-like message
|
64 |
+
if result == "Low":
|
65 |
+
chatbot_message = "It looks like you might be experiencing low mental health. Consider reaching out to a friend or professional for support."
|
66 |
+
elif result == "Moderate":
|
67 |
+
chatbot_message = "Your mental health seems moderate. Keep up with self-care practices, and consider talking to someone if you feel overwhelmed."
|
68 |
+
else: # High
|
69 |
+
chatbot_message = "Great news! Your mental health appears to be high. Keep maintaining your healthy habits!"
|
70 |
+
|
71 |
+
return jsonify({
|
72 |
+
'prediction': result,
|
73 |
+
'probabilities': prob_dict,
|
74 |
+
'disclaimer': disclaimer,
|
75 |
+
'model_accuracy': 'This model has a cross-validation accuracy of 76.8%.',
|
76 |
+
'chatbot_message': chatbot_message
|
77 |
+
})
|
78 |
+
except Exception as e:
|
79 |
+
return jsonify({'error': str(e)}), 400
|
80 |
+
|
81 |
+
@app.route('/feedback', methods=['POST'])
|
82 |
+
def feedback():
|
83 |
+
try:
|
84 |
+
feedback_data = request.get_json()
|
85 |
+
with open('feedback.log', 'a') as f:
|
86 |
+
f.write(f"Prediction: {feedback_data['prediction']}, Accurate: {feedback_data['accurate']}\n")
|
87 |
+
return jsonify({'message': 'Feedback received'})
|
88 |
+
except Exception as e:
|
89 |
+
return jsonify({'error': str(e)}), 400
|
90 |
+
|
91 |
+
if __name__ == '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health Prediction</title>
|
5 |
+
<style>
|
6 |
+
body {
|
7 |
+
font-family: Arial, sans-serif;
|
8 |
+
margin: 0;
|
9 |
+
padding: 0;
|
10 |
+
display: flex;
|
11 |
+
justify-content: center;
|
12 |
+
align-items: center;
|
13 |
+
height: 100vh;
|
14 |
+
background: linear-gradient(to right, #36d1dc, #5b86e5);
|
15 |
+
animation: fadeIn 1s ease-in-out;
|
16 |
+
}
|
17 |
+
@keyframes fadeIn {
|
18 |
+
from { opacity: 0; }
|
19 |
+
to { opacity: 1; }
|
20 |
+
}
|
21 |
+
.container {
|
22 |
+
background: white;
|
23 |
+
padding: 20px;
|
24 |
+
border-radius: 10px;
|
25 |
+
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
|
26 |
+
text-align: center;
|
27 |
+
width: 400px;
|
28 |
+
animation: slideUp 0.8s ease-in-out;
|
29 |
+
}
|
30 |
+
@keyframes slideUp {
|
31 |
+
from { transform: translateY(30px); opacity: 0; }
|
32 |
+
to { transform: translateY(0); opacity: 1; }
|
33 |
+
}
|
34 |
+
h2 {
|
35 |
+
color: #333;
|
36 |
+
}
|
37 |
+
label {
|
38 |
+
display: block;
|
39 |
+
text-align: left;
|
40 |
+
margin: 5px 0 2px;
|
41 |
+
color: #555;
|
42 |
+
}
|
43 |
+
input, select, button {
|
44 |
+
width: 100%;
|
45 |
+
padding: 10px;
|
46 |
+
margin: 5px 0 10px;
|
47 |
+
border: 1px solid #ccc;
|
48 |
+
border-radius: 5px;
|
49 |
+
transition: 0.3s;
|
50 |
+
box-sizing: border-box;
|
51 |
+
}
|
52 |
+
input:focus, select:focus {
|
53 |
+
border-color: #5b86e5;
|
54 |
+
box-shadow: 0 0 8px rgba(91, 134, 229, 0.6);
|
55 |
+
}
|
56 |
+
button {
|
57 |
+
background: #007bff;
|
58 |
+
color: white;
|
59 |
+
cursor: pointer;
|
60 |
+
border: none;
|
61 |
+
font-weight: bold;
|
62 |
+
transition: 0.3s;
|
63 |
+
}
|
64 |
+
button:hover {
|
65 |
+
background: #0056b3;
|
66 |
+
transform: scale(1.05);
|
67 |
+
}
|
68 |
+
#result, #probabilities, #disclaimer, #model-accuracy {
|
69 |
+
margin-top: 10px;
|
70 |
+
color: #333;
|
71 |
+
}
|
72 |
+
#result {
|
73 |
+
font-weight: bold;
|
74 |
+
color: #007bff;
|
75 |
+
}
|
76 |
+
#probabilities table {
|
77 |
+
width: 100%;
|
78 |
+
margin-top: 10px;
|
79 |
+
border-collapse: collapse;
|
80 |
+
}
|
81 |
+
#probabilities th, #probabilities td {
|
82 |
+
padding: 5px;
|
83 |
+
border: 1px solid #ccc;
|
84 |
+
text-align: center;
|
85 |
+
}
|
86 |
+
#probabilities th {
|
87 |
+
background: #f0f0f0;
|
88 |
+
}
|
89 |
+
#disclaimer {
|
90 |
+
font-style: italic;
|
91 |
+
color: #e74c3c;
|
92 |
+
}
|
93 |
+
#model-accuracy {
|
94 |
+
font-size: 0.9em;
|
95 |
+
color: #555;
|
96 |
+
}
|
97 |
+
</style>
|
98 |
+
<script>
|
99 |
+
function predictMentalHealth() {
|
100 |
+
// Collect form data
|
101 |
+
const form = document.getElementById('predictionForm');
|
102 |
+
const formData = new FormData(form);
|
103 |
+
|
104 |
+
// Convert form data to JSON
|
105 |
+
const data = {
|
106 |
+
Sentiment_Score: parseFloat(formData.get('Sentiment_Score')),
|
107 |
+
HRV: parseFloat(formData.get('HRV')),
|
108 |
+
Sleep_Hours: parseFloat(formData.get('Sleep_Hours')),
|
109 |
+
Activity: parseInt(formData.get('Activity')),
|
110 |
+
Age: parseInt(formData.get('Age')),
|
111 |
+
Gender: formData.get('Gender'),
|
112 |
+
Work_Study_Hours: parseFloat(formData.get('Work_Study_Hours'))
|
113 |
+
};
|
114 |
+
|
115 |
+
// Validate input
|
116 |
+
if (isNaN(data.Sentiment_Score) || data.Sentiment_Score < 0 || data.Sentiment_Score > 1) {
|
117 |
+
document.getElementById('result').innerText = 'Error: Sentiment Score must be between 0 and 1';
|
118 |
+
return;
|
119 |
+
}
|
120 |
+
if (isNaN(data.HRV) || data.HRV < 0) {
|
121 |
+
document.getElementById('result').innerText = 'Error: HRV cannot be negative';
|
122 |
+
return;
|
123 |
+
}
|
124 |
+
if (isNaN(data.Sleep_Hours) || data.Sleep_Hours < 0) {
|
125 |
+
document.getElementById('result').innerText = 'Error: Sleep Hours cannot be negative';
|
126 |
+
return;
|
127 |
+
}
|
128 |
+
if (isNaN(data.Activity) || data.Activity < 0) {
|
129 |
+
document.getElementById('result').innerText = 'Error: Activity cannot be negative';
|
130 |
+
return;
|
131 |
+
}
|
132 |
+
if (isNaN(data.Age) || data.Age < 0) {
|
133 |
+
document.getElementById('result').innerText = 'Error: Age cannot be negative';
|
134 |
+
return;
|
135 |
+
}
|
136 |
+
if (isNaN(data.Work_Study_Hours) || data.Work_Study_Hours < 0) {
|
137 |
+
document.getElementById('result').innerText = 'Error: Work/Study Hours cannot be negative';
|
138 |
+
return;
|
139 |
+
}
|
140 |
+
|
141 |
+
// Send request to the Flask API
|
142 |
+
fetch('/predict', {
|
143 |
+
method: 'POST',
|
144 |
+
headers: {
|
145 |
+
'Content-Type': 'application/json'
|
146 |
+
},
|
147 |
+
body: JSON.stringify(data)
|
148 |
+
})
|
149 |
+
.then(response => response.json())
|
150 |
+
.then(data => {
|
151 |
+
if (data.prediction) {
|
152 |
+
// Display prediction
|
153 |
+
document.getElementById('result').innerText = 'Prediction: ' + data.prediction;
|
154 |
+
|
155 |
+
// Display probabilities in a table
|
156 |
+
const probsTable = document.getElementById('probabilities-table');
|
157 |
+
probsTable.innerHTML = '';
|
158 |
+
for (const [status, prob] of Object.entries(data.probabilities)) {
|
159 |
+
const row = probsTable.insertRow();
|
160 |
+
row.insertCell(0).innerText = status;
|
161 |
+
row.insertCell(1).innerText = (prob * 100).toFixed(2) + '%';
|
162 |
+
}
|
163 |
+
|
164 |
+
// Display disclaimer and model accuracy
|
165 |
+
document.getElementById('disclaimer').innerText = data.disclaimer || '';
|
166 |
+
document.getElementById('model-accuracy').innerText = data.model_accuracy;
|
167 |
+
} else {
|
168 |
+
document.getElementById('result').innerText = 'Error: ' + data.error;
|
169 |
+
document.getElementById('probabilities-table').innerHTML = '';
|
170 |
+
document.getElementById('disclaimer').innerText = '';
|
171 |
+
document.getElementById('model-accuracy').innerText = '';
|
172 |
+
}
|
173 |
+
})
|
174 |
+
.catch(error => {
|
175 |
+
document.getElementById('result').innerText = 'Request failed';
|
176 |
+
document.getElementById('probabilities-table').innerHTML = '';
|
177 |
+
document.getElementById('disclaimer').innerText = '';
|
178 |
+
document.getElementById('model-accuracy').innerText = '';
|
179 |
+
console.error('Error:', error);
|
180 |
+
});
|
181 |
+
}
|
182 |
+
</script>
|
183 |
+
</head>
|
184 |
+
<body>
|
185 |
+
<div class="container">
|
186 |
+
<h2>Mental Health Prediction</h2>
|
187 |
+
<form id="predictionForm" onsubmit="event.preventDefault(); predictMentalHealth();">
|
188 |
+
<label>Sentiment Score (0 to 1):</label>
|
189 |
+
<input type="number" step="0.01" name="Sentiment_Score" required>
|
190 |
+
|
191 |
+
<label>HRV (Heart Rate Variability):</label>
|
192 |
+
<input type="number" step="0.01" name="HRV" required>
|
193 |
+
|
194 |
+
<label>Sleep Hours:</label>
|
195 |
+
<input type="number" step="0.1" name="Sleep_Hours" required>
|
196 |
+
|
197 |
+
<label>Activity (Steps/Day):</label>
|
198 |
+
<input type="number" name="Activity" required>
|
199 |
+
|
200 |
+
<label>Age:</label>
|
201 |
+
<input type="number" name="Age" required>
|
202 |
+
|
203 |
+
<label>Gender:</label>
|
204 |
+
<select name="Gender" required>
|
205 |
+
<option value="Male">Male</option>
|
206 |
+
<option value="Female">Female</option>
|
207 |
+
</select>
|
208 |
+
|
209 |
+
<label>Work/Study Hours:</label>
|
210 |
+
<input type="number" step="0.1" name="Work_Study_Hours" required>
|
211 |
+
|
212 |
+
<button type="submit">Predict</button>
|
213 |
+
</form>
|
214 |
+
<h3 id="result"></h3>
|
215 |
+
<div id="probabilities">
|
216 |
+
<table id="probabilities-table">
|
217 |
+
<tr><th>Status</th><th>Probability</th></tr>
|
218 |
+
</table>
|
219 |
+
</div>
|
220 |
+
<div id="disclaimer"></div>
|
221 |
+
<div id="model-accuracy"></div>
|
222 |
+
</div>
|
223 |
+
</body>
|
224 |
+
</html>
|
le_gender_final.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ae0c50f426a66d9e70886753e66e2493f723d9ca80c53709ea7796ce47eac18a
|
3 |
+
size 543
|
le_target_final.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:356dcc80ba23896d339529d04c0b7cda36bedd28f3ab9d56661f5c6433135eb1
|
3 |
+
size 555
|
mental_health_model_final.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9003f5d318ac9071a886e2af9384936b511bf4417a8969e595b67c05fa76db05
|
3 |
+
size 1261470
|
requirements.txt
ADDED
Binary file (5.81 kB). View file
|
|
scaler_final.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d9dbdce9b70795ca2c1d9396f541c96e5084971eb9d9d5138e3898b97112587e
|
3 |
+
size 1167
|
upload_to_hf.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from huggingface_hub import HfApi
|
2 |
+
|
3 |
+
# Initialize the API (will use the token from CLI authentication)
|
4 |
+
api = HfApi()
|
5 |
+
|
6 |
+
# Define your repository ID
|
7 |
+
repo_id = "AshutoshAI/mental-health-predictor"
|
8 |
+
|
9 |
+
# Upload the entire folder, excluding unnecessary files
|
10 |
+
api.upload_folder(
|
11 |
+
folder_path="D:/DSMINIPRO",
|
12 |
+
repo_id=repo_id,
|
13 |
+
repo_type="model",
|
14 |
+
ignore_patterns=["*.csv", "*le_gender.pkl", "*le_target.pkl", "*mental_health_model.pkl", "*scaler.pkl", "*scaler_updated.pkl", "*.log"],
|
15 |
+
)
|
16 |
+
print(f"Successfully uploaded to {repo_id}")
|