rework organization of project
Browse files- admin/admin_service.py +0 -0
- app.py +5 -218
- config/settings.py +15 -0
- data/contexts.py +16 -0
- data/dataset_manager.py +34 -0
- models/f5tts_model.py +29 -0
- models/kokoro_model.py +0 -0
- models/manager.py +15 -0
- models/omnivoice_model.py +38 -0
- models/parlerTTS_model.py +0 -0
- requirements.txt +0 -0
- services/annotation_service.py +127 -0
- services/audio_service.py +15 -0
admin/admin_service.py
ADDED
|
File without changes
|
app.py
CHANGED
|
@@ -12,12 +12,11 @@ import torch
|
|
| 12 |
from huggingface_hub import snapshot_download
|
| 13 |
import numpy as np
|
| 14 |
|
| 15 |
-
from
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
import tempfile
|
| 21 |
|
| 22 |
timestamp = datetime.now(UTC).isoformat()
|
| 23 |
|
|
@@ -29,219 +28,7 @@ SENTENCES = [
|
|
| 29 |
"Les systèmes vocaux progressent rapidement"
|
| 30 |
]
|
| 31 |
|
| 32 |
-
CONTEXTS = {
|
| 33 |
-
"Train station announcement":
|
| 34 |
-
"Le train 7891 à destination de Paris Nord, prévu à 14h35, partira voie 8.",
|
| 35 |
-
|
| 36 |
-
"Weather forecast":
|
| 37 |
-
"Demain, les températures atteindront 27 degrés avec un ciel dégagé.",
|
| 38 |
-
|
| 39 |
-
"Voice assistant":
|
| 40 |
-
"Je peux vous aider à trouver le restaurant le plus proche.",
|
| 41 |
-
|
| 42 |
-
"Audiobook":
|
| 43 |
-
"Le vieux château se dressait au sommet de la colline depuis plusieurs siècles.",
|
| 44 |
-
|
| 45 |
-
"Customer service":
|
| 46 |
-
"Votre demande a bien été prise en compte et sera traitée sous quarante-huit heures."
|
| 47 |
-
}
|
| 48 |
-
|
| 49 |
-
# ============================
|
| 50 |
-
# CONFIGURATION
|
| 51 |
-
# ============================
|
| 52 |
-
|
| 53 |
-
ADMIN_PASSWORD_HASH = "eb32da077dfaf326cd6f73e0716b628da6427aa318a2d0b9fafa9ef315b5e885"
|
| 54 |
-
|
| 55 |
-
# ============================
|
| 56 |
-
# HF DATASET CONFIGURATION
|
| 57 |
-
# ============================
|
| 58 |
-
|
| 59 |
-
HF_DATASET_NAME = "PaulineDV/TTS_annotations_data" # replace with your HF dataset
|
| 60 |
-
HF_TOKEN = os.environ.get("MyJulySecretToken") # store your token as a secret in Spaces
|
| 61 |
-
login(HF_TOKEN)
|
| 62 |
-
|
| 63 |
-
# ============================
|
| 64 |
-
# MODEL AND GENERATING AUDIO
|
| 65 |
-
# ============================
|
| 66 |
-
|
| 67 |
-
REFERENCE_AUDIO = "references/basic_ref_en.wav"
|
| 68 |
-
|
| 69 |
-
REFERENCE_TEXT = (
|
| 70 |
-
"Some call me nature, others call me mother nature."
|
| 71 |
-
)
|
| 72 |
-
|
| 73 |
-
MODEL_OPTIONS = [
|
| 74 |
-
"OmniVoice",
|
| 75 |
-
"F5-TTS",
|
| 76 |
-
"Qwen3-TTS"
|
| 77 |
-
]
|
| 78 |
-
|
| 79 |
-
print("Loading OmniVoice...")
|
| 80 |
-
|
| 81 |
-
tts_model = OmniVoice.from_pretrained(
|
| 82 |
-
"k2-fsa/OmniVoice",
|
| 83 |
-
device_map="cpu",
|
| 84 |
-
dtype=torch.float32
|
| 85 |
-
)
|
| 86 |
-
|
| 87 |
-
SAMPLING_RATE = tts_model.sampling_rate
|
| 88 |
-
|
| 89 |
-
print("OmniVoice loaded")
|
| 90 |
-
|
| 91 |
-
print("Loading F5-TTS...")
|
| 92 |
-
|
| 93 |
-
#f5tts = F5TTS()
|
| 94 |
-
|
| 95 |
-
print("F5-TTS loaded")
|
| 96 |
-
|
| 97 |
-
def generate_audio(model_name, context):
|
| 98 |
-
|
| 99 |
-
sentence = CONTEXTS[context]
|
| 100 |
-
|
| 101 |
-
if model_name == "OmniVoice":
|
| 102 |
-
return generate_omnivoice(sentence)
|
| 103 |
-
|
| 104 |
-
elif model_name == "F5-TTS":
|
| 105 |
-
return generate_f5(sentence)
|
| 106 |
-
|
| 107 |
-
#elif model_name == "Qwen3-TTS":
|
| 108 |
-
# return generate_qwen(sentence)
|
| 109 |
-
|
| 110 |
-
def generate_omnivoice(sentence):
|
| 111 |
-
|
| 112 |
-
config = OmniVoiceGenerationConfig(
|
| 113 |
-
num_step=32,
|
| 114 |
-
guidance_scale=2.0
|
| 115 |
-
)
|
| 116 |
-
|
| 117 |
-
audio = tts_model.generate(
|
| 118 |
-
text=sentence,
|
| 119 |
-
language="French",
|
| 120 |
-
generation_config=config
|
| 121 |
-
)
|
| 122 |
-
|
| 123 |
-
#convertir l'array en fichier audio
|
| 124 |
-
waveform = (audio[0] * 32767).astype(np.int16)
|
| 125 |
-
|
| 126 |
-
return (SAMPLING_RATE, waveform), {"played": 0}
|
| 127 |
-
|
| 128 |
-
def generate_f5(sentence):
|
| 129 |
-
|
| 130 |
-
output_wav_path = tempfile.mktemp(
|
| 131 |
-
suffix = ".wav"
|
| 132 |
-
)
|
| 133 |
-
|
| 134 |
-
wav, sr, _ = f5tts.infer(
|
| 135 |
-
ref_file = REFERENCE_AUDIO,
|
| 136 |
-
ref_text = REFERENCE_TEXT,
|
| 137 |
-
gen_text = sentence,
|
| 138 |
-
file_wave = output_wav_path,
|
| 139 |
-
remove_silence = False,
|
| 140 |
-
)
|
| 141 |
-
|
| 142 |
-
return output_wav_path, {"played": 0}
|
| 143 |
-
|
| 144 |
-
# ============================
|
| 145 |
-
# USER FUNCTIONS
|
| 146 |
-
# ============================
|
| 147 |
-
api = HfApi(token = HF_TOKEN)
|
| 148 |
-
|
| 149 |
-
def load_hf_dataset():
|
| 150 |
-
"""Load existing HF Dataset or create empty one if not exists."""
|
| 151 |
-
try:
|
| 152 |
-
ds = load_dataset(HF_DATASET_NAME, split="train")
|
| 153 |
-
except:
|
| 154 |
-
# Dataset does not exist yet
|
| 155 |
-
df = pd.DataFrame(columns=["user_id", "gender", "audio_file", "score"])
|
| 156 |
-
ds = Dataset.from_pandas(df)
|
| 157 |
-
ds.push_to_hub(HF_DATASET_NAME, private=True)
|
| 158 |
-
return ds
|
| 159 |
-
|
| 160 |
-
def submit_annotation_hf(
|
| 161 |
-
user_id,
|
| 162 |
-
age_group,
|
| 163 |
-
gender,
|
| 164 |
-
native_language,
|
| 165 |
-
tts_experience,
|
| 166 |
-
device_type,
|
| 167 |
-
selected_model,
|
| 168 |
-
context,
|
| 169 |
-
score,
|
| 170 |
-
context_score
|
| 171 |
-
):
|
| 172 |
-
annotation = {
|
| 173 |
-
"user_id": user_id,
|
| 174 |
-
"age_group": age_group,
|
| 175 |
-
"gender": gender,
|
| 176 |
-
"native_language": native_language,
|
| 177 |
-
"tts_experience": tts_experience,
|
| 178 |
-
"device_type": device_type,
|
| 179 |
-
|
| 180 |
-
"model_name": selected_model,
|
| 181 |
-
"context": context,
|
| 182 |
-
|
| 183 |
-
"score": score,
|
| 184 |
-
"context_score": context_score,
|
| 185 |
-
"timestamp": datetime.now(UTC).isoformat()
|
| 186 |
-
}
|
| 187 |
-
|
| 188 |
-
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 189 |
-
short_id = str(uuid.uuid4()) [:8]
|
| 190 |
-
|
| 191 |
-
file_name = (
|
| 192 |
-
f"{selected_model}_{user_id}_{timestamp}_{short_id}"
|
| 193 |
-
)
|
| 194 |
-
|
| 195 |
-
local_file = f"{file_name}.json"
|
| 196 |
-
|
| 197 |
-
with open(local_file, "w", encoding="utf-8") as f:
|
| 198 |
-
json.dump(annotation, f, indent=2)
|
| 199 |
-
|
| 200 |
-
api.upload_file(
|
| 201 |
-
path_or_fileobj=local_file,
|
| 202 |
-
path_in_repo=f"annotations/{local_file}",
|
| 203 |
-
repo_id=HF_DATASET_NAME,
|
| 204 |
-
repo_type="dataset"
|
| 205 |
-
)
|
| 206 |
-
|
| 207 |
-
os.remove(local_file)
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
def submit_annotation(
|
| 211 |
-
user_id,
|
| 212 |
-
age_group,
|
| 213 |
-
gender,
|
| 214 |
-
native_language,
|
| 215 |
-
tts_experience,
|
| 216 |
-
device_type,
|
| 217 |
-
selected_model,
|
| 218 |
-
context,
|
| 219 |
-
score,
|
| 220 |
-
context_score
|
| 221 |
-
):
|
| 222 |
-
try:
|
| 223 |
-
|
| 224 |
-
submit_annotation_hf(
|
| 225 |
-
user_id,
|
| 226 |
-
age_group,
|
| 227 |
-
gender,
|
| 228 |
-
native_language,
|
| 229 |
-
tts_experience,
|
| 230 |
-
device_type,
|
| 231 |
-
selected_model,
|
| 232 |
-
context,
|
| 233 |
-
score,
|
| 234 |
-
context_score
|
| 235 |
-
)
|
| 236 |
-
|
| 237 |
-
return f"Annotation saved for {context}."
|
| 238 |
-
|
| 239 |
-
except Exception as e:
|
| 240 |
-
print("Submit error")
|
| 241 |
-
print(type(e).__name__)
|
| 242 |
-
print(e)
|
| 243 |
|
| 244 |
-
return f"ERROR: {e}"
|
| 245 |
|
| 246 |
def update_sentence(context):
|
| 247 |
|
|
|
|
| 12 |
from huggingface_hub import snapshot_download
|
| 13 |
import numpy as np
|
| 14 |
|
| 15 |
+
from config.settings import *
|
| 16 |
+
from data.contexts import CONTEXTS
|
| 17 |
+
from data.dataset_manager import *
|
| 18 |
+
from services import audio_service
|
| 19 |
+
|
|
|
|
| 20 |
|
| 21 |
timestamp = datetime.now(UTC).isoformat()
|
| 22 |
|
|
|
|
| 28 |
"Les systèmes vocaux progressent rapidement"
|
| 29 |
]
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
|
|
|
| 32 |
|
| 33 |
def update_sentence(context):
|
| 34 |
|
config/settings.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
HF_DATASET_NAME = "PaulineDV/TTS_annotations_data"
|
| 2 |
+
|
| 3 |
+
REFERENCE_AUDIO = "references/basic_ref_en.wav"
|
| 4 |
+
|
| 5 |
+
REFERENCE_TEXT = (
|
| 6 |
+
"Some call me nature, others call me mother nature."
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
MODEL_OPTIONS = [
|
| 10 |
+
"OmniVoice",
|
| 11 |
+
"F5-TTS",
|
| 12 |
+
"Qwen3-TTS"
|
| 13 |
+
]
|
| 14 |
+
|
| 15 |
+
ADMIN_PASSWORD_HASH = "eb32da077dfaf326cd6f73e0716b628da6427aa318a2d0b9fafa9ef315b5e885"
|
data/contexts.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
CONTEXTS = {
|
| 2 |
+
"Train station announcement":
|
| 3 |
+
"Le train 7891 à destination de Paris Nord, prévu à 14h35, partira voie 8.",
|
| 4 |
+
|
| 5 |
+
"Weather forecast":
|
| 6 |
+
"Demain, les températures atteindront 27 degrés avec un ciel dégagé.",
|
| 7 |
+
|
| 8 |
+
"Voice assistant":
|
| 9 |
+
"Je peux vous aider à trouver le restaurant le plus proche.",
|
| 10 |
+
|
| 11 |
+
"Audiobook":
|
| 12 |
+
"Le vieux château se dressait au sommet de la colline depuis plusieurs siècles.",
|
| 13 |
+
|
| 14 |
+
"Customer service":
|
| 15 |
+
"Votre demande a bien été prise en compte et sera traitée sous quarante-huit heures."
|
| 16 |
+
}
|
data/dataset_manager.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import HfApi
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import os
|
| 5 |
+
import hashlib
|
| 6 |
+
from datasets import Dataset, concatenate_datasets, load_dataset
|
| 7 |
+
from huggingface_hub import login
|
| 8 |
+
import json
|
| 9 |
+
import uuid
|
| 10 |
+
from datetime import datetime, UTC
|
| 11 |
+
import torch
|
| 12 |
+
from huggingface_hub import snapshot_download
|
| 13 |
+
import numpy as np
|
| 14 |
+
|
| 15 |
+
from config.settings import *
|
| 16 |
+
from data.contexts import CONTEXTS
|
| 17 |
+
|
| 18 |
+
HF_TOKEN = os.environ.get("MyJulySecretToken") # store your token as a secret in Spaces
|
| 19 |
+
login(HF_TOKEN)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
api = HfApi(token = HF_TOKEN)
|
| 24 |
+
|
| 25 |
+
def load_hf_dataset():
|
| 26 |
+
"""Load existing HF Dataset or create empty one if not exists."""
|
| 27 |
+
try:
|
| 28 |
+
ds = load_dataset(HF_DATASET_NAME, split="train")
|
| 29 |
+
except:
|
| 30 |
+
# Dataset does not exist yet
|
| 31 |
+
df = pd.DataFrame(columns=["user_id", "gender", "audio_file", "score"])
|
| 32 |
+
ds = Dataset.from_pandas(df)
|
| 33 |
+
ds.push_to_hub(HF_DATASET_NAME, private=True)
|
| 34 |
+
return ds
|
models/f5tts_model.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from f5_tts.api import F5TTS
|
| 2 |
+
import tempfile
|
| 3 |
+
|
| 4 |
+
from config.settings import (
|
| 5 |
+
REFERENCE_AUDIO,
|
| 6 |
+
REFERENCE_TEXT
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
print("Loading F5-TTS...")
|
| 10 |
+
|
| 11 |
+
model = F5TTS()
|
| 12 |
+
|
| 13 |
+
print("F5-TTS loaded")
|
| 14 |
+
|
| 15 |
+
def generate(sentence):
|
| 16 |
+
|
| 17 |
+
output_wav = tempfile.mkstemp(
|
| 18 |
+
suffix = ".wav"
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
model.infer(
|
| 22 |
+
ref_file = REFERENCE_AUDIO,
|
| 23 |
+
ref_text = REFERENCE_TEXT,
|
| 24 |
+
gen_text = sentence,
|
| 25 |
+
file_wave = output_wav,
|
| 26 |
+
remove_silence = False,
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
return output_wav
|
models/kokoro_model.py
ADDED
|
File without changes
|
models/manager.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from models.omnivoice_model import generate as generate_omnivoice
|
| 2 |
+
from models.f5tts_model import generate as generate_f5
|
| 3 |
+
|
| 4 |
+
def generate_audio(model_name, sentence):
|
| 5 |
+
|
| 6 |
+
if model_name == "OmniVoice":
|
| 7 |
+
return generate_omnivoice(sentence)
|
| 8 |
+
|
| 9 |
+
elif model_name == "F5-TTS":
|
| 10 |
+
return generate_f5(sentence)
|
| 11 |
+
|
| 12 |
+
raise ValueError(
|
| 13 |
+
f"Unknown model: {model_name}"
|
| 14 |
+
)
|
| 15 |
+
|
models/omnivoice_model.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
from omnivoice import (
|
| 5 |
+
OmniVoice,
|
| 6 |
+
OmniVoiceGenerationConfig,
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
print("Loading OmniVoice...")
|
| 10 |
+
|
| 11 |
+
tts_model = OmniVoice.from_pretrained(
|
| 12 |
+
"k2-fsa/OmniVoice",
|
| 13 |
+
device_map="cpu",
|
| 14 |
+
dtype=torch.float32
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
SAMPLING_RATE = tts_model.sampling_rate
|
| 18 |
+
|
| 19 |
+
print("OmniVoice loaded")
|
| 20 |
+
|
| 21 |
+
def generate(sentence):
|
| 22 |
+
|
| 23 |
+
config = OmniVoiceGenerationConfig(
|
| 24 |
+
num_step=32,
|
| 25 |
+
guidance_scale=2.0
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
audio = tts_model.generate(
|
| 29 |
+
text=sentence,
|
| 30 |
+
language="French",
|
| 31 |
+
generation_config=config
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
#convertir l'array en fichier audio
|
| 35 |
+
waveform = (audio[0] * 32767).astype(np.int16)
|
| 36 |
+
|
| 37 |
+
return (SAMPLING_RATE, waveform)
|
| 38 |
+
|
models/parlerTTS_model.py
ADDED
|
File without changes
|
requirements.txt
ADDED
|
File without changes
|
services/annotation_service.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import HfApi
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import os
|
| 5 |
+
from datetime import datetime, UTC
|
| 6 |
+
from datasets import Dataset, concatenate_datasets, load_dataset
|
| 7 |
+
from huggingface_hub import login
|
| 8 |
+
import json
|
| 9 |
+
import uuid
|
| 10 |
+
from datetime import datetime, UTC
|
| 11 |
+
import torch
|
| 12 |
+
from huggingface_hub import snapshot_download
|
| 13 |
+
|
| 14 |
+
from config.settings import *
|
| 15 |
+
|
| 16 |
+
HF_TOKEN = os.environ.get("MyJulySecretToken") # store your token as a secret in Spaces
|
| 17 |
+
login(HF_TOKEN)
|
| 18 |
+
|
| 19 |
+
HF_DATASET_NAME = "PaulineDV/TTS_annotations_data"
|
| 20 |
+
|
| 21 |
+
api = HfApi(token = HF_TOKEN)
|
| 22 |
+
|
| 23 |
+
def load_hf_dataset():
|
| 24 |
+
"""Load existing HF Dataset or create empty one if not exists."""
|
| 25 |
+
try:
|
| 26 |
+
ds = load_dataset(HF_DATASET_NAME, split="train")
|
| 27 |
+
except:
|
| 28 |
+
# Dataset does not exist yet
|
| 29 |
+
df = pd.DataFrame(columns=["user_id", "gender", "audio_file", "score"])
|
| 30 |
+
ds = Dataset.from_pandas(df)
|
| 31 |
+
ds.push_to_hub(HF_DATASET_NAME, private=True)
|
| 32 |
+
return ds
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
timestamp = datetime.now(UTC).isoformat()
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def submit_annotation_hf(
|
| 40 |
+
user_id,
|
| 41 |
+
age_group,
|
| 42 |
+
gender,
|
| 43 |
+
native_language,
|
| 44 |
+
tts_experience,
|
| 45 |
+
device_type,
|
| 46 |
+
selected_model,
|
| 47 |
+
context,
|
| 48 |
+
score,
|
| 49 |
+
context_score
|
| 50 |
+
):
|
| 51 |
+
annotation = {
|
| 52 |
+
"user_id": user_id,
|
| 53 |
+
"age_group": age_group,
|
| 54 |
+
"gender": gender,
|
| 55 |
+
"native_language": native_language,
|
| 56 |
+
"tts_experience": tts_experience,
|
| 57 |
+
"device_type": device_type,
|
| 58 |
+
|
| 59 |
+
"model_name": selected_model,
|
| 60 |
+
"context": context,
|
| 61 |
+
|
| 62 |
+
"score": score,
|
| 63 |
+
"context_score": context_score,
|
| 64 |
+
"timestamp": datetime.now(UTC).isoformat()
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 68 |
+
short_id = str(uuid.uuid4()) [:8]
|
| 69 |
+
|
| 70 |
+
file_name = (
|
| 71 |
+
f"{selected_model}_{user_id}_{timestamp}_{short_id}"
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
local_file = f"{file_name}.json"
|
| 75 |
+
|
| 76 |
+
with open(local_file, "w", encoding="utf-8") as f:
|
| 77 |
+
json.dump(annotation, f, indent=2)
|
| 78 |
+
|
| 79 |
+
api.upload_file(
|
| 80 |
+
path_or_fileobj=local_file,
|
| 81 |
+
path_in_repo=f"annotations/{local_file}",
|
| 82 |
+
repo_id=HF_DATASET_NAME,
|
| 83 |
+
repo_type="dataset"
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
os.remove(local_file)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def submit_annotation(
|
| 93 |
+
user_id,
|
| 94 |
+
age_group,
|
| 95 |
+
gender,
|
| 96 |
+
native_language,
|
| 97 |
+
tts_experience,
|
| 98 |
+
device_type,
|
| 99 |
+
selected_model,
|
| 100 |
+
context,
|
| 101 |
+
score,
|
| 102 |
+
context_score
|
| 103 |
+
):
|
| 104 |
+
try:
|
| 105 |
+
|
| 106 |
+
submit_annotation_hf(
|
| 107 |
+
user_id,
|
| 108 |
+
age_group,
|
| 109 |
+
gender,
|
| 110 |
+
native_language,
|
| 111 |
+
tts_experience,
|
| 112 |
+
device_type,
|
| 113 |
+
selected_model,
|
| 114 |
+
context,
|
| 115 |
+
score,
|
| 116 |
+
context_score
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
return f"Annotation saved for {context}."
|
| 120 |
+
|
| 121 |
+
except Exception as e:
|
| 122 |
+
print("Submit error")
|
| 123 |
+
print(type(e).__name__)
|
| 124 |
+
print(e)
|
| 125 |
+
|
| 126 |
+
return f"ERROR: {e}"
|
| 127 |
+
|
services/audio_service.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from data.contexts import CONTEXTS
|
| 2 |
+
from models.manager import generate_audio
|
| 3 |
+
|
| 4 |
+
def generate_audio_from_context(
|
| 5 |
+
model_name,
|
| 6 |
+
context
|
| 7 |
+
):
|
| 8 |
+
sentence = CONTEXTS[context]
|
| 9 |
+
|
| 10 |
+
audio = generate_audio(
|
| 11 |
+
model_name,
|
| 12 |
+
sentence
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
return audio, {"played": 0}
|