Spaces:
Sleeping
Sleeping
Update translator_utils.py
Browse files- translator_utils.py +30 -15
translator_utils.py
CHANGED
|
@@ -6,10 +6,16 @@ from huggingface_hub import HfApi
|
|
| 6 |
import spacy
|
| 7 |
from spacy.lang.en import English
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# Global model cache
|
| 15 |
_translator = None
|
|
@@ -27,7 +33,8 @@ def download_dataset():
|
|
| 27 |
print("Download complete.")
|
| 28 |
except Exception as e:
|
| 29 |
print(f"Error downloading dataset: {e}")
|
| 30 |
-
raise
|
|
|
|
| 31 |
else:
|
| 32 |
# print(f"File {LOCAL_FILENAME} already exists.")
|
| 33 |
pass
|
|
@@ -36,19 +43,22 @@ def upload_dataset(token):
|
|
| 36 |
"""
|
| 37 |
Uploads the local CSV file back to the Hugging Face Hub.
|
| 38 |
"""
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
print("No HF Token provided. Skipping upload.")
|
| 41 |
return "Skipped Upload (No Token)"
|
| 42 |
|
| 43 |
-
print(f"Uploading {LOCAL_FILENAME} to {
|
| 44 |
try:
|
| 45 |
api = HfApi()
|
| 46 |
api.upload_file(
|
| 47 |
path_or_fileobj=LOCAL_FILENAME,
|
| 48 |
-
path_in_repo=
|
| 49 |
-
repo_id=
|
| 50 |
repo_type="dataset",
|
| 51 |
-
token=
|
| 52 |
commit_message="Update translations via Auto AI Translator"
|
| 53 |
)
|
| 54 |
print("Upload successful!")
|
|
@@ -191,8 +201,14 @@ def process_rows(indices, token=None, progress=None):
|
|
| 191 |
df.to_csv(LOCAL_FILENAME, index=False)
|
| 192 |
|
| 193 |
upload_status = "Skipped Upload"
|
| 194 |
-
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
return df, f"Processed {count} rows. {upload_status}"
|
| 198 |
|
|
@@ -217,8 +233,7 @@ def revert_rows(indices, token=None, progress=None):
|
|
| 217 |
print("Saving to CSV...")
|
| 218 |
df.to_csv(LOCAL_FILENAME, index=False)
|
| 219 |
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
upload_status = upload_dataset(token)
|
| 223 |
|
| 224 |
return df, f"Reverted {len(indices)} rows. {upload_status}"
|
|
|
|
| 6 |
import spacy
|
| 7 |
from spacy.lang.en import English
|
| 8 |
|
| 9 |
+
# --- Configuration & Globals ---
|
| 10 |
+
# Using persistent storage path if available in typical HF spaces, or fallback
|
| 11 |
+
DATA_FILE = "data_berita.csv"
|
| 12 |
+
# Initial Env Vars (Fallback)
|
| 13 |
+
ENV_HF_TOKEN = os.getenv("HF_TOKEN", "")
|
| 14 |
+
# Default to Brian045/data_berita if env var is not set, but prefer env var
|
| 15 |
+
ENV_REPO_ID = os.getenv("DATASET_REPO_ID", "Brian045/data_berita")
|
| 16 |
+
|
| 17 |
+
DATASET_URL = f"https://huggingface.co/datasets/{ENV_REPO_ID}/resolve/main/{DATA_FILE}"
|
| 18 |
+
LOCAL_FILENAME = DATA_FILE
|
| 19 |
|
| 20 |
# Global model cache
|
| 21 |
_translator = None
|
|
|
|
| 33 |
print("Download complete.")
|
| 34 |
except Exception as e:
|
| 35 |
print(f"Error downloading dataset: {e}")
|
| 36 |
+
# Do not raise, just print error and create empty if needed or handle gracefully
|
| 37 |
+
# raise e
|
| 38 |
else:
|
| 39 |
# print(f"File {LOCAL_FILENAME} already exists.")
|
| 40 |
pass
|
|
|
|
| 43 |
"""
|
| 44 |
Uploads the local CSV file back to the Hugging Face Hub.
|
| 45 |
"""
|
| 46 |
+
# Use passed token, or fallback to env var
|
| 47 |
+
token_to_use = token if token else ENV_HF_TOKEN
|
| 48 |
+
|
| 49 |
+
if not token_to_use:
|
| 50 |
print("No HF Token provided. Skipping upload.")
|
| 51 |
return "Skipped Upload (No Token)"
|
| 52 |
|
| 53 |
+
print(f"Uploading {LOCAL_FILENAME} to {ENV_REPO_ID}...")
|
| 54 |
try:
|
| 55 |
api = HfApi()
|
| 56 |
api.upload_file(
|
| 57 |
path_or_fileobj=LOCAL_FILENAME,
|
| 58 |
+
path_in_repo=DATA_FILE,
|
| 59 |
+
repo_id=ENV_REPO_ID,
|
| 60 |
repo_type="dataset",
|
| 61 |
+
token=token_to_use,
|
| 62 |
commit_message="Update translations via Auto AI Translator"
|
| 63 |
)
|
| 64 |
print("Upload successful!")
|
|
|
|
| 201 |
df.to_csv(LOCAL_FILENAME, index=False)
|
| 202 |
|
| 203 |
upload_status = "Skipped Upload"
|
| 204 |
+
|
| 205 |
+
# Use explicit token if provided, else rely on upload_dataset to pick up env var if None passed
|
| 206 |
+
# But UI usually passes "" if empty.
|
| 207 |
+
token_arg = token if token and token.strip() else None
|
| 208 |
+
|
| 209 |
+
# Always attempt upload if we have a token from somewhere (arg or env)
|
| 210 |
+
# logic is handled in upload_dataset
|
| 211 |
+
upload_status = upload_dataset(token_arg)
|
| 212 |
|
| 213 |
return df, f"Processed {count} rows. {upload_status}"
|
| 214 |
|
|
|
|
| 233 |
print("Saving to CSV...")
|
| 234 |
df.to_csv(LOCAL_FILENAME, index=False)
|
| 235 |
|
| 236 |
+
token_arg = token if token and token.strip() else None
|
| 237 |
+
upload_status = upload_dataset(token_arg)
|
|
|
|
| 238 |
|
| 239 |
return df, f"Reverted {len(indices)} rows. {upload_status}"
|