File size: 7,243 Bytes
36e2bdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# TTS Status API Documentation

This document describes the new status tracking endpoints that provide real-time information about TTS processing.

## Overview

The status API allows you to monitor TTS request processing in real-time, view progress information, check statistics, and review request history.

## Endpoints

### πŸ” GET `/status`

Get comprehensive TTS processing status information.

**Query Parameters:**

- `include_memory` (boolean): Include memory usage information
- `include_history` (boolean): Include recent request history
- `include_stats` (boolean): Include processing statistics
- `history_limit` (integer): Number of history records to return (1-20)

**Example:**

```bash
curl "http://localhost:4123/status?include_memory=true&include_stats=true"
```

**Response:**

```json
{
  "status": "generating_audio",
  "is_processing": true,
  "request_id": "abc12345",
  "start_time": 1704067200.0,
  "duration_seconds": 2.5,
  "text_length": 156,
  "text_preview": "This is the text being processed...",
  "voice_source": "default",
  "parameters": {
    "exaggeration": 0.7,
    "cfg_weight": 0.5,
    "temperature": 0.8
  },
  "progress": {
    "current_chunk": 2,
    "total_chunks": 4,
    "current_step": "Generating audio for chunk 2/4",
    "progress_percentage": 50.0,
    "estimated_completion": 1704067205.0
  },
  "total_requests": 42
}
```

### ⚑ GET `/status/progress`

Get lightweight progress information (optimized for polling).

**Example:**

```bash
curl "http://localhost:4123/status/progress"
```

**Response when processing:**

```json
{
  "is_processing": true,
  "status": "generating_audio",
  "current_step": "Generating audio for chunk 2/4",
  "current_chunk": 2,
  "total_chunks": 4,
  "progress_percentage": 50.0,
  "duration_seconds": 2.5,
  "estimated_completion": 1704067205.0,
  "text_preview": "This is the text being processed..."
}
```

**Response when idle:**

```json
{
  "is_processing": false,
  "status": "idle",
  "message": "No active TTS requests"
}
```

### πŸ“Š GET `/status/statistics`

Get comprehensive processing statistics.

**Query Parameters:**

- `include_memory` (boolean): Include current memory usage

**Example:**

```bash
curl "http://localhost:4123/status/statistics?include_memory=true"
```

**Response:**

```json
{
  "total_requests": 42,
  "completed_requests": 38,
  "error_requests": 4,
  "success_rate": 90.5,
  "average_duration_seconds": 3.2,
  "average_text_length": 124.5,
  "is_processing": false,
  "current_memory": {
    "cpu_memory_mb": 256.7,
    "gpu_memory_allocated_mb": 1024.3
  }
}
```

### πŸ“ GET `/status/history`

Get recent TTS request history.

**Query Parameters:**

- `limit` (integer): Number of records to return (1-50, default: 10)

**Example:**

```bash
curl "http://localhost:4123/status/history?limit=5"
```

**Response:**

```json
{
  "request_history": [
    {
      "request_id": "abc12345",
      "status": "completed",
      "start_time": 1704067200.0,
      "end_time": 1704067203.5,
      "duration_seconds": 3.5,
      "text_length": 156,
      "text_preview": "Hello world, this is a test...",
      "voice_source": "default",
      "parameters": {
        "exaggeration": 0.7,
        "cfg_weight": 0.5,
        "temperature": 0.8
      }
    }
  ],
  "total_records": 5,
  "limit": 5
}
```

### πŸ—‘οΈ POST `/status/history/clear`

Clear TTS request history.

**Query Parameters:**

- `confirm` (boolean): Required confirmation flag

**Example:**

```bash
curl -X POST "http://localhost:4123/status/history/clear?confirm=true"
```

### πŸ“‹ GET `/info`

Get comprehensive API information including status, memory, and statistics.

**Example:**

```bash
curl "http://localhost:4123/info"
```

**Response:**

```json
{
  "api_name": "Chatterbox TTS API",
  "version": "1.0.0",
  "status": "operational",
  "tts_status": {
    /* current status */
  },
  "statistics": {
    /* processing stats */
  },
  "memory_info": {
    /* memory usage */
  },
  "recent_requests": [
    /* last 3 requests */
  ],
  "uptime_info": {
    "total_requests": 42,
    "success_rate": 90.5,
    "is_processing": false
  }
}
```

## Status Values

The `status` field can have these values:

- `idle`: No active requests
- `initializing`: Starting request processing
- `processing_text`: Validating and preparing text
- `chunking`: Splitting text into chunks
- `generating_audio`: Generating audio for chunks
- `concatenating`: Combining audio chunks
- `finalizing`: Converting to output format
- `completed`: Request completed successfully
- `error`: Request failed with error

## Endpoint Aliases

All endpoints support multiple path formats for compatibility:

| Primary Path         | Aliases                                           |
| -------------------- | ------------------------------------------------- |
| `/status`            | `/v1/status`, `/processing`, `/processing/status` |
| `/status/progress`   | `/v1/status/progress`, `/progress`                |
| `/status/history`    | `/v1/status/history`, `/history`                  |
| `/status/statistics` | `/v1/status/statistics`, `/stats`                 |
| `/info`              | `/v1/info`, `/api/info`                           |

## Frontend Integration

### Real-time Progress Monitoring

```typescript
import { createTTSService } from './services/tts';

const ttsService = createTTSService('http://localhost:4123');

// Monitor progress during generation
const monitorProgress = async () => {
  const interval = setInterval(async () => {
    try {
      const progress = await ttsService.getProgress();
      if (progress.is_processing) {
        console.log(`Progress: ${progress.progress_percentage}%`);
        console.log(`Step: ${progress.current_step}`);
      } else {
        clearInterval(interval);
        console.log('Processing complete');
      }
    } catch (error) {
      console.error('Failed to get progress:', error);
    }
  }, 1000);
};
```

### React Hook Example

```typescript
import { useQuery } from '@tanstack/react-query';

const useProcessingStatus = () => {
  return useQuery({
    queryKey: ['tts-status'],
    queryFn: () => ttsService.getProgress(),
    refetchInterval: 1000, // Poll every second
    enabled: true,
  });
};

// Usage in component
const { data: status } = useProcessingStatus();
if (status?.is_processing) {
  // Show progress UI
}
```

## Testing

Run the status endpoint tests:

```bash
python tests/test_status.py
```

This will test:

- βœ… All status endpoints
- 🎀 Status tracking during TTS generation
- πŸ”„ Concurrent request handling
- πŸ“Š Real-time progress monitoring

## Notes

- Status information is thread-safe for concurrent requests
- Progress percentages are calculated based on chunk processing
- Memory information requires memory monitoring to be enabled
- History is limited to the last 10 requests by default
- Estimated completion times are calculated based on current progress

## Error Handling

All endpoints return appropriate HTTP status codes:

- `200`: Success
- `400`: Bad request (invalid parameters)
- `500`: Internal server error

Error responses follow this format:

```json
{
  "error": {
    "message": "Error description",
    "type": "error_type"
  }
}
```