|
|
|
@@ -3,23 +3,23 @@ import requests |
|
|
|
import json |
|
|
|
from reportlab.lib.pagesizes import letter |
|
|
|
from reportlab.lib.units import inch |
|
|
|
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Flowable |
|
|
|
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer |
|
|
|
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle |
|
|
|
from reportlab.lib.enums import TA_JUSTIFY |
|
|
|
from reportlab.pdfbase import pdfmetrics |
|
|
|
from reportlab.pdfbase.ttfonts import TTFont |
|
|
|
import os |
|
|
|
import os, time |
|
|
|
|
|
|
|
# Configuration |
|
|
|
DEBUG = True |
|
|
|
PDF_PATH = "Traduction\TaniaBorecMemoir(Ukr).pdf" |
|
|
|
PDF_PATH = "Traduction/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 = "Traduction/checkpoint.json" |
|
|
|
TEMP_OUTPUT_TXT = "Traduction/output_temp.txt" |
|
|
|
FINAL_OUTPUT_PDF = PDF_PATH.replace(".pdf",f"({TARGET_LANGUAGE.upper()[:2]})_V9.pdf") |
|
|
|
FINAL_OUTPUT_TXT = PDF_PATH.replace(".pdf",f"({TARGET_LANGUAGE.upper()[:2]})_V9.txt") |
|
|
|
|
|
|
|
DEBUG = True |
|
|
|
|
|
|
|
@@ -341,6 +341,7 @@ def main(): |
|
|
|
print(f"Batches manquants détectés : {missing_batches}") |
|
|
|
|
|
|
|
# Traduction des paragraphes manquants |
|
|
|
temps_cumule = 0.0 |
|
|
|
for i in missing_batches: |
|
|
|
batch = paragraphs[i:i + batch_size] |
|
|
|
paragraph_cumul = "\n".join(batch) |
|
|
|
@@ -348,13 +349,24 @@ def main(): |
|
|
|
print(f"{15 * '-'} Traduction des paragraphes manquants {i+1} à {min(i + batch_size, len(paragraphs))} / {len(paragraphs)}") |
|
|
|
|
|
|
|
try: |
|
|
|
debut_chrono = time.time() |
|
|
|
result = send_to_ollama(paragraph_cumul) |
|
|
|
fin_chrono = time.time() |
|
|
|
temps_paragraphe = fin_chrono - debut_chrono |
|
|
|
temps_cumule += temps_paragraphe |
|
|
|
|
|
|
|
# Conversion en minutes et secondes |
|
|
|
minutes_paragraphe, secondes_paragraphe = divmod(temps_paragraphe, 60) |
|
|
|
minutes_cumule, secondes_cumule = divmod(temps_cumule, 60) |
|
|
|
|
|
|
|
print(f"{result}") |
|
|
|
results[str(i)] = result |
|
|
|
save_checkpoint(len(paragraphs), results) # Met à jour le dernier indice du batch |
|
|
|
save_temp_results(results) |
|
|
|
except Exception as e: |
|
|
|
print(f"Erreur lors de la traduction du paragraphe {i}: {e}") |
|
|
|
print(f" Temps de traduction : {int(minutes_paragraphe)} min {secondes_paragraphe:.2f} sec") |
|
|
|
print(f" Temps cumulé : {int(minutes_cumule)} min {secondes_cumule:.2f} sec") |
|
|
|
|
|
|
|
# Traitement des paragraphes suivants |
|
|
|
for i in range(last_index + 1, len(paragraphs), batch_size): |
|
|
|
@@ -364,7 +376,16 @@ def main(): |
|
|
|
print(f"{15 * '-'} Traduction des paragraphes {i+1} à {min(i + batch_size, len(paragraphs))} / {len(paragraphs)}") |
|
|
|
|
|
|
|
try: |
|
|
|
debut_chrono = time.time() |
|
|
|
result = send_to_ollama(paragraph_cumul) |
|
|
|
fin_chrono = time.time() |
|
|
|
temps_paragraphe = fin_chrono - debut_chrono |
|
|
|
temps_cumule += temps_paragraphe |
|
|
|
|
|
|
|
# Conversion en minutes et secondes |
|
|
|
minutes_paragraphe, secondes_paragraphe = divmod(temps_paragraphe, 60) |
|
|
|
minutes_cumule, secondes_cumule = divmod(temps_cumule, 60) |
|
|
|
|
|
|
|
print(f"{result}") |
|
|
|
results[str(i)] = result |
|
|
|
save_checkpoint(i + batch_size - 1, results) |
|
|
|
@@ -372,6 +393,9 @@ def main(): |
|
|
|
except Exception as e: |
|
|
|
print(f"Erreur : {e}") |
|
|
|
continue |
|
|
|
print(f" Temps de traduction : {int(minutes_paragraphe)} min {secondes_paragraphe:.2f} sec") |
|
|
|
print(f" Temps cumulé : {int(minutes_cumule)} min {secondes_cumule:.2f} sec") |
|
|
|
|
|
|
|
|
|
|
|
save_temp_results(results) |
|
|
|
create_pdf_from_results(results, FINAL_OUTPUT_PDF) |