File size: 17,605 Bytes
329b9df
 
0443b2c
 
 
 
329b9df
b3563c0
 
 
329b9df
b3563c0
 
 
 
 
 
 
 
 
329b9df
 
 
b3563c0
 
 
 
 
329b9df
b3563c0
329b9df
 
 
b3563c0
0443b2c
 
 
 
 
b3563c0
 
 
 
 
 
 
 
 
 
 
0443b2c
b3563c0
329b9df
b3563c0
329b9df
 
 
 
b3563c0
 
329b9df
b3563c0
 
 
 
329b9df
 
b3563c0
329b9df
 
 
 
 
b3563c0
 
 
 
 
 
 
329b9df
 
 
b3563c0
329b9df
b3563c0
 
 
 
329b9df
 
 
 
b3563c0
329b9df
b3563c0
329b9df
 
 
 
 
b3563c0
329b9df
b3563c0
329b9df
b3563c0
 
 
329b9df
b3563c0
329b9df
b3563c0
329b9df
 
 
 
 
 
 
 
 
 
 
 
b3563c0
329b9df
 
 
b3563c0
329b9df
 
 
b3563c0
 
329b9df
b3563c0
329b9df
 
 
 
 
b3563c0
329b9df
 
b3563c0
 
329b9df
b3563c0
 
329b9df
b3563c0
329b9df
 
 
b3563c0
 
329b9df
b3563c0
329b9df
 
 
 
b3563c0
 
 
329b9df
b3563c0
 
 
 
 
 
 
 
 
 
329b9df
b3563c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
329b9df
 
b3563c0
 
 
 
 
329b9df
 
 
b3563c0
 
 
 
 
 
 
329b9df
 
 
 
 
 
 
 
 
 
 
 
 
b3563c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
329b9df
 
 
b3563c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
329b9df
 
 
 
 
 
b3563c0
 
 
329b9df
 
 
 
 
 
 
b3563c0
329b9df
b3563c0
 
 
 
 
 
 
 
 
 
329b9df
b3563c0
 
329b9df
b3563c0
329b9df
 
 
 
 
 
 
 
 
 
 
 
 
 
b3563c0
 
329b9df
 
 
 
 
 
 
 
 
b3563c0
 
 
 
 
 
 
 
 
 
 
 
329b9df
 
b3563c0
329b9df
b3563c0
 
 
329b9df
b3563c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
329b9df
 
 
 
b3563c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
329b9df
b3563c0
329b9df
b3563c0
 
 
 
 
 
329b9df
0443b2c
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# DataFlow API Reference

**Base URL:** `https://data-processing-ai-agents.onrender.com`

> The backend runs on Render's free tier, which spins down after inactivity β€” the
> first request after idle may take ~30–60s to cold-start before responding.

**Health check:** `GET /health` β†’ `{ "status": "ok" }`

---

## Fair use

This is a free public demo API. A few things to know before building on it:

- **No API key required** β€” anyone can call it.
- **One request runs at a time** β€” requests queue behind each other (no parallelism).
- **No data is stored** β€” uploaded files are deleted immediately after analysis.
- **Quota-limited** β€” the backend runs on free-tier LLM providers. Heavy automated use will exhaust the daily quota for everyone. Please be reasonable.
- **No SLA** β€” this is a portfolio project. Uptime is best-effort.

---

## POST /analyze

Runs the 6-agent analysis pipeline and streams results back as Server-Sent Events.

### Request

`Content-Type: multipart/form-data`

| Field | Type | Required | Constraints | Description |
|---|---|---|---|---|
| `context` | string | Yes | Max 2000 chars | Natural-language question or description of what to analyze |
| `files` | File (binary) | No | Up to 3 files, max 10 MB each, 15 MB combined | Data files to analyze together |

> Large text files are trimmed to a model-safe budget (~160 K characters) before
> analysis so they fit the free-tier model context window; the analysis notes when
> a file was truncated.

**Accepted file types:** `.csv` `.json` `.txt` `.pdf` `.xml`

**Validation errors** (returned as JSON before the stream opens):

```json
{ "error": "Context is required." }
{ "error": "Context too long (2100 chars, max 2000)." }
{ "error": "Too many files (max 3)." }
{ "error": "File 'data.exe': type '.exe' not supported. Allowed: .csv, .json, .pdf, .txt, .xml" }
{ "error": "File 'huge.csv' too large (15360KB, max 10MB)." }
{ "error": "Combined upload too large (max 15MB across all files)." }
```

### JavaScript

```js
const form = new FormData()
form.append('context', 'Show me the top 5 products by revenue in Q3')
form.append('files', file1)   // optional β€” repeat for multiple files
form.append('files', file2)

const res = await fetch('https://pymite6941-data-analyst-ai-agent.hf.space/analyze', {
  method: 'POST',
  body: form,
})
```

### Python

```python
import requests

res = requests.post(
    'https://pymite6941-data-analyst-ai-agent.hf.space/analyze',
    data={'context': 'What are the top error types in this log?'},
    files=[
        ('files', ('server.log', open('server.log', 'rb'))),
        ('files', ('metrics.csv', open('metrics.csv', 'rb'))),  # optional second file
    ],
    stream=True,
)
```

### curl

```bash
curl -N -X POST https://pymite6941-data-analyst-ai-agent.hf.space/analyze \
  -F "context=Show me monthly revenue trends" \
  -F "files=@sales.csv"
```

---

## Response β€” Server-Sent Events

The response stays open as an SSE stream while the 6-agent pipeline runs (~1–3 min). Every frame has the form:

```
data: <JSON-encoded payload>\n\n
```

Three payload shapes arrive in order:

| Shape | When | Action |
|---|---|---|
| `"some log string"` | Throughout the run | Display in a live feed |
| `{"type": "result", "content": "<json string>"}` | Pipeline complete | Parse `content` as JSON |
| `"__DONE__"` | After result (or on timeout/error) | Close the stream |

A heartbeat comment (`: ping`) fires every 30s on idle to keep the connection alive through proxies.

### Reading loop β€” JavaScript

```js
const reader = res.body.getReader()
const decoder = new TextDecoder()
let buf = ''

while (true) {
  const { done, value } = await reader.read()
  if (done) break

  buf += decoder.decode(value, { stream: true })
  const lines = buf.split('\n')
  buf = lines.pop()                           // hold incomplete line

  for (const raw of lines) {
    if (!raw.startsWith('data: ')) continue
    const payload = JSON.parse(raw.slice(6))  // strip "data: "

    if (payload === '__DONE__') return
    if (payload?.type === 'result') {
      const result = JSON.parse(payload.content)
      handleResult(result)
    } else {
      appendLog(payload)                      // string log line
    }
  }
}
```

### Reading loop β€” Python

```python
import json

for raw in res.iter_lines():
    line = raw.decode('utf-8')
    if not line.startswith('data: '):
        continue
    payload = json.loads(line[6:])
    if payload == '__DONE__':
        break
    if isinstance(payload, dict) and payload.get('type') == 'result':
        result = json.loads(payload['content'])
        handle_result(result)
    else:
        print('[LOG]', payload)
```

---

## Result schema

`payload.content` is a JSON string. Parse it to get the result object.

Every result includes these fields regardless of type:

| Field | Type | Description |
|---|---|---|
| `output_type` | string | One of 7 types β€” see below |
| `summary` | string | 2–3 sentences answering the original question |
| `findings` | string[] | 3–5 specific factual findings from the data |
| `recommendations` | string[] | 2–3 actionable next steps |
| `quality_score` | int \| null | QA Critic's 1–10 rating of the analysis quality |
| `quality_verdict` | string \| null | 1–2 sentence explanation of the score |

The `output_type` field determines which additional fields are populated.

---

### `output_type: "chart"`

Visual comparison of numeric values.

| Field | Type | Notes |
|---|---|---|
| `chart_type` | string | `"bar"` `"line"` `"pie"` `"scatter"` `"funnel"` `"radar"` |
| `chart_title` | string | |
| `x_axis_label` | string \| null | |
| `y_axis_label` | string \| null | |
| `data_points` | DataPoint[] | |
| `radar_b_label` | string \| null | Label for the second radar series (when using `value2`) |

**DataPoint:**
```ts
{
  label:    string          // category name or axis label
  value:    number          // primary value (Y axis, series A)
  category: string | null   // optional grouping
  x_value:  number | null   // scatter: X axis value
  value2:   number | null   // radar: second series value
}
```

**Chart type guide:**
- `bar` β€” named categories (products, regions, error types)
- `line` β€” sequential time periods (days, weeks, months)
- `pie` β€” parts of a whole, 2–6 slices
- `scatter` β€” correlation; `x_value` and `value` set per point
- `funnel` β€” sequential stages with drop-off (conversion pipelines)
- `radar` β€” multi-attribute profile; optionally dual-series with `value2` + `radar_b_label`

```json
{
  "output_type": "chart",
  "chart_type": "bar",
  "chart_title": "Top 5 Products by Revenue β€” Q3",
  "y_axis_label": "Revenue (USD)",
  "data_points": [
    { "label": "Widget A", "value": 84200, "category": "hardware" },
    { "label": "Widget B", "value": 61500, "category": "hardware" }
  ],
  "summary": "Widget A led Q3 with $84,200 in revenue...",
  "findings": ["Widget A outperformed Q2 by 18%"],
  "recommendations": ["Increase Widget A inventory for Q4"],
  "quality_score": 9,
  "quality_verdict": "All top products identified with exact revenue figures."
}
```

---

### `output_type: "metrics"`

A dashboard of key numbers or KPIs.

| Field | Type |
|---|---|
| `metrics` | MetricItem[] |

**MetricItem:**
```ts
{
  label:   string          // e.g. "Total Requests"
  value:   string          // formatted string: "541,466" / "98.5%" / "$4.2M"
  unit:    string | null   // e.g. "ms" or "req/hr"
  trend:   "up" | "down" | "neutral" | null
  change:  string | null   // e.g. "+12%" or "-3.2"
  context: string | null   // e.g. "vs last week"
}
```

```json
{
  "output_type": "metrics",
  "metrics": [
    { "label": "Avg Latency", "value": "142", "unit": "ms", "trend": "down", "change": "-18%", "context": "vs last week" },
    { "label": "Error Rate",  "value": "0.4%", "trend": "up", "change": "+0.1pp" }
  ],
  "summary": "System performance improved significantly this week...",
  "findings": ["P99 latency dropped from 890ms to 610ms"],
  "recommendations": ["Investigate remaining error spike at 03:00 UTC"]
}
```

---

### `output_type: "code"`

Runnable scripts, queries, or algorithms.

| Field | Type |
|---|---|
| `code_blocks` | CodeBlock[] (1–3 blocks) |

**CodeBlock:**
```ts
{
  language: "python" | "sql" | "bash" | "r" | "javascript"
  title:    string   // short label, e.g. "Filter failed logins"
  code:     string   // runnable code snippet
}
```

```json
{
  "output_type": "code",
  "code_blocks": [
    {
      "language": "python",
      "title": "Load and filter Q3 sales",
      "code": "import pandas as pd\ndf = pd.read_csv('sales.csv')\nq3 = df[df['quarter'] == 'Q3']\ntop5 = q3.groupby('product')['revenue'].sum().nlargest(5)"
    }
  ],
  "summary": "This script loads the sales CSV and extracts Q3 top performers.",
  "findings": ["Widget A leads Q3 at $84,200"],
  "recommendations": ["Run weekly to track ranking shifts"]
}
```

---

### `output_type: "table"`

A labelled grid of rows and columns (up to 20 rows).

| Field | Type |
|---|---|
| `table_headers` | string[] |
| `table_rows` | string[][] (each row same length as headers) |

```json
{
  "output_type": "table",
  "table_headers": ["Product", "Q3 Revenue", "Q2 Revenue", "Growth"],
  "table_rows": [
    ["Widget A", "$84,200", "$71,300", "+18%"],
    ["Widget B", "$61,500", "$58,900", "+4.4%"]
  ],
  "summary": "Five products drove 72% of Q3 revenue.",
  "findings": ["Widget A is the only product with >10% QoQ growth"],
  "recommendations": ["Prioritise Widget A restocking before Q4"]
}
```

---

### `output_type: "comparison"`

Side-by-side comparison of two named entities across multiple metrics.

| Field | Type |
|---|---|
| `comparison_a_label` | string | Name of entity A (e.g. "Q1 2024") |
| `comparison_b_label` | string | Name of entity B (e.g. "Q2 2024") |
| `comparison_rows` | ComparisonRow[] |

**ComparisonRow:**
```ts
{
  metric:  string                       // e.g. "Avg Revenue"
  value_a: string                       // formatted value for A
  value_b: string                       // formatted value for B
  winner:  "a" | "b" | "tie" | null    // which entity wins on this metric
}
```

```json
{
  "output_type": "comparison",
  "comparison_a_label": "Model A",
  "comparison_b_label": "Model B",
  "comparison_rows": [
    { "metric": "Accuracy",  "value_a": "94.2%", "value_b": "91.7%", "winner": "a" },
    { "metric": "Latency",   "value_a": "340ms", "value_b": "180ms", "winner": "b" },
    { "metric": "F1 Score",  "value_a": "0.91",  "value_b": "0.91",  "winner": "tie" }
  ],
  "summary": "Model A is more accurate; Model B is faster...",
  "findings": ["Model B has 47% lower latency"],
  "recommendations": ["Use Model A for batch, Model B for real-time"]
}
```

---

### `output_type: "heatmap"`

A matrix of numeric values β€” correlation tables, frequency grids, time-of-day activity.

| Field | Type |
|---|---|
| `heatmap_title` | string \| null |
| `heatmap_row_labels` | string[] | Max 10 |
| `heatmap_col_labels` | string[] | Max 10 |
| `heatmap_values` | number[][] | `[row_idx][col_idx]` β€” same dimensions as labels |

```json
{
  "output_type": "heatmap",
  "heatmap_title": "Error frequency by hour and day",
  "heatmap_row_labels": ["Mon", "Tue", "Wed"],
  "heatmap_col_labels": ["00:00", "06:00", "12:00", "18:00"],
  "heatmap_values": [
    [2, 0, 5, 12],
    [1, 0, 3, 8],
    [4, 1, 6, 15]
  ],
  "summary": "Errors peak at 18:00 across all weekdays...",
  "findings": ["Wednesday 18:00 has the highest error count (15)"],
  "recommendations": ["Schedule maintenance windows at 06:00"]
}
```

---

### `output_type: "report"`

Qualitative or narrative findings. No extra fields β€” only the common fields apply.

```json
{
  "output_type": "report",
  "summary": "Error rate spiked 300% between 2–4 AM on three consecutive nights.",
  "findings": ["HTTP 500 errors concentrated at 02:15–03:45 UTC"],
  "recommendations": ["Investigate cron job scheduled at 02:00 UTC"],
  "quality_score": 7,
  "quality_verdict": "Question answered but specific error counts were not available."
}
```

---

## Minimal reusable client

### JavaScript (full client)

```js
const API = 'https://pymite6941-data-analyst-ai-agent.hf.space'

/**
 * @param {string} context      - What to analyze
 * @param {File[]} files        - Optional data files (up to 3)
 * @param {(line: string) => void} onLog    - Called for each log line
 * @param {(result: object) => void} onResult - Called with the parsed result object
 */
async function analyze(context, files = [], onLog, onResult) {
  const form = new FormData()
  form.append('context', context)
  files.forEach((f) => form.append('files', f))

  const res = await fetch(`${API}/analyze`, { method: 'POST', body: form })

  if (!res.ok) {
    const { error } = await res.json()
    throw new Error(error)
  }

  const reader = res.body.getReader()
  const decoder = new TextDecoder()
  let buf = ''

  while (true) {
    const { done, value } = await reader.read()
    if (done) break
    buf += decoder.decode(value, { stream: true })
    const lines = buf.split('\n')
    buf = lines.pop()
    for (const raw of lines) {
      if (!raw.startsWith('data: ')) continue
      const payload = JSON.parse(raw.slice(6))
      if (payload === '__DONE__') return
      if (payload?.type === 'result') onResult(JSON.parse(payload.content))
      else onLog(payload)
    }
  }
}

// Usage
analyze(
  'Show me which products had the highest Q3 revenue',
  [document.querySelector('input[type=file]').files[0]],
  (line) => console.log('[LOG]', line),
  (result) => {
    console.log('Type:', result.output_type)
    console.log('Summary:', result.summary)
    console.log('Quality:', result.quality_score, 'β€”', result.quality_verdict)
  },
)
```

### Python (full client)

```python
import json
import requests

API = 'https://pymite6941-data-analyst-ai-agent.hf.space'

def analyze(context, file_paths=None, on_log=print, on_result=print):
    """
    Runs the DataFlow pipeline and streams results.
    
    Args:
        context:    str β€” what to analyze
        file_paths: list[str] β€” optional local file paths (up to 3)
        on_log:     callable(str) β€” called for each log line
        on_result:  callable(dict) β€” called with the final parsed result
    """
    files = []
    handles = []
    if file_paths:
        for path in file_paths[:3]:
            h = open(path, 'rb')
            handles.append(h)
            files.append(('files', (path.split('/')[-1], h)))

    try:
        res = requests.post(
            f'{API}/analyze',
            data={'context': context},
            files=files or None,
            stream=True,
            timeout=660,   # slightly over the 10-min server timeout
        )
        res.raise_for_status()

        for raw in res.iter_lines():
            line = raw.decode('utf-8')
            if not line.startswith('data: '):
                continue
            payload = json.loads(line[6:])
            if payload == '__DONE__':
                break
            if isinstance(payload, dict) and payload.get('type') == 'result':
                on_result(json.loads(payload['content']))
            else:
                on_log(payload)
    finally:
        for h in handles:
            h.close()


# Usage β€” no file
analyze('What are the main causes of HTTP 500 errors in an e-commerce backend?')

# Usage β€” with CSV
analyze(
    context='Show me monthly revenue trends and flag any anomalies',
    file_paths=['sales_2024.csv'],
    on_log=lambda line: print(f'  {line}'),
    on_result=lambda r: print(json.dumps(r, indent=2)),
)
```

---

## Output type reference

| `output_type` | When the agent picks it | Key extra fields |
|---|---|---|
| `code` | Answer is or includes runnable code/queries | `code_blocks` |
| `metrics` | Answer is a set of KPIs or key numbers | `metrics` |
| `comparison` | Two named entities compared across metrics | `comparison_a_label`, `comparison_b_label`, `comparison_rows` |
| `heatmap` | Data is a matrix (correlation, frequency, activity) | `heatmap_row_labels`, `heatmap_col_labels`, `heatmap_values` |
| `table` | Ranked/multi-attribute list | `table_headers`, `table_rows` |
| `chart` | Visual comparison of 2+ numeric values | `chart_type`, `data_points` |
| `report` | Qualitative or narrative findings | *(common fields only)* |

The agent picks the **first** type in this priority order whose condition is met.

---

## Pipeline internals

The `/analyze` endpoint runs a sequential 6-agent CrewAI pipeline:

1. **Context Agent** β€” rewrites the user's context into an unambiguous analysis directive
2. **Data Quality Inspector** β€” reads uploaded files, reports column names, row counts, and data issues
3. **Prompt Engineer** β€” converts the directive + quality report into a step-by-step analysis prompt
4. **Senior Data Analyst** β€” reads files exactly once with FileReadTool and executes the analysis
5. **Output Formatter** β€” serialises findings into one of the 7 output types above
6. **QA Critic** β€” rates the completed analysis 1–10 and writes a verdict

On upstream 429/402/404/503/529 errors, the pipeline automatically rotates across a pool of **GitHub Models** (`gpt-4o-mini`, `gpt-4o`) and **OpenRouter** free-tier models and retries. OpenRouter's free models share one daily quota per key, so when any of them returns the daily 429 the whole OpenRouter pool is cooled together and rotation falls back to GitHub Models (which has an independent quota). Hard timeout: 10 minutes.