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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# PPD Recommendation API - Documentation
|
| 2 |
+
|
| 3 |
+
## Base URL
|
| 4 |
+
```
|
| 5 |
+
http://localhost:8000
|
| 6 |
+
```
|
| 7 |
+
|
| 8 |
+
## Authentication
|
| 9 |
+
Currently no authentication required. For production, implement API key or JWT authentication.
|
| 10 |
+
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
## Endpoints
|
| 14 |
+
|
| 15 |
+
### 1. Health Check
|
| 16 |
+
**GET** `/`
|
| 17 |
+
|
| 18 |
+
**Response:**
|
| 19 |
+
```json
|
| 20 |
+
{
|
| 21 |
+
"status": "online",
|
| 22 |
+
"engine_ready": true,
|
| 23 |
+
"version": "1.5"
|
| 24 |
+
}
|
| 25 |
+
```
|
| 26 |
+
|
| 27 |
+
---
|
| 28 |
+
|
| 29 |
+
### 2. Detailed Health Check
|
| 30 |
+
**GET** `/api/health`
|
| 31 |
+
|
| 32 |
+
**Description:** Container health monitoring endpoint
|
| 33 |
+
|
| 34 |
+
**Response:**
|
| 35 |
+
```json
|
| 36 |
+
{
|
| 37 |
+
"status": "healthy",
|
| 38 |
+
"timestamp": "2026-01-29T18:10:00",
|
| 39 |
+
"checks": {
|
| 40 |
+
"database": "ok",
|
| 41 |
+
"model": "ok",
|
| 42 |
+
"articles_loaded": 139
|
| 43 |
+
}
|
| 44 |
+
}
|
| 45 |
+
```
|
| 46 |
+
|
| 47 |
+
---
|
| 48 |
+
|
| 49 |
+
### 3. System Statistics
|
| 50 |
+
**GET** `/api/stats`
|
| 51 |
+
|
| 52 |
+
**Description:** Get system metrics and article counts
|
| 53 |
+
|
| 54 |
+
**Response:**
|
| 55 |
+
```json
|
| 56 |
+
{
|
| 57 |
+
"total_articles": 139,
|
| 58 |
+
"articles_by_type": {
|
| 59 |
+
"pubmed": 136,
|
| 60 |
+
"text": 2,
|
| 61 |
+
"html": 1
|
| 62 |
+
},
|
| 63 |
+
"articles_by_risk": {
|
| 64 |
+
"All": 136,
|
| 65 |
+
"Moderate": 2,
|
| 66 |
+
"High": 1
|
| 67 |
+
},
|
| 68 |
+
"model_vocabulary_size": 20652,
|
| 69 |
+
"last_updated": "2026-01-29T18:10:00"
|
| 70 |
+
}
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
---
|
| 74 |
+
|
| 75 |
+
### 4. Get Recommendations (Main Endpoint)
|
| 76 |
+
**POST** `/api/recommend`
|
| 77 |
+
|
| 78 |
+
**Description:** Get personalized article recommendations based on symptoms and crisis level
|
| 79 |
+
|
| 80 |
+
**Request Body:**
|
| 81 |
+
```json
|
| 82 |
+
{
|
| 83 |
+
"risk_level": "Moderate",
|
| 84 |
+
"symptoms_text": "I feel sad and have trouble sleeping",
|
| 85 |
+
"top_n": 5
|
| 86 |
+
}
|
| 87 |
+
```
|
| 88 |
+
|
| 89 |
+
**Parameters:**
|
| 90 |
+
- `risk_level` (string, required): One of "Low", "Moderate", "High"
|
| 91 |
+
- `symptoms_text` (string, required): User's symptom description
|
| 92 |
+
- `top_n` (integer, optional): Number of recommendations (default: 5)
|
| 93 |
+
|
| 94 |
+
**Response:**
|
| 95 |
+
```json
|
| 96 |
+
{
|
| 97 |
+
"status": "success",
|
| 98 |
+
"risk_assessment": "Moderate",
|
| 99 |
+
"recommendations": [
|
| 100 |
+
{
|
| 101 |
+
"article_id": 134,
|
| 102 |
+
"title": "Sleep disturbances in postpartum depression",
|
| 103 |
+
"category": "General",
|
| 104 |
+
"risk_level": "All",
|
| 105 |
+
"format_type": "pubmed",
|
| 106 |
+
"external_url": "https://pubmed.ncbi.nlm.nih.gov/12345678/",
|
| 107 |
+
"access_type": "External Link",
|
| 108 |
+
"final_score": 0.856
|
| 109 |
+
}
|
| 110 |
+
]
|
| 111 |
+
}
|
| 112 |
+
```
|
| 113 |
+
|
| 114 |
+
---
|
| 115 |
+
|
| 116 |
+
### 5. Get Article Content
|
| 117 |
+
**GET** `/api/article/{article_id}`
|
| 118 |
+
|
| 119 |
+
**Description:** Retrieve full content for a specific article
|
| 120 |
+
|
| 121 |
+
**Parameters:**
|
| 122 |
+
- `article_id` (integer, path): Article ID from recommendation
|
| 123 |
+
|
| 124 |
+
**Response:**
|
| 125 |
+
```json
|
| 126 |
+
{
|
| 127 |
+
"article_id": 134,
|
| 128 |
+
"title": "Sleep disturbances in postpartum depression",
|
| 129 |
+
"category": "General",
|
| 130 |
+
"format_type": "pubmed",
|
| 131 |
+
"external_url": "https://pubmed.ncbi.nlm.nih.gov/12345678/",
|
| 132 |
+
"content": "Full article abstract or content..."
|
| 133 |
+
}
|
| 134 |
+
```
|
| 135 |
+
|
| 136 |
+
**Error Response (404):**
|
| 137 |
+
```json
|
| 138 |
+
{
|
| 139 |
+
"detail": "Article not found"
|
| 140 |
+
}
|
| 141 |
+
```
|
| 142 |
+
|
| 143 |
+
---
|
| 144 |
+
|
| 145 |
+
### 3. Real-Time PubMed Search
|
| 146 |
+
**GET** `/api/pubmed/search`
|
| 147 |
+
|
| 148 |
+
**Description:** Proxy endpoint to fetch articles directly from PubMed in real-time.
|
| 149 |
+
|
| 150 |
+
**Parameters:**
|
| 151 |
+
- `query` (string, optional): Search term (default: "postpartum depression")
|
| 152 |
+
- `limit` (int, optional): Number of results (default: 10)
|
| 153 |
+
|
| 154 |
+
**Response:**
|
| 155 |
+
```json
|
| 156 |
+
{
|
| 157 |
+
"status": "success",
|
| 158 |
+
"count": 2,
|
| 159 |
+
"results": [
|
| 160 |
+
{
|
| 161 |
+
"title": "Article Title",
|
| 162 |
+
"content": "Abstract content...",
|
| 163 |
+
"url": "https://pubmed.ncbi.nlm.nih.gov/12345/"
|
| 164 |
+
}
|
| 165 |
+
]
|
| 166 |
+
}
|
| 167 |
+
```
|
| 168 |
+
|
| 169 |
+
---
|
| 170 |
+
|
| 171 |
+
### 6. Rebuild TF-IDF Model (Admin)
|
| 172 |
+
**POST** `/api/admin/rebuild-model`
|
| 173 |
+
|
| 174 |
+
**Description:** Manually trigger model rebuild from existing database
|
| 175 |
+
|
| 176 |
+
**Response:**
|
| 177 |
+
```json
|
| 178 |
+
{
|
| 179 |
+
"status": "success",
|
| 180 |
+
"message": "Weighted TF-IDF model rebuilt and reloaded."
|
| 181 |
+
}
|
| 182 |
+
```
|
| 183 |
+
|
| 184 |
+
---
|
| 185 |
+
|
| 186 |
+
### 7. Trigger PubMed Ingestion (Admin)
|
| 187 |
+
**POST** `/api/admin/trigger-ingestion`
|
| 188 |
+
|
| 189 |
+
**Description:** Manually fetch new articles from PubMed and rebuild model
|
| 190 |
+
|
| 191 |
+
**Response:**
|
| 192 |
+
```json
|
| 193 |
+
{
|
| 194 |
+
"status": "success",
|
| 195 |
+
"message": "Ingested 15 new articles and rebuilt model."
|
| 196 |
+
}
|
| 197 |
+
```
|
| 198 |
+
|
| 199 |
+
---
|
| 200 |
+
|
| 201 |
+
## Error Responses
|
| 202 |
+
|
| 203 |
+
All endpoints return standard HTTP status codes:
|
| 204 |
+
- `200`: Success
|
| 205 |
+
- `404`: Resource not found
|
| 206 |
+
- `500`: Internal server error
|
| 207 |
+
|
| 208 |
+
Error format:
|
| 209 |
+
```json
|
| 210 |
+
{
|
| 211 |
+
"detail": "Error message description"
|
| 212 |
+
}
|
| 213 |
+
```
|
| 214 |
+
|
| 215 |
+
---
|
| 216 |
+
|
| 217 |
+
## Integration Examples
|
| 218 |
+
|
| 219 |
+
### JavaScript (Fetch API)
|
| 220 |
+
```javascript
|
| 221 |
+
// Get recommendations
|
| 222 |
+
const response = await fetch('http://localhost:8000/api/recommend', {
|
| 223 |
+
method: 'POST',
|
| 224 |
+
headers: { 'Content-Type': 'application/json' },
|
| 225 |
+
body: JSON.stringify({
|
| 226 |
+
risk_level: 'Moderate',
|
| 227 |
+
symptoms_text: 'anxiety and sleep problems',
|
| 228 |
+
top_n: 3
|
| 229 |
+
})
|
| 230 |
+
});
|
| 231 |
+
const data = await response.json();
|
| 232 |
+
console.log(data.recommendations);
|
| 233 |
+
```
|
| 234 |
+
|
| 235 |
+
### Python (Requests)
|
| 236 |
+
```python
|
| 237 |
+
import requests
|
| 238 |
+
|
| 239 |
+
response = requests.post('http://localhost:8000/api/recommend', json={
|
| 240 |
+
'risk_level': 'High',
|
| 241 |
+
'symptoms_text': 'severe depression and suicidal thoughts',
|
| 242 |
+
'top_n': 5
|
| 243 |
+
})
|
| 244 |
+
recommendations = response.json()['recommendations']
|
| 245 |
+
```
|
| 246 |
+
|
| 247 |
+
### cURL
|
| 248 |
+
```bash
|
| 249 |
+
curl -X POST http://localhost:8000/api/recommend \
|
| 250 |
+
-H "Content-Type: application/json" \
|
| 251 |
+
-d '{"risk_level":"Low","symptoms_text":"mild anxiety","top_n":3}'
|
| 252 |
+
```
|
| 253 |
+
|
| 254 |
+
---
|
| 255 |
+
|
| 256 |
+
## Rate Limiting
|
| 257 |
+
Currently no rate limiting. Recommended for production:
|
| 258 |
+
- 100 requests per minute per IP
|
| 259 |
+
- 1000 requests per hour per IP
|
| 260 |
+
|
| 261 |
+
---
|
| 262 |
+
|
| 263 |
+
## CORS
|
| 264 |
+
CORS is enabled for all origins in development. For production, restrict to specific domains.
|