Spaces:
Sleeping

doluong6 commited on
Commit
ce7cbdd
·
verified ·
1 Parent(s): e8e48cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -10
app.py CHANGED
@@ -732,6 +732,10 @@ def chuan_hoa_ngay(value):
732
  async def quan_ly_hoa_chat():
733
  try:
734
  HF_TOKEN = os.environ.get("HF_TOKEN")
 
 
 
 
735
  file_path = hf_hub_download(
736
  repo_id="doluong6/Quatest1",
737
  filename="Quan_ly_hoa_chat.xlsx",
@@ -740,31 +744,36 @@ async def quan_ly_hoa_chat():
740
  cache_dir="/tmp"
741
  )
742
 
 
743
  df = pd.read_excel(file_path, sheet_name="HOA CHAT SU DUNG")
744
  df = df.fillna("")
745
 
746
- # Cột Ngày nhập kho và Ngày hết hạn có thể là datetime
747
  def xu_ly_tinh_trang(row):
748
  try:
749
- ngay_nhap = pd.to_datetime(row['NGÀY NHẬP KHO'])
750
- het_han = pd.to_datetime(row['NGÀY HẾT HẠN'])
751
- hom_nay = pd.Timestamp.today()
752
- con_lai = (het_han - hom_nay).days
753
- if con_lai < 0:
754
- return f"HẾT HẠN ({ngay_nhap.date()} - {het_han.date()})"
 
 
 
755
  else:
756
- return f"CÒN: {con_lai} NGÀY ({ngay_nhap.date()} - {het_han.date()})"
757
  except:
758
- return row.get("TÌNH TRẠNG", "") # fallback
759
 
760
  df["TÌNH TRẠNG"] = df.apply(xu_ly_tinh_trang, axis=1)
761
 
 
762
  columns = df.columns.tolist()
763
  rows = df.astype(str).values.tolist()
764
 
765
  return {"columns": columns, "rows": rows}
766
 
767
  except Exception as e:
768
- raise HTTPException(status_code=500, detail=f"Lỗi đọc danh sách hóa chất: {str(e)}")
769
 
770
 
 
732
  async def quan_ly_hoa_chat():
733
  try:
734
  HF_TOKEN = os.environ.get("HF_TOKEN")
735
+ if not HF_TOKEN:
736
+ raise ValueError("Biến môi trường HF_TOKEN chưa được thiết lập.")
737
+
738
+ # Tải file từ Hugging Face dataset
739
  file_path = hf_hub_download(
740
  repo_id="doluong6/Quatest1",
741
  filename="Quan_ly_hoa_chat.xlsx",
 
744
  cache_dir="/tmp"
745
  )
746
 
747
+ # Đọc dữ liệu từ sheet
748
  df = pd.read_excel(file_path, sheet_name="HOA CHAT SU DUNG")
749
  df = df.fillna("")
750
 
751
+ # Chuẩn hóa ngày hết hạn + tính tình trạng
752
  def xu_ly_tinh_trang(row):
753
  try:
754
+ het_han = pd.to_datetime(row['NGÀY HẾT HẠN'], errors='coerce')
755
+ today = pd.Timestamp.today()
756
+ if pd.isna(het_han):
757
+ return "KHÔNG RÕ"
758
+ diff = (het_han - today).days
759
+ if diff < 0:
760
+ return "HẾT HẠN"
761
+ elif diff <= 30:
762
+ return f"CÒN: {diff} NGÀY ⚠"
763
  else:
764
+ return f"CÒN: {diff} NGÀY"
765
  except:
766
+ return "KHÔNG RÕ"
767
 
768
  df["TÌNH TRẠNG"] = df.apply(xu_ly_tinh_trang, axis=1)
769
 
770
+ # Trả về kết quả
771
  columns = df.columns.tolist()
772
  rows = df.astype(str).values.tolist()
773
 
774
  return {"columns": columns, "rows": rows}
775
 
776
  except Exception as e:
777
+ raise HTTPException(status_code=500, detail=f"Lỗi đọc danh sách hóa chất: {str(e)}\n{traceback.format_exc()}")
778
 
779