File size: 2,170 Bytes
aad1c54 38d59be aad1c54 5180806 79929d7 38d59be 5180806 0b3fc64 38d59be aad1c54 38d59be e559262 aad1c54 38d59be 5180806 38d59be 5180806 aad1c54 5180806 aad1c54 5180806 aad1c54 38d59be e559262 aad1c54 0b3fc64 aad1c54 38d59be aad1c54 5180806 aad1c54 38d59be |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
import os
import hashlib
import time
from huggingface_hub import HfApi
REPO_ID = "axxam/LibreTranslate_Kabyle"
DEST_PATH_IN_REPO = "suggestions/suggestions.db"
HF_TOKEN = os.getenv("HF_TOKEN")
CHECKSUM_FILE = "/tmp/.last_suggestions_checksum"
def get_suggestions_db_path():
cwd = os.getcwd()
print(f"Running in CWD: {cwd}")
possible_paths = [
os.path.join(cwd, "db", "suggestions.db"),
"/root/.local/share/db/suggestions.db",
"/home/libretranslate/.local/share/db/suggestions.db",
"/app/.local/share/db/suggestions.db"
]
for path in possible_paths:
if os.path.exists(path):
print(f"Found suggestions.db at {path}")
return path
print("suggestions.db not found in any known path.")
return None
def file_checksum(path):
h = hashlib.sha256()
with open(path, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
h.update(chunk)
return h.hexdigest()
def load_last_checksum():
if os.path.exists(CHECKSUM_FILE):
with open(CHECKSUM_FILE, "r") as f:
return f.read().strip()
return None
def save_checksum(checksum):
with open(CHECKSUM_FILE, "w") as f:
f.write(checksum)
def upload_if_updated():
if not HF_TOKEN:
print("HF_TOKEN not set β skipping upload.")
return
db_path = get_suggestions_db_path()
if not db_path:
return
current_checksum = file_checksum(db_path)
last_checksum = load_last_checksum()
if current_checksum == last_checksum:
print("suggestions.db unchanged β skipping upload.")
return
print("Uploading updated suggestions.db to Hugging Face...")
try:
api = HfApi()
api.upload_file(
path_or_fileobj=db_path,
path_in_repo=DEST_PATH_IN_REPO,
repo_id=REPO_ID,
repo_type="space",
token=HF_TOKEN
)
save_checksum(current_checksum)
print(f"Upload successful at {time.strftime('%Y-%m-%d %H:%M:%S')}")
except Exception as e:
print(f"Upload failed: {e}")
if __name__ == "__main__":
upload_if_updated()
|