Delete app.py
Browse files
app.py
DELETED
|
@@ -1,136 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import yaml
|
| 3 |
-
import shutil
|
| 4 |
-
from pathlib import Path
|
| 5 |
-
try:
|
| 6 |
-
import tomllib
|
| 7 |
-
except ModuleNotFoundError: # Py<3.11
|
| 8 |
-
import tomli as tomllib
|
| 9 |
-
import streamlit as st
|
| 10 |
-
|
| 11 |
-
from database import set_db_path, init_schema
|
| 12 |
-
from api_client import APIClient
|
| 13 |
-
from utils import ensure_dirs
|
| 14 |
-
from auth import initialize_auth_system, render_login_form, render_sidebar, require_login
|
| 15 |
-
from mobile_verification import render_mobile_verification_screen, get_user_permissions
|
| 16 |
-
from compliance_client import compliance_client
|
| 17 |
-
from page_modules.process_video import render_process_video_page
|
| 18 |
-
from page_modules.analyze_transcriptions import render_analyze_transcriptions_page
|
| 19 |
-
from page_modules.statistics import render_statistics_page
|
| 20 |
-
from page_modules.validation import render_validation_page
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
# -- Move DB ---
|
| 24 |
-
os.environ["STREAMLIT_DATA_DIRECTORY"] = "/tmp/.streamlit"
|
| 25 |
-
Path("/tmp/.streamlit").mkdir(parents=True, exist_ok=True)
|
| 26 |
-
Path("/tmp/data").mkdir(parents=True, exist_ok=True)
|
| 27 |
-
source_db = "init_data/veureu.db"
|
| 28 |
-
target_db = "/tmp/data/app.db"
|
| 29 |
-
if not os.path.exists(target_db) and os.path.exists(source_db):
|
| 30 |
-
shutil.copy(source_db, target_db)
|
| 31 |
-
|
| 32 |
-
static_videos = Path(__file__).parent / "videos"
|
| 33 |
-
runtime_videos = Path("/tmp/data/videos")
|
| 34 |
-
if not runtime_videos.exists():
|
| 35 |
-
shutil.copytree(static_videos, runtime_videos, dirs_exist_ok=True)
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
# --- Config ---
|
| 39 |
-
def _load_yaml(path="config.yaml") -> dict:
|
| 40 |
-
with open(path, "r", encoding="utf-8") as f:
|
| 41 |
-
cfg = yaml.safe_load(f) or {}
|
| 42 |
-
# interpolación sencilla de ${VARS} si las usas en el YAML
|
| 43 |
-
def _subst(s: str) -> str:
|
| 44 |
-
return os.path.expandvars(s) if isinstance(s, str) else s
|
| 45 |
-
|
| 46 |
-
# aplica sustitución en los campos que te interesan
|
| 47 |
-
if "api" in cfg:
|
| 48 |
-
cfg["api"]["base_url"] = _subst(cfg["api"].get("base_url", ""))
|
| 49 |
-
cfg["api"]["token"] = _subst(cfg["api"].get("token", ""))
|
| 50 |
-
|
| 51 |
-
if "storage" in cfg and "root_dir" in cfg["storage"]:
|
| 52 |
-
cfg["storage"]["root_dir"] = _subst(cfg["storage"]["root_dir"])
|
| 53 |
-
|
| 54 |
-
if "sqlite" in cfg and "path" in cfg["sqlite"]:
|
| 55 |
-
cfg["sqlite"]["path"] = _subst(cfg["sqlite"]["path"])
|
| 56 |
-
|
| 57 |
-
return cfg
|
| 58 |
-
|
| 59 |
-
CFG = _load_yaml("config.yaml")
|
| 60 |
-
|
| 61 |
-
# Ajuste de variables según tu esquema YAML
|
| 62 |
-
DATA_DIR = CFG.get("storage", {}).get("root_dir", "data")
|
| 63 |
-
BACKEND_BASE_URL = CFG.get("api", {}).get("base_url", "http://localhost:8000")
|
| 64 |
-
USE_MOCK = bool(CFG.get("app", {}).get("use_mock", False)) # si no la tienes en el yaml, queda False
|
| 65 |
-
API_TOKEN = CFG.get("api", {}).get("token") or os.getenv("API_SHARED_TOKEN")
|
| 66 |
-
|
| 67 |
-
os.makedirs(DATA_DIR, exist_ok=True)
|
| 68 |
-
ensure_dirs(DATA_DIR)
|
| 69 |
-
DB_PATH = os.path.join(DATA_DIR, "app.db")
|
| 70 |
-
set_db_path(DB_PATH)
|
| 71 |
-
init_schema()
|
| 72 |
-
|
| 73 |
-
# Initialize authentication system and sync default users
|
| 74 |
-
initialize_auth_system(DB_PATH)
|
| 75 |
-
|
| 76 |
-
api = APIClient(BACKEND_BASE_URL, use_mock=USE_MOCK, data_dir=DATA_DIR, token=API_TOKEN)
|
| 77 |
-
|
| 78 |
-
st.set_page_config(page_title="Veureu — Audiodescripció", page_icon="🎬", layout="wide")
|
| 79 |
-
|
| 80 |
-
# Initialize session state for user
|
| 81 |
-
if "user" not in st.session_state:
|
| 82 |
-
st.session_state.user = None
|
| 83 |
-
|
| 84 |
-
# Render sidebar and get navigation
|
| 85 |
-
page, role = render_sidebar()
|
| 86 |
-
|
| 87 |
-
# Pre-login screen
|
| 88 |
-
if not st.session_state.user:
|
| 89 |
-
st.title("Veureu — Audiodescripció")
|
| 90 |
-
render_login_form()
|
| 91 |
-
st.stop()
|
| 92 |
-
|
| 93 |
-
# Post-login: Verificación por móvil si es necesaria
|
| 94 |
-
if st.session_state.user and 'sms_verified' not in st.session_state:
|
| 95 |
-
st.session_state.sms_verified = None
|
| 96 |
-
|
| 97 |
-
permissions = None
|
| 98 |
-
if st.session_state.user:
|
| 99 |
-
username = st.session_state.user['username']
|
| 100 |
-
role = st.session_state.user['role']
|
| 101 |
-
|
| 102 |
-
# Obtener permisos para ver si requiere SMS
|
| 103 |
-
permissions = get_user_permissions(role, st.session_state.get('sms_verified'))
|
| 104 |
-
|
| 105 |
-
# Si requiere SMS y aún no está verificado/omitido, mostrar pantalla de verificación
|
| 106 |
-
if permissions["requires_sms"] and st.session_state.sms_verified is None:
|
| 107 |
-
result = render_mobile_verification_screen(username, role)
|
| 108 |
-
if result is None:
|
| 109 |
-
# Aún en proceso de verificación
|
| 110 |
-
st.stop()
|
| 111 |
-
# Si result es True o False, ya se ha completado/omitido y continúa
|
| 112 |
-
|
| 113 |
-
# --- Pages ---
|
| 114 |
-
if page == "Processar vídeo nou":
|
| 115 |
-
require_login(render_login_form)
|
| 116 |
-
|
| 117 |
-
permissions = get_user_permissions(role, st.session_state.get('sms_verified'))
|
| 118 |
-
if not permissions["procesar_videos"]:
|
| 119 |
-
st.error("No tens permisos per processar nous vídeos. Verifica el teu mòbil per obtenir accés complet.")
|
| 120 |
-
st.stop()
|
| 121 |
-
|
| 122 |
-
render_process_video_page()
|
| 123 |
-
|
| 124 |
-
elif page == "Analitzar video-transcripcions":
|
| 125 |
-
require_login(render_login_form)
|
| 126 |
-
permissions = get_user_permissions(role, st.session_state.get('sms_verified'))
|
| 127 |
-
render_analyze_transcriptions_page(api, permissions)
|
| 128 |
-
|
| 129 |
-
elif page == "Estadístiques":
|
| 130 |
-
require_login(render_login_form)
|
| 131 |
-
render_statistics_page()
|
| 132 |
-
|
| 133 |
-
elif page == "Validació":
|
| 134 |
-
require_login(render_login_form)
|
| 135 |
-
permissions = get_user_permissions(role, st.session_state.get('sms_verified'))
|
| 136 |
-
render_validation_page(compliance_client, runtime_videos, permissions, username)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|