Annotate_Samples / admin /admin_service.py
MeysamSh's picture
new version
c340098
Raw History Blame Contribute Delete
3.58 kB
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