Download app/backend/api/upload.py from jkbytecrafter/autonomous-data-analyst: direct link, hf CLI and curl.
- Browser
- Download file 1.15 kB
-
https://huggingface.co/spaces/jkbytecrafter/autonomous-data-analyst/resolve/main/app/backend/api/upload.py
- Command line
-
hf download hf://spaces/jkbytecrafter/autonomous-data-analyst/app/backend/api/upload.py
-
curl -L -o upload.py https://huggingface.co/spaces/jkbytecrafter/autonomous-data-analyst/resolve/main/app/backend/api/upload.py
1.15 kB
| 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() | |
| 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)}") | |