BeeCareCore / db_sync.py
devorah-ai-2016's picture
Upload db_sync.py
22d01ec verified
Raw History Blame Contribute Delete
3.04 kB
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