PERDYPTO commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,53 +4,32 @@ import tempfile
|
|
| 4 |
|
| 5 |
# NOTE: requirements.txt needs: fastapi, uvicorn, huggingface_hub>=1.5.0
|
| 6 |
|
| 7 |
-
from
|
|
|
|
| 8 |
|
| 9 |
TOKEN = os.environ["HF_TOKEN"]
|
| 10 |
|
| 11 |
SOURCE_BUCKET = "PERDYPTO/db_snapshot"
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
api = HfApi(token=TOKEN)
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
"""Find the first .sqlite/.db/.sqlite3 file in the bucket (searches all prefixes)."""
|
| 19 |
-
candidates = []
|
| 20 |
-
for item in list_bucket_tree(bucket, token=TOKEN):
|
| 21 |
-
if item["type"] == "file" and item["path"].lower().endswith((".db", ".sqlite", ".sqlite3")):
|
| 22 |
-
candidates.append(item["path"])
|
| 23 |
-
if not candidates:
|
| 24 |
-
raise FileNotFoundError(f"No .db/.sqlite/.sqlite3 file found in bucket '{bucket}'")
|
| 25 |
-
if len(candidates) > 1:
|
| 26 |
-
print(f"[warn] multiple db files found in {bucket}: {candidates}. Using the first one.")
|
| 27 |
-
return candidates[0]
|
| 28 |
|
| 29 |
|
| 30 |
def main():
|
| 31 |
-
# 1.
|
| 32 |
-
print(f"[backup] copying hf://buckets/{SOURCE_BUCKET} -> hf://buckets/{BACKUP_BUCKET}")
|
| 33 |
-
api.copy_files(
|
| 34 |
-
f"hf://buckets/{SOURCE_BUCKET}",
|
| 35 |
-
f"hf://buckets/{BACKUP_BUCKET}",
|
| 36 |
-
)
|
| 37 |
-
print("[backup] done")
|
| 38 |
-
|
| 39 |
-
# 2. Locate and download the sqlite db from the source bucket
|
| 40 |
-
remote_db_path = find_db_file(SOURCE_BUCKET)
|
| 41 |
-
print(f"[download] found db at hf://buckets/{SOURCE_BUCKET}/{remote_db_path}")
|
| 42 |
-
|
| 43 |
workdir = tempfile.mkdtemp()
|
| 44 |
-
local_db_path = os.path.join(workdir, os.path.basename(
|
| 45 |
|
|
|
|
| 46 |
download_bucket_files(
|
| 47 |
SOURCE_BUCKET,
|
| 48 |
-
[(
|
| 49 |
token=TOKEN,
|
| 50 |
)
|
| 51 |
print(f"[download] saved to {local_db_path}")
|
| 52 |
|
| 53 |
-
#
|
| 54 |
conn = sqlite3.connect(local_db_path)
|
| 55 |
cur = conn.cursor()
|
| 56 |
|
|
@@ -84,22 +63,16 @@ def main():
|
|
| 84 |
conn.close()
|
| 85 |
print("=" * 60)
|
| 86 |
|
| 87 |
-
#
|
| 88 |
-
print(f"[upload] pushing indexed db back to hf://buckets/{SOURCE_BUCKET}/{
|
| 89 |
batch_bucket_files(
|
| 90 |
SOURCE_BUCKET,
|
| 91 |
-
add=[(local_db_path,
|
| 92 |
token=TOKEN,
|
| 93 |
)
|
| 94 |
print("[upload] done")
|
| 95 |
|
| 96 |
|
| 97 |
-
from fastapi import FastAPI
|
| 98 |
-
|
| 99 |
-
app = FastAPI()
|
| 100 |
-
_status = {"state": "starting"}
|
| 101 |
-
|
| 102 |
-
|
| 103 |
@app.on_event("startup")
|
| 104 |
def on_startup():
|
| 105 |
try:
|
|
|
|
| 4 |
|
| 5 |
# NOTE: requirements.txt needs: fastapi, uvicorn, huggingface_hub>=1.5.0
|
| 6 |
|
| 7 |
+
from fastapi import FastAPI
|
| 8 |
+
from huggingface_hub import download_bucket_files, batch_bucket_files
|
| 9 |
|
| 10 |
TOKEN = os.environ["HF_TOKEN"]
|
| 11 |
|
| 12 |
SOURCE_BUCKET = "PERDYPTO/db_snapshot"
|
| 13 |
+
DB_PATH_IN_BUCKET = "github_pipeline.db"
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
app = FastAPI()
|
| 16 |
+
_status = {"state": "starting"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
|
| 19 |
def main():
|
| 20 |
+
# 1. Download the sqlite db from the bucket
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
workdir = tempfile.mkdtemp()
|
| 22 |
+
local_db_path = os.path.join(workdir, os.path.basename(DB_PATH_IN_BUCKET))
|
| 23 |
|
| 24 |
+
print(f"[download] pulling hf://buckets/{SOURCE_BUCKET}/{DB_PATH_IN_BUCKET}")
|
| 25 |
download_bucket_files(
|
| 26 |
SOURCE_BUCKET,
|
| 27 |
+
[(DB_PATH_IN_BUCKET, local_db_path)],
|
| 28 |
token=TOKEN,
|
| 29 |
)
|
| 30 |
print(f"[download] saved to {local_db_path}")
|
| 31 |
|
| 32 |
+
# 2. Inspect schema + index every column of every table
|
| 33 |
conn = sqlite3.connect(local_db_path)
|
| 34 |
cur = conn.cursor()
|
| 35 |
|
|
|
|
| 63 |
conn.close()
|
| 64 |
print("=" * 60)
|
| 65 |
|
| 66 |
+
# 3. Upload the indexed db back to the bucket (overwrite in place)
|
| 67 |
+
print(f"[upload] pushing indexed db back to hf://buckets/{SOURCE_BUCKET}/{DB_PATH_IN_BUCKET}")
|
| 68 |
batch_bucket_files(
|
| 69 |
SOURCE_BUCKET,
|
| 70 |
+
add=[(local_db_path, DB_PATH_IN_BUCKET)],
|
| 71 |
token=TOKEN,
|
| 72 |
)
|
| 73 |
print("[upload] done")
|
| 74 |
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
@app.on_event("startup")
|
| 77 |
def on_startup():
|
| 78 |
try:
|