File size: 11,383 Bytes
64a008c
 
 
 
 
b7dddbe
5a01a63
64a008c
b7dddbe
64a008c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7dddbe
64a008c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea0e2eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64a008c
ea0e2eb
64a008c
 
ea0e2eb
64a008c
 
 
 
 
 
 
 
 
ea0e2eb
 
 
64a008c
 
 
 
 
 
 
 
faefb1f
64a008c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import json
from typing import Annotated, Any, Dict, List, Optional

from fastapi import APIRouter, File, Form, HTTPException, Request, UploadFile
from pydantic import BaseModel, ValidationError

from app.api.deps import get_redis_scripts
from app.config import get_settings
from app.services.chat_service import chat_completion
from app.services.csv_analysis_service import (
    execute_csv_chat_blocks,
    get_dataset_info,
)
from app.services.prompts import get_csv_system_prompt
from app.utils.json_utils import extract_json_blocks


class _AnalyzeBlock(BaseModel):
    description: str = ""
    python_code: str = ""


class _VisualizationBlock(BaseModel):
    description: str = ""
    python_code: str = ""


class _AIResponse(BaseModel):
    analyze: List[_AnalyzeBlock] = []
    visualization: List[_VisualizationBlock] = []
    message: str = ""

router = APIRouter()
_settings = get_settings()
_MAX_UPLOAD_BYTES = _settings.max_upload_bytes


@router.post(
    "/csv/info",
    summary="Get metadata for up to 10 CSV files (upload or URL)",
)
async def get_csv_info(
    files: Annotated[Optional[List[UploadFile]], File(description="CSV files to inspect (max 10 total with URLs)")] = None,
    urls: Annotated[Optional[str], Form(description="JSON array of file URLs (max 10 total with files)")] = None,
):
    parsed_urls: List[str] = []
    if urls:
        try:
            parsed_urls = json.loads(urls)
            if not isinstance(parsed_urls, list) or not all(isinstance(u, str) for u in parsed_urls):
                raise ValueError("urls must be a JSON array of strings")
        except (json.JSONDecodeError, ValueError) as exc:
            raise HTTPException(status_code=400, detail=str(exc))

    file_count = len(files) if files else 0
    url_count = len(parsed_urls)
    total = file_count + url_count

    if total == 0:
        raise HTTPException(status_code=400, detail="Provide at least one file or URL")
    if total > 10:
        raise HTTPException(status_code=400, detail=f"Maximum 10 sources allowed (got {total})")

    results: List[dict] = []

    if files:
        for f in files:
            try:
                data = await f.read()
            except Exception as exc:
                results.append({"source": getattr(f, "filename", "unknown"), "success": False, "error": f"Read error: {exc}"})
                continue

            if len(data) > _MAX_UPLOAD_BYTES:
                results.append({"source": f.filename or "unknown", "success": False, "error": f"File exceeds {_settings.max_upload_mb} MB limit"})
                continue

            if not data:
                results.append({"source": f.filename or "unknown", "success": False, "error": "Empty file"})
                continue

            try:
                meta = await get_dataset_info(data)
                meta["source"] = f.filename or "upload"
                results.append(meta)
            except Exception as exc:
                results.append({"source": f.filename or "upload", "success": False, "error": str(exc)})

    for url in parsed_urls:
        if not url.startswith(("http://", "https://")):
            results.append({"source": url, "success": False, "error": "Only http/https URLs are supported"})
            continue

        try:
            meta = await get_dataset_info(url)
            meta["source"] = url
            results.append(meta)
        except Exception as exc:
            results.append({"source": url, "success": False, "error": str(exc)})

    return {
        "success": True,
        "total": total,
        "succeeded": sum(1 for r in results if r.get("success")),
        "failed": sum(1 for r in results if not r.get("success")),
        "results": results,
    }


# @router.post(
#     "/csv/analyze",
#     summary="Execute Python analysis code against a CSV file (upload or URL)",
# )
# async def analyze_csv(
#     file: Annotated[Optional[UploadFile], File(description="CSV file to analyze")] = None,
#     url: Annotated[Optional[str], Form(description="URL to a CSV file")] = None,
#     code: str = Form(..., description="Python code to execute (df pre-loaded with CSV data)"),
#     token: str = Depends(require_auth),
# ):
#     if not file and not url:
#         raise HTTPException(status_code=400, detail="Provide either a file or a URL")

#     if file and url:
#         raise HTTPException(status_code=400, detail="Provide either a file or a URL, not both")

#     if file:
#         data = await file.read()
#         if len(data) > _MAX_UPLOAD_BYTES:
#             raise HTTPException(status_code=413, detail=f"File exceeds {_settings.max_upload_mb} MB limit")
#         if not data:
#             raise HTTPException(status_code=400, detail="Empty file")
#         result = await analyze_csv_dataset(data, code)
#     else:
#         result = await analyze_csv_dataset(url, code)

#     return result


# @router.post(
#     "/csv/chart",
#     summary="Generate a chart from CSV data and return as base64 PNG (upload or URL)",
# )
# async def chart_csv(
#     file: Annotated[Optional[UploadFile], File(description="CSV file for chart generation")] = None,
#     url: Annotated[Optional[str], Form(description="URL to a CSV file")] = None,
#     code: str = Form(..., description="Python chart code (df pre-loaded, use matplotlib/seaborn)"),
#     token: str = Depends(require_auth),
# ):
#     if not file and not url:
#         raise HTTPException(status_code=400, detail="Provide either a file or a URL")

#     if file and url:
#         raise HTTPException(status_code=400, detail="Provide either a file or a URL, not both")

#     if file:
#         data = await file.read()
#         if len(data) > _MAX_UPLOAD_BYTES:
#             raise HTTPException(status_code=413, detail=f"File exceeds {_settings.max_upload_mb} MB limit")
#         if not data:
#             raise HTTPException(status_code=400, detail="Empty file")
#         result = await create_csv_chart(data, code)
#     else:
#         result = await create_csv_chart(url, code)

#     return result


@router.post(
    "/csv/chat",
    summary="Chat with AI about a CSV file — returns analysis + chart code results",
)
async def csv_chat(
    request: Request,
    file: Annotated[Optional[UploadFile], File(description="CSV file to analyze")] = None,
):
    content_type = request.headers.get("content-type", "")

    url: Optional[str] = None
    query: Optional[str] = None

    if "application/json" in content_type:
        try:
            body = await request.json()
            url = body.get("url")
            query = body.get("query")
        except Exception as exc:
            raise HTTPException(status_code=400, detail=f"Invalid JSON body: {exc}")
    else:
        form = await request.form()
        url = form.get("url")
        query = form.get("query")

    has_file = file is not None
    has_url = bool(url)

    if not has_file and not has_url:
        raise HTTPException(status_code=400, detail="Provide either a file or a URL")
    if has_file and has_url:
        raise HTTPException(status_code=400, detail="Provide either a file or a URL, not both")

    if has_file:
        data = await file.read()
        if len(data) > _MAX_UPLOAD_BYTES:
            raise HTTPException(status_code=413, detail=f"File exceeds {_settings.max_upload_mb} MB limit")
        if not data:
            raise HTTPException(status_code=400, detail="Empty file")
        source: Any = data
    else:
        source = url

    if not query:
        raise HTTPException(status_code=400, detail="Query is required")

    metadata = await get_dataset_info(source)
    system_prompt = get_csv_system_prompt(metadata)

    messages = [
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": query},
    ]

    redis, scripts = get_redis_scripts(request)

    try:
        ai_response = await chat_completion(
            messages=messages,
            response_format={"type": "json_object"},
            max_tokens=12000,
            redis=redis,
            scripts=scripts,
        )
    except RuntimeError as e:
        raise HTTPException(status_code=502, detail=str(e))

    parsed = ai_response.get("parsed")
    if not parsed:
        choices = ai_response.get("choices", [])
        content = choices[0].get("message", {}).get("content", "") if choices else ""
        blocks = extract_json_blocks(content)
        if blocks:
            parsed = blocks[0]
        else:
            try:
                parsed = json.loads(content)
            except (json.JSONDecodeError, TypeError):
                pass

    if not isinstance(parsed, dict):
        return {
            "success": False,
            "message": None,
            "analyze": [],
            "visualizations": [],
            "error": "AI response was not valid JSON",
        }

    try:
        ai_data = _AIResponse(**parsed)
    except ValidationError as exc:
        return {
            "success": False,
            "message": None,
            "analyze": [],
            "visualizations": [],
            "error": f"AI response failed schema validation: {exc}",
        }

    message_text = ai_data.message
    has_content = bool(message_text.strip()) if message_text else False

    analyze_blocks_raw = [b.model_dump() for b in ai_data.analyze]
    viz_blocks_raw = [b.model_dump() for b in ai_data.visualization]

    exec_result = await execute_csv_chat_blocks(
        source=source,
        analyze_blocks=analyze_blocks_raw,
        viz_blocks=viz_blocks_raw,
    )

    if not exec_result["success"]:
        return {
            "success": False,
            "message": ai_data.message if has_content else None,
            "analyze": [],
            "visualizations": [],
            "error": exec_result.get("error", "Code execution failed"),
        }

    results = exec_result.get("results", {})
    raw_analyze = results.get("analyze", [])
    raw_visualizations = results.get("visualization", [])

    analyze_results: List[Dict[str, Any]] = []
    for i, block in enumerate(ai_data.analyze):
        raw = raw_analyze[i] if i < len(raw_analyze) else {}
        code = block.python_code.strip()
        if not code:
            continue
        analyze_results.append({
            "description": block.description,
            "code": code,
            "success": raw.get("success", False),
            "output": raw.get("output", ""),
            "error": raw.get("error"),
            "execution_time_ms": exec_result["execution_time_ms"],
        })

    viz_results: List[Dict[str, Any]] = []
    for i, block in enumerate(ai_data.visualization):
        raw = raw_visualizations[i] if i < len(raw_visualizations) else {}
        code = block.python_code.strip()
        if not code:
            continue
        viz_results.append({
            "description": block.description,
            "code": code,
            "success": raw.get("success", False),
            "image_base64": raw.get("image_base64"),
            "error": raw.get("error"),
            "execution_time_ms": exec_result["execution_time_ms"],
        })

    return {
        "success": True,
        "message": ai_data.message if has_content else None,
        "analyze": analyze_results,
        "visualizations": viz_results,
        "error": None,
    }