Spaces:
Sleeping
Sleeping
Update code
Browse files- 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 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
return True
|
| 38 |
|
| 39 |
-
print(f"Database not found
|
| 40 |
try:
|
| 41 |
from huggingface_hub import hf_hub_download
|
|
|
|
| 42 |
|
| 43 |
-
# Get token from environment
|
| 44 |
token = os.environ.get("HF_TOKEN")
|
|
|
|
|
|
|
| 45 |
if not token:
|
| 46 |
-
token_file = os.path.join(
|
| 47 |
if os.path.exists(token_file):
|
| 48 |
with open(token_file) as f:
|
| 49 |
token = f.read().strip()
|
|
|
|
| 50 |
|
| 51 |
-
# Download
|
| 52 |
-
|
| 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"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
return True
|
| 62 |
except Exception as e:
|
| 63 |
print(f"β Failed to download database: {e}")
|
| 64 |
-
|
|
|
|
| 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 |
|