Update upload_suggestions.py
Browse files- upload_suggestions.py +54 -19
upload_suggestions.py
CHANGED
@@ -1,40 +1,75 @@
|
|
1 |
import os
|
|
|
|
|
|
|
2 |
from huggingface_hub import HfApi
|
3 |
|
4 |
-
# List of possible locations where suggestions.db might be created
|
5 |
-
possible_paths = [
|
6 |
-
"/app/suggestions/suggestions.db",
|
7 |
-
"/app/suggestions.db",
|
8 |
-
"/root/.local/share/db/suggestions.db",
|
9 |
-
"/home/libretranslate/.local/share/db/suggestions.db"
|
10 |
-
]
|
11 |
-
|
12 |
-
# Find the first existing path
|
13 |
-
SOURCE_DB = next((p for p in possible_paths if os.path.exists(p)), None)
|
14 |
DEST_PATH_IN_REPO = "suggestions/suggestions.db"
|
15 |
REPO_ID = "axxam/LibreTranslate_Kabyle"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
def
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
20 |
print("HF_TOKEN not set — skipping upload.")
|
21 |
return
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
return
|
25 |
|
|
|
26 |
api = HfApi()
|
27 |
try:
|
28 |
api.upload_file(
|
29 |
-
path_or_fileobj=
|
30 |
path_in_repo=DEST_PATH_IN_REPO,
|
31 |
repo_id=REPO_ID,
|
32 |
repo_type="space",
|
33 |
-
token=
|
|
|
34 |
)
|
35 |
-
|
|
|
36 |
except Exception as e:
|
37 |
print("Upload failed:", e)
|
38 |
|
39 |
if __name__ == "__main__":
|
40 |
-
|
|
|
1 |
import os
|
2 |
+
import subprocess
|
3 |
+
import hashlib
|
4 |
+
import time
|
5 |
from huggingface_hub import HfApi
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
DEST_PATH_IN_REPO = "suggestions/suggestions.db"
|
8 |
REPO_ID = "axxam/LibreTranslate_Kabyle"
|
9 |
+
CHECKSUM_FILE = "/app/.last_suggestions_checksum"
|
10 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
11 |
+
|
12 |
+
def find_suggestions_db():
|
13 |
+
try:
|
14 |
+
result = subprocess.check_output(["find", "/", "-type", "f", "-name", "suggestions.db"], stderr=subprocess.DEVNULL)
|
15 |
+
paths = result.decode().strip().split("\n")
|
16 |
+
for path in paths:
|
17 |
+
if os.path.exists(path):
|
18 |
+
print(f"Found suggestions.db at: {path}")
|
19 |
+
return path
|
20 |
+
except subprocess.CalledProcessError:
|
21 |
+
pass
|
22 |
+
print("suggestions.db not found.")
|
23 |
+
return None
|
24 |
+
|
25 |
+
def file_checksum(path):
|
26 |
+
h = hashlib.sha256()
|
27 |
+
with open(path, "rb") as f:
|
28 |
+
for chunk in iter(lambda: f.read(4096), b""):
|
29 |
+
h.update(chunk)
|
30 |
+
return h.hexdigest()
|
31 |
+
|
32 |
+
def load_last_checksum():
|
33 |
+
if os.path.exists(CHECKSUM_FILE):
|
34 |
+
with open(CHECKSUM_FILE, "r") as f:
|
35 |
+
return f.read().strip()
|
36 |
+
return None
|
37 |
|
38 |
+
def save_checksum(checksum):
|
39 |
+
with open(CHECKSUM_FILE, "w") as f:
|
40 |
+
f.write(checksum)
|
41 |
+
|
42 |
+
def upload_if_updated():
|
43 |
+
if not HF_TOKEN:
|
44 |
print("HF_TOKEN not set — skipping upload.")
|
45 |
return
|
46 |
+
|
47 |
+
db_path = find_suggestions_db()
|
48 |
+
if not db_path:
|
49 |
+
return
|
50 |
+
|
51 |
+
current_checksum = file_checksum(db_path)
|
52 |
+
last_checksum = load_last_checksum()
|
53 |
+
|
54 |
+
if current_checksum == last_checksum:
|
55 |
+
print("No change in suggestions.db — skipping upload.")
|
56 |
return
|
57 |
|
58 |
+
print("New changes detected — uploading suggestions.db...")
|
59 |
api = HfApi()
|
60 |
try:
|
61 |
api.upload_file(
|
62 |
+
path_or_fileobj=db_path,
|
63 |
path_in_repo=DEST_PATH_IN_REPO,
|
64 |
repo_id=REPO_ID,
|
65 |
repo_type="space",
|
66 |
+
token=HF_TOKEN,
|
67 |
+
overwrite=True
|
68 |
)
|
69 |
+
save_checksum(current_checksum)
|
70 |
+
print(f"Upload successful at {time.strftime('%Y-%m-%d %H:%M:%S')}")
|
71 |
except Exception as e:
|
72 |
print("Upload failed:", e)
|
73 |
|
74 |
if __name__ == "__main__":
|
75 |
+
upload_if_updated()
|