Spaces:
Sleeping
Sleeping
Download app.py from tamdang1350/test: direct link, hf CLI and curl.
- Browser
- Download file 1.82 kB
-
https://huggingface.co/spaces/tamdang1350/test/resolve/main/app.py
- Command line
-
hf download hf://spaces/tamdang1350/test/app.py
-
curl -L -o app.py https://huggingface.co/spaces/tamdang1350/test/resolve/main/app.py
1.82 kB
| from fastapi import FastAPI, UploadFile, File, HTTPException | |
| from fastapi.responses import JSONResponse | |
| import pandas as pd | |
| import json | |
| import io | |
| app = FastAPI() | |
| def root(): | |
| return {"status": "ok", "message": "Server is running"} | |
| # --- Endpoint 1: Upload CSV → trả về thống kê --- | |
| async def process_csv(file: UploadFile = File(...)): | |
| if not file.filename.endswith(".csv"): | |
| raise HTTPException(status_code=400, detail="Chỉ chấp nhận file CSV") | |
| content = await file.read() | |
| df = pd.read_csv(io.BytesIO(content)) | |
| result = { | |
| "filename": file.filename, | |
| "rows": len(df), | |
| "columns": list(df.columns), | |
| "summary": json.loads(df.describe().to_json()) | |
| } | |
| return JSONResponse(result) | |
| # --- Endpoint 2: Upload JSON → lọc/xử lý dữ liệu --- | |
| async def process_json(file: UploadFile = File(...)): | |
| content = await file.read() | |
| data = json.loads(content) | |
| # Ví dụ: đếm keys, trả về cấu trúc | |
| result = { | |
| "filename": file.filename, | |
| "type": type(data).__name__, | |
| "length": len(data) if isinstance(data, list) else len(data.keys()), | |
| "preview": data[:3] if isinstance(data, list) else dict(list(data.items())[:3]) | |
| } | |
| return JSONResponse(result) | |
| # --- Endpoint 3: Xử lý text thuần --- | |
| async def process_text(file: UploadFile = File(...)): | |
| content = await file.read() | |
| text = content.decode("utf-8") | |
| lines = text.splitlines() | |
| result = { | |
| "filename": file.filename, | |
| "lines": len(lines), | |
| "words": sum(len(l.split()) for l in lines), | |
| "chars": len(text), | |
| "preview": lines[:5] | |
| } | |
| return JSONResponse(result) |