| import json | import json | ||||
| from reportlab.lib.pagesizes import letter | from reportlab.lib.pagesizes import letter | ||||
| from reportlab.lib.units import inch | from reportlab.lib.units import inch | ||||
| from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer | |||||
| from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Flowable | |||||
| from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle | from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle | ||||
| from reportlab.lib.enums import TA_JUSTIFY | from reportlab.lib.enums import TA_JUSTIFY | ||||
| from reportlab.pdfbase import pdfmetrics | from reportlab.pdfbase import pdfmetrics | ||||
| from reportlab.pdfbase.ttfonts import TTFont | from reportlab.pdfbase.ttfonts import TTFont | ||||
| import os | import os | ||||
| from datetime import datetime | |||||
| # Configuration | # Configuration | ||||
| DEBUG = True | |||||
| PDF_PATH = "TaniaBorecMemoir(Ukr).pdf" | PDF_PATH = "TaniaBorecMemoir(Ukr).pdf" | ||||
| OLLAMA_MODEL = "traductionUkrainienVersFrancais:latest" | OLLAMA_MODEL = "traductionUkrainienVersFrancais:latest" | ||||
| OLLAMA_URL = "http://localhost:11434/api/generate" | OLLAMA_URL = "http://localhost:11434/api/generate" | ||||
| TARGET_LANGUAGE = "français" | TARGET_LANGUAGE = "français" | ||||
| CHECKPOINT_FILE = "checkpoint.json" | CHECKPOINT_FILE = "checkpoint.json" | ||||
| TEMP_OUTPUT_TXT = "output_temp.txt" | TEMP_OUTPUT_TXT = "output_temp.txt" | ||||
| FINAL_OUTPUT_PDF = PDF_PATH.replace(".pdf",f" ({TARGET_LANGUAGE.upper()[:2]})_V4.pdf") | |||||
| FINAL_OUTPUT_TXT = PDF_PATH.replace(".pdf",f" ({TARGET_LANGUAGE.upper()[:2]})_V4.txt") | |||||
| FINAL_OUTPUT_PDF = PDF_PATH.replace(".pdf",f"({TARGET_LANGUAGE.upper()[:2]})_V4.pdf") | |||||
| FINAL_OUTPUT_TXT = PDF_PATH.replace(".pdf",f"({TARGET_LANGUAGE.upper()[:2]})_V4.txt") | |||||
| DEBUG = True | |||||
| def extract_parameters_from_template(template_str): | def extract_parameters_from_template(template_str): | ||||
| """Extrait les paramètres du modèle à partir du template.""" | """Extrait les paramètres du modèle à partir du template.""" | ||||
| params_section = template_str | params_section = template_str | ||||
| # Parse les lignes de paramètres | # Parse les lignes de paramètres | ||||
| # Format: "stop "<end_of_turn>"" | |||||
| # Format: "stop "<end_of_turn>"" | |||||
| # "temperature 0.1" | # "temperature 0.1" | ||||
| lines = params_section.split('\n') | lines = params_section.split('\n') | ||||
| return "Informations du modèle non disponibles" | return "Informations du modèle non disponibles" | ||||
| def register_unicode_font(): | def register_unicode_font(): | ||||
| """Enregistre une police TrueType qui supporte le cyrilique.""" | |||||
| # Recherche une police système qui supporte le cyrilique | |||||
| """Enregistre une police TrueType qui supporte le cyrillique.""" | |||||
| # Recherche une police système qui supporte le cyrillique | |||||
| font_paths = [ | font_paths = [ | ||||
| r"C:\Windows\Fonts\DejaVuSans.ttf", | r"C:\Windows\Fonts\DejaVuSans.ttf", | ||||
| r"C:\Windows\Fonts\Calibri.ttf", | r"C:\Windows\Fonts\Calibri.ttf", | ||||
| else: | else: | ||||
| raise Exception(f"Erreur Ollama: {response.text}") | raise Exception(f"Erreur Ollama: {response.text}") | ||||
| # Création du PDF final (inchangée) | |||||
| # Création du PDF final avec numéros de chapitres dans la marge | |||||
| def create_pdf_from_results(results, output_path): | def create_pdf_from_results(results, output_path): | ||||
| """Crée un PDF à partir des résultats de traduction.""" | |||||
| doc = SimpleDocTemplate(output_path, pagesize=letter, topMargin=inch, bottomMargin=inch) | |||||
| """Crée un PDF à partir des résultats de traduction, avec des notes dans la marge et un numéro de page.""" | |||||
| story = [] | story = [] | ||||
| # Enregistre une police qui supporte le cyrilique | |||||
| font_name = register_unicode_font() | font_name = register_unicode_font() | ||||
| # Style personnalisé | |||||
| # Styles personnalisés | |||||
| styles = getSampleStyleSheet() | styles = getSampleStyleSheet() | ||||
| title_style = ParagraphStyle( | title_style = ParagraphStyle( | ||||
| 'CustomTitle', | 'CustomTitle', | ||||
| alignment=TA_JUSTIFY, | alignment=TA_JUSTIFY, | ||||
| fontName=font_name | fontName=font_name | ||||
| ) | ) | ||||
| page_style = ParagraphStyle( | page_style = ParagraphStyle( | ||||
| 'PageHeading', | 'PageHeading', | ||||
| parent=styles['Heading2'], | parent=styles['Heading2'], | ||||
| spaceBefore=0.2*inch, | spaceBefore=0.2*inch, | ||||
| fontName=font_name | fontName=font_name | ||||
| ) | ) | ||||
| body_style = ParagraphStyle( | body_style = ParagraphStyle( | ||||
| 'CustomBody', | 'CustomBody', | ||||
| parent=styles['BodyText'], | parent=styles['BodyText'], | ||||
| spaceAfter=0.2*inch, | spaceAfter=0.2*inch, | ||||
| fontName=font_name | fontName=font_name | ||||
| ) | ) | ||||
| note_style = ParagraphStyle( | |||||
| 'CustomBody', | |||||
| parent=styles['BodyText'], | |||||
| fontSize=8, | |||||
| alignment=TA_JUSTIFY, | |||||
| spaceAfter=0, | |||||
| fontName=font_name | |||||
| ) | |||||
| # Création du document avec les callbacks pour les notes et le numéro de page | |||||
| doc = SimpleDocTemplate( | |||||
| output_path, | |||||
| pagesize=letter, | |||||
| topMargin=inch, | |||||
| bottomMargin=inch, | |||||
| ) | |||||
| # Titre avec la langue cible | # Titre avec la langue cible | ||||
| story.append(Paragraph(f"Traduction - Ukrainien vers {TARGET_LANGUAGE.capitalize()}", title_style)) | story.append(Paragraph(f"Traduction - Ukrainien vers {TARGET_LANGUAGE.capitalize()}", title_style)) | ||||
| story.append(Paragraph(f"Document : {PDF_PATH}", title_style)) | story.append(Paragraph(f"Document : {PDF_PATH}", title_style)) | ||||
| story.append(Spacer(1, 0.2*inch)) | story.append(Spacer(1, 0.2*inch)) | ||||
| # Contenu | # Contenu | ||||
| for page_num, translation in results.items(): | |||||
| # Préserver la mise en page en convertissant les sauts de ligne | |||||
| for paragraph_num, translation in results.items(): | |||||
| formatted_text = translation.replace("\n", "<br/>") | formatted_text = translation.replace("\n", "<br/>") | ||||
| if DEBUG: | |||||
| # Ajoute le paragraphe avec sa note | |||||
| story.append(Paragraph(paragraph_num, note_style)) | |||||
| story.append(Paragraph(formatted_text, body_style)) | story.append(Paragraph(formatted_text, body_style)) | ||||
| # story.append(Spacer(1, 0.1*inch)) | |||||
| # Infos sur le LLM | # Infos sur le LLM | ||||
| story.append(Spacer(1, 0.2*inch)) | story.append(Spacer(1, 0.2*inch)) | ||||
| story.append(Paragraph(display_llm_info(), page_style)) | story.append(Paragraph(display_llm_info(), page_style)) | ||||
| # Construction du PDF | # Construction du PDF | ||||
| doc.build(story) | doc.build(story) | ||||
| print(f"PDF généré avec succès : {output_path}") | print(f"PDF généré avec succès : {output_path}") | ||||
| def create_txt_from_results(results, output_path): | def create_txt_from_results(results, output_path): | ||||
| """Crée un fichier TXT à partir des résultats de traduction.""" | """Crée un fichier TXT à partir des résultats de traduction.""" | ||||
| OUTPUT_TXT_PATH = output_path.replace(".pdf", f".txt") # Chemin du fichier TXT de sortie | OUTPUT_TXT_PATH = output_path.replace(".pdf", f".txt") # Chemin du fichier TXT de sortie | ||||
| txt_file.write(title_text + "\n\n") | txt_file.write(title_text + "\n\n") | ||||
| # Contenu | # Contenu | ||||
| for page_num, translation in results.items(): | |||||
| for paragraph_num, translation in results.items(): | |||||
| # Ajoute les numéro de paragraphe et chapitre | |||||
| if(DEBUG): txt_file.write(f"{paragraph_num}\n") | |||||
| # Préserver la mise en page en convertissant les sauts de ligne | # Préserver la mise en page en convertissant les sauts de ligne | ||||
| txt_file.write(translation + "\n\n") | txt_file.write(translation + "\n\n") | ||||
| paragraphs = split_pages_in_paragraphs(pages) | paragraphs = split_pages_in_paragraphs(pages) | ||||
| # Traitement des paragraphes | # Traitement des paragraphes | ||||
| batch_size = 3 | |||||
| batch_size = 5 | |||||
| for i in range(last_index + 1, len(paragraphs), batch_size): | for i in range(last_index + 1, len(paragraphs), batch_size): | ||||
| batch = paragraphs[i:i + batch_size] | batch = paragraphs[i:i + batch_size] | ||||
| paragraph_cumul = "\n".join(batch) | paragraph_cumul = "\n".join(batch) | ||||
| print("Traduction terminée !") | print("Traduction terminée !") | ||||
| if __name__ == "__main__": | if __name__ == "__main__": | ||||
| main() | |||||
| main() |