devorah-ai-2016 commited on
Commit
898d3ef
·
verified ·
1 Parent(s): 216f9c9

Upload db_sync.py

Browse files
Files changed (1) hide show
  1. db_sync.py +90 -0
db_sync.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shutil
3
+ from huggingface_hub import hf_hub_download, HfApi
4
+
5
+ REPO_ID = "devorah-ai-2016/BeeCareCore"
6
+
7
+ SYNC_FILES = [
8
+ "data/clients.xlsx",
9
+ "data/counseling_logs.xlsx",
10
+ "data/case_management.xlsx",
11
+ "data/needs_assessment.xlsx",
12
+ "data/care_plan_alerts.xlsx"
13
+ ]
14
+
15
+ def get_hf_token():
16
+ # 1. Check environment variable (set on HF spaces)
17
+ token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_TOKEN")
18
+ if token:
19
+ return token.strip()
20
+
21
+ # 2. Check local token files (for local testing)
22
+ token_candidates = [
23
+ os.path.join(os.path.dirname(os.path.abspath(__file__)), "huggingfacetoken.txt"),
24
+ os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "huggingfacetoken.txt"),
25
+ os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "huggingfacetoken.txt"),
26
+ "/Users/devorah/connect/Beecarewebsite/huggingfacetoken.txt",
27
+ os.path.expanduser("~/.cache/huggingface/token")
28
+ ]
29
+ for path in token_candidates:
30
+ if os.path.exists(path):
31
+ try:
32
+ with open(path, "r") as f:
33
+ t = f.read().strip()
34
+ if t:
35
+ return t
36
+ except Exception:
37
+ pass
38
+ return None
39
+
40
+ def download_all():
41
+ token = get_hf_token()
42
+ if not token:
43
+ print("HF_TOKEN not found, skipping download.")
44
+ return False
45
+
46
+ success = True
47
+ for relative_path in SYNC_FILES:
48
+ # Determine local path
49
+ local_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), relative_path)
50
+ os.makedirs(os.path.dirname(local_path), exist_ok=True)
51
+ try:
52
+ downloaded_path = hf_hub_download(
53
+ repo_id=REPO_ID,
54
+ filename=relative_path,
55
+ repo_type="space",
56
+ token=token
57
+ )
58
+ shutil.copy(downloaded_path, local_path)
59
+ print(f"Downloaded {relative_path} to {local_path}")
60
+ except Exception as e:
61
+ print(f"Failed to download {relative_path}: {e}")
62
+ success = False
63
+ return success
64
+
65
+ def upload_all():
66
+ token = get_hf_token()
67
+ if not token:
68
+ print("HF_TOKEN not found, skipping upload.")
69
+ return False
70
+
71
+ success = True
72
+ api = HfApi()
73
+ for relative_path in SYNC_FILES:
74
+ local_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), relative_path)
75
+ if not os.path.exists(local_path):
76
+ print(f"Local file {local_path} does not exist. Skipping upload.")
77
+ continue
78
+ try:
79
+ api.upload_file(
80
+ path_or_fileobj=local_path,
81
+ path_in_repo=relative_path,
82
+ repo_id=REPO_ID,
83
+ repo_type="space",
84
+ token=token
85
+ )
86
+ print(f"Uploaded {relative_path} to {REPO_ID}")
87
+ except Exception as e:
88
+ print(f"Failed to upload {relative_path}: {e}")
89
+ success = False
90
+ return success