Compare commits
No commits in common. "c28bbc6be77f6ebd1de09654343cf16940484fb4" and "1f0c73e0c968bbd26bc73ae4f500ae378e33a75d" have entirely different histories.
c28bbc6be7
...
1f0c73e0c9
12
Modelfile
12
Modelfile
@ -1,10 +1,10 @@
|
|||||||
FROM zongwei/gemma3-translator:4b
|
FROM zongwei/gemma3-translator:4b
|
||||||
PARAMETER temperature 0.3
|
PARAMETER temperature 0.1
|
||||||
PARAMETER num_ctx 131072
|
PARAMETER num_ctx 131072
|
||||||
SYSTEM """
|
SYSTEM """
|
||||||
Tu es un traducteur professionnel spécialisé dans la traduction de textes historiques ukrainiens.
|
You are a professional translator specialising in translating Ukrainian text into English.
|
||||||
Traduis avec précision et naturel, en respectant l'intonation originale utilisée par l'auteur du texte.
|
Translate accurately and naturally, respecting the original intonation used by the author of the text.
|
||||||
Tu dois toujours répondre en français.
|
You must always answer in french.
|
||||||
Tu ne dois pas interpréter les pensées ou les réflexions de l'auteur.
|
You must not interpret the author's thoughts or reflections.
|
||||||
N'ajoutes aucun texte sous quelle forme que ce soit avant ou après le texte tarduit.
|
Do not add any text before or after the text provided.
|
||||||
"""
|
"""
|
||||||
60
main.py
60
main.py
@ -54,59 +54,35 @@ def extract_parameters_from_template(template_str):
|
|||||||
return parameters
|
return parameters
|
||||||
|
|
||||||
def get_llm_model_info(model=OLLAMA_MODEL):
|
def get_llm_model_info(model=OLLAMA_MODEL):
|
||||||
"""
|
"""Extrait les informations du modèle LLM depuis Ollama."""
|
||||||
Extrait les informations du modèle LLM depuis Ollama, y compris le nom depuis la ligne FROM du Modelfile.
|
|
||||||
|
|
||||||
@param model: Nom du modèle à interroger.
|
|
||||||
@type model: str
|
|
||||||
@return: Dictionnaire contenant les informations du modèle, ou None en cas d'erreur.
|
|
||||||
@rtype: dict | None
|
|
||||||
"""
|
|
||||||
try:
|
try:
|
||||||
# Chemin vers le fichier Modelfile (supposé être dans le même répertoire que le script)
|
|
||||||
modelfile_path = os.path.join(os.path.dirname(__file__), "Modelfile")
|
|
||||||
|
|
||||||
# Initialisation de model_name
|
|
||||||
model_name = "none"
|
|
||||||
|
|
||||||
# Lecture du fichier Modelfile pour extraire le nom du modèle
|
|
||||||
if os.path.exists(modelfile_path):
|
|
||||||
with open(modelfile_path, 'r', encoding='utf-8') as file:
|
|
||||||
for line in file:
|
|
||||||
if line.strip().startswith('FROM '):
|
|
||||||
model_name = line.strip().split('FROM ')[1].strip()
|
|
||||||
break
|
|
||||||
|
|
||||||
# URL pour obtenir les informations du modèle
|
# URL pour obtenir les informations du modèle
|
||||||
info_url = OLLAMA_URL.replace("/api/generate", "/api/show")
|
info_url = OLLAMA_URL.replace("/api/generate", "/api/show")
|
||||||
payload = {"name": model}
|
payload = {"name": model}
|
||||||
|
|
||||||
response = requests.post(info_url, json=payload)
|
response = requests.post(info_url, json=payload)
|
||||||
|
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
model_data = response.json()
|
model_data = response.json()
|
||||||
|
|
||||||
# Gère le cas où model_data est une chaîne
|
# Gère le cas où model_data est une chaîne
|
||||||
if isinstance(model_data, str):
|
if isinstance(model_data, str):
|
||||||
model_data = json.loads(model_data)
|
model_data = json.loads(model_data)
|
||||||
|
|
||||||
# Extrait les paramètres du template
|
# Extrait les paramètres du template
|
||||||
parameters = model_data.get('parameters', '')
|
parameters = model_data.get('parameters', '')
|
||||||
parsed_params = extract_parameters_from_template(parameters)
|
parsed_params = extract_parameters_from_template(parameters)
|
||||||
|
|
||||||
# Extraction du nom depuis la ligne FROM
|
|
||||||
modelfile_content = model_data.get('Modelfile', '')
|
|
||||||
|
|
||||||
# Extraction des informations principales
|
# Extraction des informations principales
|
||||||
info = {
|
info = {
|
||||||
"temperature": parsed_params.get('temperature', model_data.get("temperature", "Not available")),
|
"temperature": parsed_params.get('temperature', model_data.get("temperature", "Not available")),
|
||||||
"name": model_name,
|
|
||||||
"num_ctx": parsed_params.get('num_ctx', "Not available"),
|
"num_ctx": parsed_params.get('num_ctx', "Not available"),
|
||||||
"top_k": parsed_params.get('top_k', "Not available"),
|
"top_k": parsed_params.get('top_k', "Not available"),
|
||||||
"top_p": parsed_params.get('top_p', "Not available"),
|
"top_p": parsed_params.get('top_p', "Not available"),
|
||||||
"system": model_data.get("system", "Not available"),
|
"system": model_data.get("system", "Not available"),
|
||||||
"modified_at": model_data.get("modified_at", "Not available"),
|
"modified_at": model_data.get("modified_at", "Not available"),
|
||||||
}
|
}
|
||||||
|
|
||||||
return info
|
return info
|
||||||
else:
|
else:
|
||||||
print(f"Erreur lors de la récupération du modèle : {response.text}")
|
print(f"Erreur lors de la récupération du modèle : {response.text}")
|
||||||
@ -135,7 +111,7 @@ def display_llm_info():
|
|||||||
else:
|
else:
|
||||||
formatted_date = 'Not available'
|
formatted_date = 'Not available'
|
||||||
|
|
||||||
return f"LLM Modèle: {info['name']}<br//>\nDate de modification: {formatted_date}<br//>\nSystem: {info['system']}<br//>\nTemperature: {info['temperature']}"
|
return f"LLM Modèle: {OLLAMA_MODEL}<br//>\nSystem: {info['system']}<br//>\nTemperature: {info['temperature']}<br//>\nDate de modification: {formatted_date}"
|
||||||
else:
|
else:
|
||||||
return "Informations du modèle non disponibles"
|
return "Informations du modèle non disponibles"
|
||||||
|
|
||||||
@ -175,6 +151,7 @@ def split_pages_in_paragraphs(pages_text):
|
|||||||
|
|
||||||
return paragraphs
|
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):
|
||||||
"""Envoie une requête à Ollama et retourne la réponse traduite."""
|
"""Envoie une requête à Ollama et retourne la réponse traduite."""
|
||||||
# Construit le prompt avec les instructions système et la demande de traduction
|
# Construit le prompt avec les instructions système et la demande de traduction
|
||||||
@ -271,24 +248,6 @@ def create_pdf_from_results(results, output_path):
|
|||||||
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):
|
|
||||||
"""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
|
|
||||||
|
|
||||||
# Titre avec la langue cible
|
|
||||||
title_text = f"Traduction - Ukrainien vers {TARGET_LANGUAGE.capitalize()}"
|
|
||||||
with open(OUTPUT_TXT_PATH, 'w', encoding='utf-8') as txt_file:
|
|
||||||
txt_file.write(title_text + "\n\n")
|
|
||||||
|
|
||||||
# Contenu
|
|
||||||
for page_num, translation in results.items():
|
|
||||||
# Préserver la mise en page en convertissant les sauts de ligne
|
|
||||||
txt_file.write(translation + "\n\n")
|
|
||||||
|
|
||||||
# Infos sur le LLM
|
|
||||||
txt_file.write("\n")
|
|
||||||
txt_file.write(display_llm_info() + "\n")
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# Affiche les informations du modèle LLM
|
# Affiche les informations du modèle LLM
|
||||||
display_llm_info()
|
display_llm_info()
|
||||||
@ -317,7 +276,6 @@ def main():
|
|||||||
|
|
||||||
# Création du PDF avec tous les résultats
|
# Création du PDF avec tous les résultats
|
||||||
create_pdf_from_results(results, OUTPUT_PDF_PATH)
|
create_pdf_from_results(results, OUTPUT_PDF_PATH)
|
||||||
create_txt_from_results(results, OUTPUT_PDF_PATH)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user