Spaces:
Running
Running
cool commited on
Update upload_suggestions.py
Browse files- upload_suggestions.py +17 -24
upload_suggestions.py
CHANGED
|
@@ -7,11 +7,13 @@ from huggingface_hub import HfApi
|
|
| 7 |
|
| 8 |
# Settings
|
| 9 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 10 |
-
REPO_ID = "axxam/libretranslate-suggestions"
|
| 11 |
DEST_PATH_IN_REPO = "suggestions.json"
|
| 12 |
REPO_TYPE = "dataset"
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Known or expected locations for the DB
|
| 17 |
possible_paths = [
|
|
@@ -22,11 +24,12 @@ possible_paths = [
|
|
| 22 |
]
|
| 23 |
|
| 24 |
def find_db():
|
|
|
|
| 25 |
for path in possible_paths:
|
| 26 |
if os.path.exists(path):
|
| 27 |
print(f"Found suggestions.db at {path}")
|
| 28 |
return path
|
| 29 |
-
print("suggestions.db not found.")
|
| 30 |
return None
|
| 31 |
|
| 32 |
def extract_suggestions(db_path):
|
|
@@ -60,24 +63,15 @@ def merge_with_existing(suggestions, json_path):
|
|
| 60 |
for item in json.load(f):
|
| 61 |
existing[item["id"]] = item
|
| 62 |
except Exception as e:
|
| 63 |
-
print(f"
|
| 64 |
|
| 65 |
-
# Add only new suggestions
|
| 66 |
-
new_data_added = False
|
| 67 |
for s in suggestions:
|
| 68 |
-
|
| 69 |
-
existing[s["id"]] = s
|
| 70 |
-
new_data_added = True
|
| 71 |
-
|
| 72 |
-
if not new_data_added:
|
| 73 |
-
print("No new suggestions — nothing to update.")
|
| 74 |
-
return False
|
| 75 |
|
| 76 |
-
# Only write if new data was added
|
| 77 |
with open(json_path, "w", encoding="utf-8") as f:
|
| 78 |
json.dump(list(existing.values()), f, indent=2, ensure_ascii=False)
|
| 79 |
-
|
| 80 |
-
return
|
| 81 |
|
| 82 |
def get_checksum(filepath):
|
| 83 |
if not os.path.exists(filepath):
|
|
@@ -94,7 +88,7 @@ def upload_if_updated(json_path):
|
|
| 94 |
old_checksum = f.read().strip()
|
| 95 |
|
| 96 |
if new_checksum != old_checksum:
|
| 97 |
-
print("
|
| 98 |
try:
|
| 99 |
api = HfApi()
|
| 100 |
api.upload_file(
|
|
@@ -106,11 +100,11 @@ def upload_if_updated(json_path):
|
|
| 106 |
)
|
| 107 |
with open(CHECKSUM_FILE, "w") as f:
|
| 108 |
f.write(new_checksum)
|
| 109 |
-
print(f"Upload
|
| 110 |
except Exception as e:
|
| 111 |
print("Upload failed:", e)
|
| 112 |
else:
|
| 113 |
-
print("
|
| 114 |
|
| 115 |
def main():
|
| 116 |
if not HF_TOKEN:
|
|
@@ -123,12 +117,11 @@ def main():
|
|
| 123 |
|
| 124 |
suggestions = extract_suggestions(db_path)
|
| 125 |
if not suggestions:
|
| 126 |
-
print("No suggestions found.")
|
| 127 |
return
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
upload_if_updated(JSON_OUTPUT_PATH)
|
| 132 |
|
| 133 |
if __name__ == "__main__":
|
| 134 |
main()
|
|
|
|
| 7 |
|
| 8 |
# Settings
|
| 9 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 10 |
+
REPO_ID = "axxam/libretranslate-suggestions" # Dataset repo on HF
|
| 11 |
DEST_PATH_IN_REPO = "suggestions.json"
|
| 12 |
REPO_TYPE = "dataset"
|
| 13 |
+
|
| 14 |
+
# ✅ Safe paths
|
| 15 |
+
JSON_OUTPUT_PATH = "/tmp/suggestions.json"
|
| 16 |
+
CHECKSUM_FILE = "/tmp/.last_suggestions_checksum"
|
| 17 |
|
| 18 |
# Known or expected locations for the DB
|
| 19 |
possible_paths = [
|
|
|
|
| 24 |
]
|
| 25 |
|
| 26 |
def find_db():
|
| 27 |
+
print(f"Running in CWD: {os.getcwd()}")
|
| 28 |
for path in possible_paths:
|
| 29 |
if os.path.exists(path):
|
| 30 |
print(f"Found suggestions.db at {path}")
|
| 31 |
return path
|
| 32 |
+
print("suggestions.db not found in any known path.")
|
| 33 |
return None
|
| 34 |
|
| 35 |
def extract_suggestions(db_path):
|
|
|
|
| 63 |
for item in json.load(f):
|
| 64 |
existing[item["id"]] = item
|
| 65 |
except Exception as e:
|
| 66 |
+
print(f"Failed to read existing JSON: {e}")
|
| 67 |
|
|
|
|
|
|
|
| 68 |
for s in suggestions:
|
| 69 |
+
existing[s["id"]] = s
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
|
|
|
| 71 |
with open(json_path, "w", encoding="utf-8") as f:
|
| 72 |
json.dump(list(existing.values()), f, indent=2, ensure_ascii=False)
|
| 73 |
+
|
| 74 |
+
return json_path
|
| 75 |
|
| 76 |
def get_checksum(filepath):
|
| 77 |
if not os.path.exists(filepath):
|
|
|
|
| 88 |
old_checksum = f.read().strip()
|
| 89 |
|
| 90 |
if new_checksum != old_checksum:
|
| 91 |
+
print("Uploading updated suggestions.json to Hugging Face...")
|
| 92 |
try:
|
| 93 |
api = HfApi()
|
| 94 |
api.upload_file(
|
|
|
|
| 100 |
)
|
| 101 |
with open(CHECKSUM_FILE, "w") as f:
|
| 102 |
f.write(new_checksum)
|
| 103 |
+
print(f"Upload successful at {datetime.now().isoformat()}")
|
| 104 |
except Exception as e:
|
| 105 |
print("Upload failed:", e)
|
| 106 |
else:
|
| 107 |
+
print("No changes in suggestions.json — skipping upload.")
|
| 108 |
|
| 109 |
def main():
|
| 110 |
if not HF_TOKEN:
|
|
|
|
| 117 |
|
| 118 |
suggestions = extract_suggestions(db_path)
|
| 119 |
if not suggestions:
|
| 120 |
+
print("No suggestions found — skipping.")
|
| 121 |
return
|
| 122 |
|
| 123 |
+
json_path = merge_with_existing(suggestions, JSON_OUTPUT_PATH)
|
| 124 |
+
upload_if_updated(json_path)
|
|
|
|
| 125 |
|
| 126 |
if __name__ == "__main__":
|
| 127 |
main()
|