Update app.py
Browse files
app.py
CHANGED
|
@@ -540,31 +540,34 @@ async def doanh_thu_noi_bo():
|
|
| 540 |
async def xem_pdf(file_name: str):
|
| 541 |
try:
|
| 542 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
|
|
|
|
|
|
|
|
|
| 543 |
repo_id = "doluong6/Quatest1"
|
| 544 |
repo_type = "dataset"
|
| 545 |
folder = "Tai_lieu"
|
| 546 |
|
| 547 |
-
# ✅ Chuẩn hóa tên cần tìm (bỏ dấu, thường hóa)
|
| 548 |
normalized_request_name = unidecode(file_name).lower().strip()
|
| 549 |
|
| 550 |
-
|
|
|
|
| 551 |
files = list_repo_files(repo_id=repo_id, repo_type=repo_type, token=HF_TOKEN)
|
| 552 |
tai_lieu_files = [f for f in files if f.startswith(folder + "/") and f.lower().endswith(".pdf")]
|
| 553 |
|
| 554 |
-
# ✅ Duyệt để tìm file khớp tên không dấu
|
| 555 |
matched_file = None
|
| 556 |
for f in tai_lieu_files:
|
| 557 |
original_name = os.path.basename(f)
|
| 558 |
normalized_file_name = unidecode(original_name).lower().strip()
|
| 559 |
-
|
| 560 |
if normalized_file_name == normalized_request_name:
|
| 561 |
matched_file = f
|
| 562 |
break
|
| 563 |
|
| 564 |
if not matched_file:
|
| 565 |
-
raise HTTPException(status_code=404, detail=f"Không tìm thấy file: {file_name}")
|
|
|
|
|
|
|
| 566 |
|
| 567 |
-
# ✅ Tải file khớp về từ Hugging Face Dataset
|
| 568 |
local_path = hf_hub_download(
|
| 569 |
repo_id=repo_id,
|
| 570 |
filename=matched_file,
|
|
@@ -576,7 +579,8 @@ async def xem_pdf(file_name: str):
|
|
| 576 |
return FileResponse(local_path, media_type="application/pdf")
|
| 577 |
|
| 578 |
except Exception as e:
|
| 579 |
-
|
|
|
|
| 580 |
@app.get("/danh_sach_pdf")
|
| 581 |
async def danh_sach_pdf():
|
| 582 |
try:
|
|
|
|
| 540 |
async def xem_pdf(file_name: str):
|
| 541 |
try:
|
| 542 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 543 |
+
if not HF_TOKEN:
|
| 544 |
+
raise Exception("❌ Thiếu HF_TOKEN")
|
| 545 |
+
|
| 546 |
repo_id = "doluong6/Quatest1"
|
| 547 |
repo_type = "dataset"
|
| 548 |
folder = "Tai_lieu"
|
| 549 |
|
|
|
|
| 550 |
normalized_request_name = unidecode(file_name).lower().strip()
|
| 551 |
|
| 552 |
+
print(f"📄 Đang tìm file: {normalized_request_name}")
|
| 553 |
+
|
| 554 |
files = list_repo_files(repo_id=repo_id, repo_type=repo_type, token=HF_TOKEN)
|
| 555 |
tai_lieu_files = [f for f in files if f.startswith(folder + "/") and f.lower().endswith(".pdf")]
|
| 556 |
|
|
|
|
| 557 |
matched_file = None
|
| 558 |
for f in tai_lieu_files:
|
| 559 |
original_name = os.path.basename(f)
|
| 560 |
normalized_file_name = unidecode(original_name).lower().strip()
|
| 561 |
+
print(f"🔍 So sánh với: {normalized_file_name}")
|
| 562 |
if normalized_file_name == normalized_request_name:
|
| 563 |
matched_file = f
|
| 564 |
break
|
| 565 |
|
| 566 |
if not matched_file:
|
| 567 |
+
raise HTTPException(status_code=404, detail=f"❌ Không tìm thấy file: {file_name}")
|
| 568 |
+
|
| 569 |
+
print(f"✅ Tải file: {matched_file}")
|
| 570 |
|
|
|
|
| 571 |
local_path = hf_hub_download(
|
| 572 |
repo_id=repo_id,
|
| 573 |
filename=matched_file,
|
|
|
|
| 579 |
return FileResponse(local_path, media_type="application/pdf")
|
| 580 |
|
| 581 |
except Exception as e:
|
| 582 |
+
print("❌ LỖI:", traceback.format_exc())
|
| 583 |
+
return JSONResponse(status_code=500, content={"detail": str(e)})
|
| 584 |
@app.get("/danh_sach_pdf")
|
| 585 |
async def danh_sach_pdf():
|
| 586 |
try:
|