Spaces:
Sleeping
Sleeping
File size: 3,039 Bytes
22d01ec | 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | import os
import shutil
from huggingface_hub import hf_hub_download, HfApi
REPO_ID = "devorah-ai-2016/BeeCareCore"
SYNC_FILES = [
"data/clients.xlsx",
"data/counseling_logs.xlsx",
"data/case_management.xlsx",
"data/needs_assessment.xlsx",
"data/care_plan_alerts.xlsx"
]
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"),
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_all():
token = get_hf_token()
if not token:
print("HF_TOKEN not found, skipping download.")
return False
success = True
for relative_path in SYNC_FILES:
# Determine local path
local_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), relative_path)
os.makedirs(os.path.dirname(local_path), exist_ok=True)
try:
downloaded_path = hf_hub_download(
repo_id=REPO_ID,
filename=relative_path,
repo_type="space",
token=token
)
shutil.copy(downloaded_path, local_path)
print(f"Downloaded {relative_path} to {local_path}")
except Exception as e:
print(f"Failed to download {relative_path}: {e}")
success = False
return success
def upload_all():
token = get_hf_token()
if not token:
print("HF_TOKEN not found, skipping upload.")
return False
success = True
api = HfApi()
for relative_path in SYNC_FILES:
local_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), relative_path)
if not os.path.exists(local_path):
print(f"Local file {local_path} does not exist. Skipping upload.")
continue
try:
api.upload_file(
path_or_fileobj=local_path,
path_in_repo=relative_path,
repo_id=REPO_ID,
repo_type="space",
token=token
)
print(f"Uploaded {relative_path} to {REPO_ID}")
except Exception as e:
print(f"Failed to upload {relative_path}: {e}")
success = False
return success
|