File size: 1,149 Bytes
d77e086
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import APIRouter, UploadFile, File, HTTPException
from fastapi.responses import JSONResponse
import sys
import os

# Add root to sys.path to allow imports
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))))

from app.utils.data_ingestion import save_uploaded_file, load_dataset, infer_dataset_schema

router = APIRouter()

@router.post("/upload")
async def upload_dataset(file: UploadFile = File(...)):
    if not file.filename.endswith(('.csv', '.xls', '.xlsx', '.json')):
        raise HTTPException(status_code=400, detail="Invalid file format. Supported: CSV, Excel, JSON")
    
    try:
        # Save file
        file_path = save_uploaded_file(file)
        
        # Load and infer
        df = load_dataset(file_path)
        schema = infer_dataset_schema(df)
        
        return JSONResponse(content={
            "message": "File uploaded and parsed successfully",
            "file_path": file_path,
            "schema": schema
        })
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"An error occurred while processing the file: {str(e)}")