tungtung-chatbot / upload_test_docs.py
diminch's picture
feat(rag): lazy load models and pre-download them in Dockerfile to prevent HF Space startup timeouts
6fd4123
Raw
History Blame Contribute Delete
1.87 kB
import os
import requests
import time
API_URL = "https://diminch-tungtung-chatbot.hf.space"
API_KEY = "super-secret-internal-key"
headers = {
"x-api-key": API_KEY
}
# 1. Chờ Space khởi động xong (Build and run)
print("Checking HF Space status...")
for i in range(30):
try:
r = requests.get(API_URL + "/")
print(f"HF Space response status: {r.status_code}")
if r.status_code in [200, 404]:
print("HF Space is active!")
break
except Exception as e:
print(f"Waiting for HF Space to start... ({e})")
time.sleep(10)
# 2. Upload các tài liệu nghiệp vụ (business)
docs_dir = "test_documents"
if not os.path.exists(docs_dir):
print(f"Directory {docs_dir} not found!")
exit(1)
files = os.listdir(docs_dir)
for filename in files:
filepath = os.path.join(docs_dir, filename)
if not os.path.isfile(filepath):
continue
# Xác định category dựa trên tiền tố của tên file
if filename.startswith("business"):
category = "business"
elif filename.startswith("learning"):
category = "learning"
else:
continue
print(f"\nUploading {filename} as category: {category}...")
with open(filepath, "rb") as f:
file_data = {"file": (filename, f, "text/plain")}
params = {"doc_category": category, "uploaded_by": "admin"}
try:
r = requests.post(f"{API_URL}/upload", headers=headers, files=file_data, params=params)
print(f"Status: {r.status_code}")
try:
print(r.json())
except Exception:
print(r.content.decode("utf-8", errors="ignore").encode("ascii", "replace").decode("ascii"))
except Exception as e:
print(f"Error uploading {filename}: {e}")
print("\nDone uploading all documents.")