Spaces:
Sleeping

doluong6 commited on
Commit
5d2ae37
·
verified ·
1 Parent(s): a99b6db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -10
app.py CHANGED
@@ -537,22 +537,46 @@ async def doanh_thu_noi_bo():
537
  traceback.print_exc()
538
  return JSONResponse(status_code=500, content={"error": str(e)})
539
  @app.get("/xem_pdf")
540
- async def xem_pdf(file_name: str = Query(..., description="Tên file PDF muốn xem")):
541
  try:
542
- # ✅ Lấy HF token từ biến môi trường (đã thiết lập trong Hugging Face Space)
543
  HF_TOKEN = os.environ.get("HF_TOKEN")
544
-
545
- # ✅ Tải file từ dataset Hugging Face
546
- file_path = hf_hub_download(
547
- repo_id="doluong6/Quatest1",
548
- filename=f"Tai_lieu/{file_name}",
549
- repo_type="dataset",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
550
  token=HF_TOKEN,
551
  cache_dir="/tmp"
552
  )
553
- return FileResponse(file_path, media_type="application/pdf", filename=file_name)
 
 
554
  except Exception as e:
555
- raise HTTPException(status_code=404, detail=f"Không tìm thấy file: {file_name}")
556
  @app.get("/danh_sach_pdf")
557
  async def danh_sach_pdf():
558
  try:
 
537
  traceback.print_exc()
538
  return JSONResponse(status_code=500, content={"error": str(e)})
539
  @app.get("/xem_pdf")
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
+ # ✅ Lấy toàn bộ danh sách file trong thư mục Tai_lieu/
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,
571
+ repo_type=repo_type,
572
  token=HF_TOKEN,
573
  cache_dir="/tmp"
574
  )
575
+
576
+ return FileResponse(local_path, media_type="application/pdf")
577
+
578
  except Exception as e:
579
+ raise HTTPException(status_code=500, detail=str(e))
580
  @app.get("/danh_sach_pdf")
581
  async def danh_sach_pdf():
582
  try: