File size: 5,255 Bytes
1826b8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# PPD Recommendation API - Documentation

## Base URL
```

http://localhost:8000

```

## Authentication
Currently no authentication required. For production, implement API key or JWT authentication.

---

## Endpoints

### 1. Health Check
**GET** `/`

**Response:**
```json

{

  "status": "online",

  "engine_ready": true,

  "version": "1.5"

}

```

---

### 2. Detailed Health Check
**GET** `/api/health`

**Description:** Container health monitoring endpoint

**Response:**
```json

{

  "status": "healthy",

  "timestamp": "2026-01-29T18:10:00",

  "checks": {

    "database": "ok",

    "model": "ok",

    "articles_loaded": 139

  }

}

```

---

### 3. System Statistics
**GET** `/api/stats`

**Description:** Get system metrics and article counts

**Response:**
```json

{

  "total_articles": 139,

  "articles_by_type": {

    "pubmed": 136,

    "text": 2,

    "html": 1

  },

  "articles_by_risk": {

    "All": 136,

    "Moderate": 2,

    "High": 1

  },

  "model_vocabulary_size": 20652,

  "last_updated": "2026-01-29T18:10:00"

}

```

---

### 4. Get Recommendations (Main Endpoint)
**POST** `/api/recommend`

**Description:** Get personalized article recommendations based on symptoms and crisis level

**Request Body:**
```json

{

  "risk_level": "Moderate",

  "symptoms_text": "I feel sad and have trouble sleeping",

  "top_n": 5

}

```

**Parameters:**
- `risk_level` (string, required): One of "Low", "Moderate", "High"
- `symptoms_text` (string, required): User's symptom description
- `top_n` (integer, optional): Number of recommendations (default: 5)

**Response:**
```json

{

  "status": "success",

  "risk_assessment": "Moderate",

  "recommendations": [

    {

      "article_id": 134,

      "title": "Sleep disturbances in postpartum depression",

      "category": "General",

      "risk_level": "All",

      "format_type": "pubmed",

      "external_url": "https://pubmed.ncbi.nlm.nih.gov/12345678/",

      "access_type": "External Link",

      "final_score": 0.856

    }

  ]

}

```

---

### 5. Get Article Content
**GET** `/api/article/{article_id}`

**Description:** Retrieve full content for a specific article

**Parameters:**
- `article_id` (integer, path): Article ID from recommendation

**Response:**
```json

{

  "article_id": 134,

  "title": "Sleep disturbances in postpartum depression",

  "category": "General",

  "format_type": "pubmed",

  "external_url": "https://pubmed.ncbi.nlm.nih.gov/12345678/",

  "content": "Full article abstract or content..."

}

```

**Error Response (404):**
```json

{

  "detail": "Article not found"

}

```

---

### 3. Real-Time PubMed Search
**GET** `/api/pubmed/search`

**Description:** Proxy endpoint to fetch articles directly from PubMed in real-time.

**Parameters:**
- `query` (string, optional): Search term (default: "postpartum depression")
- `limit` (int, optional): Number of results (default: 10)

**Response:**
```json

{

  "status": "success",

  "count": 2,

  "results": [

    {

      "title": "Article Title",

      "content": "Abstract content...",

      "url": "https://pubmed.ncbi.nlm.nih.gov/12345/"

    }

  ]

}

```

---

### 6. Rebuild TF-IDF Model (Admin)
**POST** `/api/admin/rebuild-model`

**Description:** Manually trigger model rebuild from existing database

**Response:**
```json

{

  "status": "success",

  "message": "Weighted TF-IDF model rebuilt and reloaded."

}

```

---

### 7. Trigger PubMed Ingestion (Admin)
**POST** `/api/admin/trigger-ingestion`

**Description:** Manually fetch new articles from PubMed and rebuild model

**Response:**
```json

{

  "status": "success",

  "message": "Ingested 15 new articles and rebuilt model."

}

```

---

## Error Responses

All endpoints return standard HTTP status codes:
- `200`: Success
- `404`: Resource not found
- `500`: Internal server error

Error format:
```json

{

  "detail": "Error message description"

}

```

---

## Integration Examples

### JavaScript (Fetch API)
```javascript

// Get recommendations

const response = await fetch('http://localhost:8000/api/recommend', {

  method: 'POST',

  headers: { 'Content-Type': 'application/json' },

  body: JSON.stringify({

    risk_level: 'Moderate',

    symptoms_text: 'anxiety and sleep problems',

    top_n: 3

  })

});

const data = await response.json();

console.log(data.recommendations);

```

### Python (Requests)
```python

import requests



response = requests.post('http://localhost:8000/api/recommend', json={

    'risk_level': 'High',

    'symptoms_text': 'severe depression and suicidal thoughts',

    'top_n': 5

})

recommendations = response.json()['recommendations']

```

### cURL
```bash

curl -X POST http://localhost:8000/api/recommend \

  -H "Content-Type: application/json" \

  -d '{"risk_level":"Low","symptoms_text":"mild anxiety","top_n":3}'

```

---

## Rate Limiting
Currently no rate limiting. Recommended for production:
- 100 requests per minute per IP
- 1000 requests per hour per IP

---

## CORS
CORS is enabled for all origins in development. For production, restrict to specific domains.