Create upload_kemtheo.py
Browse files- routes/upload_kemtheo.py +38 -0
routes/upload_kemtheo.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, UploadFile, File, Form
|
| 2 |
+
from huggingface_hub import upload_file
|
| 3 |
+
import os
|
| 4 |
+
import tempfile
|
| 5 |
+
|
| 6 |
+
router = APIRouter()
|
| 7 |
+
|
| 8 |
+
@router.post("/upload_kemtheo")
|
| 9 |
+
async def upload_kemtheo(
|
| 10 |
+
khach_hang: str = Form(...),
|
| 11 |
+
loai_tai_lieu: str = Form(...), # Ví dụ: Scan, Hop_dong_thanh_ly, Ho_so_cong_tac, Giay_chung_nhan
|
| 12 |
+
file: UploadFile = File(...)
|
| 13 |
+
):
|
| 14 |
+
try:
|
| 15 |
+
# Lưu file tạm vào thư mục hệ thống
|
| 16 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
| 17 |
+
temp_file_path = os.path.join(temp_dir, file.filename)
|
| 18 |
+
with open(temp_file_path, "wb") as f:
|
| 19 |
+
content = await file.read()
|
| 20 |
+
f.write(content)
|
| 21 |
+
|
| 22 |
+
# Định dạng đường dẫn trên Hugging Face dataset
|
| 23 |
+
remote_path = f"Khach_hang/{khach_hang}/{loai_tai_lieu}/{file.filename}"
|
| 24 |
+
|
| 25 |
+
# Upload lên Hugging Face dataset
|
| 26 |
+
upload_file(
|
| 27 |
+
path_or_fileobj=temp_file_path,
|
| 28 |
+
path_in_repo=remote_path,
|
| 29 |
+
repo_id="doluong6/Quatest1",
|
| 30 |
+
repo_type="dataset",
|
| 31 |
+
token=os.environ.get("HF_TOKEN"),
|
| 32 |
+
commit_message=f"📎 Upload {loai_tai_lieu} cho {khach_hang}: {file.filename}"
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
return {"status": "success", "message": f"Đã upload {file.filename} vào {remote_path}"}
|
| 36 |
+
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return {"status": "error", "message": str(e)}
|