Spaces:
Sleeping
Sleeping
Update correction_app.py
Browse files- correction_app.py +34 -0
correction_app.py
CHANGED
|
@@ -22,6 +22,7 @@ from pathlib import Path
|
|
| 22 |
|
| 23 |
import numpy as np
|
| 24 |
import gradio as gr
|
|
|
|
| 25 |
|
| 26 |
# ββ Paths ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 27 |
IS_HF = os.environ.get("SPACE_ID") is not None
|
|
@@ -36,6 +37,36 @@ HF_TOKEN = os.environ.get("HF_TOKEN")
|
|
| 36 |
MODEL_V1 = "Adpacci/darija-whisper-medium-stt"
|
| 37 |
MODEL_V2 = "Adpacci/darija-whisper-medium-stt-v2"
|
| 38 |
ARABIC_RE = re.compile(r"[\u0600-\u06FF\u0750-\u077F]")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
# ββ Load segments (once at startup) βββββββββββββββββββββββββββββββββββββββββββ
|
| 41 |
def _load_segments() -> list:
|
|
@@ -328,6 +359,8 @@ def on_save(idx, prog, corrector, arabizi, arabic):
|
|
| 328 |
prog[key] = f"{txt} |AR| {ar}" if ar else txt
|
| 329 |
_save_progress(prog)
|
| 330 |
_save_to_dataset(seg, txt, ar, corrector.strip())
|
|
|
|
|
|
|
| 331 |
nxt = next((i for i in range(idx + 1, N) if SEGMENTS[i]["segment_file"] not in prog), None)
|
| 332 |
return render(nxt if nxt is not None else idx, prog, msg="β
SauvegardΓ© !")
|
| 333 |
|
|
@@ -335,6 +368,7 @@ def on_skip(idx, prog):
|
|
| 335 |
key = SEGMENTS[idx]["segment_file"]
|
| 336 |
prog[key] = "__SKIP__"
|
| 337 |
_save_progress(prog)
|
|
|
|
| 338 |
nxt = next((i for i in range(idx + 1, N) if SEGMENTS[i]["segment_file"] not in prog), None)
|
| 339 |
return render(nxt if nxt is not None else idx, prog, msg="β SkippΓ©.")
|
| 340 |
|
|
|
|
| 22 |
|
| 23 |
import numpy as np
|
| 24 |
import gradio as gr
|
| 25 |
+
from huggingface_hub import HfApi, hf_hub_download
|
| 26 |
|
| 27 |
# ββ Paths ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 28 |
IS_HF = os.environ.get("SPACE_ID") is not None
|
|
|
|
| 37 |
MODEL_V1 = "Adpacci/darija-whisper-medium-stt"
|
| 38 |
MODEL_V2 = "Adpacci/darija-whisper-medium-stt-v2"
|
| 39 |
ARABIC_RE = re.compile(r"[\u0600-\u06FF\u0750-\u077F]")
|
| 40 |
+
HF_DATASET_REPO = "Adpacci/darija-corrections" # crΓ©e ce dataset PRIVΓ sur HF si pas dΓ©jΓ fait
|
| 41 |
+
|
| 42 |
+
def restore_from_hub():
|
| 43 |
+
"""RetΓ©lΓ©charge progress + dataset corrigΓ© depuis le Hub si absents localement
|
| 44 |
+
(cas d'un redΓ©marrage du Space sans Persistent Storage, ou disque neuf)."""
|
| 45 |
+
for path in (PROGRESS_FILE, CSV_OUTPUT):
|
| 46 |
+
if path.exists():
|
| 47 |
+
continue
|
| 48 |
+
try:
|
| 49 |
+
downloaded = hf_hub_download(
|
| 50 |
+
repo_id=HF_DATASET_REPO, filename=path.name,
|
| 51 |
+
repo_type="dataset", token=HF_TOKEN,
|
| 52 |
+
)
|
| 53 |
+
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
| 54 |
+
path.write_bytes(Path(downloaded).read_bytes())
|
| 55 |
+
print(f"β
RestaurΓ© depuis le Hub : {path.name}")
|
| 56 |
+
except Exception as e:
|
| 57 |
+
print(f"βΉοΈ Rien Γ restaurer pour {path.name} ({e})")
|
| 58 |
+
|
| 59 |
+
def push_to_hub(path: Path):
|
| 60 |
+
"""Pousse le fichier vers le dataset privé de backup après chaque sauvegarde."""
|
| 61 |
+
try:
|
| 62 |
+
HfApi().upload_file(
|
| 63 |
+
path_or_fileobj=str(path), path_in_repo=path.name,
|
| 64 |
+
repo_id=HF_DATASET_REPO, repo_type="dataset", token=HF_TOKEN,
|
| 65 |
+
)
|
| 66 |
+
except Exception as e:
|
| 67 |
+
print(f"β οΈ Push HF Γ©chouΓ© pour {path.name} (sauvegarde locale OK) : {e}")
|
| 68 |
+
|
| 69 |
+
restore_from_hub() # tenter la restauration dès le chargement du module
|
| 70 |
|
| 71 |
# ββ Load segments (once at startup) βββββββββββββββββββββββββββββββββββββββββββ
|
| 72 |
def _load_segments() -> list:
|
|
|
|
| 359 |
prog[key] = f"{txt} |AR| {ar}" if ar else txt
|
| 360 |
_save_progress(prog)
|
| 361 |
_save_to_dataset(seg, txt, ar, corrector.strip())
|
| 362 |
+
push_to_hub(PROGRESS_FILE)
|
| 363 |
+
push_to_hub(CSV_OUTPUT)
|
| 364 |
nxt = next((i for i in range(idx + 1, N) if SEGMENTS[i]["segment_file"] not in prog), None)
|
| 365 |
return render(nxt if nxt is not None else idx, prog, msg="β
SauvegardΓ© !")
|
| 366 |
|
|
|
|
| 368 |
key = SEGMENTS[idx]["segment_file"]
|
| 369 |
prog[key] = "__SKIP__"
|
| 370 |
_save_progress(prog)
|
| 371 |
+
push_to_hub(PROGRESS_FILE)
|
| 372 |
nxt = next((i for i in range(idx + 1, N) if SEGMENTS[i]["segment_file"] not in prog), None)
|
| 373 |
return render(nxt if nxt is not None else idx, prog, msg="β SkippΓ©.")
|
| 374 |
|