Compare commits
No commits in common. "1f0c73e0c968bbd26bc73ae4f500ae378e33a75d" and "db20e54f5d08ab72232ce8b16ac6d6eb49606440" have entirely different histories.
1f0c73e0c9
...
db20e54f5d
4
.vscode/settings.json
vendored
4
.vscode/settings.json
vendored
@ -1,4 +0,0 @@
|
||||
{
|
||||
"python.formatting.provider": "black",
|
||||
"python.pythonPath": "venv\\Scripts\\python.exe"
|
||||
}
|
||||
@ -4,7 +4,6 @@ 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 always answer in french.
|
||||
You must not interpret the author's thoughts or reflections.
|
||||
Do not add any text before or after the text provided.
|
||||
"""
|
||||
51
main.py
51
main.py
@ -17,6 +17,9 @@ 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.)
|
||||
OUTPUT_PDF_PATH = PDF_PATH.replace(".pdf", f" ({TARGET_LANGUAGE.upper()[:2]}).pdf") # Chemin du PDF de sortie
|
||||
|
||||
# Prompt système personnalisé (instructions pour le LLM)
|
||||
SYSTEM_PROMPT = """"""
|
||||
|
||||
def extract_parameters_from_template(template_str):
|
||||
"""Extrait les paramètres du modèle à partir du template."""
|
||||
import re
|
||||
@ -129,33 +132,47 @@ def extract_text_from_pdf(pdf_path):
|
||||
text_by_page.append(text)
|
||||
return text_by_page
|
||||
|
||||
def split_pages_in_paragraphs(pages_text):
|
||||
"""
|
||||
Divise le texte en paragraphes en détectant un point suivi d'un saut de ligne ou d'un retour à la ligne.
|
||||
Conserve les sauts de ligne à l'intérieur des paragraphes.
|
||||
"""
|
||||
def merge_paragraphs_across_pages(pages_text):
|
||||
"""Divise le texte en chunks raisonnables pour la traduction."""
|
||||
import re
|
||||
|
||||
# Concatène tout le texte
|
||||
full_text = "\n".join(pages_text)
|
||||
|
||||
# Remplace les sauts de ligne à l'intérieur des paragraphes par des espaces
|
||||
# (pour éviter les sauts de ligne intempestifs dans un même paragraphe)
|
||||
full_text = re.sub(r'(?<![.!?])\n+(?![.!?])', ' ', full_text)
|
||||
# Essaie d'abord de diviser par les doubles sauts de ligne
|
||||
paragraphs = re.split(r'\n\s*\n+', full_text.strip())
|
||||
|
||||
# Divise le texte en paragraphes : un point suivi d'un saut de ligne
|
||||
paragraphs = re.split(r'(?<=[.!?])\s*\n+', full_text.strip())
|
||||
# Si on obtient seulement un paragraphe, on divise par une taille maximale
|
||||
if len(paragraphs) == 1:
|
||||
print("Aucune séparation par double saut de ligne détectée. Division par taille...")
|
||||
# Divise par les phrases (points suivis d'un espace)
|
||||
sentences = re.split(r'(?<=[.!?])\s+', full_text.strip())
|
||||
|
||||
# Nettoie chaque paragraphe : remplace les sauts de ligne restants par des espaces
|
||||
paragraphs = [re.sub(r'\s+', ' ', p).strip() for p in paragraphs if p.strip()]
|
||||
# Regroupe les phrases en chunks d'environ 1500 caractères
|
||||
max_chunk_size = 1500
|
||||
paragraphs = []
|
||||
current_chunk = ""
|
||||
|
||||
for sentence in sentences:
|
||||
if len(current_chunk) + len(sentence) < max_chunk_size:
|
||||
current_chunk += (" " + sentence) if current_chunk else sentence
|
||||
else:
|
||||
if current_chunk:
|
||||
paragraphs.append(current_chunk)
|
||||
current_chunk = sentence
|
||||
|
||||
if current_chunk:
|
||||
paragraphs.append(current_chunk)
|
||||
else:
|
||||
# Normalise les sauts de ligne internes
|
||||
paragraphs = [re.sub(r'\n+', ' ', p.strip()) for p in paragraphs if p.strip()]
|
||||
|
||||
return paragraphs
|
||||
|
||||
|
||||
def send_to_ollama(text, target_lang=TARGET_LANGUAGE, model=OLLAMA_MODEL, context_size=128000):
|
||||
def send_to_ollama(text, target_lang=TARGET_LANGUAGE, model=OLLAMA_MODEL, context_size=128000, system_prompt=SYSTEM_PROMPT):
|
||||
"""Envoie une requête à Ollama et retourne la réponse traduite."""
|
||||
# Construit le prompt avec les instructions système et la demande de traduction
|
||||
full_prompt = f"\n\nTraduis le texte suivant de l'ukrainien vers le {target_lang} :\n{text}"
|
||||
full_prompt = f"{system_prompt}\n\nTraduis le texte suivant de l'ukrainien vers le {target_lang} :\n{text}"
|
||||
payload = {
|
||||
"model": model,
|
||||
"prompt": full_prompt,
|
||||
@ -238,7 +255,7 @@ def create_pdf_from_results(results, output_path):
|
||||
# Préserver la mise en page en convertissant les sauts de ligne
|
||||
formatted_text = translation.replace("\n", "<br/>")
|
||||
story.append(Paragraph(formatted_text, body_style))
|
||||
# story.append(Spacer(1, 0.1*inch))
|
||||
story.append(Spacer(1, 0.1*inch))
|
||||
|
||||
# Infos sur le LLM
|
||||
story.append(Spacer(1, 0.2*inch))
|
||||
@ -257,7 +274,7 @@ def main():
|
||||
print(f"Nombre de pages extraites : {len(pages)}")
|
||||
|
||||
# Fusion des paragraphes qui s'étendent sur plusieurs pages
|
||||
paragraphs = split_pages_in_paragraphs(pages)
|
||||
paragraphs = merge_paragraphs_across_pages(pages)
|
||||
print(f"Nombre de paragraphes complets extraits : {len(paragraphs)}")
|
||||
|
||||
# Dictionnaire pour stocker les résultats
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user