File size: 9,671 Bytes
c024705
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1ce623
c024705
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eeacc46
c024705
 
 
 
 
923dca5
c024705
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# AIMHSA - AI Mental Health Support Assistant with Automated Therapy Booking

## Overview

AIMHSA is an advanced AI-powered mental health support system specifically designed for Rwanda. The system combines conversational AI with automated therapy booking capabilities, providing immediate support and professional intervention when needed.

## πŸš€ New Features: Automated Therapy Booking System

### Core Functionality

1. **Real-time Risk Assessment**: Every user message is analyzed for mental health risk indicators
2. **Automated Professional Matching**: AI matches users with appropriate mental health professionals
3. **Emergency Session Booking**: Automatic booking for high-risk cases
4. **Professional Dashboard**: Complete session management for mental health professionals
5. **Admin Dashboard**: System monitoring and professional management
6. **Notification System**: Real-time alerts for professionals and administrators

### Risk Detection Levels

- **Critical**: Immediate professional intervention required (suicidal ideation, self-harm)
- **High**: Urgent professional support needed (severe depression, crisis)
- **Medium**: Professional consultation recommended (anxiety, stress)
- **Low**: General support and monitoring (mild concerns)

## πŸ—οΈ System Architecture

### Backend (Flask API)
- **Port**: 7060
- **Database**: SQLite with extended schema
- **AI Models**: Ollama (llama3.2:3b for chat, nomic-embed-text for embeddings)
- **Risk Assessment**: Multi-layered analysis (pattern matching + AI analysis)

### Frontend Components
- **Main Chat Interface**: Enhanced with risk assessment display
- **Admin Dashboard**: Professional and system management
- **Professional Dashboard**: Session management and notifications
- **Professional Login**: Secure access for mental health professionals

## πŸ“Š Database Schema

### New Tables Added

1. **professionals**: Mental health professional profiles
2. **risk_assessments**: Real-time risk analysis records
3. **automated_bookings**: Emergency session bookings
4. **professional_notifications**: Alert system for professionals
5. **therapy_sessions**: Session records and notes
6. **admin_users**: Administrative access control

## πŸ”§ Installation & Setup

### Prerequisites
- Python 3.8+
- Ollama installed and running
- Required Python packages (see requirements.txt)

### Setup Steps

1. **Install Dependencies**
   ```bash
   pip install flask flask-cors ollama python-dotenv sqlite3 werkzeug numpy pytesseract
   ```

2. **Initialize Database**
   ```bash
   python app.py
   # This will create all necessary tables
   ```

3. **Create Sample Data**
   ```bash
   python create_sample_data.py
   # Creates sample professionals, users, and admin user
   ```

4. **Test Login System**
   ```bash
   python test_login.py
   # Verifies all login functionality
   ```

5. **Start Backend**
   ```bash
   python app.py
   # Runs on https://prodevroger-ishingiro.hf.space
   ```

5. **Start Frontend**
   ```bash
   python run_frontend.py
   # Runs on https://prodevroger-ishingiro.hf.space
   ```

## πŸ‘₯ User Roles & Access

### 1. Regular Users
- **Access**: Main chat interface
- **Features**: 
  - AI mental health support
  - Risk assessment display
  - Emergency booking notifications
  - Conversation history

### 2. Mental Health Professionals
- **Access**: Professional dashboard
- **Login**: `/professional_login.html`
- **Features**:
  - View assigned sessions
  - Accept/decline bookings
  - Add session notes
  - Manage treatment plans
  - Receive notifications

### 3. Administrators
- **Access**: Admin dashboard
- **Login**: Use admin credentials
- **Features**:
  - Manage professionals
  - Monitor risk assessments
  - View all bookings
  - System analytics
  - Real-time monitoring

## πŸ” Default Credentials

### Sample Users
- **testuser** / `password123`
- **john_doe** / `password123`
- **jane_smith** / `password123`
- **rwanda_user** / `password123`

### Sample Professionals
- **Dr. Marie Mukamana** (Psychiatrist): `dr_mukamana` / `password123`
- **Jean Ntwari** (Counselor): `counselor_ntwari` / `password123`
- **Grace Umutoni** (Psychologist): `psychologist_umutoni` / `password123`
- **Claudine Nyiraneza** (Social Worker): `social_worker_nyiraneza` / `password123`

### Admin User
- **Username**: `admin`
- **Password**: `admin123`

## 🚨 Emergency Response Flow

1. **User sends message** β†’ Risk assessment triggered
2. **High/Critical risk detected** β†’ Professional matching algorithm activated
3. **Best professional selected** β†’ Automated booking created
4. **Professional notified** β†’ Real-time notification sent
5. **Session scheduled** β†’ User receives confirmation
6. **Professional accepts** β†’ Session confirmed
7. **Session conducted** β†’ Notes and treatment plan recorded

## πŸ“± API Endpoints

### User Endpoints
- `POST /ask` - Enhanced with risk assessment
- `GET /session` - Session management
- `GET /history` - Conversation history

### Professional Endpoints
- `POST /professional/login` - Professional authentication
- `GET /professional/notifications` - Get notifications
- `PUT /professional/notifications/{id}/read` - Mark as read
- `GET /professional/sessions` - Get assigned sessions
- `PUT /professional/sessions/{id}/status` - Accept/decline sessions
- `POST /professional/sessions/{id}/notes` - Add session notes

### Admin Endpoints
- `POST /admin/professionals` - Create professional
- `GET /admin/professionals` - List professionals
- `GET /admin/bookings` - View all bookings
- `GET /admin/risk-assessments` - Risk assessment history

### Monitoring Endpoints
- `GET /monitor/risk-stats` - Real-time risk statistics
- `GET /monitor/recent-assessments` - Recent assessments

## 🎯 Key Features

### Risk Assessment Engine
- **Pattern Matching**: Regex-based detection of risk indicators
- **AI Analysis**: Ollama-powered sentiment and context analysis
- **Conversation Patterns**: Escalation detection across message history
- **Rwanda-Specific**: Specialized indicators for local context

### Professional Matching Algorithm
- **Specialization Mapping**: Matches risk types to professional expertise
- **Location Proximity**: Considers geographical distance
- **Availability**: Real-time availability checking
- **Experience Scoring**: Prioritizes experienced professionals

### Notification System
- **Real-time Alerts**: Immediate notifications for professionals
- **Priority Levels**: Urgent, high, normal priority classification
- **Multi-channel**: Dashboard notifications with visual indicators
- **Auto-refresh**: Live updates every 30 seconds

## πŸ”§ Configuration

### Environment Variables
```bash
CHAT_MODEL=llama3.2:3b
EMBED_MODEL=nomic-embed-text
SENT_EMBED_MODEL=nomic-embed-text
```

### Risk Assessment Thresholds
- **Critical**: β‰₯ 0.8 risk score
- **High**: β‰₯ 0.6 risk score
- **Medium**: β‰₯ 0.4 risk score
- **Low**: < 0.4 risk score

### Session Scheduling
- **Critical Risk**: 1 hour from detection
- **High Risk**: 24 hours from detection
- **Emergency Sessions**: Immediate professional contact

## πŸ“ˆ Monitoring & Analytics

### Real-time Dashboards
- **Risk Statistics**: Live count by risk level
- **Professional Activity**: Session acceptance rates
- **System Health**: Response times and error rates
- **Geographic Distribution**: Risk patterns by location

### Reporting
- **Daily Risk Reports**: Summary of assessments
- **Professional Performance**: Session completion rates
- **System Usage**: User engagement metrics
- **Emergency Response**: Time-to-intervention tracking

## πŸ›‘οΈ Security & Privacy

### Data Protection
- **Encrypted Storage**: Password hashing with Werkzeug
- **Session Management**: Secure session tokens
- **Access Control**: Role-based permissions
- **Audit Logging**: Complete activity tracking

### Privacy Compliance
- **Anonymized Data**: IP-based sessions for guests
- **Consent Management**: Clear data usage policies
- **Data Retention**: Configurable retention periods
- **Secure Communication**: HTTPS-ready architecture

## πŸš€ Deployment

### Production Considerations
- **Database**: Consider PostgreSQL for production
- **Caching**: Redis for session management
- **Load Balancing**: Multiple API instances
- **Monitoring**: Application performance monitoring
- **Backup**: Automated database backups

### Scaling
- **Horizontal Scaling**: Multiple API servers
- **Database Sharding**: Partition by region/district
- **CDN**: Static asset delivery
- **Microservices**: Split into specialized services

## 🀝 Contributing

### Development Setup
1. Fork the repository
2. Create feature branch
3. Implement changes
4. Add tests
5. Submit pull request

### Code Standards
- **Python**: PEP 8 compliance
- **JavaScript**: ES6+ standards
- **Documentation**: Comprehensive docstrings
- **Testing**: Unit and integration tests

## πŸ“ž Support & Contact

### Technical Support
- **Issues**: GitHub Issues
- **Documentation**: This README
- **Community**: Development discussions

### Mental Health Resources
- **Rwanda Mental Health Hotline**: 105
- **CARAES Ndera Hospital**: +250 788 305 703
- **Emergency Services**: 112

## πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

## πŸ™ Acknowledgments

- **Rwanda Mental Health Community**: For guidance and requirements
- **Open Source Contributors**: For the amazing tools and libraries
- **Mental Health Professionals**: For their expertise and feedback

---

**⚠️ Important**: This system is designed to supplement, not replace, professional mental health care. Always encourage users to seek professional help when needed.