File size: 10,287 Bytes
d345661
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""UI logic for the "Validació" page."""

from __future__ import annotations

from datetime import datetime
from pathlib import Path
from typing import Dict

import streamlit as st


def _build_candidates(runtime_videos: Path) -> Path:
    candidates = [
        runtime_videos,
        Path(__file__).resolve().parent.parent / "videos",
        Path.cwd() / "videos",
    ]
    for candidate in candidates:
        if candidate.exists():
            return candidate
    return candidates[0]


def render_validation_page(
    compliance_client,
    runtime_videos: Path,
    permissions: Dict[str, bool],
    username: str,
) -> None:
    if not permissions.get("validar", False):
        st.warning("⚠️ No tens permisos per accedir a aquesta secció de validació.")
        st.stop()

    st.header("🔍 Validació de Vídeos")

    tab_videos, tab_ads = st.tabs(["📹 Validar Vídeos", "🎬 Validar Audiodescripcions"])

    base_dir = _build_candidates(runtime_videos)
    if not base_dir.exists():
        st.info("📝 No s'ha trobat la carpeta **videos**. Crea-la i afegeix-hi subcarpetes amb els teus vídeos.")
        st.stop()

    with tab_videos:
        st.subheader("📹 Validar Vídeos Pujats")

        video_folders = []
        for folder in sorted(base_dir.iterdir()):
            if folder.is_dir() and folder.name != "completed":
                video_files = list(folder.glob("*.mp4")) + list(folder.glob("*.avi")) + list(folder.glob("*.mov"))
                if video_files:
                    mod_time = folder.stat().st_mtime
                    fecha = datetime.fromtimestamp(mod_time).strftime("%Y-%m-%d %H:%M")
                    video_folders.append(
                        {
                            "name": folder.name,
                            "path": str(folder),
                            "created_at": fecha,
                            "video_files": video_files,
                        }
                    )

        if not video_folders:
            st.info("📝 No hi ha vídeos pujats pendents de validació.")
        else:
            opciones_video = [f"{video['name']} - {video['created_at']}" for video in video_folders]
            seleccion = st.selectbox(
                "Selecciona un vídeo per validar:",
                opciones_video,
                index=0 if opciones_video else None,
            )

            if seleccion:
                indice = opciones_video.index(seleccion)
                video_seleccionat = video_folders[indice]

                col1, col2 = st.columns([2, 1])

                with col1:
                    st.markdown("### 📹 Informació del Vídeo")
                    st.markdown(f"**Nom:** {video_seleccionat['name']}")
                    st.markdown(f"**Data:** {video_seleccionat['created_at']}")
                    st.markdown(f"**Arxius:** {len(video_seleccionat['video_files'])} vídeos trobats")

                    if video_seleccionat["video_files"]:
                        video_path = str(video_seleccionat["video_files"][0])
                        st.markdown("**Vídeo principal:**")
                        st.video(video_path)
                    else:
                        st.warning("⚠️ No s'han trobat arxius de vídeo.")

                with col2:
                    st.markdown("### 🔍 Accions de Validació")

                    col_btn1, col_btn2 = st.columns(2)

                    with col_btn1:
                        if st.button("✅ Acceptar", type="primary", key=f"accept_video_{video_seleccionat['name']}"):
                            success = compliance_client.record_validator_decision(
                                document_id=f"video_{video_seleccionat['name']}",
                                validator_email=f"{username}@veureu.local",
                                decision="acceptat",
                                comments=f"Vídeo validat per {username}",
                            )
                            if success:
                                st.success("✅ Vídeo acceptat i registrat al servei de compliance")
                            else:
                                st.error("❌ Error registrant el veredicte")

                    with col_btn2:
                        if st.button("❌ Rebutjar", type="secondary", key=f"reject_video_{video_seleccionat['name']}" ):
                            success = compliance_client.record_validator_decision(
                                document_id=f"video_{video_seleccionat['name']}",
                                validator_email=f"{username}@veureu.local",
                                decision="rebutjat",
                                comments=f"Vídeo rebutjat per {username}",
                            )
                            if success:
                                st.success("✅ Vídeo rebutjat i registrat al servei de compliance")
                            else:
                                st.error("❌ Error registrant el veredicte")

    with tab_ads:
        st.subheader("🎬 Validar Audiodescripcions")

        videos_con_ad = []
        if base_dir.exists():
            for folder in sorted(base_dir.iterdir()):
                if folder.is_dir() and folder.name != "completed":
                    for subfolder_name in ["MoE", "Salamandra"]:
                        subfolder = folder / subfolder_name
                        if subfolder.exists():
                            ad_files = list(subfolder.glob("*_ad.txt")) + list(subfolder.glob("*_ad.srt"))
                            if ad_files:
                                mod_time = folder.stat().st_mtime
                                fecha = datetime.fromtimestamp(mod_time).strftime("%Y-%m-%d %H:%M")
                                videos_con_ad.append(
                                    {
                                        "name": folder.name,
                                        "path": str(folder),
                                        "created_at": fecha,
                                        "ad_files": ad_files,
                                        "ad_folder": str(subfolder),
                                    }
                                )

        if not videos_con_ad:
            st.info("📝 No hi ha audiodescripcions pendents de validació.")
        else:
            videos_ad_ordenats = sorted(videos_con_ad, key=lambda x: x["created_at"], reverse=True)
            opciones_ad = [f"{video['name']} - {video['created_at']}" for video in videos_ad_ordenats]

            seleccion_ad = st.selectbox(
                "Selecciona una audiodescripció per validar:",
                opciones_ad,
                index=0 if opciones_ad else None,
            )

            if seleccion_ad:
                indice = opciones_ad.index(seleccion_ad)
                video_seleccionat = videos_ad_ordenats[indice]

                col1, col2 = st.columns([2, 1])

                with col1:
                    st.markdown("### 🎬 Informació de l'Audiodescripció")
                    st.markdown(f"**Vídeo:** {video_seleccionat['name']}")
                    st.markdown(f"**Data:** {video_seleccionat['created_at']}")
                    st.markdown(f"**Carpeta:** {Path(video_seleccionat['ad_folder']).name}")
                    st.markdown(f"**Arxius:** {len(video_seleccionat['ad_files'])} audiodescripcions trobades")

                    if video_seleccionat["ad_files"]:
                        ad_path = video_seleccionat["ad_files"][0]
                        st.markdown(f"#### 📄 Contingut ({ad_path.name}):")
                        try:
                            texto = ad_path.read_text(encoding="utf-8")
                        except Exception:
                            texto = ad_path.read_text(errors="ignore")
                        st.text_area("Contingut de l'audiodescripció:", texto, height=300, disabled=True)
                    else:
                        st.warning("⚠️ No s'han trobat arxius d'audiodescripció.")

                with col2:
                    st.markdown("### 🔍 Accions de Validació")

                    col_btn1, col_btn2 = st.columns(2)

                    with col_btn1:
                        if st.button("✅ Acceptar", type="primary", key=f"accept_ad_{video_seleccionat['name']}"):
                            success = compliance_client.record_validator_decision(
                                document_id=f"ad_{video_seleccionat['name']}",
                                validator_email=f"{username}@veureu.local",
                                decision="acceptat",
                                comments=f"Audiodescripció validada per {username}",
                            )
                            if success:
                                st.success("✅ Audiodescripció acceptada i registrada al servei de compliance")
                            else:
                                st.error("❌ Error registrant el veredicte")

                    with col_btn2:
                        if st.button("❌ Rebutjar", type="secondary", key=f"reject_ad_{video_seleccionat['name']}" ):
                            success = compliance_client.record_validator_decision(
                                document_id=f"ad_{video_seleccionat['name']}",
                                validator_email=f"{username}@veureu.local",
                                decision="rebutjat",
                                comments=f"Audiodescripció rebutjada per {username}",
                            )
                            if success:
                                st.success("✅ Audiodescripció rebutjada i registrada al servei de compliance")
                            else:
                                st.error("❌ Error registrant el veredicte")

    st.markdown("---")
    st.markdown("### ℹ️ Informació del Procés de Validació")
    st.markdown(
        """
        - **Tots els veredictes** es registren al servei de compliance per garantir la traçabilitat
        - **Cada validació** inclou veredicte, nom del vídeo i validador responsable
        - **Els registres** compleixen amb la normativa AI Act i GDPR
        """
    )