Spaces:
Sleeping

doluong6 commited on
Commit
d9ed467
·
verified ·
1 Parent(s): 3c6ec61

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -15
app.py CHANGED
@@ -550,19 +550,15 @@ async def xem_pdf(file_name: str):
550
  repo_type = "dataset"
551
  folder = "Tai_lieu"
552
 
553
- normalized_request_name = unidecode(file_name).lower().strip()
554
-
555
- print(f"📄 Đang tìm file: {normalized_request_name}")
556
 
557
  files = list_repo_files(repo_id=repo_id, repo_type=repo_type, token=HF_TOKEN)
558
  tai_lieu_files = [f for f in files if f.startswith(folder + "/") and f.lower().endswith(".pdf")]
559
 
560
  matched_file = None
561
  for f in tai_lieu_files:
562
- original_name = os.path.basename(f)
563
- normalized_file_name = unidecode(original_name).lower().strip()
564
- print(f"🔍 So sánh với: {normalized_file_name}")
565
- if normalized_file_name == normalized_request_name:
566
  matched_file = f
567
  break
568
 
@@ -571,12 +567,17 @@ async def xem_pdf(file_name: str):
571
 
572
  print(f"✅ Tải file: {matched_file}")
573
 
 
 
 
 
 
574
  local_path = hf_hub_download(
575
  repo_id=repo_id,
576
  filename=matched_file,
577
  repo_type=repo_type,
578
  token=HF_TOKEN,
579
- cache_dir="/tmp/hf_cache"
580
  )
581
 
582
  return FileResponse(local_path, media_type="application/pdf")
@@ -584,15 +585,14 @@ async def xem_pdf(file_name: str):
584
  except Exception as e:
585
  print("❌ LỖI:", traceback.format_exc())
586
  return JSONResponse(status_code=500, content={"detail": str(e)})
 
 
587
  @app.get("/danh_sach_pdf")
588
  async def danh_sach_pdf():
589
  try:
590
  HF_TOKEN = os.environ.get("HF_TOKEN")
591
- repo_id = "doluong6/Quatest1"
592
-
593
- all_files = list_repo_files(repo_id=repo_id, repo_type="dataset", token=HF_TOKEN)
594
- pdfs = [f.split("/")[-1] for f in all_files if f.startswith("Tai_lieu/") and f.endswith(".pdf")]
595
-
596
- return {"pdfs": pdfs}
597
  except Exception as e:
598
- return {"pdfs": [], "error": str(e)}
 
550
  repo_type = "dataset"
551
  folder = "Tai_lieu"
552
 
553
+ request_name = file_name.strip().lower()
554
+ print(f"📄 Đang tìm file: {request_name}")
 
555
 
556
  files = list_repo_files(repo_id=repo_id, repo_type=repo_type, token=HF_TOKEN)
557
  tai_lieu_files = [f for f in files if f.startswith(folder + "/") and f.lower().endswith(".pdf")]
558
 
559
  matched_file = None
560
  for f in tai_lieu_files:
561
+ if os.path.basename(f).lower() == request_name:
 
 
 
562
  matched_file = f
563
  break
564
 
 
567
 
568
  print(f"✅ Tải file: {matched_file}")
569
 
570
+ # ✅ Set thư mục cache có quyền ghi
571
+ cache_dir = "/tmp/hf_cache"
572
+ os.makedirs(cache_dir, exist_ok=True)
573
+ os.environ["HF_HUB_CACHE"] = cache_dir
574
+
575
  local_path = hf_hub_download(
576
  repo_id=repo_id,
577
  filename=matched_file,
578
  repo_type=repo_type,
579
  token=HF_TOKEN,
580
+ cache_dir=cache_dir
581
  )
582
 
583
  return FileResponse(local_path, media_type="application/pdf")
 
585
  except Exception as e:
586
  print("❌ LỖI:", traceback.format_exc())
587
  return JSONResponse(status_code=500, content={"detail": str(e)})
588
+
589
+
590
  @app.get("/danh_sach_pdf")
591
  async def danh_sach_pdf():
592
  try:
593
  HF_TOKEN = os.environ.get("HF_TOKEN")
594
+ all_files = list_repo_files("doluong6/Quatest1", repo_type="dataset", token=HF_TOKEN)
595
+ pdfs = [f.split("/")[-1] for f in all_files if f.startswith("Tai_lieu/") and f.lower().endswith(".pdf")]
596
+ return JSONResponse(content={"pdfs": pdfs})
 
 
 
597
  except Exception as e:
598
+ raise HTTPException(status_code=500, detail=f"Lỗi khi lấy danh sách PDF: {str(e)}")