File size: 31,472 Bytes
6bcbd7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
import os
import csv
import json
import logging
import shutil
from pathlib import Path
from typing import TypedDict, Annotated, List, Dict, Union
from langgraph.graph import StateGraph, END
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
from langchain_openai import ChatOpenAI
from operator import itemgetter

# --- Configuraci贸n y Herramientas ---

# Directorios de trabajo
BASE_DIR = Path(__file__).resolve().parent
TEMP_DIR = BASE_DIR / "temp"
TEMP_DIR.mkdir(exist_ok=True)

LOG_FILE = TEMP_DIR / "finetuning.log"

# Configurar el logging
logging.basicConfig(
    level=logging.INFO,
    format='%(levelname)s: %(message)s',
    handlers=[
        logging.StreamHandler(),
        logging.FileHandler(LOG_FILE, encoding="utf-8")
    ],
)
logger = logging.getLogger(__name__)

# Aseg煤rate de configurar tu API Key en la variable de entorno OPENAI_API_KEY.
api_key = os.environ.get("OPENAI_API_KEY")
if not api_key:
    raise EnvironmentError("OPENAI_API_KEY no est谩 configurada. Define la variable de entorno antes de ejecutar finetuning.py.")

# Inicializar LLM (se usa GPT-4o por su capacidad de razonamiento)
# En producci贸n, considera un modelo que soporte tus tokens y latencia requeridas.
llm = ChatOpenAI(model="gpt-4o", temperature=0.3)

# --- Ficheros de Ejemplo ---

# Fichero SRT inicial (Narrador)
INITIAL_SRT_CONTENT = """
1
00:00:00,000 --> 00:00:05,340
[Sandra] Per貌 de veritat crec que aquest projecte canviar脿 la nostra nota final.

2
00:00:04,340 --> 00:00:05,790
[Luc铆a] Hem de donar-ho tot.

3
00:00:05,790 --> 00:00:08,790
[Sandra] Ho s茅, ho s茅.

4
00:00:08,000 --> 00:00:10,000
(AD) De sobte, s贸n al parc.

5
00:00:10,000 --> 00:00:14,000
(AD) Ara tallen menjar i fan una amanida a una cuina.
"""

# Fichero JSON de contexto (ejemplo de la respuesta anterior, pero simplificado para el Narrador)
CONTEXT_JSON_CONTENT = """
{
  "segments": [
    {"id": 1, "start": "00:00:00,000", "end": "00:00:05,340", "type": "dialog", "text": "[Sandra] Per貌 de veritat crec que aquest projecte canviar脿 la nostra nota final."},
    {"id": 2, "start": "00:00:04,340", "end": "00:00:05,790", "type": "dialog", "text": "[Luc铆a] Hem de donar-ho tot."},
    {"id": 3, "start": "00:00:05,790", "end": "00:00:08,790", "type": "dialog", "text": "[Sandra] Ho s茅, ho s茅."},
    {"id": 4, "start": "00:00:08,000", "end": "00:00:10,000", "type": "visual_context", "text": "Cambio de escena a un parque. Personajes caminando."},
    {"id": 5, "start": "00:00:10,000", "end": "00:00:14,000", "type": "visual_context", "text": "Escena en una cocina. Los personajes est谩n cortando vegetales y haciendo una ensalada."}
  ]
}
"""

# Fichero de Reglas UNE (Norma T茅cnica para el Cr铆tico)
# Nota: Aqu铆 se usa un resumen de las reglas pertinentes para un LLM.
UNE_RULES = """
### Reglas UNE de Audiodescripci贸n (Para el Cr铆tico)
1.  **Objetividad y Foco Visual:** La descripci贸n debe ser puramente objetiva, describiendo solo lo que se ve. Debe priorizar la acci贸n y los elementos relevantes (personajes, objetos, localizaci贸n).
2.  **Tiempo y Espacio (Sincronizaci贸n):** Las audiodescripciones (AD) deben insertarse en los silencios del di谩logo. El tiempo de la AD (entre START y END) debe ser suficiente para narrar el contenido sin solaparse con el di谩logo o la m煤sica importante.
3.  **Concisi贸n y Claridad:** Usar lenguaje simple y conciso. Evitar redundancias y juicios de valor.
4.  **Formato:** Cada segmento de AD debe tener un formato SRT v谩lido, incluyendo el marcador (AD) al principio de la l铆nea de texto.
5.  **Utilidad:** Cada segmento de AD debe ser 煤til para la comprensi贸n y nunca ser redundante. En caso de repetir algo ya explicado antes, mejor no decir nada.
"""

EVALUATION_CRITERIA = [
    "Precisi贸 Descriptiva",
    "Sincronitzaci贸 Temporal",
    "Claredat i Concisi贸",
    "Inclusi贸 de Di脿leg/So",
    "Contextualitzaci贸",
    "Flux i Ritme de la Narraci贸",
]

CRITERIA_WEIGHTS = {
    "Precisi贸 Descriptiva": 1,
    "Sincronitzaci贸 Temporal": 4,
    "Claredat i Concisi贸": 1,
    "Inclusi贸 de Di脿leg/So": 1,
    "Contextualitzaci贸": 1,
    "Flux i Ritme de la Narraci贸": 1,
}

# Inicializar ficheros para la ejecuci贸n
def setup_files(initial_srt_content: str, context_json_content: str):
    """Crea los ficheros iniciales necesarios en el sistema de archivos local."""
    (TEMP_DIR / "une_ad_0.srt").write_text(initial_srt_content, encoding="utf-8")
    (TEMP_DIR / "json_ad.json").write_text(context_json_content, encoding="utf-8")
    logger.info("Ficheros iniciales 'une_ad_0.srt' y 'json_ad.json' creados.")

# --- Utilidades ---
def _strip_markdown_fences(content: str) -> str:
    """Elimina fences ```...``` alrededor de una respuesta JSON si existen."""
    text = content.strip()
    if text.startswith("```"):
        lines = text.splitlines()
        # descartar primera l铆nea con ``` o ```json
        lines = lines[1:]
        # eliminar el cierre ``` (pueden existir varias l铆neas en blanco finales)
        while lines and lines[-1].strip() == "```":
            lines.pop()
        text = "\n".join(lines).strip()
    return text


def generate_evaluation_report(srt_content: str, iteration: int) -> tuple[float, float, Path]:
    """Solicita al LLM una avaluaci贸 estructurada i guarda'n el CSV."""
    criteria_formatted = "\n".join(f"- {name}" for name in EVALUATION_CRITERIA)
    prompt = (
        "Actua com un auditor UNE. Avalua l'SRT generat, puntuant cada caracter铆stica de 0 a 7 "
        "segons la qualitat observada. D贸nega justificaci贸 breve per貌 concreta per a cada cas. "
        "Les caracter铆stiques obligat貌ries s贸n:\n"
        f"{criteria_formatted}\n"
        "Retorna 脷NICAMENT un array JSON d'objectes amb les claus: "
        "'caracteristica', 'valoracio' (nombre enter de 0 a 7) i 'justificacio'."
    )

    response = llm.invoke(
        [
            SystemMessage(content=prompt),
            HumanMessage(
                content=(
                    "# SRT AVALUAT\n"
                    f"{srt_content}\n\n"
                    "Assegura't de complir el format indicat."
                )
            ),
        ]
    )

    cleaned = _strip_markdown_fences(response.content)
    try:
        data = json.loads(cleaned)
        if not isinstance(data, list):
            raise ValueError("La resposta no 茅s una llista.")
    except Exception as exc:
        logger.error(
            "Error al generar l'avaluaci贸 estructurada: %s. Resposta original: %s",
            exc,
            response.content,
        )
        data = [
            {
                "caracteristica": "Avaluaci贸 fallida",
                "valoracio": 1,
                "justificacio": "No s'ha pogut obtenir l'avaluaci贸 del LLM.",
            }
        ]

    eval_path = TEMP_DIR / f"eval_{iteration}.csv"
    with eval_path.open("w", encoding="utf-8", newline="") as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(["Caracteristica", "Valoracio (0-7)", "Justificacio"])
        for item in data:
            writer.writerow(
                [
                    item.get("caracteristica", ""),
                    item.get("valoracio", 0),
                    item.get("justificacio", ""),
                ]
            )

    scores = []
    weighted_sum = 0.0
    total_weight = 0.0

    for entry in data:
        if not isinstance(entry, dict):
            continue
        try:
            score = float(entry.get("valoracio", 0))
        except (TypeError, ValueError):
            score = 0.0
        scores.append(score)

        weight = CRITERIA_WEIGHTS.get(entry.get("caracteristica", ""), 1)
        weighted_sum += score * weight
        total_weight += weight

    mean_score = sum(scores) / len(scores) if scores else 0.0
    weighted_mean = weighted_sum / total_weight if total_weight else mean_score
    return mean_score, weighted_mean, eval_path

# --- Definici贸n del Estado de la Gr谩fica (StateGraph) ---
class ReflectionState(TypedDict):
    """Representa el estado del bucle de reflexi贸n."""
    iteration: int  # Ciclo actual (empezando en 0)
    current_srt_path: str  # Ruta al archivo SRT actual (e.g., une_ad_0.srt, une_ad_1.srt)
    critic_report: Dict[str, Union[float, str]]  # 脷ltimo informe del cr铆tico (puntuaci贸n y texto)
    history: List[SystemMessage] # Historial de mensajes entre agentes
    evaluation_mean: float
    best_iteration: int
    best_weighted_mean: float
    best_srt_path: str
    best_eval_path: str

# --- Nodos/Agentes de la Gr谩fica ---
def narrator_agent(state: ReflectionState):
    """
    Agente que genera o reescribe el SRT.
    - En el ciclo 0, genera el SRT inicial.
    - En ciclos > 0, reescribe el SRT bas谩ndose en el critic_report.
    """
    iteration = state["iteration"]
    critic_report = state["critic_report"]
    history = state["history"]
    
    # Cargar contexto y 煤ltimo SRT
    json_context = (TEMP_DIR / "json_ad.json").read_text(encoding="utf-8")
    current_srt = Path(state["current_srt_path"]).read_text(encoding="utf-8")

    # 1. Definir el prompt
    if iteration == 0:
        # Tarea inicial (aunque en este caso ya se proporciona une_ad_0.srt)
        # Aqu铆 se simula la generaci贸n inicial.
        prompt = (
            "Ets un Narrador expert en Audiodescripci贸 (AD). La teva tasca inicial 茅s generar "
            "un fitxer SRT d'audiodescripcions basat en el JSON de context visual. "
            "TOT I AIX脥, per a aquesta primera iteraci贸, l'SRT ja s'ha generat. "
            "Simplement retorna el contingut de 'une_ad_0.srt' com si fos la teva sortida. "
            "Assegura't que totes les audiodescripcions estiguin en catal脿 i que cadascuna pugui ser locutada "
            "dins del temps disponible (utilitza un m脿xim aproximat d'11 car脿cters per segon). Si el tram de temps "
            "茅s massa curt (<1.5s), combina'l amb el bloc d'AD m茅s proper i ajusta els timestamps perqu猫 la narraci贸 sigui fluida. "
            "Evita redund脿ncies: no repeteixis informaci贸 ja descrita en segments d'AD anteriors o al di脿leg, i elimina qualsevol detall que no sigui essencial."
        )
        output_srt = current_srt
        reflection_text = "Generaci贸n inicial. No hay reflexi贸n."
    else:
        # Tarea de reflexi贸n
        prompt = (
            "Ets un Narrador expert en Audiodescripci贸 (AD). Has rebut una cr铆tica sobre la teva 煤ltima versi贸 de l'SRT. "
            "La teva tasca 茅s REESCRIURE el contingut d'audiodescripci贸 (l铆nies amb '(AD)') del fitxer SRT, "
            "assegurant que sigui coherent amb el JSON de context i, sobretot, que CORREGEIXIS TOTS els problemes "
            "mencionats a l'Informe Cr铆tic adjunt. Mant茅n intactes els di脿legs (l铆nies amb [Nom]) i escriu totes les audiodescripcions en catal脿 natural. "
            "Garanteix que cada bloc d'AD pugui ser locutat dins del seu interval temporal disponible considerant un m脿xim d'11 car脿cters per segon. "
            "Si l'interval 茅s massa curt (<1.5s), fusiona'l amb el bloc d'AD anterior o posterior m茅s proper i ajusta els timestamps perqu猫 quedin cont铆nues. "
            "Prefereix frases concises i accionables, prioritzant la informaci贸 visual essencial, i elimina redund脿ncies amb AD anteriors o amb els di脿legs."
        )
        
        # Concatenar la entrada para el LLM
        input_content = f"""
        # INFORME CR脥TICO
        Porcentaje de Fiabilidad Anterior: {critic_report.get('reliability_percentage')}
        Cr铆tica Cualitativa: {critic_report.get('qualitative_critique')}

        # JSON DE CONTEXTO VISUAL (Gu铆a para la AD)
        {json_context}

        # 脷LTIMO ARCHIVO SRT GENERADO (une_ad_{iteration-1}.srt)
        {current_srt}

        REGLAS: Tu respuesta debe ser *SOLAMENTE* el contenido completo del nuevo archivo SRT (incluyendo di谩logos), sin ning煤n comentario o explicaci贸n adicional.
        """
        
        # Llamada al LLM
        response = llm.invoke(
            [
                SystemMessage(content=prompt),
                HumanMessage(content=input_content)
            ]
        )
        
        output_srt = response.content
        reflection_text = f"Reescrito en base al informe cr铆tico: {critic_report.get('qualitative_critique', 'N/A')}"

    # 2. Guardar la nueva salida
    new_srt_path = TEMP_DIR / f"une_ad_{iteration}.srt"
    new_srt_path.write_text(output_srt, encoding="utf-8")

    # 3. Guardar el pensamiento (reflection_text)
    (TEMP_DIR / f"thinking_{iteration}.txt").write_text(reflection_text, encoding="utf-8")

    logger.info(f"Narrador: Generada la versi贸n {iteration} del SRT en '{new_srt_path}'.")

    # 4. Actualizar el estado
    new_history = history + [AIMessage(content=f"Narrador v{iteration} completado. Raz贸n de reflexi贸n: {reflection_text}")]
    return {
        "iteration": iteration,
        "current_srt_path": str(new_srt_path),
        "history": new_history,
        "evaluation_mean": state.get("evaluation_mean", 0.0),
        "best_iteration": state.get("best_iteration", -1),
        "best_weighted_mean": state.get("best_weighted_mean", 0.0),
        "best_srt_path": state.get("best_srt_path", str(new_srt_path)),
        "best_eval_path": state.get("best_eval_path", str(TEMP_DIR / f"eval_{iteration}.csv")),
    }

def identity_manager_agent(state: ReflectionState):
    """
    Agente que gestiona la identidad del usuario.
    """
    iteration = state["iteration"]
    history = state["history"]
    current_srt = Path(state["current_srt_path"]).read_text(encoding="utf-8")
    
    prompt = (
        "Ets un gestor d'identitats. La teva tasca 茅s verificar la identitat de l'usuari "
        "i assegurar-te que les seves dades estiguin actualitzades."
    )
    
    input_content = f"""
    # 脷LTIMO ARCHIVO SRT GENERADO (une_ad_{iteration}.srt):
    {current_srt}

    REGLAS: Tu respuesta debe ser *SOLAMENTE* un objeto JSON con la informaci贸n de la identidad del usuario.
    """
    
    # Llamada al LLM
    response = llm.invoke(
        [
            SystemMessage(content=prompt),
            HumanMessage(content=input_content)
        ]
    )

    # Intentar parsear la respuesta del LLM (puede fallar, por eso se usa un try/except)
    try:
        cleaned_response = _strip_markdown_fences(response.content)
        identity_info = json.loads(cleaned_response)
        if not isinstance(identity_info, dict):
            raise ValueError("Estructura JSON incorrecta.")
    except Exception as e:
        logger.error(f"Error al parsear el JSON de la identidad: {e}. Respuesta: {response.content}")
        identity_info = {"error": "No s'ha pogut obtenir la informaci贸 d'identitat."}

    logger.info(f"Identity Manager: Informaci贸n de identidad actualizada.")
    
    new_history = history + [AIMessage(content=f"Identity Manager v{iteration} completado.")]
    return {
        "iteration": iteration,
        "current_srt_path": state["current_srt_path"],
        "history": new_history,
        "evaluation_mean": state.get("evaluation_mean", 0.0),
        "best_iteration": state.get("best_iteration", -1),
        "best_weighted_mean": state.get("best_weighted_mean", 0.0),
        "best_srt_path": state.get("best_srt_path", state["current_srt_path"]),
        "best_eval_path": state.get("best_eval_path", str(TEMP_DIR / f"eval_{iteration}.csv")),
    }

def critic_agent(state: ReflectionState):
    """
    Agente que eval煤a la calidad del SRT generado por el Narrador bas谩ndose en las Reglas UNE.
    Devuelve una puntuaci贸n y una cr铆tica cualitativa.
    """
    iteration = state["iteration"]
    history = state["history"]
    current_srt = Path(state["current_srt_path"]).read_text(encoding="utf-8")
    
    prompt = (
        "Ets un Cr铆tic d'Audiodescripci贸 molt estricte. La teva tasca 茅s avaluar l'SRT adjunt "
        "煤nicament segons les Regles UNE proporcionades. L'avaluaci贸 ha de ser doble: "
        "1. **Num猫rica**: Un percentatge de fiabilitat (ex. 85.5) de 0 a 100%. "
        "2. **Qualitativa**: Una cr铆tica constructiva sobre les principals mancances de les AD respecte a les regles. "
        "Has de ser EXTREMADAMENT estricte amb la sincronitzaci贸 (sense solapament amb el di脿leg), "
        "amb l'adequaci贸 temporal (velocitat m脿xima recomanada d'11 car脿cters per segon) i amb l'abs猫ncia de redund脿ncies. "
        "Comprova tamb茅 que totes les audiodescripcions estan escrites en catal脿 natural."
    )
    
    input_content = f"""
    # REGLAS UNE DE AUDIODESCRIPCI脫N:
    {UNE_RULES}

    # ARCHIVO SRT A EVALUAR (une_ad_{iteration}.srt):
    {current_srt}

    REGLAS DE RESPUESTA:
    Tu respuesta debe ser *SOLAMENTE* un objeto JSON con dos claves:
    1. "reliability_percentage": (float) El porcentaje de fiabilidad.
    2. "qualitative_critique": (string) La cr铆tica cualitativa y sugerencias de mejora.
    Ejemplo de respuesta: {{"reliability_percentage": 75.0, "qualitative_critique": "El segmento 4 se solapa 0.34s con el di谩logo de Sandra. El segmento 5 es demasiado gen茅rico y no describe bien la acci贸n."}}
    """
    
    # Llamada al LLM
    response = llm.invoke(
        [
            SystemMessage(content=prompt),
            HumanMessage(content=input_content)
        ]
    )

    # Intentar parsear la respuesta del LLM (puede fallar, por eso se usa un try/except)
    try:
        cleaned_response = _strip_markdown_fences(response.content)
        report = json.loads(cleaned_response)
        if not isinstance(report, dict) or 'reliability_percentage' not in report:
            raise ValueError("Estructura JSON incorrecta.")
    except Exception as e:
        logger.error(f"Error al parsear el JSON del Cr铆tico: {e}. Respuesta: {response.content}")
        report = {"reliability_percentage": 1.0, "qualitative_critique": "El Cr铆tico no devolvi贸 un JSON v谩lido. Reintentar."}

    logger.info(f"Cr铆tico: Evaluaci贸n completada. Fiabilidad: {report.get('reliability_percentage')}%.")
    
    mean_score, weighted_mean, eval_path = generate_evaluation_report(current_srt, iteration)

    thinking_path = TEMP_DIR / f"thinking_{iteration}.txt"
    if thinking_path.exists():
        previous_text = thinking_path.read_text(encoding="utf-8")
        thinking_path.write_text(
            (
                f"{previous_text}\n\nMitjana simple d'avaluaci贸: {mean_score:.2f} / 7"
                f"\nMitjana ponderada d'avaluaci贸: {weighted_mean:.2f} / 7"
            ),
            encoding="utf-8",
        )

    best_iteration = state.get("best_iteration", -1)
    best_weighted_mean = state.get("best_weighted_mean", -1.0)
    best_srt_path = state.get("best_srt_path", state["current_srt_path"])
    best_eval_path = state.get("best_eval_path", str(TEMP_DIR / f"eval_{iteration}.csv"))

    if weighted_mean > best_weighted_mean:
        best_iteration = iteration
        best_weighted_mean = weighted_mean
        best_srt_path = state["current_srt_path"]
        best_eval_path = str(eval_path)

    new_history = history + [
        AIMessage(
            content=(
                "Cr铆tico v{iter} completado. Fiabilidad: {reliab}%. "
                "Mitjana simple: {mean:.2f}/7. Mitjana ponderada: {wmean:.2f}/7"
            ).format(
                iter=iteration,
                reliab=report.get("reliability_percentage"),
                mean=mean_score,
                wmean=weighted_mean,
            )
        )
    ]
    return {
        "iteration": iteration + 1,
        "critic_report": report,
        "history": new_history,
        "evaluation_mean": weighted_mean,
        "best_iteration": best_iteration,
        "best_weighted_mean": best_weighted_mean,
        "best_srt_path": best_srt_path,
        "best_eval_path": best_eval_path,
    }

def identity_manager_agent(state: ReflectionState):
    """
    Agente que verifica coherencia entre hablantes en SRT, casting.csv y contexto visual.
    Corrige asignaciones de hablantes y genera log de cambios.
    """
    iteration = state["iteration"]
    
    # Cargar archivos
    current_srt = Path(state["current_srt_path"]).read_text(encoding="utf-8")
    casting_path = TEMP_DIR / "casting.csv"
    json_context = (TEMP_DIR / "json_ad.json").read_text(encoding="utf-8")

    # Verificar existencia de casting.csv
    if not casting_path.exists():
        logger.warning("Casting.csv no encontrado. Saltando identity_manager.")
        return state

    casting_content = casting_path.read_text(encoding="utf-8")

    prompt = (
        "Ets un Identity Manager. La teva tasca 茅s:\n"
        "1. Verificar que les assignacions de parlants a l'SRT coincideixen amb casting.csv\n"
        "2. Comprovar que els parlants assignats s贸n coherents amb el context visual de json_ad.json\n"
        "3. Si trobes inconsist猫ncies, re-assigna els parlants corregint les etiquetes [Nom]\n"
        "4. Justifica canvis al fitxer identity_log.txt\n"
        "\n"
        "Dades d'entrada:\n"
        f"- CASTING.CSV:\n{casting_content}\n"
        f"- JSON CONTEXT:\n{json_context}\n"
        f"- SRT ACTUAL:\n{current_srt}\n"
        "\n"
        "REGLES:\n"
        "- Nom茅s modifica les l铆nies de di脿leg (ex: [Nom])\n"
        "- Mant茅 la numeraci贸 i timestamps\n"
        "- Si no hi ha canvis, retorna l'SRT original\n"
        "\n"
        "Format de sortida:\n"
        "```json\n"
        "{{\n"
        "  \"srt_content\": \"<nou contingut SRT>\",\n"
        "  \"log_message\": \"<explicaci贸 canvis o 'Sense canvis'>\"\n"
        "}}\n"
        "```"
    )

    response = llm.invoke([SystemMessage(content=prompt)])
    
    try:
        # Parsejar resposta JSON
        cleaned = _strip_markdown_fences(response.content)
        data = json.loads(cleaned)
        new_srt = data["srt_content"]
        log_msg = data["log_message"]
        
        # Escriure log
        log_path = TEMP_DIR / f"identity_log_{iteration}.txt"
        log_path.write_text(f"Iteraci贸 {iteration}: {log_msg}", encoding="utf-8")
        
        # Actualitzar SRT si hi ha canvis
        if new_srt != current_srt:
            new_srt_path = TEMP_DIR / f"une_ad_{iteration}_corrected.srt"
            new_srt_path.write_text(new_srt, encoding="utf-8")
            logger.info(f"Identity Manager: Correccions aplicades. Detalls: {log_msg}")
            return {
                **state,
                "current_srt_path": str(new_srt_path)
            }
            
    except Exception as e:
        logger.error(f"Error en identity_manager: {e}")
    
    return state

def background_descriptor_agent(state: ReflectionState):
    """
    Agente que verifica coherencia entre escenarios en SRT y scenarios.csv.
    Corrige nombres de escenarios usando descripciones coherentes.
    """
    iteration = state["iteration"]
    
    # Cargar archivos
    current_srt = Path(state["current_srt_path"]).read_text(encoding="utf-8")
    scenarios_path = TEMP_DIR / "scenarios.csv"

    # Verificar existencia de scenarios.csv
    if not scenarios_path.exists():
        logger.warning("Scenarios.csv no encontrado. Saltando background_descriptor.")
        return state

    scenarios_content = scenarios_path.read_text(encoding="utf-8")

    prompt = (
        "Ets un Background Descriptor. La teva tasca 茅s:\n"
        "1. Verificar que les descripcions d'escenaris a l'SRT coincideixen amb scenarios.csv\n"
        "2. Si trobes coincid猫ncies, reempla莽a les descripcions gen猫riques pel nom oficial de l'escenari\n"
        "3. Justifica canvis al fitxer background_log.txt\n"
        "\n"
        "Dades d'entrada:\n"
        f"- SCENARIOS.CSV:\n{scenarios_content}\n"
        f"- SRT ACTUAL:\n{current_srt}\n"
        "\n"
        "REGLES:\n"
        "- Nom茅s modifica l铆nies d'audiodescripci贸 (ex: (AD) ...)\n"
        "- Mant茅 la numeraci贸 i timestamps\n"
        "- Si no hi ha canvis, retorna l'SRT original\n"
        "\n"
        "Format de sortida:\n"
        "```json\n"
        "{{\n"
        "  \"srt_content\": \"<nou contingut SRT>\",\n"
        "  \"log_message\": \"<explicaci贸 canvis o 'Sense canvis'>\"\n"
        "}}\n"
        "```"
    )

    response = llm.invoke([SystemMessage(content=prompt)])
    
    try:
        # Parsejar resposta JSON
        cleaned = _strip_markdown_fences(response.content)
        data = json.loads(cleaned)
        new_srt = data["srt_content"]
        log_msg = data["log_message"]
        
        # Escriure log
        log_path = TEMP_DIR / f"background_log_{iteration}.txt"
        log_path.write_text(f"Iteraci贸 {iteration}: {log_msg}", encoding="utf-8")
        
        # Actualitzar SRT si hi ha canvis
        if new_srt != current_srt:
            new_srt_path = TEMP_DIR / f"une_ad_{iteration}_scenario_corrected.srt"
            new_srt_path.write_text(new_srt, encoding="utf-8")
            logger.info(f"Background Descriptor: Correccions aplicades. Detalls: {log_msg}")
            return {
                **state,
                "current_srt_path": str(new_srt_path)
            }
            
    except Exception as e:
        logger.error(f"Error en background_descriptor: {e}")
    
    return state

# --- Condici贸n de Salida del Bucle ---

def should_continue(state: ReflectionState) -> str:
    """
    Funci贸n de chequeo que decide si continuar iterando o finalizar.
    """
    MAX_ITERATIONS = 5  # N煤mero m谩ximo de ciclos
    MIN_AVERAGE_SCORE = 6.0  # Umbral de calidad sobre 7

    iteration = state["iteration"]
    mean_score = state.get("evaluation_mean", 0.0)

    if mean_score >= MIN_AVERAGE_SCORE:
        logger.info(f"FIN: Mitjana ponderada d'avaluaci贸 assolida ({mean_score:.2f} >= {MIN_AVERAGE_SCORE}).")
        return "end"

    if iteration >= MAX_ITERATIONS:
        logger.info(f"FIN: S'ha assolit el m脿xim d'iteracions ({iteration} / {MAX_ITERATIONS}).")
        return "end"

    logger.info(f"CONTINUAR: Iteraci贸 {iteration} / {MAX_ITERATIONS}. Mitjana ponderada actual: {mean_score:.2f} / 7.")
    return "continue"

# --- Construcci贸n de la Gr谩fica ---

# 1. Configurar el estado inicial
initial_state: ReflectionState = {
    "iteration": 0,
    "current_srt_path": str(TEMP_DIR / "une_ad_0.srt"),
    "critic_report": {"reliability_percentage": 0.0, "qualitative_critique": "Inicializando el proceso."},
    "history": [],
    "evaluation_mean": 0.0,
    "best_iteration": -1,
    "best_weighted_mean": -1.0,
    "best_srt_path": str(TEMP_DIR / "une_ad_0.srt"),
    "best_eval_path": str(TEMP_DIR / "eval_0.csv"),
}

# 2. Definir la gr谩fica
workflow = StateGraph(ReflectionState)

# Nodos
workflow.add_node("narrator", narrator_agent)
workflow.add_node("identity_manager", identity_manager_agent)
workflow.add_node("background_descriptor", background_descriptor_agent)
workflow.add_node("critic", critic_agent)

# Estructura del bucle: Narrator -> Identity Manager -> Background Descriptor -> Critic -> Check
workflow.set_entry_point("narrator")
workflow.add_edge("narrator", "identity_manager")
workflow.add_edge("identity_manager", "background_descriptor")
workflow.add_edge("background_descriptor", "critic")

# Condici贸n (puente de ramificaci贸n)
workflow.add_conditional_edges(
    "critic",
    should_continue,
    {
        "continue": "narrator", # Si no se cumple el umbral/ciclo, vuelve al narrador
        "end": END               # Si se cumple, termina
    }
)

# Compilar la gr谩fica
app = workflow.compile()


def generate_free_ad_from_srt(srt_path: Path) -> Path:
    """Genera una narraci贸n libre detallada a partir del SRT final."""
    srt_content = srt_path.read_text(encoding="utf-8")
    prompt = (
        "Actua com una narradora professional d'audiodescripcions lliures. "
        "A partir de l'SRT proporcionat, escriu un text narratiu en catal脿 que descrigui "
        "de manera exhaustiva i fluida tot el que succeeix a la pe莽a audiovisual. "
        "Inclou accions, aparen莽a, gestos, canvis d'escena i qualsevol detall rellevant, "
        "sense limitar-te a les restriccions temporals del format SRT. "
        "Evita repetir literalment els di脿legs, per貌 contextualitza'ls quan sigui 煤til. "
        "La narraci贸 ha de ser clara, coherent i apta per ser locutada com una narraci贸 lliure."
    )

    response = llm.invoke(
        [
            SystemMessage(content=prompt),
            HumanMessage(
                content=(
                    "# SRT FINAL\n"
                    f"{srt_content}\n\n"
                    "Respon 煤nicamente con la narraci贸 lliure sin cap comentario adicional."
                )
            ),
        ]
    )

    free_ad_path = TEMP_DIR / "free_ad.txt"
    free_ad_path.write_text(response.content, encoding="utf-8")
    logger.info(f"Narraci贸 lliure generada en '{free_ad_path}'.")
    return free_ad_path

def run_reflection_pipeline(srt_content: str, context_json: str | None = None) -> str:
    """Ejecuta el grafo de finetuning sobre un SRT y devuelve el SRT final.

    Args:
        srt_content: Contenido SRT inicial.
        context_json: Contexto visual en JSON; si es None se usa CONTEXT_JSON_CONTENT.

    Returns:
        Contenido del SRT final seleccionado como mejor iteraci贸n.
    """

    # Inicializar el entorno de trabajo a partir del SRT recibido
    setup_files(srt_content, context_json or CONTEXT_JSON_CONTENT)

    logger.info("--- Comenzando el Bucle de Finetuning (run_reflection_pipeline) ---")

    # Ejecutar la gr谩fica
    final_state = app.invoke(initial_state)

    logger.info("\n--- Bucle Finalizado ---")

    best_iteration = final_state.get("best_iteration", -1)
    best_weighted_mean = final_state.get("best_weighted_mean", 0.0)
    best_srt_path = Path(final_state.get("best_srt_path", final_state["current_srt_path"]))
    best_eval_path = Path(final_state.get("best_eval_path", TEMP_DIR / "eval_0.csv"))

    final_srt_path = TEMP_DIR / "une_ad.srt"
    final_eval_path = TEMP_DIR / "eval.csv"

    try:
        shutil.copy(best_srt_path, final_srt_path)
        logger.info(f"SRT final copiado a '{final_srt_path}'.")
    except Exception as exc:
        logger.error(f"No se pudo copiar el SRT final: {exc}")

    try:
        shutil.copy(best_eval_path, final_eval_path)
        logger.info(f"Evaluaci贸n final copiada a '{final_eval_path}'.")
    except Exception as exc:
        logger.error(f"No se pudo copiar el CSV final: {exc}")

    logger.info(
        "Resultado 贸ptimo en iteraci贸n %s (mitjana ponderada %.2f/7)",
        best_iteration,
        best_weighted_mean,
    )

    return final_srt_path.read_text(encoding="utf-8")


# --- Ejecuci贸n Principal ---

if __name__ == "__main__":
    # Inicializar el entorno con el SRT y contexto de ejemplo
    final_srt_text = run_reflection_pipeline(INITIAL_SRT_CONTENT, CONTEXT_JSON_CONTENT)

    # Generar, si es posible, la narraci贸n libre a partir del SRT final
    free_ad_path: Union[Path, None] = None
    try:
        final_srt_path = TEMP_DIR / "une_ad.srt"
        free_ad_path = generate_free_ad_from_srt(final_srt_path)
    except Exception as exc:
        logger.error(f"No s'ha pogut generar la narraci贸 lliure: {exc}")

    # Mostrar resultados b脿sics per comprovaci贸 manual
    print("\n--- Contenido del SRT Final ---")
    print(final_srt_text)

    if free_ad_path is not None:
        print("\n--- Narraci贸 Lliure ---")
        print(free_ad_path.read_text(encoding="utf-8"))