Code-API commited on
Commit
ea0e2eb
·
1 Parent(s): a81801b

deploy: auto-deploy 17:41:13

Browse files
Files changed (1) hide show
  1. app/api/v1/csv_analysis.py +28 -9
app/api/v1/csv_analysis.py CHANGED
@@ -3,7 +3,7 @@ from __future__ import annotations
3
  import json
4
  from typing import Annotated, Any, Dict, List, Optional
5
 
6
- from fastapi import APIRouter, Depends, File, Form, HTTPException, Request, UploadFile
7
  from pydantic import BaseModel, Field, ValidationError
8
 
9
  from app.api.deps import require_auth
@@ -34,7 +34,7 @@ class _AIResponse(BaseModel):
34
  visualization: List[_VisualizationBlock] = []
35
  message: str = ""
36
 
37
- router = APIRouter()
38
  _settings = get_settings()
39
  _MAX_UPLOAD_BYTES = _settings.max_upload_bytes
40
 
@@ -46,7 +46,6 @@ _MAX_UPLOAD_BYTES = _settings.max_upload_bytes
46
  async def get_csv_info(
47
  files: Annotated[Optional[List[UploadFile]], File(description="CSV files to inspect (max 10 total with URLs)")] = None,
48
  urls: Annotated[Optional[str], Form(description="JSON array of file URLs (max 10 total with files)")] = None,
49
- token: str = Depends(require_auth),
50
  ):
51
  parsed_urls: List[str] = []
52
  if urls:
@@ -177,16 +176,33 @@ async def get_csv_info(
177
  async def csv_chat(
178
  request: Request,
179
  file: Annotated[Optional[UploadFile], File(description="CSV file to analyze")] = None,
180
- url: Annotated[Optional[str], Form(description="URL to a CSV file")] = None,
181
- query: str = Form(..., description="Natural language query about the CSV data"),
182
- token: str = Depends(require_auth),
183
  ):
184
- if not file and not url:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
  raise HTTPException(status_code=400, detail="Provide either a file or a URL")
186
- if file and url:
187
  raise HTTPException(status_code=400, detail="Provide either a file or a URL, not both")
188
 
189
- if file:
190
  data = await file.read()
191
  if len(data) > _MAX_UPLOAD_BYTES:
192
  raise HTTPException(status_code=413, detail=f"File exceeds {_settings.max_upload_mb} MB limit")
@@ -196,6 +212,9 @@ async def csv_chat(
196
  else:
197
  source = url
198
 
 
 
 
199
  metadata = await get_dataset_info(source)
200
  system_prompt = get_csv_system_prompt(metadata)
201
 
 
3
  import json
4
  from typing import Annotated, Any, Dict, List, Optional
5
 
6
+ from fastapi import APIRouter, Depends, File, HTTPException, Request, UploadFile
7
  from pydantic import BaseModel, Field, ValidationError
8
 
9
  from app.api.deps import require_auth
 
34
  visualization: List[_VisualizationBlock] = []
35
  message: str = ""
36
 
37
+ router = APIRouter(dependencies=[Depends(require_auth)])
38
  _settings = get_settings()
39
  _MAX_UPLOAD_BYTES = _settings.max_upload_bytes
40
 
 
46
  async def get_csv_info(
47
  files: Annotated[Optional[List[UploadFile]], File(description="CSV files to inspect (max 10 total with URLs)")] = None,
48
  urls: Annotated[Optional[str], Form(description="JSON array of file URLs (max 10 total with files)")] = None,
 
49
  ):
50
  parsed_urls: List[str] = []
51
  if urls:
 
176
  async def csv_chat(
177
  request: Request,
178
  file: Annotated[Optional[UploadFile], File(description="CSV file to analyze")] = None,
 
 
 
179
  ):
180
+ content_type = request.headers.get("content-type", "")
181
+
182
+ url: Optional[str] = None
183
+ query: Optional[str] = None
184
+
185
+ if "application/json" in content_type:
186
+ try:
187
+ body = await request.json()
188
+ url = body.get("url")
189
+ query = body.get("query")
190
+ except Exception as exc:
191
+ raise HTTPException(status_code=400, detail=f"Invalid JSON body: {exc}")
192
+ else:
193
+ form = await request.form()
194
+ url = form.get("url")
195
+ query = form.get("query")
196
+
197
+ has_file = file is not None
198
+ has_url = bool(url)
199
+
200
+ if not has_file and not has_url:
201
  raise HTTPException(status_code=400, detail="Provide either a file or a URL")
202
+ if has_file and has_url:
203
  raise HTTPException(status_code=400, detail="Provide either a file or a URL, not both")
204
 
205
+ if has_file:
206
  data = await file.read()
207
  if len(data) > _MAX_UPLOAD_BYTES:
208
  raise HTTPException(status_code=413, detail=f"File exceeds {_settings.max_upload_mb} MB limit")
 
212
  else:
213
  source = url
214
 
215
+ if not query:
216
+ raise HTTPException(status_code=400, detail="Query is required")
217
+
218
  metadata = await get_dataset_info(source)
219
  system_prompt = get_csv_system_prompt(metadata)
220