rottg commited on
Commit
e7fbe02
Β·
1 Parent(s): b82647d

Update code

Browse files
Files changed (1) hide show
  1. dashboard.py +25 -11
dashboard.py CHANGED
@@ -28,40 +28,54 @@ from collections import defaultdict
28
  # ==========================================
29
  HF_DATASET_REPO = "rottg/telegram-db"
30
  DB_FILENAME = "telegram.db"
 
 
31
 
32
 
33
  def ensure_db_exists():
34
  """Download DB from HF Dataset repo if it doesn't exist locally."""
35
- if os.path.exists(DB_FILENAME):
36
- print(f"βœ“ Database found: {DB_FILENAME}")
 
 
 
 
37
  return True
38
 
39
- print(f"Database not found locally. Downloading from HF Dataset...")
40
  try:
41
  from huggingface_hub import hf_hub_download
 
42
 
43
- # Get token from environment or file
44
  token = os.environ.get("HF_TOKEN")
 
 
45
  if not token:
46
- token_file = os.path.join(os.path.dirname(__file__), ".hf_token")
47
  if os.path.exists(token_file):
48
  with open(token_file) as f:
49
  token = f.read().strip()
 
50
 
51
- # Download the DB file
52
- local_path = hf_hub_download(
53
  repo_id=HF_DATASET_REPO,
54
  filename=DB_FILENAME,
55
  repo_type="dataset",
56
  token=token,
57
- local_dir=".",
58
- local_dir_use_symlinks=False
59
  )
60
- print(f"βœ“ Database downloaded: {local_path}")
 
 
 
 
 
61
  return True
62
  except Exception as e:
63
  print(f"βœ— Failed to download database: {e}")
64
- print(" Make sure to upload telegram.db to the Dataset repo first.")
 
65
  return False
66
 
67
 
 
28
  # ==========================================
29
  HF_DATASET_REPO = "rottg/telegram-db"
30
  DB_FILENAME = "telegram.db"
31
+ APP_DIR = os.path.dirname(os.path.abspath(__file__))
32
+ DB_PATH_FULL = os.path.join(APP_DIR, DB_FILENAME)
33
 
34
 
35
  def ensure_db_exists():
36
  """Download DB from HF Dataset repo if it doesn't exist locally."""
37
+ print(f"[DB] Checking for database at: {DB_PATH_FULL}")
38
+ print(f"[DB] Current working directory: {os.getcwd()}")
39
+
40
+ if os.path.exists(DB_PATH_FULL):
41
+ size_mb = os.path.getsize(DB_PATH_FULL) / (1024 * 1024)
42
+ print(f"βœ“ Database found: {DB_PATH_FULL} ({size_mb:.0f} MB)")
43
  return True
44
 
45
+ print(f"[DB] Database not found. Downloading from HF Dataset {HF_DATASET_REPO}...")
46
  try:
47
  from huggingface_hub import hf_hub_download
48
+ import shutil
49
 
50
+ # Get token from environment
51
  token = os.environ.get("HF_TOKEN")
52
+ print(f"[DB] HF_TOKEN from env: {'set' if token else 'NOT SET'}")
53
+
54
  if not token:
55
+ token_file = os.path.join(APP_DIR, ".hf_token")
56
  if os.path.exists(token_file):
57
  with open(token_file) as f:
58
  token = f.read().strip()
59
+ print(f"[DB] HF_TOKEN from file: set")
60
 
61
+ # Download to cache, then copy to app dir
62
+ cached_path = hf_hub_download(
63
  repo_id=HF_DATASET_REPO,
64
  filename=DB_FILENAME,
65
  repo_type="dataset",
66
  token=token,
 
 
67
  )
68
+ print(f"[DB] Downloaded to cache: {cached_path}")
69
+
70
+ # Copy to app directory
71
+ shutil.copy2(cached_path, DB_PATH_FULL)
72
+ size_mb = os.path.getsize(DB_PATH_FULL) / (1024 * 1024)
73
+ print(f"βœ“ Database ready: {DB_PATH_FULL} ({size_mb:.0f} MB)")
74
  return True
75
  except Exception as e:
76
  print(f"βœ— Failed to download database: {e}")
77
+ import traceback
78
+ traceback.print_exc()
79
  return False
80
 
81