Spaces:
Sleeping
Sleeping
File size: 2,421 Bytes
56dd3d3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | import os
import shutil
from huggingface_hub import hf_hub_download, HfApi
REPO_ID = "devorah-ai-2016/ClientCenter"
def get_hf_token():
# 1. Check environment variable (set on HF spaces)
token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_TOKEN")
if token:
return token.strip()
# 2. Check local token files (for local testing)
token_candidates = [
os.path.join(os.path.dirname(os.path.abspath(__file__)), "huggingfacetoken.txt"),
os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "huggingfacetoken.txt"),
"/Users/devorah/connect/Beecarewebsite/huggingfacetoken.txt",
os.path.expanduser("~/.cache/huggingface/token")
]
for path in token_candidates:
if os.path.exists(path):
try:
with open(path, "r") as f:
t = f.read().strip()
if t:
return t
except Exception:
pass
return None
def download_db(db_path):
token = get_hf_token()
if not token:
print("HF_TOKEN not found, skipping download.")
return False
try:
os.makedirs(os.path.dirname(db_path), exist_ok=True)
# Download from HF space (repo_type="space")
downloaded_path = hf_hub_download(
repo_id=REPO_ID,
filename="data/clients.db",
repo_type="space",
token=token
)
# Copy to the local path
shutil.copy(downloaded_path, db_path)
print(f"Downloaded clients.db from {REPO_ID} to {db_path}")
return True
except Exception as e:
print(f"Failed to download database: {e}")
return False
def upload_db(db_path):
token = get_hf_token()
if not token:
print("HF_TOKEN not found, skipping upload.")
return False
try:
if not os.path.exists(db_path):
print(f"Local database {db_path} does not exist. Cannot upload.")
return False
api = HfApi()
api.upload_file(
path_or_fileobj=db_path,
path_in_repo="data/clients.db",
repo_id=REPO_ID,
repo_type="space",
token=token
)
print(f"Uploaded clients.db to {REPO_ID}")
return True
except Exception as e:
print(f"Failed to upload database: {e}")
return False
|