feat: Add post-processing functionality for CSV files with intelligent agent integration, including a new GUI tab for user instructions and code generation, and update costs for the new model. Enhance .gitignore to exclude database files.
This commit is contained in:
@@ -4,5 +4,6 @@ from gui.tabs.results_tab import ResultsTab
|
||||
from gui.tabs.main_tab import MainProcessingTab
|
||||
from gui.tabs.preclass_tab import PreclassificationTab
|
||||
from gui.tabs.metrics_tab import MetricsTab
|
||||
from gui.tabs.postprocess_tab import PostProcessTab
|
||||
|
||||
__all__ = ["SettingsTab", "ResultsTab", "MainProcessingTab", "PreclassificationTab", "MetricsTab"]
|
||||
__all__ = ["SettingsTab", "ResultsTab", "MainProcessingTab", "PreclassificationTab", "MetricsTab", "PostProcessTab"]
|
||||
|
||||
+336
-253
@@ -1,12 +1,15 @@
|
||||
"""Pestaña Dashboard & Métricas de la aplicación GUI."""
|
||||
"""Pestaña Dashboard & Métricas de la aplicación GUI.
|
||||
|
||||
Muestra KPIs, gráficos y detalle de costos para Extracción, Normalización
|
||||
y Post-Procesamiento.
|
||||
"""
|
||||
import customtkinter as ctk
|
||||
import tkinter.ttk as ttk
|
||||
from gui.styles import AppStyles
|
||||
|
||||
# matplotlib y numpy son opcionales
|
||||
try:
|
||||
import matplotlib
|
||||
matplotlib.use("Agg") # backend no interactivo para render en canvas Tk
|
||||
matplotlib.use("Agg")
|
||||
import numpy as np
|
||||
from matplotlib.figure import Figure
|
||||
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
|
||||
@@ -16,253 +19,333 @@ except Exception:
|
||||
|
||||
|
||||
class MetricsTab:
|
||||
"""Encapsula la UI de la pestaña Dashboard & Métricas."""
|
||||
|
||||
def __init__(self, parent_tab):
|
||||
"""Inicializa la pestaña de Métricas.
|
||||
|
||||
Args:
|
||||
parent_tab: El CTkFrame que representa la pestaña.
|
||||
"""
|
||||
self.tab = parent_tab
|
||||
self.fonts = AppStyles.get_fonts()
|
||||
self.colors = AppStyles.get_colors()
|
||||
self.spacing = AppStyles.get_spacing()
|
||||
|
||||
# Variables internas para gráficos
|
||||
self._cost_line_points_x = []
|
||||
self._cost_line_points_y = []
|
||||
|
||||
self._setup_layout()
|
||||
self._create_kpi_section()
|
||||
self._create_charts_section()
|
||||
self._create_comparison_section()
|
||||
self._create_usage_section()
|
||||
|
||||
def _setup_layout(self):
|
||||
"""Configura el layout base de la pestaña."""
|
||||
self.tab.grid_columnconfigure(0, weight=1)
|
||||
self.tab.grid_rowconfigure(1, weight=1)
|
||||
self.tab.grid_rowconfigure(3, weight=1)
|
||||
|
||||
def _create_kpi_section(self):
|
||||
"""Crea la sección de KPIs."""
|
||||
self.kpi_frame = ctk.CTkFrame(self.tab)
|
||||
self.kpi_frame.grid(row=0, column=0, sticky="ew", padx=15, pady=15)
|
||||
self.kpi_frame.grid_columnconfigure((0, 1, 2), weight=1)
|
||||
|
||||
# Helper para crear cards KPI
|
||||
def create_kpi_card(parent, title, row, col):
|
||||
card = ctk.CTkFrame(parent)
|
||||
card.grid(row=row, column=col, sticky="nsew", padx=5, pady=5)
|
||||
lbl_title = ctk.CTkLabel(
|
||||
card,
|
||||
text=title,
|
||||
font=ctk.CTkFont(size=14, weight="bold")
|
||||
)
|
||||
lbl_title.pack(pady=(10, 5), padx=10, anchor="w")
|
||||
lbl_content = ctk.CTkLabel(
|
||||
card,
|
||||
text="...",
|
||||
font=self.fonts["mono"],
|
||||
justify="left"
|
||||
)
|
||||
lbl_content.pack(pady=(0, 10), padx=10, anchor="w")
|
||||
return lbl_content
|
||||
|
||||
self.kpi_overall = create_kpi_card(self.kpi_frame, "Total General", 0, 0)
|
||||
self.kpi_extract = create_kpi_card(self.kpi_frame, "Fase: Extracción", 0, 1)
|
||||
self.kpi_normalize = create_kpi_card(self.kpi_frame, "Fase: Normalización", 0, 2)
|
||||
|
||||
self.kpi_overall.configure(text="input: 0\ncached: 0\noutput: 0\nUSD: $0.00")
|
||||
self.kpi_extract.configure(text="input: 0\ncached: 0\noutput: 0\nUSD: $0.00")
|
||||
self.kpi_normalize.configure(text="input: 0\ncached: 0\noutput: 0\nUSD: $0.00")
|
||||
|
||||
def _create_charts_section(self):
|
||||
"""Crea la sección de gráficos."""
|
||||
self.charts_frame = ctk.CTkFrame(self.tab)
|
||||
self.charts_frame.grid(row=1, column=0, sticky="nsew", padx=15, pady=5)
|
||||
self.charts_frame.grid_columnconfigure((0, 1), weight=1)
|
||||
self.charts_frame.grid_rowconfigure(0, weight=1)
|
||||
|
||||
# Gráfico Línea
|
||||
if HAS_MPL:
|
||||
self.fig_line = Figure(figsize=(4, 3), dpi=100)
|
||||
self.fig_line.patch.set_facecolor('#f0f0f0')
|
||||
self.ax_line = self.fig_line.add_subplot(111)
|
||||
self.ax_line.set_title("Costo Acumulado (USD)", fontsize=10)
|
||||
self.ax_line.tick_params(labelsize=8)
|
||||
self._line_plot, = self.ax_line.plot([], [], marker="o", color="#1f77b4")
|
||||
|
||||
self.canvas_line = FigureCanvasTkAgg(self.fig_line, master=self.charts_frame)
|
||||
self.canvas_line.get_tk_widget().grid(row=0, column=0, padx=10, pady=10, sticky="nsew")
|
||||
else:
|
||||
ctk.CTkLabel(self.charts_frame, text="Gráficos no disponibles").grid(row=0, column=0)
|
||||
|
||||
# Gráfico Barras
|
||||
if HAS_MPL:
|
||||
self.fig_bars = Figure(figsize=(4, 3), dpi=100)
|
||||
self.fig_bars.patch.set_facecolor('#f0f0f0')
|
||||
self.ax_bars = self.fig_bars.add_subplot(111)
|
||||
self.ax_bars.set_title("Consumo de Tokens", fontsize=10)
|
||||
self.ax_bars.tick_params(labelsize=8)
|
||||
|
||||
self.canvas_bars = FigureCanvasTkAgg(self.fig_bars, master=self.charts_frame)
|
||||
self.canvas_bars.get_tk_widget().grid(row=0, column=1, padx=10, pady=10, sticky="nsew")
|
||||
|
||||
def _create_comparison_section(self):
|
||||
"""Crea la sección de comparación de tarifas."""
|
||||
self.comparison_frame = ctk.CTkFrame(self.tab)
|
||||
self.comparison_frame.grid(row=2, column=0, sticky="ew", padx=15, pady=5)
|
||||
self.comparison_frame.grid_columnconfigure(0, weight=1)
|
||||
|
||||
lbl_comp_title = ctk.CTkLabel(
|
||||
self.comparison_frame,
|
||||
text="Tarifas de Modelos (USD por 1M tokens)",
|
||||
font=ctk.CTkFont(size=14, weight="bold")
|
||||
)
|
||||
lbl_comp_title.grid(row=0, column=0, sticky="w", padx=10, pady=5)
|
||||
|
||||
# Tabla de comparación
|
||||
self.tree_comparison = ttk.Treeview(
|
||||
self.comparison_frame,
|
||||
columns=("model", "input", "cached", "output"),
|
||||
show="headings",
|
||||
height=4
|
||||
)
|
||||
self.tree_comparison.heading("model", text="Modelo")
|
||||
self.tree_comparison.heading("input", text="Input / 1M")
|
||||
self.tree_comparison.heading("cached", text="Cached / 1M")
|
||||
self.tree_comparison.heading("output", text="Output / 1M")
|
||||
|
||||
self.tree_comparison.column("model", width=150, anchor="w")
|
||||
self.tree_comparison.column("input", width=100, anchor="e")
|
||||
self.tree_comparison.column("cached", width=100, anchor="e")
|
||||
self.tree_comparison.column("output", width=100, anchor="e")
|
||||
|
||||
self.tree_comparison.grid(row=1, column=0, sticky="ew", padx=10, pady=(0, 10))
|
||||
|
||||
def _create_usage_section(self):
|
||||
"""Crea la sección de detalle de uso."""
|
||||
self.usage_text = ctk.CTkTextbox(self.tab, height=150, font=self.fonts["mono"])
|
||||
self.usage_text.grid(row=3, column=0, sticky="ew", padx=15, pady=15)
|
||||
|
||||
# --- API Pública ---
|
||||
|
||||
def append_usage(self, text: str) -> None:
|
||||
"""Añade texto al área de uso."""
|
||||
self.usage_text.insert("end", text + "\n")
|
||||
self.usage_text.see("end")
|
||||
|
||||
def append_usage_safe(self, text: str) -> None:
|
||||
"""Añade texto al área de uso de forma thread-safe."""
|
||||
self.append_usage(text)
|
||||
|
||||
def clear(self) -> None:
|
||||
"""Limpia todos los datos de la pestaña."""
|
||||
self.usage_text.delete("1.0", "end")
|
||||
try:
|
||||
for item in self.tree_comparison.get_children():
|
||||
self.tree_comparison.delete(item)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Reset KPIs
|
||||
zero_txt = "input: 0\ncached: 0\noutput: 0\nUSD: $0.0000"
|
||||
self.kpi_overall.configure(text=zero_txt)
|
||||
self.kpi_extract.configure(text=zero_txt)
|
||||
self.kpi_normalize.configure(text=zero_txt)
|
||||
|
||||
# Reset Charts
|
||||
if HAS_MPL:
|
||||
self._cost_line_points_x.clear()
|
||||
self._cost_line_points_y.clear()
|
||||
self._line_plot.set_data([], [])
|
||||
self.ax_line.relim()
|
||||
self.ax_line.autoscale_view()
|
||||
self.canvas_line.draw_idle()
|
||||
|
||||
self.ax_bars.clear()
|
||||
self.ax_bars.set_title("Consumo de Tokens")
|
||||
self.canvas_bars.draw_idle()
|
||||
|
||||
def set_usage_totals(self, **kwargs):
|
||||
"""Legacy method, handled by KPIs now."""
|
||||
pass
|
||||
|
||||
def update_kpis(self, overall: dict, extract_phase: dict, normalize_phase: dict) -> None:
|
||||
"""Actualiza los KPIs con los datos proporcionados."""
|
||||
def fmt(d):
|
||||
return (
|
||||
f"input: {d.get('input',0)}\n"
|
||||
f"cached: {d.get('cached',0)}\n"
|
||||
f"output: {d.get('output',0)}\n"
|
||||
f"USD: ${d.get('usd',0.0):.4f}"
|
||||
)
|
||||
self.kpi_overall.configure(text=fmt(overall))
|
||||
self.kpi_extract.configure(text=fmt(extract_phase))
|
||||
self.kpi_normalize.configure(text=fmt(normalize_phase))
|
||||
|
||||
def update_cost_line(self, doc_index: int, total_cost_usd: float) -> None:
|
||||
"""Actualiza el gráfico de línea de costo acumulado."""
|
||||
if not HAS_MPL:
|
||||
return
|
||||
try:
|
||||
self._cost_line_points_x.append(doc_index)
|
||||
self._cost_line_points_y.append(total_cost_usd)
|
||||
self._line_plot.set_data(self._cost_line_points_x, self._cost_line_points_y)
|
||||
self.ax_line.relim()
|
||||
self.ax_line.autoscale_view()
|
||||
self.canvas_line.draw_idle()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def update_token_bars(self, input_extract: int, cached_extract: int, output_extract: int,
|
||||
input_norm: int, cached_norm: int, output_norm: int) -> None:
|
||||
"""Actualiza el gráfico de barras de tokens."""
|
||||
if not HAS_MPL:
|
||||
return
|
||||
try:
|
||||
self.ax_bars.clear()
|
||||
self.ax_bars.set_title("Tokens por Fase")
|
||||
labels = ["Input", "Cached", "Output"]
|
||||
extract_vals = [input_extract, cached_extract, output_extract]
|
||||
norm_vals = [input_norm, cached_norm, output_norm]
|
||||
|
||||
x = np.arange(len(labels))
|
||||
width = 0.35
|
||||
|
||||
self.ax_bars.bar(x - width/2, extract_vals, width, label="Extracción", color="#1f77b4")
|
||||
self.ax_bars.bar(x + width/2, norm_vals, width, label="Normalización", color="#ff7f0e")
|
||||
|
||||
self.ax_bars.set_xticks(x, labels)
|
||||
self.ax_bars.legend()
|
||||
self.canvas_bars.draw_idle()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def update_comparison_table(self, comparisons: list[dict]) -> None:
|
||||
"""Actualiza la tabla de comparación de tarifas."""
|
||||
try:
|
||||
# Limpiar tabla
|
||||
for item in self.tree_comparison.get_children():
|
||||
self.tree_comparison.delete(item)
|
||||
|
||||
# Insertar filas
|
||||
for c in comparisons:
|
||||
self.tree_comparison.insert("", "end", values=(
|
||||
c.get("model", "N/D"),
|
||||
f"${c.get('input', 0.0):.2f}",
|
||||
f"${c.get('cached_input', 0.0):.2f}",
|
||||
f"${c.get('output', 0.0):.2f}"
|
||||
))
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def append_usage_row(self, phase: str, radicado: str, usage: dict | None, cost: dict | None) -> None:
|
||||
"""Añade una fila de detalle de uso."""
|
||||
try:
|
||||
usd = (cost or {}).get('total_usd', 0.0)
|
||||
self.append_usage(f"[{phase:<10}] {radicado:<20} | cost=${usd:.4f} | usage={usage}")
|
||||
except Exception:
|
||||
pass
|
||||
"""Encapsula la UI de la pestaña Dashboard & Métricas."""
|
||||
|
||||
def __init__(self, parent_tab):
|
||||
self.tab = parent_tab
|
||||
self.fonts = AppStyles.get_fonts()
|
||||
self.colors = AppStyles.get_colors()
|
||||
self.spacing = AppStyles.get_spacing()
|
||||
|
||||
self._cost_line_points_x: list[int] = []
|
||||
self._cost_line_points_y: list[float] = []
|
||||
|
||||
self._pp_history: list[dict] = []
|
||||
|
||||
self._setup_layout()
|
||||
self._create_kpi_section()
|
||||
self._create_pp_kpi_section()
|
||||
self._create_charts_section()
|
||||
self._create_comparison_section()
|
||||
self._create_detail_section()
|
||||
|
||||
# -- Layout ----------------------------------------------------------------
|
||||
|
||||
def _setup_layout(self):
|
||||
self.tab.grid_columnconfigure(0, weight=1)
|
||||
self.tab.grid_rowconfigure(0, weight=0)
|
||||
|
||||
self.scroll = ctk.CTkScrollableFrame(self.tab, fg_color="transparent")
|
||||
self.scroll.grid(row=0, column=0, sticky="nsew", padx=5, pady=5)
|
||||
self.scroll.grid_columnconfigure(0, weight=1)
|
||||
self.tab.grid_rowconfigure(0, weight=1)
|
||||
|
||||
# -- KPI cards helper ------------------------------------------------------
|
||||
|
||||
@staticmethod
|
||||
def _make_kpi_card(parent, title, row, col, accent=None):
|
||||
card = ctk.CTkFrame(parent)
|
||||
card.grid(row=row, column=col, sticky="nsew", padx=4, pady=4)
|
||||
card.grid_columnconfigure(0, weight=1)
|
||||
|
||||
header = ctk.CTkFrame(card, height=6, fg_color=accent or ("#1f77b4", "#1f77b4"), corner_radius=0)
|
||||
header.grid(row=0, column=0, sticky="ew")
|
||||
header.grid_propagate(False)
|
||||
|
||||
lbl_title = ctk.CTkLabel(card, text=title, font=ctk.CTkFont(size=12, weight="bold"))
|
||||
lbl_title.grid(row=1, column=0, sticky="w", padx=12, pady=(8, 2))
|
||||
|
||||
lbl_value = ctk.CTkLabel(card, text="—", font=ctk.CTkFont(family="Consolas", size=11), justify="left")
|
||||
lbl_value.grid(row=2, column=0, sticky="w", padx=12, pady=(0, 8))
|
||||
|
||||
return lbl_value
|
||||
|
||||
# -- Extracción KPIs -------------------------------------------------------
|
||||
|
||||
def _create_kpi_section(self):
|
||||
frame = ctk.CTkFrame(self.scroll)
|
||||
frame.grid(row=0, column=0, sticky="ew", padx=2, pady=(2, 1))
|
||||
frame.grid_columnconfigure((0, 1, 2, 3), weight=1)
|
||||
|
||||
lbl = ctk.CTkLabel(frame, text="Extracción de Documentos", font=ctk.CTkFont(size=15, weight="bold"))
|
||||
lbl.grid(row=0, column=0, columnspan=4, sticky="w", padx=12, pady=(10, 4))
|
||||
|
||||
self.kpi_overall = self._make_kpi_card(frame, "Total Sesión", 1, 0, accent=("#2E86C1", "#2E86C1"))
|
||||
self.kpi_extract = self._make_kpi_card(frame, "Fase: Extracción", 1, 1, accent=("#1f77b4", "#1f77b4"))
|
||||
self.kpi_normalize = self._make_kpi_card(frame, "Fase: Normalización", 1, 2, accent=("#ff7f0e", "#ff7f0e"))
|
||||
self.kpi_docs = self._make_kpi_card(frame, "Documentos", 1, 3, accent=("#27AE60", "#27AE60"))
|
||||
|
||||
zero = "input: 0\ncached: 0\noutput: 0\nUSD: $0.0000"
|
||||
self.kpi_overall.configure(text=zero)
|
||||
self.kpi_extract.configure(text=zero)
|
||||
self.kpi_normalize.configure(text=zero)
|
||||
self.kpi_docs.configure(text="Procesados: 0\nCosto prom: $0.0000")
|
||||
|
||||
# -- Post-Procesamiento KPIs ----------------------------------------------
|
||||
|
||||
def _create_pp_kpi_section(self):
|
||||
frame = ctk.CTkFrame(self.scroll)
|
||||
frame.grid(row=1, column=0, sticky="ew", padx=2, pady=1)
|
||||
frame.grid_columnconfigure((0, 1, 2, 3), weight=1)
|
||||
|
||||
lbl = ctk.CTkLabel(frame, text="Post-Procesamiento (Agente Codex)", font=ctk.CTkFont(size=15, weight="bold"))
|
||||
lbl.grid(row=0, column=0, columnspan=4, sticky="w", padx=12, pady=(10, 4))
|
||||
|
||||
self.kpi_pp_total = self._make_kpi_card(frame, "Total Acumulado", 1, 0, accent=("#8E44AD", "#8E44AD"))
|
||||
self.kpi_pp_calls = self._make_kpi_card(frame, "Llamadas API", 1, 1, accent=("#9B59B6", "#9B59B6"))
|
||||
self.kpi_pp_tokens = self._make_kpi_card(frame, "Tokens", 1, 2, accent=("#AF7AC5", "#AF7AC5"))
|
||||
self.kpi_pp_last = self._make_kpi_card(frame, "Última ejecución", 1, 3, accent=("#D2B4DE", "#D2B4DE"))
|
||||
|
||||
self.kpi_pp_total.configure(text="USD: $0.0000")
|
||||
self.kpi_pp_calls.configure(text="Total: 0\nReintentos: 0")
|
||||
self.kpi_pp_tokens.configure(text="input: 0\noutput: 0")
|
||||
self.kpi_pp_last.configure(text="—")
|
||||
|
||||
# -- Charts ----------------------------------------------------------------
|
||||
|
||||
def _create_charts_section(self):
|
||||
frame = ctk.CTkFrame(self.scroll)
|
||||
frame.grid(row=2, column=0, sticky="ew", padx=2, pady=1)
|
||||
frame.grid_columnconfigure((0, 1), weight=1)
|
||||
|
||||
lbl = ctk.CTkLabel(frame, text="Gráficos", font=ctk.CTkFont(size=15, weight="bold"))
|
||||
lbl.grid(row=0, column=0, columnspan=2, sticky="w", padx=12, pady=(10, 4))
|
||||
|
||||
if HAS_MPL:
|
||||
bg = "#2b2b2b"
|
||||
|
||||
self.fig_line = Figure(figsize=(4.5, 2.8), dpi=100, facecolor=bg)
|
||||
self.ax_line = self.fig_line.add_subplot(111)
|
||||
self._style_axis(self.ax_line, "Costo Acumulado por Documento (USD)")
|
||||
self._line_plot, = self.ax_line.plot([], [], marker="o", markersize=4, linewidth=1.5, color="#2ecc71")
|
||||
self.fig_line.tight_layout(pad=1.5)
|
||||
|
||||
self.canvas_line = FigureCanvasTkAgg(self.fig_line, master=frame)
|
||||
self.canvas_line.get_tk_widget().grid(row=1, column=0, padx=8, pady=(0, 8), sticky="nsew")
|
||||
|
||||
self.fig_bars = Figure(figsize=(4.5, 2.8), dpi=100, facecolor=bg)
|
||||
self.ax_bars = self.fig_bars.add_subplot(111)
|
||||
self._style_axis(self.ax_bars, "Tokens por Fase")
|
||||
self.fig_bars.tight_layout(pad=1.5)
|
||||
|
||||
self.canvas_bars = FigureCanvasTkAgg(self.fig_bars, master=frame)
|
||||
self.canvas_bars.get_tk_widget().grid(row=1, column=1, padx=8, pady=(0, 8), sticky="nsew")
|
||||
else:
|
||||
ctk.CTkLabel(frame, text="matplotlib no disponible — instálelo para gráficos").grid(
|
||||
row=1, column=0, columnspan=2, pady=10,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _style_axis(ax, title):
|
||||
bg = "#2b2b2b"
|
||||
fg = "#cccccc"
|
||||
ax.set_facecolor(bg)
|
||||
ax.set_title(title, fontsize=10, color=fg, pad=8)
|
||||
ax.tick_params(colors=fg, labelsize=8)
|
||||
for spine in ax.spines.values():
|
||||
spine.set_color("#555555")
|
||||
ax.xaxis.label.set_color(fg)
|
||||
ax.yaxis.label.set_color(fg)
|
||||
|
||||
# -- Comparison table ------------------------------------------------------
|
||||
|
||||
def _create_comparison_section(self):
|
||||
frame = ctk.CTkFrame(self.scroll)
|
||||
frame.grid(row=3, column=0, sticky="ew", padx=2, pady=1)
|
||||
frame.grid_columnconfigure(0, weight=1)
|
||||
|
||||
lbl = ctk.CTkLabel(frame, text="Tarifas de Modelos (USD / 1M tokens)", font=ctk.CTkFont(size=15, weight="bold"))
|
||||
lbl.grid(row=0, column=0, sticky="w", padx=12, pady=(10, 4))
|
||||
|
||||
style = ttk.Style()
|
||||
style.configure("Metrics.Treeview", rowheight=26, font=("Consolas", 10))
|
||||
style.configure("Metrics.Treeview.Heading", font=("Roboto", 11, "bold"))
|
||||
|
||||
self.tree_comparison = ttk.Treeview(
|
||||
frame,
|
||||
columns=("model", "input", "cached", "output"),
|
||||
show="headings", height=6, style="Metrics.Treeview",
|
||||
)
|
||||
self.tree_comparison.heading("model", text="Modelo")
|
||||
self.tree_comparison.heading("input", text="Input")
|
||||
self.tree_comparison.heading("cached", text="Cached Input")
|
||||
self.tree_comparison.heading("output", text="Output")
|
||||
|
||||
self.tree_comparison.column("model", width=180, anchor="w")
|
||||
self.tree_comparison.column("input", width=110, anchor="e")
|
||||
self.tree_comparison.column("cached", width=110, anchor="e")
|
||||
self.tree_comparison.column("output", width=110, anchor="e")
|
||||
|
||||
self.tree_comparison.grid(row=1, column=0, sticky="ew", padx=12, pady=(0, 10))
|
||||
|
||||
# -- Detail log ------------------------------------------------------------
|
||||
|
||||
def _create_detail_section(self):
|
||||
frame = ctk.CTkFrame(self.scroll)
|
||||
frame.grid(row=4, column=0, sticky="ew", padx=2, pady=(1, 2))
|
||||
frame.grid_columnconfigure(0, weight=1)
|
||||
|
||||
lbl = ctk.CTkLabel(frame, text="Detalle de uso (log)", font=ctk.CTkFont(size=15, weight="bold"))
|
||||
lbl.grid(row=0, column=0, sticky="w", padx=12, pady=(10, 4))
|
||||
|
||||
self.usage_text = ctk.CTkTextbox(frame, height=160, font=self.fonts["mono_small"], wrap="none")
|
||||
self.usage_text.grid(row=1, column=0, sticky="ew", padx=12, pady=(0, 10))
|
||||
|
||||
# =========================================================================
|
||||
# API PÚBLICA
|
||||
# =========================================================================
|
||||
|
||||
# -- Extracción KPIs -------------------------------------------------------
|
||||
|
||||
def update_kpis(self, overall: dict, extract_phase: dict, normalize_phase: dict) -> None:
|
||||
def fmt(d):
|
||||
return (
|
||||
f"input: {d.get('input', 0):,}\n"
|
||||
f"cached: {d.get('cached', 0):,}\n"
|
||||
f"output: {d.get('output', 0):,}\n"
|
||||
f"USD: ${d.get('usd', 0.0):.4f}"
|
||||
)
|
||||
self.kpi_overall.configure(text=fmt(overall))
|
||||
self.kpi_extract.configure(text=fmt(extract_phase))
|
||||
self.kpi_normalize.configure(text=fmt(normalize_phase))
|
||||
|
||||
def update_doc_kpi(self, processed: int, avg_cost: float) -> None:
|
||||
self.kpi_docs.configure(text=f"Procesados: {processed}\nCosto prom: ${avg_cost:.4f}")
|
||||
|
||||
# -- Post-Procesamiento KPIs ----------------------------------------------
|
||||
|
||||
def update_pp_kpis(
|
||||
self,
|
||||
total_usd: float,
|
||||
total_calls: int,
|
||||
total_retries: int,
|
||||
input_tokens: int,
|
||||
output_tokens: int,
|
||||
last_description: str,
|
||||
) -> None:
|
||||
self.kpi_pp_total.configure(text=f"USD: ${total_usd:.4f}")
|
||||
self.kpi_pp_calls.configure(text=f"Total: {total_calls}\nReintentos: {total_retries}")
|
||||
self.kpi_pp_tokens.configure(text=f"input: {input_tokens:,}\noutput: {output_tokens:,}")
|
||||
self.kpi_pp_last.configure(text=last_description or "—")
|
||||
|
||||
# -- Charts ----------------------------------------------------------------
|
||||
|
||||
def update_cost_line(self, doc_index: int, total_cost_usd: float) -> None:
|
||||
if not HAS_MPL:
|
||||
return
|
||||
try:
|
||||
self._cost_line_points_x.append(doc_index)
|
||||
self._cost_line_points_y.append(total_cost_usd)
|
||||
self._line_plot.set_data(self._cost_line_points_x, self._cost_line_points_y)
|
||||
self.ax_line.relim()
|
||||
self.ax_line.autoscale_view()
|
||||
self.canvas_line.draw_idle()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def update_token_bars(
|
||||
self,
|
||||
input_extract: int, cached_extract: int, output_extract: int,
|
||||
input_norm: int, cached_norm: int, output_norm: int,
|
||||
input_pp: int = 0, output_pp: int = 0,
|
||||
) -> None:
|
||||
if not HAS_MPL:
|
||||
return
|
||||
try:
|
||||
self.ax_bars.clear()
|
||||
self._style_axis(self.ax_bars, "Tokens por Fase")
|
||||
|
||||
labels = ["Input", "Cached", "Output"]
|
||||
extract_vals = [input_extract, cached_extract, output_extract]
|
||||
norm_vals = [input_norm, cached_norm, output_norm]
|
||||
pp_vals = [input_pp, 0, output_pp]
|
||||
|
||||
has_pp = input_pp > 0 or output_pp > 0
|
||||
|
||||
x = np.arange(len(labels))
|
||||
width = 0.25 if has_pp else 0.35
|
||||
offset = width
|
||||
|
||||
self.ax_bars.bar(x - offset, extract_vals, width, label="Extracción", color="#3498db")
|
||||
self.ax_bars.bar(x, norm_vals, width, label="Normalización", color="#e67e22")
|
||||
if has_pp:
|
||||
self.ax_bars.bar(x + offset, pp_vals, width, label="Post-Proc.", color="#9b59b6")
|
||||
|
||||
self.ax_bars.set_xticks(x, labels)
|
||||
leg = self.ax_bars.legend(fontsize=8, framealpha=0.6)
|
||||
for text in leg.get_texts():
|
||||
text.set_color("#cccccc")
|
||||
self.fig_bars.tight_layout(pad=1.5)
|
||||
self.canvas_bars.draw_idle()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# -- Comparison table ------------------------------------------------------
|
||||
|
||||
def update_comparison_table(self, comparisons: list[dict]) -> None:
|
||||
try:
|
||||
for item in self.tree_comparison.get_children():
|
||||
self.tree_comparison.delete(item)
|
||||
for c in comparisons:
|
||||
self.tree_comparison.insert("", "end", values=(
|
||||
c.get("model", "N/D"),
|
||||
f"${c.get('input', 0.0):.3f}",
|
||||
f"${c.get('cached_input', 0.0):.4f}",
|
||||
f"${c.get('output', 0.0):.2f}",
|
||||
))
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# -- Detail log ------------------------------------------------------------
|
||||
|
||||
def append_usage(self, text: str) -> None:
|
||||
self.usage_text.insert("end", text + "\n")
|
||||
self.usage_text.see("end")
|
||||
|
||||
def append_usage_safe(self, text: str) -> None:
|
||||
self.append_usage(text)
|
||||
|
||||
def append_usage_row(self, phase: str, radicado: str, usage: dict | None, cost: dict | None) -> None:
|
||||
try:
|
||||
usd = (cost or {}).get("total_usd", 0.0)
|
||||
prompt = int((usage or {}).get("prompt_tokens", 0) or 0)
|
||||
comp = int((usage or {}).get("completion_tokens", 0) or 0)
|
||||
self.append_usage(
|
||||
f"[{phase:<12}] {radicado:<22} | in={prompt:>7,} out={comp:>6,} | ${usd:.4f}"
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def clear(self) -> None:
|
||||
self.usage_text.delete("1.0", "end")
|
||||
|
||||
zero = "input: 0\ncached: 0\noutput: 0\nUSD: $0.0000"
|
||||
self.kpi_overall.configure(text=zero)
|
||||
self.kpi_extract.configure(text=zero)
|
||||
self.kpi_normalize.configure(text=zero)
|
||||
self.kpi_docs.configure(text="Procesados: 0\nCosto prom: $0.0000")
|
||||
|
||||
if HAS_MPL:
|
||||
self._cost_line_points_x.clear()
|
||||
self._cost_line_points_y.clear()
|
||||
self._line_plot.set_data([], [])
|
||||
self.ax_line.relim()
|
||||
self.ax_line.autoscale_view()
|
||||
self.canvas_line.draw_idle()
|
||||
|
||||
self.ax_bars.clear()
|
||||
self._style_axis(self.ax_bars, "Tokens por Fase")
|
||||
self.canvas_bars.draw_idle()
|
||||
|
||||
def set_usage_totals(self, **kwargs):
|
||||
pass
|
||||
|
||||
@@ -0,0 +1,227 @@
|
||||
"""Pestaña de Post-Procesamiento con agente inteligente."""
|
||||
import customtkinter as ctk
|
||||
from gui.styles import AppStyles
|
||||
|
||||
|
||||
class PostProcessTab:
|
||||
"""UI de la pestaña de Post-Procesamiento de CSV."""
|
||||
|
||||
def __init__(self, parent_tab):
|
||||
self.tab = parent_tab
|
||||
self.fonts = AppStyles.get_fonts()
|
||||
self.colors = AppStyles.get_colors()
|
||||
self.spacing = AppStyles.get_spacing()
|
||||
|
||||
self._setup_layout()
|
||||
self._create_file_section()
|
||||
self._create_preview_section()
|
||||
self._create_instructions_section()
|
||||
self._create_code_section()
|
||||
self._create_log_section()
|
||||
self._create_result_section()
|
||||
|
||||
# -- Layout ------------------------------------------------------------------
|
||||
|
||||
def _setup_layout(self):
|
||||
self.tab.grid_columnconfigure(0, weight=1)
|
||||
self.tab.grid_rowconfigure(0, weight=1)
|
||||
|
||||
self.scroll = ctk.CTkScrollableFrame(self.tab, fg_color="transparent")
|
||||
self.scroll.grid(row=0, column=0, sticky="nsew", padx=10, pady=10)
|
||||
self.scroll.grid_columnconfigure(0, weight=1)
|
||||
|
||||
# -- Sección archivo ---------------------------------------------------------
|
||||
|
||||
def _create_file_section(self):
|
||||
frame = ctk.CTkFrame(self.scroll)
|
||||
frame.grid(row=0, column=0, sticky="ew", padx=5, pady=(5, 2))
|
||||
frame.grid_columnconfigure(1, weight=1)
|
||||
|
||||
lbl_title = ctk.CTkLabel(frame, text="Archivo CSV", font=self.fonts["section"])
|
||||
lbl_title.grid(row=0, column=0, columnspan=3, sticky="w", padx=15, pady=(10, 5))
|
||||
|
||||
self.btn_select_csv = ctk.CTkButton(
|
||||
frame, text="Seleccionar CSV", font=self.fonts["ui"], width=160,
|
||||
)
|
||||
self.btn_select_csv.grid(row=1, column=0, padx=15, pady=8, sticky="w")
|
||||
|
||||
self.lbl_csv_path = ctk.CTkLabel(
|
||||
frame, text="Ningún archivo seleccionado",
|
||||
font=self.fonts["italic_small"], text_color=self.colors["gray_light"],
|
||||
)
|
||||
self.lbl_csv_path.grid(row=1, column=1, padx=10, pady=8, sticky="w")
|
||||
|
||||
# -- Preview datos -----------------------------------------------------------
|
||||
|
||||
def _create_preview_section(self):
|
||||
frame = ctk.CTkFrame(self.scroll)
|
||||
frame.grid(row=1, column=0, sticky="ew", padx=5, pady=2)
|
||||
frame.grid_columnconfigure(0, weight=1)
|
||||
|
||||
lbl = ctk.CTkLabel(frame, text="Vista previa (muestra)", font=self.fonts["section"])
|
||||
lbl.grid(row=0, column=0, sticky="w", padx=15, pady=(10, 5))
|
||||
|
||||
self.txt_preview = ctk.CTkTextbox(
|
||||
frame, height=200, font=self.fonts["mono_small"],
|
||||
state="disabled", wrap="none",
|
||||
)
|
||||
self.txt_preview.grid(row=1, column=0, sticky="ew", padx=15, pady=(0, 10))
|
||||
|
||||
# -- Instrucciones -----------------------------------------------------------
|
||||
|
||||
def _create_instructions_section(self):
|
||||
frame = ctk.CTkFrame(self.scroll)
|
||||
frame.grid(row=2, column=0, sticky="ew", padx=5, pady=2)
|
||||
frame.grid_columnconfigure(0, weight=1)
|
||||
|
||||
lbl = ctk.CTkLabel(
|
||||
frame, text="Instrucciones de transformación", font=self.fonts["section"],
|
||||
)
|
||||
lbl.grid(row=0, column=0, sticky="w", padx=15, pady=(10, 2))
|
||||
|
||||
hint = ctk.CTkLabel(
|
||||
frame,
|
||||
text="Describe qué cambios quieres aplicar y en qué columnas. Ejemplo: "
|
||||
"\"En la columna 'monto', multiplicar por 1.19 para agregar IVA\"",
|
||||
font=self.fonts["italic_small"], text_color=self.colors["gray_light"],
|
||||
wraplength=900, justify="left",
|
||||
)
|
||||
hint.grid(row=1, column=0, sticky="w", padx=15, pady=(0, 5))
|
||||
|
||||
self.txt_instructions = ctk.CTkTextbox(
|
||||
frame, height=90, font=self.fonts["ui"], wrap="word",
|
||||
)
|
||||
self.txt_instructions.grid(row=2, column=0, sticky="ew", padx=15, pady=(0, 5))
|
||||
|
||||
btn_frame = ctk.CTkFrame(frame, fg_color="transparent")
|
||||
btn_frame.grid(row=3, column=0, sticky="ew", padx=15, pady=(0, 10))
|
||||
|
||||
self.btn_run = ctk.CTkButton(
|
||||
btn_frame, text="Generar y Aplicar", font=self.fonts["button_large"],
|
||||
fg_color=self.colors["primary_button"], hover_color=self.colors["primary_hover"],
|
||||
height=38,
|
||||
)
|
||||
self.btn_run.pack(side="left")
|
||||
|
||||
self.lbl_status = ctk.CTkLabel(
|
||||
btn_frame, text="", font=self.fonts["italic_small"],
|
||||
text_color=self.colors["gray_light"],
|
||||
)
|
||||
self.lbl_status.pack(side="left", padx=15)
|
||||
|
||||
# -- Código generado ---------------------------------------------------------
|
||||
|
||||
def _create_code_section(self):
|
||||
frame = ctk.CTkFrame(self.scroll)
|
||||
frame.grid(row=3, column=0, sticky="ew", padx=5, pady=2)
|
||||
frame.grid_columnconfigure(0, weight=1)
|
||||
|
||||
lbl = ctk.CTkLabel(frame, text="Código generado", font=self.fonts["section"])
|
||||
lbl.grid(row=0, column=0, sticky="w", padx=15, pady=(10, 5))
|
||||
|
||||
self.txt_code = ctk.CTkTextbox(
|
||||
frame, height=180, font=self.fonts["mono_small"],
|
||||
state="disabled", wrap="none",
|
||||
)
|
||||
self.txt_code.grid(row=1, column=0, sticky="ew", padx=15, pady=(0, 10))
|
||||
|
||||
# -- Log de ejecución --------------------------------------------------------
|
||||
|
||||
def _create_log_section(self):
|
||||
frame = ctk.CTkFrame(self.scroll)
|
||||
frame.grid(row=4, column=0, sticky="ew", padx=5, pady=2)
|
||||
frame.grid_columnconfigure(0, weight=1)
|
||||
|
||||
lbl = ctk.CTkLabel(frame, text="Log de ejecución", font=self.fonts["section"])
|
||||
lbl.grid(row=0, column=0, sticky="w", padx=15, pady=(10, 5))
|
||||
|
||||
self.txt_log = ctk.CTkTextbox(
|
||||
frame, height=100, font=self.fonts["mono_small"],
|
||||
state="disabled", wrap="word",
|
||||
)
|
||||
self.txt_log.grid(row=1, column=0, sticky="ew", padx=15, pady=(0, 10))
|
||||
|
||||
# -- Resultado ---------------------------------------------------------------
|
||||
|
||||
def _create_result_section(self):
|
||||
frame = ctk.CTkFrame(self.scroll)
|
||||
frame.grid(row=5, column=0, sticky="ew", padx=5, pady=(2, 5))
|
||||
frame.grid_columnconfigure(0, weight=1)
|
||||
|
||||
top = ctk.CTkFrame(frame, fg_color="transparent")
|
||||
top.grid(row=0, column=0, sticky="ew", padx=15, pady=(10, 5))
|
||||
top.grid_columnconfigure(0, weight=1)
|
||||
|
||||
lbl = ctk.CTkLabel(top, text="Vista previa del resultado", font=self.fonts["section"])
|
||||
lbl.pack(side="left")
|
||||
|
||||
self.btn_save = ctk.CTkButton(
|
||||
top, text="Guardar CSV", font=self.fonts["ui"], width=140,
|
||||
fg_color=self.colors["success"], hover_color=self.colors["success_hover"],
|
||||
state="disabled",
|
||||
)
|
||||
self.btn_save.pack(side="right")
|
||||
|
||||
self.txt_result = ctk.CTkTextbox(
|
||||
frame, height=200, font=self.fonts["mono_small"],
|
||||
state="disabled", wrap="none",
|
||||
)
|
||||
self.txt_result.grid(row=1, column=0, sticky="ew", padx=15, pady=(0, 10))
|
||||
|
||||
# -- API pública -------------------------------------------------------------
|
||||
|
||||
def set_on_select_csv(self, callback):
|
||||
self.btn_select_csv.configure(command=callback)
|
||||
|
||||
def set_on_run(self, callback):
|
||||
self.btn_run.configure(command=callback)
|
||||
|
||||
def set_on_save(self, callback):
|
||||
self.btn_save.configure(command=callback)
|
||||
|
||||
def set_csv_path(self, path: str):
|
||||
self.lbl_csv_path.configure(text=path or "Ningún archivo seleccionado")
|
||||
|
||||
def get_instructions(self) -> str:
|
||||
return self.txt_instructions.get("1.0", "end").strip()
|
||||
|
||||
def set_status(self, text: str):
|
||||
self.lbl_status.configure(text=text)
|
||||
|
||||
def set_preview(self, text: str):
|
||||
self.txt_preview.configure(state="normal")
|
||||
self.txt_preview.delete("1.0", "end")
|
||||
self.txt_preview.insert("1.0", text)
|
||||
self.txt_preview.configure(state="disabled")
|
||||
|
||||
def set_code(self, text: str):
|
||||
self.txt_code.configure(state="normal")
|
||||
self.txt_code.delete("1.0", "end")
|
||||
self.txt_code.insert("1.0", text)
|
||||
self.txt_code.configure(state="disabled")
|
||||
|
||||
def append_log(self, text: str):
|
||||
self.txt_log.configure(state="normal")
|
||||
self.txt_log.insert("end", text + "\n")
|
||||
self.txt_log.see("end")
|
||||
self.txt_log.configure(state="disabled")
|
||||
|
||||
def clear_log(self):
|
||||
self.txt_log.configure(state="normal")
|
||||
self.txt_log.delete("1.0", "end")
|
||||
self.txt_log.configure(state="disabled")
|
||||
|
||||
def set_result(self, text: str):
|
||||
self.txt_result.configure(state="normal")
|
||||
self.txt_result.delete("1.0", "end")
|
||||
self.txt_result.insert("1.0", text)
|
||||
self.txt_result.configure(state="disabled")
|
||||
|
||||
def enable_save(self, enabled: bool):
|
||||
self.btn_save.configure(state="normal" if enabled else "disabled")
|
||||
|
||||
def enable_controls(self, enabled: bool):
|
||||
state = "normal" if enabled else "disabled"
|
||||
self.btn_select_csv.configure(state=state)
|
||||
self.btn_run.configure(state=state)
|
||||
self.txt_instructions.configure(state=state)
|
||||
Reference in New Issue
Block a user