import pandas as pd from openai import OpenAI import os import re import logging from openai_api_key import api_key from master_export2translate import transformar_texto client = OpenAI(api_key=api_key()) # Diccionario de idiomas IDIOMAS = { 1: ("English", "en"), 2: ("Portuguese", "pt"), 3: ("Spanish", "es"), 4: ("Russian", "ru"), 5: ("French", "fr"), 6: ("German", "de"), } def configurar_logger(): logger = logging.getLogger("translate_logger") logger.setLevel(logging.DEBUG) # Cambiado a DEBUG para más información fh = logging.FileHandler("translate_log.log", encoding="utf-8") fh.setLevel(logging.DEBUG) formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s") fh.setFormatter(formatter) logger.addHandler(fh) return logger logger = configurar_logger() def mostrar_idiomas(): print("Selecciona el idioma de destino:") for numero, (nombre, _) in IDIOMAS.items(): print(f"{numero}: {nombre}") def translate_text(text, source_lang, target_lang): logger.info( f"Solicitando traducción de {source_lang} a {target_lang} para el texto: {text}" ) response = client.chat.completions.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": f"You are a translator."}, { "role": "user", "content": f"Translate the following text from {source_lang} to {target_lang} while preserving special fields like <> and <#>. This texts are for an HMI industrial machine: {text}", }, ], max_tokens=150, temperature=0.3, ) translated_text = response.choices[0].message.content.strip() logger.info(f"Respuesta recibida: {translated_text}") return translated_text def translate_batch(texts, source_lang, target_lang): joined_text = "\n".join(texts) logger.info( f"Solicitando traducción de {source_lang} a {target_lang} para el lote de textos:\n{joined_text}" ) response = client.chat.completions.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": f"You are a translator."}, { "role": "user", "content": f"Translate the following texts from {source_lang} to {target_lang} while preserving special fields like <> and <#>:\n\n{joined_text}", }, ], max_tokens=1500, temperature=0.3, ) translations = response.choices[0].message.content.strip().split("\n") logger.info(f"Respuestas recibidas:\n{translations}") return translations def texto_requiere_traduccion(texto): palabras = re.findall(r"\b\w{4,}\b", texto) campos_especiales = re.findall(r"<.*?>", texto) requiere_traduccion = len(palabras) > 0 or len(campos_especiales) != len( re.findall(r"<#>", texto) ) logger.debug( f"Decisión de traducción para texto '{texto}': {'Sí' if requiere_traduccion else 'No'} (palabras > 3 letras: {len(palabras) > 0}, solo campos especiales: {len(campos_especiales) == len(re.findall(r'<#>', texto))})" ) return requiere_traduccion def main(file_path, target_lang_code, traducir_todo, batch_size=10): df = pd.read_excel(file_path) source_col = "it-IT" target_col = f"{target_lang_code} Translated" if target_col in df.columns and not traducir_todo: df[target_col] = df[target_col] else: df[target_col] = None texts_to_translate = [] indices_to_translate = [] if traducir_todo: for index, text in df[source_col].astype(str).items(): processed_text = transformar_texto(text) if texto_requiere_traduccion(processed_text): texts_to_translate.append(text) indices_to_translate.append(index) else: for index, text in ( df.loc[df[target_col].isnull(), source_col].astype(str).items() ): processed_text = transformar_texto(text) if texto_requiere_traduccion(processed_text): texts_to_translate.append(text) indices_to_translate.append(index) num_texts = len(texts_to_translate) logger.info(f"Número total de textos a traducir: {num_texts}") translations = [] for start_idx in range(0, num_texts, batch_size): end_idx = min(start_idx + batch_size, num_texts) batch_texts = texts_to_translate[start_idx:end_idx] batch_translations = translate_batch(batch_texts, "Italian", target_lang_code) translations.extend(batch_translations) logger.info(f"Número total de traducciones recibidas: {len(translations)}") if len(translations) != len(indices_to_translate): logger.warning( f"Desajuste entre el número de traducciones ({len(translations)}) y el número de índices ({len(indices_to_translate)})" ) for i, index in enumerate(indices_to_translate): if i < len(translations): df.at[index, target_col] = translations[i] else: logger.error(f"No hay traducción disponible para el índice {index}") output_path = os.path.join( os.path.dirname(file_path), "master_export2translate_translated.xlsx" ) df.to_excel(output_path, index=False) logger.info(f"Archivo traducido guardado en: {output_path}") print(f"Archivo traducido guardado en: {output_path}") if __name__ == "__main__": batch_size = 10 translate_file = "master_export2translate.xlsx" mostrar_idiomas() seleccion_idioma = int(input("Introduce el número del idioma de destino: ")) if seleccion_idioma not in IDIOMAS: print("Selección inválida.") else: _, target_lang_code = IDIOMAS[seleccion_idioma] traducir_todo = ( input("¿Desea traducir todas las celdas (s/n)? ").strip().lower() == "s" ) main(translate_file, target_lang_code, traducir_todo, batch_size)