Compare commits
No commits in common. "182e6e7a9803a4dbac084505acc2a9b1e4b52c1a" and "defcc3843574f517e4c0d6bfb057437d71d3c8f0" have entirely different histories.
182e6e7a98
...
defcc38435
File diff suppressed because it is too large
Load Diff
@ -1,152 +0,0 @@
|
||||
import torch
|
||||
from datasets import load_dataset
|
||||
from transformers import (
|
||||
AutoTokenizer,
|
||||
AutoModelForCausalLM,
|
||||
TrainingArguments,
|
||||
)
|
||||
from peft import (
|
||||
LoraConfig,
|
||||
get_peft_model,
|
||||
prepare_model_for_kbit_training,
|
||||
)
|
||||
from trl import SFTTrainer
|
||||
import os
|
||||
os.environ["TORCHDYNAMO_DISABLE"] = "1"
|
||||
|
||||
# ----------------------------
|
||||
# Model configuration
|
||||
# ----------------------------
|
||||
MODEL_NAME = "Qwen/Qwen2.5-14B-Instruct"
|
||||
|
||||
print("=== Starting fine-tuning script ===")
|
||||
|
||||
print(f"{80 * '_'}\n[1/7] Loading tokenizer...")
|
||||
tokenizer = AutoTokenizer.from_pretrained(
|
||||
MODEL_NAME,
|
||||
trust_remote_code=True
|
||||
)
|
||||
|
||||
# Ensure padding token is defined
|
||||
tokenizer.pad_token = tokenizer.eos_token
|
||||
tokenizer.model_max_length = 1024
|
||||
|
||||
print("Tokenizer loaded and configured.")
|
||||
|
||||
print(f"{80 * '_'}\n[2/7] Loading model in 4-bit mode (QLoRA)...")
|
||||
model = AutoModelForCausalLM.from_pretrained(
|
||||
MODEL_NAME,
|
||||
load_in_4bit=True,
|
||||
device_map="auto",
|
||||
torch_dtype=torch.float16, # OK for weights
|
||||
trust_remote_code=True,
|
||||
)
|
||||
print("Model loaded.")
|
||||
|
||||
print(f"{80 * '_'}\n[3/7] Preparing model for k-bit training...")
|
||||
model = prepare_model_for_kbit_training(model)
|
||||
print("Model prepared for k-bit training.")
|
||||
|
||||
# ----------------------------
|
||||
# LoRA configuration
|
||||
# ----------------------------
|
||||
print(f"{80 * '_'}\n[4/7] Configuring LoRA adapters...")
|
||||
lora_config = LoraConfig(
|
||||
r=16,
|
||||
lora_alpha=32,
|
||||
lora_dropout=0.05,
|
||||
bias="none",
|
||||
task_type="CAUSAL_LM",
|
||||
target_modules=[
|
||||
"q_proj",
|
||||
"k_proj",
|
||||
"v_proj",
|
||||
"o_proj",
|
||||
"gate_proj",
|
||||
"up_proj",
|
||||
"down_proj",
|
||||
],
|
||||
)
|
||||
|
||||
model = get_peft_model(model, lora_config)
|
||||
model.print_trainable_parameters()
|
||||
print("LoRA adapters attached to the model.")
|
||||
|
||||
# ----------------------------
|
||||
# Dataset loading
|
||||
# ----------------------------
|
||||
print(f"{80 * '_'}\n[5/7] Loading dataset from JSON file...")
|
||||
dataset = load_dataset(
|
||||
"json",
|
||||
data_files="traductions.json"
|
||||
)
|
||||
print(f"Dataset loaded with {len(dataset['train'])} samples.")
|
||||
|
||||
print("Formatting dataset for Ukrainian → French translation...")
|
||||
|
||||
def format_prompt(example):
|
||||
prompt = (
|
||||
"Translate the following Ukrainian text into French.\n\n"
|
||||
f"Ukrainian: {example['text']}\n"
|
||||
f"French: {example['translation']}"
|
||||
)
|
||||
return {"text": prompt}
|
||||
|
||||
dataset = dataset.map(format_prompt, remove_columns=dataset["train"].column_names)
|
||||
print("Dataset formatting completed.")
|
||||
|
||||
# ----------------------------
|
||||
# Training arguments
|
||||
# ----------------------------
|
||||
print(f"{80 * '_'}\n[6/7] Initializing training arguments...")
|
||||
training_args = TrainingArguments(
|
||||
output_dir="./qwen-uk-fr-lora",
|
||||
per_device_train_batch_size=1,
|
||||
gradient_accumulation_steps=8,
|
||||
learning_rate=2e-4,
|
||||
num_train_epochs=3,
|
||||
|
||||
fp16=False,
|
||||
bf16=False,
|
||||
|
||||
logging_steps=10,
|
||||
save_steps=500,
|
||||
save_total_limit=2,
|
||||
|
||||
# Use 32-bit optimizer
|
||||
optim="paged_adamw_32bit",
|
||||
|
||||
report_to="none",
|
||||
)
|
||||
|
||||
|
||||
print("Training arguments ready.")
|
||||
|
||||
# ----------------------------
|
||||
# Trainer
|
||||
# ----------------------------
|
||||
print("Initializing SFTTrainer...")
|
||||
trainer = SFTTrainer(
|
||||
model=model,
|
||||
train_dataset=dataset["train"],
|
||||
processing_class=tokenizer,
|
||||
args=training_args,
|
||||
)
|
||||
print("Trainer initialized.")
|
||||
|
||||
# ----------------------------
|
||||
# Train
|
||||
# ----------------------------
|
||||
print(f"{80 * '_'}\n[7/7] Starting training...")
|
||||
trainer.train()
|
||||
print("Training completed successfully.")
|
||||
|
||||
# ----------------------------
|
||||
# Save LoRA adapter
|
||||
# ----------------------------
|
||||
print("Saving LoRA adapter and tokenizer...")
|
||||
trainer.model.save_pretrained("./qwen-uk-fr-lora")
|
||||
tokenizer.save_pretrained("./qwen-uk-fr-lora")
|
||||
|
||||
print("=== Fine-tuning finished ===")
|
||||
print("LoRA adapter saved in ./qwen-uk-fr-lora")
|
||||
File diff suppressed because it is too large
Load Diff
@ -1,35 +0,0 @@
|
||||
import json
|
||||
from collections import defaultdict
|
||||
|
||||
# Chemin vers ton fichier d'entrée et de sortie
|
||||
input_file = "Paires de phrases en ukrainien-français - 2026-01-06.tsv" # Remplace par ton chemin
|
||||
output_file = "paires.json" # Fichier de sortie
|
||||
|
||||
# Dictionnaire pour stocker les paires uniques (clé = phrase ukrainienne, valeur = liste de traductions)
|
||||
unique_pairs = defaultdict(list)
|
||||
|
||||
# Lire le fichier d'entrée
|
||||
with open(input_file, "r", encoding="utf-8") as f:
|
||||
for line in f:
|
||||
# Diviser la ligne en colonnes (séparateur = tabulation)
|
||||
parts = line.strip().split("\t")
|
||||
if len(parts) >= 3:
|
||||
uk_text = parts[1] # Texte ukrainien
|
||||
fr_text = parts[3] # Traduction française
|
||||
# Ajouter la paire au dictionnaire (évite les doublons)
|
||||
if fr_text not in unique_pairs[uk_text]:
|
||||
unique_pairs[uk_text].append(fr_text)
|
||||
|
||||
# Écrire le fichier JSONL de sortie
|
||||
with open(output_file, "w", encoding="utf-8") as f_out:
|
||||
for uk_text, fr_translations in unique_pairs.items():
|
||||
# Prendre la première traduction (ou toutes si tu préfères)
|
||||
for fr_text in fr_translations:
|
||||
# Créer une entrée JSONL
|
||||
entry = {
|
||||
"text": uk_text,
|
||||
"translation": fr_text
|
||||
}
|
||||
f_out.write(json.dumps(entry, ensure_ascii=False) + "\n")
|
||||
|
||||
print(f"Fichier JSONL généré : {output_file}")
|
||||
@ -7,10 +7,10 @@ Tu es un traducteur spécialisé dans les mémoires ukrainiennes des années 191
|
||||
- Utilise le glossaire fourni pour les noms de lieux et termes historiques.
|
||||
- Garde le style narratif et les tournures orales de l'auteur.
|
||||
Règles strictes :
|
||||
1. **Conserve tous les noms de lieux** dans leur forme originale (ex. : Львів → Lviv, mais ajoute une note si nécessaire entre [ ]).
|
||||
1. **Conserve tous les noms de lieux** dans leur forme originale (ex. : Львів → Lviv, mais ajoute une note si nécessaire : "[Lemberg en 1910]").
|
||||
2. **Respecte le style narratif** : garde les tournures orales et les expressions propres à l’auteur.
|
||||
3. **Pour les termes historiques** (ex. : "powiat"), utilise le terme français standard et ajoute une note explicative.
|
||||
4. **Conserve les citations** russe/allemand/polonais intégrés au texte (mais ajoute une note de fin de paragraphe entre [ ] en la traduisant et en précisant la langue d'origine.
|
||||
3. **Pour les termes historiques** (ex. : "powiat"), utilise le terme français standard ou ajoute une note explicative.
|
||||
4. **Ne traduis pas** les mots en russe/allemand/polonais intégrés au texte (ex. : citations, noms officiels).
|
||||
5. **Structure** : Garde les sauts de ligne et la mise en page originale.
|
||||
6. **Notes du traducteur** : Ajoute entre crochets [ ] les explications contextuelles si un contexte historique existe (ex. : "[Note : le context]").
|
||||
6. **Notes du traducteur** : Ajoute entre crochets [ ] les explications contextuelles (ex. : "[Note : ville alors sous domination autrichienne]").
|
||||
"""
|
||||
89
README.md
89
README.md
@ -9,6 +9,29 @@ Ce projet permet de traduire un document PDF page par page en utilisant un modè
|
||||
- **Python** (version 3.8 ou supérieure)
|
||||
- **Ollama** installé et en cours d'exécution sur ta machine (en mode "serveur")
|
||||
- Un **document PDF** à traduire
|
||||
- Un modèle LLM spécialisé dans la traduction avec un context long.
|
||||
|
||||
---
|
||||
|
||||
## Création d'un modèle LLM de traduction avec Ollama
|
||||
En partant du LLM [zongwei/gemma3-translator:4b](https://ollama.com/zongwei/gemma3-translator), nous allons créer un modèle optimisé pour la traduction avec Ollama.
|
||||
Pour info : Inutile de le downloader, il le serra automatiquement au lancement de la commande.
|
||||
|
||||
```shell
|
||||
FROM zongwei/gemma3-translator:4b
|
||||
PARAMETER temperature 0.1
|
||||
PARAMETER num_ctx 131072
|
||||
SYSTEM """
|
||||
You are a professional translator specialising in translating Ukrainian text into English.
|
||||
Translate accurately and naturally, respecting the original intonation used by the author of the text.
|
||||
You must not interpret the author's thoughts or reflections.
|
||||
Do not add any text before or after the text provided.
|
||||
"""
|
||||
```
|
||||
Il faut ensuite compiler le modèle avec la commande :
|
||||
```
|
||||
ollama create traductionUkrainienVersFrancais -f .\Modelfile
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
@ -21,16 +44,11 @@ Ce projet permet de traduire un document PDF page par page en utilisant un modè
|
||||
```
|
||||
|
||||
2. **Installer les dépendances Python**
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
Puis faire :
|
||||
```bash
|
||||
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
|
||||
```
|
||||
|
||||
3. **Placer votre fichier PDF** dans le répertoire `Traduction` du projet avec le nom configuré dans `main.py` (par défaut : `TaniaBorecMemoir(Ukr).pdf`)
|
||||
3. **Placer votre fichier PDF** dans le répertoire du projet avec le nom configuré dans `main.py` (par défaut : `TaniaBorecMemoir(Ukr).pdf`)
|
||||
|
||||
---
|
||||
|
||||
@ -39,16 +57,37 @@ pip install torch torchvision torchaudio --index-url https://download.pytorch.or
|
||||
### Préparation
|
||||
|
||||
1. **Démarrer Ollama en mode serveur** sur votre machine (port 11434 par défaut)
|
||||
```bash
|
||||
ollama serve
|
||||
```
|
||||
```bash
|
||||
ollama serve
|
||||
```
|
||||
|
||||
2. **Vérifier que le modèle de traduction est disponible**
|
||||
```bash
|
||||
ollama list
|
||||
```
|
||||
Vous devez voir `traductionUkrainienVersFrancais` dans la liste.
|
||||
|
||||
Si ce n'est pas le cas, vous devez le générer comme décrit plus haut (paragraphe "Création d'un modèle LLM de traduction avec Ollama")
|
||||
|
||||
3. **Placer votre PDF** dans le même répertoire que le script `main.py`
|
||||
### Paramétrage du script
|
||||
`PDF_PATH`= "TaniaBorecMemoir(Ukr).pdf" <- Le nom du fichier pdf à traduire.
|
||||
`OLLAMA_MODEL` = "mitmul/plamo-2-translate:latest" <- Le nom
|
||||
`OLLAMA_URL` = "http://localhost:11434/api/generate" <- URL par défaut d'Ollama
|
||||
`TARGET_LANGUAGE` = "français" <- Langue cible (ex: "français", "anglais", "allemand", "espagnol", etc.)
|
||||
`SYSTEM_PROMPT` = """You are a professional translator specialising in Ukrainian text translation.
|
||||
Translate accurately and naturally, respecting the original intonation used by the author of the text.
|
||||
You must not interpret the author's thoughts or reflections.
|
||||
Do not add any text before or after the text provided.
|
||||
Preserve the layout and structure of the original text."""
|
||||
|
||||
|
||||
### Exécution
|
||||
|
||||
1. **Lancer le script de traduction**
|
||||
```bash
|
||||
python main.py
|
||||
```
|
||||
```bash
|
||||
python main.py
|
||||
```
|
||||
|
||||
2. **Le programme va :**
|
||||
- Extraire le texte de toutes les pages du PDF
|
||||
@ -58,8 +97,8 @@ python main.py
|
||||
- Envoyer chaque chunk au LLM pour traduction
|
||||
- Afficher la progression dans le terminal
|
||||
|
||||
3. **Le résultat final** sera généré dans un fichier PDF nommé `[nom_original](FR).pdf`
|
||||
- Exemple : `TaniaBorecMemoir(Ukr)(FR).pdf`
|
||||
3. **Le résultat final** sera généré dans un fichier PDF nommé `[nom_original] (FR).pdf`
|
||||
- Exemple : `TaniaBorecMemoir(Ukr) (FR).pdf`
|
||||
|
||||
### Fichier de sortie
|
||||
|
||||
@ -80,3 +119,19 @@ Vous pouvez modifier les paramètres suivants dans `main.py` :
|
||||
- `OUTPUT_PDF_PATH` : Chemin et nom du fichier PDF de sortie (généré autoamtiquement)
|
||||
|
||||
---
|
||||
|
||||
## Dépannage
|
||||
|
||||
### Erreur : "Connexion refusée à Ollama"
|
||||
- Vérifiez que Ollama est en cours d'exécution avec `ollama serve`
|
||||
- Vérifiez que l'URL est correcte (par défaut : `http://localhost:11434`)
|
||||
|
||||
### Erreur : "Modèle non trouvé"
|
||||
- Exécutez : `ollama create traductionUkrainienVersFrancais -f .\Modelfile`
|
||||
|
||||
### Le texte n'est pas bien séparé en paragraphes
|
||||
- Le script détecte automatiquement les doubles sauts de ligne
|
||||
- Si absent, il divise par phrases et regroupe en chunks de 1500 caractères
|
||||
|
||||
---
|
||||
|
||||
|
||||
@ -12,14 +12,14 @@ import os
|
||||
|
||||
# Configuration
|
||||
DEBUG = True
|
||||
PDF_PATH = "Traduction\TaniaBorecMemoir(Ukr).pdf"
|
||||
PDF_PATH = "TaniaBorecMemoir(Ukr).pdf"
|
||||
OLLAMA_MODEL = "traductionUkrainienVersFrancais:latest"
|
||||
OLLAMA_URL = "http://localhost:11434/api/generate"
|
||||
TARGET_LANGUAGE = "français"
|
||||
CHECKPOINT_FILE = "Traduction\checkpoint.json"
|
||||
TEMP_OUTPUT_TXT = "Traduction\output_temp.txt"
|
||||
FINAL_OUTPUT_PDF = PDF_PATH.replace(".pdf",f"({TARGET_LANGUAGE.upper()[:2]})_V8.pdf")
|
||||
FINAL_OUTPUT_TXT = PDF_PATH.replace(".pdf",f"({TARGET_LANGUAGE.upper()[:2]})_V8.txt")
|
||||
CHECKPOINT_FILE = "checkpoint.json"
|
||||
TEMP_OUTPUT_TXT = "output_temp.txt"
|
||||
FINAL_OUTPUT_PDF = PDF_PATH.replace(".pdf",f"({TARGET_LANGUAGE.upper()[:2]})_V6.pdf")
|
||||
FINAL_OUTPUT_TXT = PDF_PATH.replace(".pdf",f"({TARGET_LANGUAGE.upper()[:2]})_V6.txt")
|
||||
|
||||
DEBUG = True
|
||||
|
||||
@ -1,16 +1,8 @@
|
||||
certifi
|
||||
charset-normalizer
|
||||
idna
|
||||
pillow
|
||||
PyPDF2
|
||||
reportlab
|
||||
requests
|
||||
urllib3
|
||||
|
||||
torch
|
||||
transformers
|
||||
datasets
|
||||
peft
|
||||
bitsandbytes
|
||||
accelerate
|
||||
trl
|
||||
certifi==2026.1.4
|
||||
charset-normalizer==3.4.4
|
||||
idna==3.11
|
||||
pillow==12.1.0
|
||||
PyPDF2==3.0.1
|
||||
reportlab==4.4.7
|
||||
requests==2.32.5
|
||||
urllib3==2.6.2
|
||||
|
||||
6
run.bat
6
run.bat
@ -11,18 +11,18 @@ REM Chemin vers l'environnement virtuel Python (relatif au répertoire courant)
|
||||
set VENV_PATH=%CURRENT_DIR%\venv
|
||||
|
||||
REM Chemin vers votre script principal (relatif au répertoire courant)
|
||||
set MAIN_SCRIPT_PATH=%CURRENT_DIR%\Traduction\main.py
|
||||
set MAIN_SCRIPT_PATH=%CURRENT_DIR%\main.py
|
||||
|
||||
REM Activer l'environnement virtuel Python
|
||||
call %VENV_PATH%\Scripts\activate.bat
|
||||
|
||||
REM Lancer la compilation du modèle LLM pour Ollama
|
||||
ollama create traductionUkrainienVersFrancais -f .\Traduction\Modelfile
|
||||
ollama create traductionUkrainienVersFrancais -f .\Modelfile
|
||||
|
||||
:: 1. Vérifie si le processus ollama.exe est en cours d'exécution
|
||||
tasklist | find "ollama.exe" >nul
|
||||
if %ERRORLEVEL% equ 0 (
|
||||
echo [OK] Le processus Ollama est bien en cours d'execution.
|
||||
echo [OK] Le processus Ollama est en cours d'execution.
|
||||
) else (
|
||||
echo [ERREUR] Ollama n'est pas lancé.
|
||||
pause
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user