Compare commits

...

2 Commits

17 changed files with 552 additions and 244 deletions

View File

@ -12,7 +12,19 @@ from translation_config import TranslationConfig
import sys import sys
import threading import threading
import os import os
import subprocess import queue
class RedirectText:
def __init__(self, queue):
self.queue = queue
def write(self, string):
self.queue.put(string)
def flush(self):
pass
class TranslationApp: class TranslationApp:
def __init__(self, master): def __init__(self, master):
@ -26,142 +38,265 @@ class TranslationApp:
self.create_widgets() self.create_widgets()
self.place_widgets() self.place_widgets()
self.queue = queue.Queue()
self.update_output()
# Cargar el log existente al iniciar
self.load_existing_log()
def create_variables(self): def create_variables(self):
self.codigo_tipo_PLC_var = tk.StringVar(value=self.config.codigo_tipo_PLC) self.codigo_tipo_PLC_var = tk.StringVar(value=self.config.codigo_tipo_PLC)
self.codigo_columna_maestra_var = tk.StringVar(value=self.config.codigo_columna_maestra) self.codigo_columna_maestra_var = tk.StringVar(
self.codigo_idioma_var = tk.StringVar(value=self.config.codigo_idioma_seleccionado) value=self.config.codigo_columna_maestra
self.codigo_idioma_var2 = tk.StringVar(value=self.config.codigo_idioma_secundario) )
self.codigo_idioma_var = tk.StringVar(
value=self.config.codigo_idioma_seleccionado
)
self.codigo_idioma_var2 = tk.StringVar(
value=self.config.codigo_idioma_secundario
)
self.work_dir_var = tk.StringVar(value=self.config.work_dir) self.work_dir_var = tk.StringVar(value=self.config.work_dir)
self.master_name_var = tk.StringVar(value=self.config.master_name) self.master_name_var = tk.StringVar(value=self.config.master_name)
self.translate_name_var = tk.StringVar(value=self.config.translate_name) self.translate_name_var = tk.StringVar(value=self.config.translate_name)
self.auto_translate_name_var = tk.StringVar(value=self.config.auto_translate_name) self.auto_translate_name_var = tk.StringVar(
value=self.config.auto_translate_name
)
def create_widgets(self): def create_widgets(self):
self.main_frame = ttk.Frame(self.master) self.main_frame = ttk.Frame(self.master)
self.main_frame.grid(row=0, column=0, sticky='nsew') self.main_frame.grid(row=0, column=0, sticky="nsew")
self.master.grid_rowconfigure(0, weight=1) self.master.grid_rowconfigure(0, weight=1)
self.master.grid_columnconfigure(0, weight=1) self.master.grid_columnconfigure(0, weight=1)
self.frame_combos = ttk.Frame(self.main_frame) self.frame_combos = ttk.Frame(self.main_frame)
self.label_tipo_PLC = ttk.Label(self.frame_combos, text="Selecciona el tipo de PLC:") self.label_tipo_PLC = ttk.Label(
self.frame_combos, text="Selecciona el tipo de PLC:"
)
self.combo_tipo_PLC = ttk.Combobox(self.frame_combos, state="readonly") self.combo_tipo_PLC = ttk.Combobox(self.frame_combos, state="readonly")
self.combo_tipo_PLC["values"] = [nombre for nombre, _ in fc.PLCs.values()] self.combo_tipo_PLC["values"] = [nombre for nombre, _ in fc.PLCs.values()]
self.combo_tipo_PLC.bind('<<ComboboxSelected>>', self.update_tipo_PLC) self.combo_tipo_PLC.bind("<<ComboboxSelected>>", self.update_tipo_PLC)
self.set_combo_value(self.combo_tipo_PLC, self.codigo_tipo_PLC_var.get()) self.set_combo_value(self.combo_tipo_PLC, self.codigo_tipo_PLC_var.get())
self.label_codigo_maestra = ttk.Label(self.frame_combos, text="Idioma Columna Maestra:") self.label_codigo_maestra = ttk.Label(
self.frame_combos, text="Idioma Columna Maestra:"
)
self.combo_codigo_maestra = ttk.Combobox(self.frame_combos, state="readonly") self.combo_codigo_maestra = ttk.Combobox(self.frame_combos, state="readonly")
self.combo_codigo_maestra["values"] = [nombre for nombre, _ in fc.IDIOMAS.values()] self.combo_codigo_maestra["values"] = [
self.combo_codigo_maestra.bind('<<ComboboxSelected>>', self.update_codigo_maestra) nombre for nombre, _ in fc.IDIOMAS.values()
self.set_combo_value(self.combo_codigo_maestra, self.codigo_columna_maestra_var.get()) ]
self.combo_codigo_maestra.bind(
"<<ComboboxSelected>>", self.update_codigo_maestra
)
self.set_combo_value(
self.combo_codigo_maestra, self.codigo_columna_maestra_var.get()
)
self.label1 = ttk.Label(self.frame_combos, text="Idioma de Traducción:") self.label1 = ttk.Label(self.frame_combos, text="Idioma de Traducción:")
self.combo = ttk.Combobox(self.frame_combos, state="readonly") self.combo = ttk.Combobox(self.frame_combos, state="readonly")
self.combo["values"] = [nombre for nombre, _ in fc.IDIOMAS.values()] self.combo["values"] = [nombre for nombre, _ in fc.IDIOMAS.values()]
self.combo.bind('<<ComboboxSelected>>', self.update_idioma) self.combo.bind("<<ComboboxSelected>>", self.update_idioma)
self.set_combo_value(self.combo, self.codigo_idioma_var.get()) self.set_combo_value(self.combo, self.codigo_idioma_var.get())
self.label2 = ttk.Label(self.frame_combos, text="Selecciona segundo idioma:") self.label2 = ttk.Label(self.frame_combos, text="Selecciona segundo idioma:")
self.combo2 = ttk.Combobox(self.frame_combos, state="readonly") self.combo2 = ttk.Combobox(self.frame_combos, state="readonly")
self.combo2["values"] = [nombre for nombre, _ in fc.IDIOMAS.values()] self.combo2["values"] = [nombre for nombre, _ in fc.IDIOMAS.values()]
self.combo2.bind('<<ComboboxSelected>>', self.update_idioma2) self.combo2.bind("<<ComboboxSelected>>", self.update_idioma2)
self.set_combo_value(self.combo2, self.codigo_idioma_var2.get()) self.set_combo_value(self.combo2, self.codigo_idioma_var2.get())
self.work_dir_frame = ttk.Frame(self.main_frame) self.work_dir_frame = ttk.Frame(self.main_frame)
self.work_dir_label = ttk.Label(self.work_dir_frame, text="Directorio de trabajo:") self.work_dir_label = ttk.Label(
self.work_dir_entry = ttk.Entry(self.work_dir_frame, textvariable=self.work_dir_var, width=50) self.work_dir_frame, text="Directorio de trabajo:"
self.work_dir_button = ttk.Button(self.work_dir_frame, text="Seleccionar", command=self.select_work_dir) )
self.open_explorer_button = ttk.Button(self.work_dir_frame, text="Abrir Explorador", command=self.open_explorer) self.work_dir_entry = ttk.Entry(
self.work_dir_frame, textvariable=self.work_dir_var, width=50
)
self.work_dir_button = ttk.Button(
self.work_dir_frame, text="Seleccionar", command=self.select_work_dir
)
self.open_explorer_button = ttk.Button(
self.work_dir_frame, text="Abrir Explorador", command=self.open_explorer
)
style = ttk.Style() style = ttk.Style()
style.configure('TButton', padding=(10, 10), font=('Helvetica', 10, 'bold')) style.configure("TButton", padding=(10, 10), font=("Helvetica", 10, "bold"))
style.configure('Excel.TButton', padding=(10, 10), font=('Helvetica', 10), background='#69cf81') style.configure(
"Excel.TButton",
padding=(10, 10),
font=("Helvetica", 10),
background="#69cf81",
)
self.button_width = 60 self.button_width = 60
self.entry_width = 50 self.entry_width = 50
self.paso1 = ttk.Button(self.main_frame, text="1 - Importar al Master", command=lambda: self.ejecutar_run(x1_importar_to_master.run), width=self.button_width) self.paso1 = ttk.Button(
self.master_name_button = ttk.Button(self.main_frame, textvariable=self.master_name_var, command=lambda: self.open_file(self.config.get_master_path()), width=self.entry_width, style='Excel.TButton') self.main_frame,
text="1 - Importar al Master",
command=lambda: self.ejecutar_run(x1_importar_to_master.run),
width=self.button_width,
)
self.master_name_button = ttk.Button(
self.main_frame,
textvariable=self.master_name_var,
command=lambda: self.open_file(self.config.get_master_path()),
width=self.entry_width,
style="Excel.TButton",
)
self.paso2 = ttk.Button(self.main_frame, text="2 - Exportar Idioma a 2_master_export2translate.xlsx", command=lambda: self.ejecutar_run(x2_master_export2translate.run), width=self.button_width) self.paso2 = ttk.Button(
self.translate_name_button = ttk.Button(self.main_frame, textvariable=self.translate_name_var, command=lambda: self.open_file(self.config.get_translate_path()), width=self.entry_width, style='Excel.TButton') self.main_frame,
text="2 - Exportar Idioma a 2_master_export2translate.xlsx",
command=lambda: self.ejecutar_run(x2_master_export2translate.run),
width=self.button_width,
)
self.translate_name_button = ttk.Button(
self.main_frame,
textvariable=self.translate_name_var,
command=lambda: self.open_file(self.config.get_translate_path()),
width=self.entry_width,
style="Excel.TButton",
)
self.paso3 = ttk.Button(self.main_frame, text="3 - Traducir y generar 3_master_export2translate_translated.xlsx", command=lambda: self.ejecutar_run(x3_llm_auto_translate.run), width=self.button_width) self.paso3 = ttk.Button(
self.auto_translate_name_button = ttk.Button(self.main_frame, textvariable=self.auto_translate_name_var, command=lambda: self.open_file(self.config.get_auto_translate_path()), width=self.entry_width, style='Excel.TButton') self.main_frame,
text="3 - Traducir y generar 3_master_export2translate_translated.xlsx",
command=lambda: self.ejecutar_run(x3_llm_auto_translate.run),
width=self.button_width,
)
self.auto_translate_name_button = ttk.Button(
self.main_frame,
textvariable=self.auto_translate_name_var,
command=lambda: self.open_file(self.config.get_auto_translate_path()),
width=self.entry_width,
style="Excel.TButton",
)
self.paso4 = ttk.Button(self.main_frame, text="4 - Integrar las traducciones al 1_hmi_master_translates", command=lambda: self.ejecutar_run(x4_integrate_translates_to_master.run), width=self.button_width) self.paso4 = ttk.Button(
self.main_frame,
text="4 - Integrar las traducciones al 1_hmi_master_translates",
command=lambda: self.ejecutar_run(x4_integrate_translates_to_master.run),
width=self.button_width,
)
self.paso4B = ttk.Button(self.main_frame, text="4B - Integrar traducciones manuales al 1_hmi_master_translates", command=lambda: self.ejecutar_run(x4B_integrate_manual_translates_to_master.run), width=self.button_width) self.paso4B = ttk.Button(
self.main_frame,
text="4B - Integrar traducciones manuales al 1_hmi_master_translates",
command=lambda: self.ejecutar_run(
x4B_integrate_manual_translates_to_master.run
),
width=self.button_width,
)
self.paso5 = ttk.Button(self.main_frame, text="5 - Completar en 1_hmi_master_translates el idioma seleccionado usando el segundo idioma", command=lambda: self.ejecutar_run(x5_complete_empty_cells_master.run), width=self.button_width) self.paso5 = ttk.Button(
self.main_frame,
text="5 - Completar en 1_hmi_master_translates el idioma seleccionado usando el segundo idioma",
command=lambda: self.ejecutar_run(x5_complete_empty_cells_master.run),
width=self.button_width,
)
self.paso6 = ttk.Button(self.main_frame, text="6 - Exportar usando un archivo exportado desde TIA Portal usando 1_hmi_master_translates", command=lambda: self.ejecutar_run(x6_update_from_master.run), width=self.button_width) self.paso6 = ttk.Button(
self.main_frame,
text="6 - Exportar usando un archivo exportado desde TIA Portal usando 1_hmi_master_translates",
command=lambda: self.ejecutar_run(x6_update_from_master.run),
width=self.button_width,
)
self.output_text = tk.Text(self.main_frame, wrap='none', height=20) self.output_text = tk.Text(self.main_frame, wrap="none", height=20)
self.scrollbar_y = ttk.Scrollbar(self.main_frame, orient='vertical', command=self.output_text.yview) self.scrollbar_y = ttk.Scrollbar(
self.scrollbar_x = ttk.Scrollbar(self.main_frame, orient='horizontal', command=self.output_text.xview) self.main_frame, orient="vertical", command=self.output_text.yview
self.output_text.configure(yscrollcommand=self.scrollbar_y.set, xscrollcommand=self.scrollbar_x.set) )
self.scrollbar_x = ttk.Scrollbar(
self.main_frame, orient="horizontal", command=self.output_text.xview
)
self.output_text.configure(
yscrollcommand=self.scrollbar_y.set, xscrollcommand=self.scrollbar_x.set
)
self.clear_button = ttk.Button(self.main_frame, text="Limpiar salida", command=self.clear_output) self.clear_button = ttk.Button(
self.open_log_button = ttk.Button(self.main_frame, text="Abrir Log", command=self.open_log_file) self.main_frame, text="Limpiar salida", command=self.clear_output
)
self.open_log_button = ttk.Button(
self.main_frame, text="Abrir Log", command=self.open_log_file
)
def place_widgets(self): def place_widgets(self):
self.main_frame.grid_columnconfigure(0, weight=1) self.main_frame.grid_columnconfigure(0, weight=1)
self.main_frame.grid_columnconfigure(1, weight=1) self.main_frame.grid_columnconfigure(1, weight=1)
self.frame_combos.grid(row=0, column=0, columnspan=2, pady=10, sticky='ew') self.frame_combos.grid(row=0, column=0, columnspan=2, pady=10, sticky="ew")
self.label_tipo_PLC.grid(row=0, column=0, padx=5, pady=5, sticky='e') self.label_tipo_PLC.grid(row=0, column=0, padx=5, pady=5, sticky="e")
self.combo_tipo_PLC.grid(row=0, column=1, padx=5, pady=5, sticky='w') self.combo_tipo_PLC.grid(row=0, column=1, padx=5, pady=5, sticky="w")
self.label_codigo_maestra.grid(row=1, column=0, padx=5, pady=5, sticky='e') self.label_codigo_maestra.grid(row=1, column=0, padx=5, pady=5, sticky="e")
self.combo_codigo_maestra.grid(row=1, column=1, padx=5, pady=5, sticky='w') self.combo_codigo_maestra.grid(row=1, column=1, padx=5, pady=5, sticky="w")
self.label1.grid(row=0, column=2, padx=5, pady=5, sticky='e') self.label1.grid(row=0, column=2, padx=5, pady=5, sticky="e")
self.combo.grid(row=0, column=3, padx=5, pady=5, sticky='w') self.combo.grid(row=0, column=3, padx=5, pady=5, sticky="w")
self.label2.grid(row=1, column=2, padx=5, pady=5, sticky='e') self.label2.grid(row=1, column=2, padx=5, pady=5, sticky="e")
self.combo2.grid(row=1, column=3, padx=5, pady=5, sticky='w') self.combo2.grid(row=1, column=3, padx=5, pady=5, sticky="w")
self.work_dir_frame.grid(row=1, column=0, columnspan=2, pady=10, sticky='ew') self.work_dir_frame.grid(row=1, column=0, columnspan=2, pady=10, sticky="ew")
self.work_dir_label.grid(row=0, column=0, padx=5, sticky='w') self.work_dir_label.grid(row=0, column=0, padx=5, sticky="w")
self.work_dir_entry.grid(row=0, column=1, padx=5, sticky='ew') self.work_dir_entry.grid(row=0, column=1, padx=5, sticky="ew")
self.work_dir_button.grid(row=0, column=2, padx=5, sticky='e') self.work_dir_button.grid(row=0, column=2, padx=5, sticky="e")
self.open_explorer_button.grid(row=0, column=3, padx=5, sticky='e') self.open_explorer_button.grid(row=0, column=3, padx=5, sticky="e")
self.work_dir_frame.grid_columnconfigure(1, weight=1) self.work_dir_frame.grid_columnconfigure(1, weight=1)
current_row = 2 current_row = 2
self.paso1.grid(row=current_row, column=0, pady=5, padx=5, sticky='ew') self.paso1.grid(row=current_row, column=0, pady=5, padx=5, sticky="ew")
self.master_name_button.grid(row=current_row, column=1, pady=5, padx=5, sticky='ew') self.master_name_button.grid(
row=current_row, column=1, pady=5, padx=5, sticky="ew"
)
current_row += 1 current_row += 1
self.paso2.grid(row=current_row, column=0, pady=5, padx=5, sticky='ew') self.paso2.grid(row=current_row, column=0, pady=5, padx=5, sticky="ew")
self.translate_name_button.grid(row=current_row, column=1, pady=5, padx=5, sticky='ew') self.translate_name_button.grid(
row=current_row, column=1, pady=5, padx=5, sticky="ew"
)
current_row += 1 current_row += 1
self.paso3.grid(row=current_row, column=0, pady=5, padx=5, sticky='ew') self.paso3.grid(row=current_row, column=0, pady=5, padx=5, sticky="ew")
self.auto_translate_name_button.grid(row=current_row, column=1, pady=5, padx=5, sticky='ew') self.auto_translate_name_button.grid(
row=current_row, column=1, pady=5, padx=5, sticky="ew"
)
current_row += 1 current_row += 1
self.paso4.grid(row=current_row, column=0, pady=5, padx=5, sticky='ew') self.paso4.grid(row=current_row, column=0, pady=5, padx=5, sticky="ew")
current_row += 1 current_row += 1
self.paso4B.grid(row=current_row, column=0, pady=5, padx=5, sticky='ew') self.paso4B.grid(row=current_row, column=0, pady=5, padx=5, sticky="ew")
current_row += 1 current_row += 1
self.paso5.grid(row=current_row, column=0, columnspan=2, pady=5, padx=5, sticky='ew') self.paso5.grid(
row=current_row, column=0, columnspan=2, pady=5, padx=5, sticky="ew"
)
current_row += 1 current_row += 1
self.paso6.grid(row=current_row, column=0, columnspan=2, pady=5, padx=5, sticky='ew') self.paso6.grid(
row=current_row, column=0, columnspan=2, pady=5, padx=5, sticky="ew"
)
current_row += 1 current_row += 1
self.output_text.grid(row=current_row, column=0, columnspan=2, padx=(10, 0), pady=10, sticky='nsew') self.output_text.grid(
self.scrollbar_y.grid(row=current_row, column=2, pady=10, sticky='ns') row=current_row,
self.scrollbar_x.grid(row=current_row+1, column=0, columnspan=2, padx=10, sticky='ew') column=0,
columnspan=2,
padx=(10, 0),
pady=10,
sticky="nsew",
)
self.scrollbar_y.grid(row=current_row, column=2, pady=10, sticky="ns")
self.scrollbar_x.grid(
row=current_row + 1, column=0, columnspan=2, padx=10, sticky="ew"
)
current_row += 2 current_row += 2
self.clear_button.grid(row=current_row, column=0, pady=10, padx=5, sticky='e') self.clear_button.grid(row=current_row, column=0, pady=10, padx=5, sticky="e")
self.open_log_button.grid(row=current_row, column=1, pady=10, padx=5, sticky='w') self.open_log_button.grid(
row=current_row, column=1, pady=10, padx=5, sticky="w"
)
self.main_frame.grid_rowconfigure(current_row-2, weight=1) self.main_frame.grid_rowconfigure(current_row - 2, weight=1)
def set_combo_value(self, combo, codigo): def set_combo_value(self, combo, codigo):
for nombre, code in fc.IDIOMAS.values(): for nombre, code in fc.IDIOMAS.values():
@ -222,8 +357,21 @@ class TranslationApp:
dir_path = filedialog.askdirectory() dir_path = filedialog.askdirectory()
if dir_path: if dir_path:
self.work_dir_var.set(dir_path) self.work_dir_var.set(dir_path)
self.config.work_dir = dir_path
self.save_config() self.save_config()
self.update_file_names() self.load_existing_log() # Cargar el log del nuevo directorio
def load_existing_log(self):
log_file = os.path.join(self.config.work_dir, "logs", "app_log.txt")
if os.path.exists(log_file):
with open(log_file, "r", encoding="utf-8") as f:
log_content = f.read()
self.output_text.delete("1.0", tk.END)
self.output_text.insert(tk.END, log_content)
self.output_text.see(tk.END)
else:
self.output_text.delete("1.0", tk.END)
self.output_text.insert(tk.END, "No se encontró un log existente.\n")
def open_file(self, file_path): def open_file(self, file_path):
if os.path.exists(file_path): if os.path.exists(file_path):
@ -235,12 +383,18 @@ class TranslationApp:
if os.path.exists(self.config.work_dir): if os.path.exists(self.config.work_dir):
os.startfile(self.config.work_dir) os.startfile(self.config.work_dir)
else: else:
messagebox.showerror("Error", f"El directorio {self.config.work_dir} no existe.") messagebox.showerror(
"Error", f"El directorio {self.config.work_dir} no existe."
)
def open_log_file(self): def open_log_file(self):
log_file_path = os.path.join(self.config.work_dir, "logs", "translate_log.log") log_file_path = os.path.join(self.config.work_dir, "logs", "translate_log.log")
if os.path.exists(log_file_path): if os.path.exists(log_file_path):
os.startfile(log_file_path) if sys.platform == "win32":
os.startfile(log_file_path)
else:
opener = "open" if sys.platform == "darwin" else "xdg-open"
subprocess.call([opener, log_file_path])
else: else:
messagebox.showerror("Error", f"El archivo de log {log_file_path} no existe.") messagebox.showerror("Error", f"El archivo de log {log_file_path} no existe.")
@ -249,59 +403,95 @@ class TranslationApp:
codigo_idioma_seleccionado = self.codigo_idioma_var.get() codigo_idioma_seleccionado = self.codigo_idioma_var.get()
self.master_name_var.set(f"1_hmi_master_translates_{codigo_tipo_PLC}.xlsx") self.master_name_var.set(f"1_hmi_master_translates_{codigo_tipo_PLC}.xlsx")
self.translate_name_var.set(f'2_master_export2translate_{codigo_tipo_PLC}_{codigo_idioma_seleccionado}.xlsx') self.translate_name_var.set(
self.auto_translate_name_var.set(f"3_master_export2translate_translated_{codigo_tipo_PLC}_{codigo_idioma_seleccionado}.xlsx") f"2_master_export2translate_{codigo_tipo_PLC}_{codigo_idioma_seleccionado}.xlsx"
)
self.auto_translate_name_var.set(
f"3_master_export2translate_translated_{codigo_tipo_PLC}_{codigo_idioma_seleccionado}.xlsx"
)
self.save_config() self.save_config()
def ejecutar_run(self, funcion_run): def ejecutar_run(self, funcion_run):
self.save_config() self.save_config()
original_stdout = sys.stdout # No limpiar el texto anterior
sys.stdout = self.RedirectText(self.output_text) # self.output_text.delete('1.0', tk.END)
sys.stdout = RedirectText(self.queue)
def run_script():
try:
if funcion_run == x1_importar_to_master.run:
archivo_importacion = filedialog.askopenfilename(
title="Seleccione el archivo para Importar",
filetypes=[("Excel files", "*.xls*")],
)
if archivo_importacion:
funcion_run(self.config,archivo_importacion)
elif funcion_run == x2_master_export2translate.run:
funcion_run(self.config)
elif funcion_run == x3_llm_auto_translate.run:
traducir_todo = messagebox.askyesno(
"Traducir todo", "¿Desea traducir todas las celdas?"
)
self.config.traducir_todo = traducir_todo
funcion_run(self.config)
elif funcion_run == x4_integrate_translates_to_master.run:
funcion_run(self.config)
elif funcion_run == x4B_integrate_manual_translates_to_master.run:
funcion_run(self.config)
elif funcion_run == x5_complete_empty_cells_master.run:
funcion_run(self.config)
elif funcion_run == x6_update_from_master.run:
archivo_to_update = filedialog.askopenfilename(
title="Seleccione el archivo a actualizar",
filetypes=[("Excel files", "*.xlsx")],
)
if archivo_to_update:
funcion_run(self.config, archivo_to_update)
except Exception as e:
self.queue.put(f"Error: {str(e)}\n")
finally:
sys.stdout = sys.__stdout__
self.save_config()
self.save_log()
self.queue.put("DONE")
threading.Thread(target=run_script, daemon=True).start()
def update_output(self):
while True:
try:
message = self.queue.get_nowait()
if message == "DONE":
break
self.output_text.insert(tk.END, message)
self.output_text.see(tk.END)
self.output_text.update_idletasks()
except queue.Empty:
break
self.master.after(100, self.update_output)
try:
if funcion_run == x1_importar_to_master.run:
funcion_run(self.config)
elif funcion_run == x2_master_export2translate.run:
funcion_run(self.config)
elif funcion_run == x3_llm_auto_translate.run:
traducir_todo = messagebox.askyesno("Traducir todo", "¿Desea traducir todas las celdas?")
self.config.traducir_todo = traducir_todo
funcion_run(self.config)
elif funcion_run == x4_integrate_translates_to_master.run:
funcion_run(self.config)
elif funcion_run == x4B_integrate_manual_translates_to_master.run:
funcion_run(self.config)
elif funcion_run == x5_complete_empty_cells_master.run:
funcion_run(self.config)
elif funcion_run == x6_update_from_master.run:
archivo_to_update = filedialog.askopenfilename(title="Seleccione el archivo a actualizar", filetypes=[("Excel files", "*.xlsx")])
if archivo_to_update:
funcion_run(self.config, archivo_to_update)
except Exception as e:
messagebox.showerror("Error", str(e))
finally:
sys.stdout = original_stdout
self.save_config()
def clear_output(self): def clear_output(self):
self.output_text.delete('1.0', tk.END) self.output_text.delete("1.0", tk.END)
self.save_log()
class RedirectText: def save_log(self):
def __init__(self, text_widget): log_content = self.output_text.get("1.0", tk.END)
self.text_widget = text_widget log_dir = os.path.join(self.config.work_dir, "logs")
os.makedirs(log_dir, exist_ok=True)
log_file = os.path.join(log_dir, "app_log.txt")
def write(self, string): with open(log_file, "w", encoding="utf-8") as f:
self.text_widget.insert(tk.END, string) f.write(log_content)
self.text_widget.see(tk.END)
def flush(self):
pass
def main(): def main():
root = tk.Tk() root = tk.Tk()
app = TranslationApp(root) app = TranslationApp(root)
root.mainloop() root.mainloop()
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View File

@ -1,6 +1,6 @@
{ {
"codigo_tipo_PLC": "allenbradley", "codigo_tipo_PLC": "allenbradley",
"codigo_columna_maestra": "es-ES", "codigo_columna_maestra": "en-US",
"codigo_idioma_seleccionado": "es-ES", "codigo_idioma_seleccionado": "es-ES",
"codigo_idioma_secundario": "en-US", "codigo_idioma_secundario": "en-US",
"work_dir": "C:/Trabajo/VM/31 - 9.3953 - Omega - Mexico - (ABradley)/Reporte/Languages", "work_dir": "C:/Trabajo/VM/31 - 9.3953 - Omega - Mexico - (ABradley)/Reporte/Languages",

View File

@ -14,6 +14,10 @@ def preprocesar_importacion(
): ):
filas_validas = [] filas_validas = []
progress_bar = fc.ProgressBar(
len(df_importacion), prefix="Preprocesando filas:", suffix="Completado"
)
for index, fila in df_importacion.iterrows(): for index, fila in df_importacion.iterrows():
clave_original = fila[codigo_columna_maestra] clave_original = fila[codigo_columna_maestra]
@ -22,6 +26,7 @@ def preprocesar_importacion(
logger.info( logger.info(
f"Fila {index}: Ignorada - Celda de columna maestra vacía o solo con espacios en blanco" f"Fila {index}: Ignorada - Celda de columna maestra vacía o solo con espacios en blanco"
) )
progress_bar.increment()
continue continue
nueva_fila = fila.copy() nueva_fila = fila.copy()
@ -40,27 +45,28 @@ def preprocesar_importacion(
filas_validas.append(nueva_fila) filas_validas.append(nueva_fila)
logger.info(f"Fila {index}: Aceptada - Clave '{clave_sustituida}' procesada") logger.info(f"Fila {index}: Aceptada - Clave '{clave_sustituida}' procesada")
progress_bar.increment()
progress_bar.finish()
df_valido = pd.DataFrame(filas_validas, columns=df_importacion.columns) df_valido = pd.DataFrame(filas_validas, columns=df_importacion.columns)
return df_valido return df_valido
def importar(config: TranslationConfig): def importar(config: TranslationConfig, archivo_importacion):
logger.info(" .... ") logger.info(" .... ")
logger.info(f"Iniciando importacion en {config.master_name}") logger.info(f"Iniciando importacion en {config.master_name}")
archivo_maestro = os.path.join(config.work_dir, config.master_name) archivo_maestro = os.path.join(config.work_dir, config.master_name)
if not archivo_importacion:
logger.info("No se seleccionó ningún archivo para importar.")
return
if not os.path.exists(archivo_maestro): if not os.path.exists(archivo_maestro):
df_maestro = pd.DataFrame(columns=[config.codigo_columna_maestra]) df_maestro = pd.DataFrame(columns=[config.codigo_columna_maestra])
else: else:
df_maestro = fc.read_dataframe_with_cleanup_retries(archivo_maestro) df_maestro = fc.read_dataframe_with_cleanup_retries(archivo_maestro)
archivo_importacion = fc.select_file("xlsx")
if not archivo_importacion:
logger.info("No se seleccionó ningún archivo para importar.")
return
df_importacion = fc.read_dataframe_with_cleanup_retries(archivo_importacion) df_importacion = fc.read_dataframe_with_cleanup_retries(archivo_importacion)
df_importacion = preprocesar_importacion( df_importacion = preprocesar_importacion(
@ -71,6 +77,10 @@ def importar(config: TranslationConfig):
filas_a_agregar = [] filas_a_agregar = []
progress_bar = fc.ProgressBar(
len(df_importacion), prefix="Procesando filas:", suffix="Completado"
)
for index, fila in df_importacion.iterrows(): for index, fila in df_importacion.iterrows():
clave = str(fila[config.codigo_columna_maestra]) clave = str(fila[config.codigo_columna_maestra])
@ -78,6 +88,7 @@ def importar(config: TranslationConfig):
logger.info( logger.info(
f"Fila {index}: Ignorada - Clave '{clave}' ya existe en el archivo maestro" f"Fila {index}: Ignorada - Clave '{clave}' ya existe en el archivo maestro"
) )
progress_bar.increment()
continue continue
claves_maestro.add(clave) claves_maestro.add(clave)
@ -88,9 +99,13 @@ def importar(config: TranslationConfig):
if col == config.codigo_columna_maestra or fc.es_columna_tipo_xxYY(col) if col == config.codigo_columna_maestra or fc.es_columna_tipo_xxYY(col)
} }
filas_a_agregar.append(fila_filtrada) filas_a_agregar.append(fila_filtrada)
progress_bar.increment()
progress_bar.finish()
if not filas_a_agregar: if not filas_a_agregar:
logger.info("No hay nuevas filas válidas para agregar.") logger.info("No hay nuevas filas válidas para agregar.")
print("No hay nuevas filas válidas para agregar.")
return return
for columna in df_importacion.columns: for columna in df_importacion.columns:
@ -109,16 +124,15 @@ def importar(config: TranslationConfig):
) )
def run(config: TranslationConfig): def run(config: TranslationConfig, archivo_importacion):
global logger global logger
logger = fc.configurar_logger(config.work_dir) logger = fc.configurar_logger(config.work_dir)
importar(config) script_name = os.path.basename(__file__)
print(f"\rIniciando: {script_name}\r")
importar(config, archivo_importacion)
if __name__ == "__main__": if __name__ == "__main__":
print( import menu_pasos_traduccion
"Este script debe ser ejecutado desde el menú principal (menu_pasos_traduccion.py)."
) menu_pasos_traduccion.main()
print(
"Por favor, ejecute menu_pasos_traduccion.py para utilizar esta funcionalidad."
)

View File

@ -1,52 +1,170 @@
import pandas as pd import pandas as pd
import os import os
import sys
import time
import PyLibrary.funciones_comunes as fc import PyLibrary.funciones_comunes as fc
from translation_config import TranslationConfig from translation_config import TranslationConfig
import langid
from openpyxl import load_workbook
from openpyxl.styles import PatternFill, Alignment
# Definir el logger a nivel de módulo # Definir el logger a nivel de módulo
logger = None logger = None
def configurar_detector_idiomas():
codigos_idioma = [code.split("-")[0] for _, code in fc.IDIOMAS.values()]
langid.set_languages(codigos_idioma)
def detectar_idioma(texto, tipo_PLC):
texto_limpio = fc.limpiar_texto(tipo_PLC, texto)
if len(texto_limpio.strip()) < 3: # No detectar idioma en textos muy cortos
return "unknown"
try:
idioma, _ = langid.classify(texto_limpio)
return idioma
except:
return "unknown"
def obtener_nombre_idioma(codigo_corto):
for nombre, codigo in fc.IDIOMAS.values():
if codigo.startswith(codigo_corto):
return nombre
return "Desconocido"
def exportar_para_traduccion(config: TranslationConfig): def exportar_para_traduccion(config: TranslationConfig):
master_path = config.get_master_path() master_path = config.get_master_path()
if not os.path.exists(master_path): if not os.path.exists(master_path):
print("El archivo maestro no existe.") print("El archivo maestro no existe.")
return return
# Leer el archivo maestro configurar_detector_idiomas()
df_maestro = fc.read_dataframe_with_cleanup_retries(master_path) df_maestro = fc.read_dataframe_with_cleanup_retries(master_path)
# Crear un nuevo DataFrame para la exportación
df_export = pd.DataFrame() df_export = pd.DataFrame()
# Copiar la primera columna y la columna del idioma de destino
primera_columna = df_maestro.columns[0] primera_columna = df_maestro.columns[0]
df_export[primera_columna] = df_maestro[primera_columna] df_export[primera_columna] = df_maestro[primera_columna]
df_export[config.codigo_idioma_seleccionado] = df_maestro[ df_export[config.codigo_idioma_seleccionado] = df_maestro[
config.codigo_idioma_seleccionado config.codigo_idioma_seleccionado
] ]
df_export["Idioma_Detectado"] = ""
# Guardar el archivo exportado
ruta_export = config.get_translate_path() ruta_export = config.get_translate_path()
# Usar ExcelWriter para tener más control sobre el proceso de escritura
with pd.ExcelWriter(ruta_export, engine="openpyxl") as writer: with pd.ExcelWriter(ruta_export, engine="openpyxl") as writer:
df_export.to_excel(writer, index=False, sheet_name="Sheet1") df_export.to_excel(writer, index=False, sheet_name="Sheet1")
# Ajustar el ancho de las columnas workbook = writer.book
worksheet = writer.sheets["Sheet1"] worksheet = writer.sheets["Sheet1"]
for idx, col in enumerate(df_export.columns):
max_length = max(df_export[col].astype(str).map(len).max(), len(col))
if max_length > 50:
max_length = 50
worksheet.column_dimensions[chr(65 + idx)].width = max_length + 2
print(f"Archivo exportado para traducción: {ruta_export}") wrap_alignment = Alignment(wrap_text=True, vertical="top")
for col in ["A", "B"]:
for cell in worksheet[col]:
cell.alignment = wrap_alignment
worksheet.column_dimensions[col].width = 50
idioma_esperado = fc.idiomas_shortcodefromcode(
config.codigo_idioma_seleccionado
)
fill = PatternFill(start_color="ADD8E6", end_color="ADD8E6", fill_type="solid")
total_rows = worksheet.max_row - 1 # Excluimos la fila de encabezado
progress_bar = fc.ProgressBar(
total_rows, prefix="Procesando filas:", suffix="Completado"
)
for row in range(2, worksheet.max_row + 1):
texto = worksheet.cell(row=row, column=2).value
if texto:
idioma_detectado = detectar_idioma(texto, config.codigo_tipo_PLC)
if (
idioma_detectado != "unknown"
and idioma_detectado != idioma_esperado
):
worksheet.cell(row=row, column=2).fill = fill
nombre_idioma = obtener_nombre_idioma(idioma_detectado)
worksheet.cell(row=row, column=3).value = nombre_idioma
progress_bar.update(row - 1)
progress_bar.finish()
print(f"\nArchivo exportado para traducción: {ruta_export}")
print("Las celdas con idioma incorrecto han sido marcadas en azul.")
print(
"Se ha añadido el nombre del idioma detectado cuando es diferente del esperado."
)
master_path = config.get_master_path()
if not os.path.exists(master_path):
print("El archivo maestro no existe.")
return
configurar_detector_idiomas()
df_maestro = fc.read_dataframe_with_cleanup_retries(master_path)
df_export = pd.DataFrame()
primera_columna = df_maestro.columns[0]
df_export[primera_columna] = df_maestro[primera_columna]
df_export[config.codigo_idioma_seleccionado] = df_maestro[
config.codigo_idioma_seleccionado
]
df_export["Idioma_Detectado"] = ""
ruta_export = config.get_translate_path()
with pd.ExcelWriter(ruta_export, engine="openpyxl") as writer:
df_export.to_excel(writer, index=False, sheet_name="Sheet1")
workbook = writer.book
worksheet = writer.sheets["Sheet1"]
wrap_alignment = Alignment(wrap_text=True, vertical="top")
for col in ["A", "B"]:
for cell in worksheet[col]:
cell.alignment = wrap_alignment
worksheet.column_dimensions[col].width = 50
idioma_esperado = fc.idiomas_shortcodefromcode(
config.codigo_idioma_seleccionado
)
fill = PatternFill(start_color="ADD8E6", end_color="ADD8E6", fill_type="solid")
total_rows = worksheet.max_row - 1 # Excluimos la fila de encabezado
progress_bar = fc.ProgressBar(
total_rows, prefix="Progreso:", suffix="Completado"
)
for row in range(2, worksheet.max_row + 1):
texto = worksheet.cell(row=row, column=2).value
if texto:
idioma_detectado = detectar_idioma(texto, config.codigo_tipo_PLC)
if (
idioma_detectado != "unknown"
and idioma_detectado != idioma_esperado
):
worksheet.cell(row=row, column=2).fill = fill
nombre_idioma = obtener_nombre_idioma(idioma_detectado)
worksheet.cell(row=row, column=3).value = nombre_idioma
progress_bar.increment()
progress_bar.finish()
print(f"\nArchivo exportado para traducción: {ruta_export}")
print("Las celdas con idioma incorrecto han sido marcadas en azul.")
print(
"Se ha añadido el nombre del idioma detectado cuando es diferente del esperado."
)
def run(config: TranslationConfig): def run(config: TranslationConfig):
global logger global logger
logger = fc.configurar_logger(config.work_dir) logger = fc.configurar_logger(config.work_dir)
script_name = os.path.basename(__file__)
print(f"\rIniciando: {script_name}\r")
exportar_para_traduccion(config) exportar_para_traduccion(config)

View File

@ -1,7 +1,6 @@
import pandas as pd import pandas as pd
from openai import OpenAI from openai import OpenAI
import os import os
import re
from openai_api_key import openai_api_key from openai_api_key import openai_api_key
from google_api_key import google_api_key from google_api_key import google_api_key
import ollama import ollama
@ -106,13 +105,13 @@ def translate_batch_openai(texts_dict, source_lang, target_lang):
return dict(zip(texts_dict.keys(), translations)) return dict(zip(texts_dict.keys(), translations))
def affinity_batch_openai(tipo_PLC, texts_dict): def affinity_batch_openai(codigo_tipo_PLC, texts_dict):
system_prompt = ( system_prompt = (
"Evaluate the semantic similarity between the following table of pairs of texts in json format on a scale from 0 to 1. " "Evaluate the semantic similarity between the following table of pairs of texts in json format on a scale from 0 to 1. "
"Return the similarity scores for every row in JSON format as a list of numbers, without any additional text or formatting." "Return the similarity scores for every row in JSON format as a list of numbers, without any additional text or formatting."
) )
original_list = [ original_list = [
fc.compactar_celda_traducida(tipo_PLC, key) for key in texts_dict.keys() fc.compactar_celda_traducida(codigo_tipo_PLC, key) for key in texts_dict.keys()
] ]
re_translated_list = list(texts_dict.values()) re_translated_list = list(texts_dict.values())
@ -204,6 +203,7 @@ def calcular_afinidad(tipo_PLC, texto1, texto2):
def main(config: TranslationConfig): def main(config: TranslationConfig):
global logger
df = fc.read_dataframe_with_cleanup_retries(config.get_translate_path()) df = fc.read_dataframe_with_cleanup_retries(config.get_translate_path())
source_col = config.codigo_columna_maestra source_col = config.codigo_columna_maestra
@ -219,146 +219,122 @@ def main(config: TranslationConfig):
texts_to_translate = {} texts_to_translate = {}
# Inicializar ProgressBar para la fase de preparación
prep_progress = fc.ProgressBar(len(df), prefix='Preparando textos:', suffix='Completado')
for index, row in df.iterrows(): for index, row in df.iterrows():
celda_clave = str(row[source_col]) celda_clave = str(row[source_col])
source_translated_text = ( source_translated_text = str(row[source_translated_col]) if source_translated_col in df.columns else ""
str(row[source_translated_col]) celda_clave_compactada = fc.compactar_celda_traducida(config.codigo_tipo_PLC, celda_clave)
if source_translated_col in df.columns
else ""
)
celda_clave_compactada = fc.compactar_celda_traducida(
config.codigo_tipo_PLC, celda_clave
)
if config.traducir_todo: if config.traducir_todo:
if fc.texto_requiere_traduccion( if fc.texto_requiere_traduccion(config.codigo_tipo_PLC, celda_clave_compactada, logger):
config.codigo_tipo_PLC, celda_clave_compactada, logger
):
df.at[index, source_translated_col] = "" df.at[index, source_translated_col] = ""
texts_to_translate[celda_clave] = celda_clave_compactada texts_to_translate[celda_clave] = celda_clave_compactada
else: else:
if ( if pd.isna(row[source_translated_col]) or source_translated_text.strip() == "":
pd.isna(row[source_translated_col]) if fc.texto_requiere_traduccion(config.codigo_tipo_PLC, celda_clave_compactada, logger) or fc.texto_con_campos_especiales(config.codigo_tipo_PLC, celda_clave_compactada):
or source_translated_text.strip() == ""
):
if fc.texto_requiere_traduccion(
config.codigo_tipo_PLC, celda_clave_compactada, logger
) or fc.texto_con_campos_especiales(
config.codigo_tipo_PLC, celda_clave_compactada
):
texts_to_translate[celda_clave] = celda_clave_compactada texts_to_translate[celda_clave] = celda_clave_compactada
num_texts = len(texts_to_translate) prep_progress.update(index + 1)
# num_texts = 40
prep_progress.finish()
num_texts = len(texts_to_translate)
logger.info(f"Número total de textos a traducir: {num_texts}") logger.info(f"Número total de textos a traducir: {num_texts}")
print(f"Número total de textos a traducir: {num_texts}") print(f"\nNúmero total de textos a traducir: {num_texts}")
# Inicializar ProgressBar para la fase de traducción
trans_progress = fc.ProgressBar(num_texts, prefix='Traduciendo:', suffix='Completado')
# Traducciones # Traducciones
# Hacer las traducciones via LLM en batch
translations = {} translations = {}
for start_idx in range(0, num_texts, batch_size): for start_idx in range(0, num_texts, batch_size):
end_idx = min(start_idx + batch_size, num_texts) end_idx = min(start_idx + batch_size, num_texts)
batch_texts = dict(list(texts_to_translate.items())[start_idx:end_idx]) batch_texts = dict(list(texts_to_translate.items())[start_idx:end_idx])
logger.info(f"Traduciendo: celdas desde {start_idx} a {end_idx}.") logger.info(f"Traduciendo: celdas desde {start_idx} a {end_idx}.")
print(f"Traduciendo : celdas desde: {start_idx} a :{end_idx}.")
retries = 4 # Número de intentos totales (1 inicial + 1 reintento) retries = 4
for attempt in range(retries): for attempt in range(retries):
try: try:
batch_translations = translate_batch_openai( batch_translations = translate_batch_openai(
batch_texts, batch_texts,
fc.idiomas_idiomafromcode(codigo_columna_maestra), fc.idiomas_idiomafromcode(config.codigo_columna_maestra),
target_lang, fc.idiomas_idiomafromcode(config.codigo_idioma_seleccionado)
) )
translations.update(batch_translations) translations.update(batch_translations)
break # Si la traducción es exitosa, salimos del bucle de reintentos break
except Exception as e: except Exception as e:
if attempt < retries - 1: # Si no es el último intento if attempt < retries - 1:
logger.warning( logger.warning(f"Error en el intento {attempt + 1} de traducción de celdas desde {start_idx} a {end_idx}: {e}. Reintentando...")
f"Error en el intento {attempt + 1} de traducción de celdas desde {start_idx} a {end_idx}: {e}. Reintentando..." print(f"Error en el intento {attempt + 1} de traducción de celdas desde {start_idx} a {end_idx}: {e}. Reintentando...")
)
print(
f"Error en el intento {attempt + 1} de traducción de celdas desde {start_idx} a {end_idx}: {e}. Reintentando..."
)
time.sleep(3) time.sleep(3)
else: # Si es el último intento else:
logger.error( logger.error(f"Error en todos los intentos de traducción de celdas desde {start_idx} a {end_idx}: {e}")
f"Error en todos los intentos de traducción de celdas desde {start_idx} a {end_idx}: {e}" print(f"Error en todos los intentos de traducción de celdas desde {start_idx} a {end_idx}: {e}")
)
print(
f"Error en todos los intentos de traducción de celdas desde {start_idx} a {end_idx}: {e}"
)
trans_progress.update(end_idx)
trans_progress.finish()
logger.info(f"Número total de traducciones recibidas: {len(translations)}") logger.info(f"Número total de traducciones recibidas: {len(translations)}")
# Traduccion inversa # Inicializar ProgressBar para la fase de actualización del DataFrame
update_progress = fc.ProgressBar(len(df), prefix='Actualizando DataFrame:', suffix='Completado')
# Actualizar el DataFrame con las traducciones y hacemos la Traduccion inversa # Actualizar el DataFrame con las traducciones y hacemos la Traduccion inversa
for index, row in tqdm( for index, row in df.iterrows():
df.iterrows(), total=df.shape[0], desc="Procesando traducciones"
):
celda_clave = str(row[source_col]) celda_clave = str(row[source_col])
if celda_clave in translations: if celda_clave in translations:
df.at[index, target_col] = translations[celda_clave] df.at[index, target_col] = translations[celda_clave]
# Realizar la traducción de verificación con Google Translate
try: try:
google_translation = google_translate( google_translation = google_translate(
translations[celda_clave], translations[celda_clave],
fc.idiomas_shortcodefromcode(codigo_columna_maestra), fc.idiomas_shortcodefromcode(config.codigo_columna_maestra)
) )
df.at[index, check_translate_col] = google_translation df.at[index, check_translate_col] = google_translation
except Exception as e: except Exception as e:
logger.error( logger.error(f"Error en la traducción de Google para el texto '{celda_clave}': {e}")
f"Error en la traducción de Google para el texto '{celda_clave}': {e}"
)
df.at[index, check_translate_col] = "Error en la traducción" df.at[index, check_translate_col] = "Error en la traducción"
df.at[index, affinity_col] = 0.0 df.at[index, affinity_col] = 0.0
update_progress.increment()
update_progress.finish()
# Inicializar ProgressBar para la fase de cálculo de afinidad
affinity_progress = fc.ProgressBar(num_texts, prefix='Calculando afinidad:', suffix='Completado')
# Afinidades # Afinidades
# Se calculan las Afinidades
affinities = {} affinities = {}
for start_idx in range(0, num_texts, batch_size): # num_text for start_idx in range(0, num_texts, batch_size):
end_idx = min(start_idx + batch_size, num_texts) end_idx = min(start_idx + batch_size, num_texts)
batch_texts = dict(list(texts_to_translate.items())[start_idx:end_idx]) batch_texts = dict(list(texts_to_translate.items())[start_idx:end_idx])
logger.info(f"Afinidad: celdas desde {start_idx} a {end_idx}.") logger.info(f"Afinidad: celdas desde {start_idx} a {end_idx}.")
print(f"Afinidad: celdas desde: {start_idx} a :{end_idx}.")
retries = 2 # Número de intentos totales (1 inicial + 1 reintento) retries = 2
for attempt in range(retries): for attempt in range(retries):
try: try:
batch_affinities = affinity_batch_openai(tipo_PLC, batch_texts) batch_affinities = affinity_batch_openai(config.codigo_tipo_PLC, batch_texts)
affinities.update(batch_affinities) affinities.update(batch_affinities)
break # Si la llamada es exitosa, salimos del bucle de reintentos break
except Exception as e: except Exception as e:
if attempt < retries - 1: # Si no es el último intento if attempt < retries - 1:
logger.warning( logger.warning(f"Error en el intento {attempt + 1} de Afinidad de celdas desde {start_idx} a {end_idx}: {e}. Reintentando...")
f"Error en el intento {attempt + 1} de Afinidad de celdas desde {start_idx} a {end_idx}: {e}. Reintentando..." print(f"Error en el intento {attempt + 1} de Afinidad de celdas desde {start_idx} a {end_idx}: {e}. Reintentando...")
)
print(
f"Error en el intento {attempt + 1} de Afinidad de celdas desde {start_idx} a {end_idx}: {e}. Reintentando..."
)
time.sleep(3) time.sleep(3)
else: # Si es el último intento else:
logger.error( logger.error(f"Error en todos los intentos de Afinidad de celdas desde {start_idx} a {end_idx}: {e}")
f"Error en todos los intentos de Afinidad de celdas desde {start_idx} a {end_idx}: {e}" print(f"Error en todos los intentos de Afinidad de celdas desde {start_idx} a {end_idx}: {e}")
)
print(
f"Error en todos los intentos de Afinidad de celdas desde {start_idx} a {end_idx}: {e}"
)
# Intentar individualmente si falla en batch
for key, value in batch_texts.items(): for key, value in batch_texts.items():
try: try:
score = calcular_afinidad(tipo_PLC, key, value) score = calcular_afinidad(config.codigo_tipo_PLC, key, value)
affinities[key] = score affinities[key] = score
except Exception as ind_e: except Exception as ind_e:
affinities[key] = "0" affinities[key] = "0"
logger.error( logger.error(f"Error en el cálculo individual de Afinidad para el texto '{key}': {ind_e}")
f"Error en el cálculo individual de Afinidad para el texto '{key}': {ind_e}" print(f"Error en el cálculo individual de Afinidad para el texto '{key}': {ind_e}")
)
print(
f"Error en el cálculo individual de Afinidad para el texto '{key}': {ind_e}"
)
affinity_progress.increment()
affinity_progress.finish()
# Actualizar el DataFrame con las Afinidades # Actualizar el DataFrame con las Afinidades
for index, row in df.iterrows(): for index, row in df.iterrows():
celda_clave = str(row[source_col]) celda_clave = str(row[source_col])
@ -368,16 +344,15 @@ def main(config: TranslationConfig):
output_path = config.get_auto_translate_path() output_path = config.get_auto_translate_path()
fc.save_dataframe_with_retries(df, output_path=output_path) fc.save_dataframe_with_retries(df, output_path=output_path)
logger.info(f"Archivo traducido guardado en: {output_path}") logger.info(f"Archivo traducido guardado en: {output_path}")
print(f"Archivo traducido guardado en: {output_path}") print(f"\nArchivo traducido guardado en: {output_path}")
def run(config: TranslationConfig): def run(config: TranslationConfig):
global logger global logger
logger = fc.configurar_logger(config.work_dir) logger = fc.configurar_logger(config.work_dir)
script_name = os.path.basename(__file__)
print(f"\rIniciando: {script_name}\r")
main(config) main(config)
if __name__ == "__main__": if __name__ == "__main__":
import menu_pasos_traduccion import menu_pasos_traduccion
menu_pasos_traduccion.main() menu_pasos_traduccion.main()

View File

@ -113,6 +113,8 @@ def marcar_errores_archivo_fuente(archivo, celdas_con_errores):
def run(config: TranslationConfig): def run(config: TranslationConfig):
global logger global logger
logger = fc.configurar_logger(config.work_dir) logger = fc.configurar_logger(config.work_dir)
script_name = os.path.basename(__file__)
print(f"\rIniciando: {script_name}\r")
importar_traduccion_manual(config) importar_traduccion_manual(config)

View File

@ -55,6 +55,10 @@ def importar_traduccion(config: TranslationConfig):
) )
columna_excel = df_maestro.columns.get_loc(master_col) + 1 columna_excel = df_maestro.columns.get_loc(master_col) + 1
celdas_modificadas[(fila_excel, columna_excel)] = valor_traducido celdas_modificadas[(fila_excel, columna_excel)] = valor_traducido
else :
logger.error(
f'Clave {clave} no encontrada en master.'
)
fc.save_dataframe_with_retries(df_maestro, output_path=archivo_maestro) fc.save_dataframe_with_retries(df_maestro, output_path=archivo_maestro)
@ -81,6 +85,8 @@ def aplicar_negrita_celdas_modificadas(archivo, celdas_modificadas):
def run(config: TranslationConfig): def run(config: TranslationConfig):
global logger global logger
logger = fc.configurar_logger(config.work_dir) logger = fc.configurar_logger(config.work_dir)
script_name = os.path.basename(__file__)
print(f"\rIniciando: {script_name}\r")
importar_traduccion(config) importar_traduccion(config)

View File

@ -45,6 +45,8 @@ def complete_emptys(config: TranslationConfig):
def run(config: TranslationConfig): def run(config: TranslationConfig):
global logger global logger
logger = fc.configurar_logger(config.work_dir) logger = fc.configurar_logger(config.work_dir)
script_name = os.path.basename(__file__)
print(f"\rIniciando: {script_name}\r")
complete_emptys(config) complete_emptys(config)

View File

@ -110,11 +110,12 @@ def marcar_celdas_con_errores(archivo_maestro, celdas_con_errores, target_lang_c
) )
def run(config: TranslationConfig): def run(config: TranslationConfig, archivo_to_update):
global logger global logger
logger = fc.configurar_logger(config.work_dir) logger = fc.configurar_logger(config.work_dir)
archivo_to_update = fc.select_file("xlsx")
if archivo_to_update: if archivo_to_update:
script_name = os.path.basename(__file__)
print(f"\rIniciando: {script_name}\r")
update_from_master(config, archivo_to_update) update_from_master(config, archivo_to_update)