Spaces:
Running
Running
Mehdi MOUSSAID commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,17 +3,24 @@ import numpy as np
|
|
| 3 |
import pandas as pd
|
| 4 |
import csv
|
| 5 |
import os
|
|
|
|
| 6 |
from itertools import combinations
|
| 7 |
from datetime import datetime
|
| 8 |
import fasttext
|
| 9 |
import fasttext.util
|
| 10 |
import enchant
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# ββ Config ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 13 |
SCORES_FILE = "scores.csv"
|
| 14 |
MODEL_PATH = "cc.fr.300.bin"
|
| 15 |
N_WORDS = 10
|
| 16 |
SCORE_WORDS = 7
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
print("Téléchargement du modèle FastText français...")
|
| 19 |
fasttext.util.download_model('fr', if_exists='ignore')
|
|
@@ -26,15 +33,47 @@ dic = enchant.Dict("fr_FR")
|
|
| 26 |
def is_valid_word(word: str) -> bool:
|
| 27 |
return dic.check(word)
|
| 28 |
|
| 29 |
-
# ββ
|
| 30 |
def init_scores_file():
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
with open(SCORES_FILE, "w", newline="") as f:
|
| 33 |
-
csv.writer(f).writerow(["score", "timestamp"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
def save_score(score: float):
|
| 36 |
-
with
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
|
| 39 |
def load_stats() -> str:
|
| 40 |
if not os.path.exists(SCORES_FILE):
|
|
@@ -195,7 +234,7 @@ def run_dat(w1, w2, w3, w4, w5, w6, w7, w8, w9, w10):
|
|
| 195 |
far, close = top_pairs(emb, words)
|
| 196 |
html_matrix = build_html_matrix(words, dist_mat)
|
| 197 |
|
| 198 |
-
save_score(score)
|
| 199 |
stats = load_stats()
|
| 200 |
|
| 201 |
result = f"""## Votre score DAT : **{score:.1f} / 100**
|
|
@@ -271,7 +310,7 @@ with gr.Blocks(title="DAT β CrΓ©ativitΓ© Divergente", css=css,
|
|
| 271 |
"---\n"
|
| 272 |
"*BasΓ© sur [Olson et al. (2021), PNAS](https://doi.org/10.1073/pnas.2022340118) Β· "
|
| 273 |
"Modèle : FastText fr (Meta/Wikipedia) · "
|
| 274 |
-
"Un outil [Fouloscopie](https://www.
|
| 275 |
)
|
| 276 |
|
| 277 |
demo.launch()
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
import csv
|
| 5 |
import os
|
| 6 |
+
import threading
|
| 7 |
from itertools import combinations
|
| 8 |
from datetime import datetime
|
| 9 |
import fasttext
|
| 10 |
import fasttext.util
|
| 11 |
import enchant
|
| 12 |
+
from huggingface_hub import HfApi, hf_hub_download
|
| 13 |
+
from huggingface_hub.utils import EntryNotFoundError
|
| 14 |
|
| 15 |
# ββ Config ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 16 |
SCORES_FILE = "scores.csv"
|
| 17 |
MODEL_PATH = "cc.fr.300.bin"
|
| 18 |
N_WORDS = 10
|
| 19 |
SCORE_WORDS = 7
|
| 20 |
+
DATASET_REPO = "MendoToto/dat-score"
|
| 21 |
+
HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
| 22 |
+
hf_api = HfApi(token=HF_TOKEN)
|
| 23 |
+
csv_lock = threading.Lock()
|
| 24 |
|
| 25 |
print("Téléchargement du modèle FastText français...")
|
| 26 |
fasttext.util.download_model('fr', if_exists='ignore')
|
|
|
|
| 33 |
def is_valid_word(word: str) -> bool:
|
| 34 |
return dic.check(word)
|
| 35 |
|
| 36 |
+
# ββ Persistance via HuggingFace Dataset ββββββββββββββββββββββββββββββββββββββ
|
| 37 |
def init_scores_file():
|
| 38 |
+
"""TΓ©lΓ©charge le CSV depuis le dataset HF, ou crΓ©e un fichier vide."""
|
| 39 |
+
try:
|
| 40 |
+
path = hf_hub_download(
|
| 41 |
+
repo_id=DATASET_REPO,
|
| 42 |
+
filename="scores.csv",
|
| 43 |
+
repo_type="dataset",
|
| 44 |
+
token=HF_TOKEN
|
| 45 |
+
)
|
| 46 |
+
import shutil
|
| 47 |
+
shutil.copy(path, SCORES_FILE)
|
| 48 |
+
print("CSV chargΓ© depuis le dataset HF.")
|
| 49 |
+
except EntryNotFoundError:
|
| 50 |
with open(SCORES_FILE, "w", newline="") as f:
|
| 51 |
+
csv.writer(f).writerow(["score", "timestamp", "mot1", "mot2", "mot3", "mot4", "mot5", "mot6", "mot7", "mot8", "mot9", "mot10"])
|
| 52 |
+
print("Nouveau CSV créé.")
|
| 53 |
+
except Exception as e:
|
| 54 |
+
print(f"Impossible de charger le CSV depuis HF : {e}")
|
| 55 |
+
if not os.path.exists(SCORES_FILE):
|
| 56 |
+
with open(SCORES_FILE, "w", newline="") as f:
|
| 57 |
+
csv.writer(f).writerow(["score", "timestamp"])
|
| 58 |
+
|
| 59 |
+
def push_scores_to_hf():
|
| 60 |
+
"""Upload le CSV local vers le dataset HF."""
|
| 61 |
+
try:
|
| 62 |
+
hf_api.upload_file(
|
| 63 |
+
path_or_fileobj=SCORES_FILE,
|
| 64 |
+
path_in_repo="scores.csv",
|
| 65 |
+
repo_id=DATASET_REPO,
|
| 66 |
+
repo_type="dataset",
|
| 67 |
+
token=HF_TOKEN
|
| 68 |
+
)
|
| 69 |
+
except Exception as e:
|
| 70 |
+
print(f"Erreur upload HF : {e}")
|
| 71 |
|
| 72 |
+
def save_score(score: float, words: list):
|
| 73 |
+
with csv_lock:
|
| 74 |
+
with open(SCORES_FILE, "a", newline="") as f:
|
| 75 |
+
csv.writer(f).writerow([round(score, 1), datetime.now().strftime("%Y-%m-%d %H:%M")] + words)
|
| 76 |
+
push_scores_to_hf()
|
| 77 |
|
| 78 |
def load_stats() -> str:
|
| 79 |
if not os.path.exists(SCORES_FILE):
|
|
|
|
| 234 |
far, close = top_pairs(emb, words)
|
| 235 |
html_matrix = build_html_matrix(words, dist_mat)
|
| 236 |
|
| 237 |
+
save_score(score, words)
|
| 238 |
stats = load_stats()
|
| 239 |
|
| 240 |
result = f"""## Votre score DAT : **{score:.1f} / 100**
|
|
|
|
| 310 |
"---\n"
|
| 311 |
"*BasΓ© sur [Olson et al. (2021), PNAS](https://doi.org/10.1073/pnas.2022340118) Β· "
|
| 312 |
"Modèle : FastText fr (Meta/Wikipedia) · "
|
| 313 |
+
"Un outil [Fouloscopie](https://www.mehdimoussaid.com/)*"
|
| 314 |
)
|
| 315 |
|
| 316 |
demo.launch()
|