File size: 3,577 Bytes
c340098
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import hashlib, shutil, uuid, os
from data.dataset_manager import load_hf_dataset, export_full_dataset, upload_admin_audio, export_admin_audio_annotations
from config.settings import *
from config.app_state import load_state, save_state, ui_state_for_mode
import gradio as gr

UPLOADS_DIR = "uploads"
os.makedirs(UPLOADS_DIR, exist_ok = True)

def hash_password(password: str) -> str:
    return hashlib.sha256(password.encode()).hexdigest()

def admin_login(input_password):
    if hash_password(input_password) == ADMIN_PASSWORD_HASH:
    #if input_password == ADMIN_PASSWORD_HASH:
        #ds = load_hf_dataset()
        #df = ds.to_pandas()
        # For download: write temp CSV
        #temp_csv = "annotations_export.csv"
        #df.to_csv(temp_csv, index=False)
        state = load_state()
        mode_label = f"Mode actuel : {'Audio fichier' if state['mode'] == 'audio_file' else 'TTS'}"
        if state["mode"] == "audio_file":
            mode_label += f" - {state.get('audio_name')} (id: {state.get('audio_id')})"
        return (
            gr.update(visible=True),
            #df,
            #temp_csv,
            gr.update(value="Admin authentication successful."),
            gr.update(value = mode_label)
        )
    else:
        return (
            gr.update(visible=False),
            #None,
            #None,
            gr.update(value="Admin authentication failed."),
            gr.update(value = "")
        )


def set_audio_file_mode(audio_files):
    """Reçoit une liste de chemins de fichiers"""
    if not audio_files:
        return (gr.update(value = 'Aucun dossier sélectionné.'),) + ui_state_for_mode("tts")

    entries = []
    for path in audio_files:
        audio_id = str(uuid.uuid4())[:8]
        audio_name = os.path.splitext(os.path.basename(path))[0]
        dest_path = os.path.abspath(os.path.join(UPLOADS_DIR, f"{audio_id}_{audio_name}.wav"))
        shutil.copy(path, dest_path)

        #Upload sur HF
        upload_admin_audio(dest_path, f"{audio_name}.wav", audio_id)

        entries.append({
            "audio_id": audio_id,
            "audio_name": audio_name,
            "audio_path": dest_path,
            "audio_folder": f"annotations_{audio_name}_{audio_id}",
            "count": 0
        })

    state = {
        "mode": "audio_file",
        "audio_files": entries
    }

    save_state(state)
    return (
        gr.update(value = f"Mode basculé : {len(entries)} audios chargés pour annotation"),
    ) + ui_state_for_mode("audio_file")

def switch_to_tts_mode():
    state = load_state()
    state["mode"] = "tts"
    save_state(state)
    return (gr.update(value = "Mode basculé : TTS (Text-to-Speech)"),) + ui_state_for_mode("tts")

def _audio_file_ui_state():
    hidden = gr.update(visible=False)
    return (
        hidden, hidden,
        hidden,
        hidden,
        hidden,
        hidden,
        hidden,
        gr.update(value = "Display Audio")
    )

def _tts_ui_state():
    visible = gr.update(visible=True)
    return (
        visible, visible,
        visible,
        visible,
        visible,
        visible,
        visible,
        gr.update(value = "Generate Audio")
    )


def export_dataset():
    try:
        zip_path = export_full_dataset()
        return zip_path
    except Exception as e:
        print(f"Export error: {e}")
        return None

def export_admin_audio_dataset():
    try:
        return export_admin_audio_annotations()
    except Exception as e:
        print(f"Export admin audio annotations error: {e}")
        return None