Spaces:
Sleeping
Sleeping
Add live PO data auto-load from private HF dataset
Browse files- requirements.txt +1 -0
- streamlit_app.py +31 -4
requirements.txt
CHANGED
|
@@ -1,3 +1,4 @@
|
|
| 1 |
python-docx>=1.0.0
|
| 2 |
openpyxl>=3.1.0
|
| 3 |
streamlit>=1.30.0
|
|
|
|
|
|
| 1 |
python-docx>=1.0.0
|
| 2 |
openpyxl>=3.1.0
|
| 3 |
streamlit>=1.30.0
|
| 4 |
+
huggingface_hub>=0.20.0
|
streamlit_app.py
CHANGED
|
@@ -87,6 +87,29 @@ def lookup_po_in_bytes(po_number, file_bytes, sheet_name=None, po_column="poNo")
|
|
| 87 |
wb.close()
|
| 88 |
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
# =============================================================================
|
| 91 |
# Page Config
|
| 92 |
# =============================================================================
|
|
@@ -872,10 +895,14 @@ if st.session_state.use_uploaded_excel and st.session_state.uploaded_excel_bytes
|
|
| 872 |
excel_bytes = st.session_state.uploaded_excel_bytes
|
| 873 |
source_text = f"📁 ไฟล์ของคุณ: {st.session_state.uploaded_excel_name}"
|
| 874 |
else:
|
| 875 |
-
|
| 876 |
-
|
| 877 |
-
|
| 878 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 879 |
|
| 880 |
src_col1, src_col2 = st.columns([4, 1])
|
| 881 |
with src_col1:
|
|
|
|
| 87 |
wb.close()
|
| 88 |
|
| 89 |
|
| 90 |
+
# =============================================================================
|
| 91 |
+
# Config: live data source (private HF Dataset, refreshed daily by cloud job)
|
| 92 |
+
# =============================================================================
|
| 93 |
+
DATA_REPO_ID = os.environ.get("DATA_REPO_ID", "procurement-tools/ptt-auto-form-data")
|
| 94 |
+
DATA_FILENAME = os.environ.get("DATA_FILENAME", "po_data.xlsx")
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
@st.cache_data(ttl=3600, show_spinner=False)
|
| 98 |
+
def load_live_data(repo_id, filename):
|
| 99 |
+
"""ดาวน์โหลดไฟล์ข้อมูล PO ล่าสุดจาก private HF Dataset
|
| 100 |
+
ใช้ token จาก Space secret `HF_TOKEN` — cache 1 ชั่วโมง; คืนค่า (bytes, เวลาแก้ไขล่าสุด)"""
|
| 101 |
+
from huggingface_hub import hf_hub_download
|
| 102 |
+
local_path = hf_hub_download(
|
| 103 |
+
repo_id=repo_id,
|
| 104 |
+
filename=filename,
|
| 105 |
+
repo_type="dataset",
|
| 106 |
+
token=os.environ.get("HF_TOKEN"),
|
| 107 |
+
)
|
| 108 |
+
with open(local_path, "rb") as f:
|
| 109 |
+
data = f.read()
|
| 110 |
+
return data, datetime.fromtimestamp(os.path.getmtime(local_path))
|
| 111 |
+
|
| 112 |
+
|
| 113 |
# =============================================================================
|
| 114 |
# Page Config
|
| 115 |
# =============================================================================
|
|
|
|
| 895 |
excel_bytes = st.session_state.uploaded_excel_bytes
|
| 896 |
source_text = f"📁 ไฟล์ของคุณ: {st.session_state.uploaded_excel_name}"
|
| 897 |
else:
|
| 898 |
+
try:
|
| 899 |
+
excel_bytes, _live_mt = load_live_data(DATA_REPO_ID, DATA_FILENAME)
|
| 900 |
+
source_text = f"🔄 ข้อมูลล่าสุด (อัปเดต {_live_mt:%Y-%m-%d %H:%M})"
|
| 901 |
+
except Exception:
|
| 902 |
+
if os.path.exists(sample_path):
|
| 903 |
+
with open(sample_path, 'rb') as f:
|
| 904 |
+
excel_bytes = f.read()
|
| 905 |
+
source_text = "🎯 ใช้ไฟล์ตัวอย่าง · ลอง PO 4500987654 / 4500987655 / 4500987656"
|
| 906 |
|
| 907 |
src_col1, src_col2 = st.columns([4, 1])
|
| 908 |
with src_col1:
|