Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| from langdetect import detect | |
| import argostranslate.package | |
| import argostranslate.translate | |
| from tqdm import tqdm | |
| import os | |
| print("--- PRÉPARATION DE L'IA DE TRADUCTION HORS-LIGNE ---") | |
| # 1. Mise à jour et téléchargement du dictionnaire Anglais -> Français | |
| argostranslate.package.update_package_index() | |
| available_packages = argostranslate.package.get_available_packages() | |
| try: | |
| package_to_install = next( | |
| filter(lambda x: x.from_code == 'en' and x.to_code == 'fr', available_packages) | |
| ) | |
| argostranslate.package.install_from_path(package_to_install.download()) | |
| print("✅ Dictionnaire installé et prêt !") | |
| except StopIteration: | |
| print("⚠️ Le dictionnaire est déjà installé ou introuvable.") | |
| # 2. Configuration des fichiers | |
| chemin_fichier_source = '1_fichiers_fusionnes_non_traduits.csv' | |
| chemin_fichier_final = '2_fichier_final_TOTALEMENT_traduit.csv' | |
| # Fonction de traduction intelligente | |
| def traduire_cellule(texte): | |
| if pd.isna(texte) or not isinstance(texte, str) or len(texte.strip()) < 3: | |
| return texte | |
| try: | |
| langue = detect(texte) | |
| if langue == 'fr': | |
| return texte | |
| elif langue == 'en': | |
| return argostranslate.translate.translate(texte, 'en', 'fr') | |
| else: | |
| return texte | |
| except Exception: | |
| return texte | |
| # 3. LECTURE PAR BLOCS (CHUNKS) - La sécurité Anti-Crash | |
| taille_bloc = 10000 # Le script sauvegarde toutes les 10 000 lignes | |
| lignes_deja_traitees = 0 | |
| # Vérifier si on a déjà commencé le travail avant | |
| if os.path.exists(chemin_fichier_final): | |
| # On compte combien de lignes sont déjà dans le fichier final | |
| df_existant = pd.read_csv(chemin_fichier_final, usecols=[0], low_memory=False) | |
| lignes_deja_traitees = len(df_existant) | |
| print(f"\n🔄 Reprise du travail détectée : {lignes_deja_traitees} lignes déjà traduites !") | |
| mode_ecriture = 'a' # 'a' pour Append (ajouter à la fin) | |
| ecrire_en_tete = False | |
| else: | |
| print("\n🚀 Démarrage d'une nouvelle traduction totale...") | |
| mode_ecriture = 'w' # 'w' pour Write (nouveau fichier) | |
| ecrire_en_tete = True | |
| print(f"--- TRADUCTION EN COURS (Sauvegarde toutes les {taille_bloc} lignes) ---") | |
| # On lit le fichier source par petits morceaux de 10 000 lignes | |
| lecteur_csv = pd.read_csv(chemin_fichier_source, chunksize=taille_bloc, low_memory=False) | |
| lignes_actuelles = 0 | |
| for bloc_df in lecteur_csv: | |
| lignes_actuelles += len(bloc_df) | |
| # On saute les blocs qui ont déjà été traduits lors d'une session précédente | |
| if lignes_actuelles <= lignes_deja_traitees: | |
| continue | |
| print(f"\nTraitement du bloc : de {lignes_actuelles - len(bloc_df)} à {lignes_actuelles} lignes...") | |
| colonnes_texte = bloc_df.select_dtypes(include=['object']).columns | |
| # On traduit toutes les colonnes de texte de ce bloc | |
| for col in colonnes_texte: | |
| bloc_df[col] = bloc_df[col].apply(traduire_cellule) | |
| # On sauvegarde immédiatement ce bloc dans le fichier final ! | |
| bloc_df.to_csv(chemin_fichier_final, mode=mode_ecriture, header=ecrire_en_tete, index=False, encoding='utf-8') | |
| # Après le premier bloc, on ne réécrit plus l'en-tête, on ajoute juste les lignes en dessous | |
| mode_ecriture = 'a' | |
| ecrire_en_tete = False | |
| print(f"💾 Sauvegarde réussie. On passe à la suite...") | |
| print(f"\n🎉 SUCCÈS ABSOLU ! Les 4,6 millions de lignes ont été traduites dans : {chemin_fichier_final}") |