File size: 15,265 Bytes
2376414
fea49f2
 
2376414
 
 
 
 
 
 
 
 
 
fea49f2
dbae9aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2376414
 
 
 
fea49f2
2376414
 
fea49f2
 
2376414
 
fea49f2
2376414
fea49f2
2376414
 
 
 
 
fea49f2
2376414
fea49f2
 
0ead6d9
 
4b6c221
 
fea49f2
2376414
fea49f2
 
2376414
 
 
 
fea49f2
2376414
 
 
 
 
 
 
fea49f2
 
2376414
 
 
 
 
fea49f2
2376414
 
32e0546
 
fea49f2
 
 
 
2376414
 
fea49f2
2376414
 
 
 
 
fea49f2
2376414
 
 
 
 
 
 
969158e
fea49f2
2376414
 
 
 
 
 
 
 
fea49f2
 
2376414
 
 
 
fea49f2
 
 
2376414
fea49f2
 
 
 
 
 
2376414
 
fea49f2
2376414
 
fea49f2
2376414
fea49f2
2376414
 
fea49f2
2376414
fea49f2
969158e
2376414
 
fea49f2
2376414
 
 
 
 
 
 
 
 
fea49f2
2376414
 
fea49f2
 
 
 
2376414
 
 
c5ea689
 
fea49f2
 
 
 
2376414
 
fea49f2
2376414
 
888e081
2376414
 
fea49f2
 
 
 
 
 
 
 
 
 
2376414
 
 
 
fea49f2
 
2376414
 
fea49f2
2376414
fea49f2
2376414
 
fea49f2
2376414
 
fea49f2
2376414
fea49f2
2376414
 
fea49f2
 
 
2376414
fea49f2
2376414
 
 
fea49f2
2376414
 
 
 
 
 
fea49f2
 
 
2376414
 
 
 
fea49f2
2376414
 
 
 
fea49f2
2376414
 
 
 
fea49f2
2376414
 
 
 
 
 
 
 
fea49f2
2376414
 
fea49f2
2376414
 
 
 
 
 
fea49f2
 
 
2376414
 
 
 
fea49f2
2376414
 
 
fea49f2
2376414
fea49f2
2376414
 
 
 
 
fea49f2
2376414
 
fea49f2
2376414
fea49f2
2376414
 
 
 
fea49f2
2376414
fea49f2
 
2376414
 
fea49f2
2376414
969158e
 
 
 
 
 
 
fea49f2
 
6098f78
f729219
fea49f2
f729219
2376414
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fea49f2
2376414
fea49f2
2376414
 
 
 
 
 
fea49f2
2376414
 
 
 
 
 
7298123
2376414
 
 
 
 
 
 
 
 
969158e
fea49f2
2376414
 
 
 
 
 
fea49f2
 
 
2376414
888e081
2376414
fea49f2
2376414
 
 
fea49f2
2376414
 
fea49f2
2376414
 
 
 
 
 
 
 
 
 
 
7298123
2376414
 
 
 
 
 
 
 
 
8a7554b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2376414
 
7a6bb2b
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
"""
Clone Vocal - Outil web de clonage vocal base sur Seed-VC (zero-shot).
Interface Gradio en francais, deploye sur HuggingFace Spaces avec ZeroGPU.
"""

import os
import sys
import logging
import tempfile
import shutil

import gradio as gr

# Monkey-patch gradio_client to fix "argument of type 'bool' is not iterable"
try:
    import gradio_client.utils as _gc_utils

    _orig_get_type = _gc_utils.get_type

    def _patched_get_type(schema, *args, **kwargs):
        if not isinstance(schema, dict):
            return "Any"
        return _orig_get_type(schema, *args, **kwargs)

    _gc_utils.get_type = _patched_get_type

    _orig_json_schema = _gc_utils._json_schema_to_python_type

    def _patched_json_schema(schema, *args, **kwargs):
        if not isinstance(schema, dict):
            return "Any"
        return _orig_json_schema(schema, *args, **kwargs)

    _gc_utils._json_schema_to_python_type = _patched_json_schema
    _gc_utils.json_schema_to_python_type = lambda schema, defs=None: _patched_json_schema(
        schema, defs
    )
except Exception:
    pass

# Setup logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
logger = logging.getLogger(__name__)

# Startup: clone Seed-VC
logger.info("Initialisation de l'application...")

from pipeline.setup import setup_seed_vc
from pipeline.storage import init_storage, list_models, download_model, delete_model, get_reference_path

try:
    setup_seed_vc()
except Exception as e:
    logger.error("Erreur lors du setup: {}".format(e))

# Initialize model storage
HF_MODELS_REPO = os.environ.get("HF_MODELS_REPO", "")
if HF_MODELS_REPO:
    init_storage(HF_MODELS_REPO)
    logger.info("Stockage HuggingFace configure: {}".format(HF_MODELS_REPO))

# Import GPU-decorated functions for ZeroGPU detection
from pipeline.training import save_voice_reference, _gpu_warmup
from pipeline.separation import separate_audio
from pipeline.inference import convert_voice


# -- Training Tab --

def train_voice_model(audio_file, model_name, progress=gr.Progress()):
    """Handler: save voice reference."""
    if audio_file is None:
        return "Erreur : Veuillez uploader un fichier audio.", None

    if not model_name or not model_name.strip():
        return "Erreur : Veuillez entrer un nom pour le modele.", None

    model_name = model_name.strip().replace(" ", "_")

    def progress_callback(value, desc):
        progress(value, desc=desc)

    try:
        progress(0.0, desc="Demarrage...")
        pth_path, ref_path = save_voice_reference(
            audio_path=audio_file,
            model_name=model_name,
            progress_callback=progress_callback,
        )

        return "Reference vocale '{}' sauvegardee avec succes !".format(model_name), ref_path

    except Exception as e:
        import traceback
        tb = traceback.format_exc()
        logger.error("Erreur training: {}".format(tb))
        return "Erreur : {}: {}\n\nDetails:\n{}".format(
            type(e).__name__, str(e), tb[-500:]
        ), None


# -- Conversion Tab --

def get_model_choices():
    """Get list of trained model names for dropdown."""
    models = list_models()
    if not models:
        return ["(aucun modele)"]
    return models


def convert_song(
    model_choice,
    song_file,
    pitch,
    similarity,
    diffusion_steps,
    vocal_volume,
    instrumental_volume,
    progress=gr.Progress(),
):
    """Full pipeline: separate + convert + mix."""
    if song_file is None:
        return "Erreur : Veuillez uploader un fichier audio.", None, None, None

    if model_choice == "(aucun modele)" or not model_choice:
        return "Erreur : Veuillez d'abord enregistrer une reference vocale.", None, None, None

    from pipeline.mixing import mix_audio

    try:
        # Step 1: Download model / find reference audio
        progress(0.05, desc="Chargement du modele...")
        pth_path, ref_or_index = download_model(model_choice)
        if not pth_path:
            return "Erreur : Modele '{}' introuvable.".format(model_choice), None, None, None

        # Find the reference audio path
        reference_path = get_reference_path(model_choice)
        if not reference_path:
            return "Erreur : Audio de reference introuvable pour '{}'.".format(model_choice), None, None, None

        # Step 2: Separate vocals from instruments
        progress(0.10, desc="Separation des pistes (Demucs)...")
        vocals_path, instruments_path = separate_audio(song_file)

        progress(0.40, desc="Conversion vocale (Seed-VC)...")

        # Step 3: Convert vocals with Seed-VC
        converted_path = convert_voice(
            audio_path=vocals_path,
            reference_path=reference_path,
            pitch=int(pitch),
            diffusion_steps=int(diffusion_steps),
            similarity=float(similarity),
        )

        progress(0.85, desc="Mixage final...")

        # Step 4: Mix converted vocals with instruments
        final_path = mix_audio(
            vocals_path=converted_path,
            instruments_path=instruments_path,
            vocal_volume=float(vocal_volume),
            instrumental_volume=float(instrumental_volume),
        )

        progress(1.0, desc="Termine !")

        return (
            "Conversion terminee avec succes !",
            vocals_path,
            converted_path,
            final_path,
        )

    except Exception as e:
        import traceback
        tb = traceback.format_exc()
        logger.error("Erreur conversion: {}".format(tb))
        return "Erreur : {}: {}\n\nDetails:\n{}".format(
            type(e).__name__, str(e), tb[-500:]
        ), None, None, None


# -- Models Tab --

def refresh_models():
    """Refresh the model list as HTML."""
    models = list_models()
    if not models:
        return "<p style='color:gray;'>Aucun modele enregistre</p>"
    rows = "".join(
        "<tr><td>{}</td><td>Disponible</td></tr>".format(m) for m in models
    )
    return (
        "<table style='width:100%;border-collapse:collapse;'>"
        "<tr><th style='text-align:left;border-bottom:1px solid #555;padding:8px;'>Nom</th>"
        "<th style='text-align:left;border-bottom:1px solid #555;padding:8px;'>Statut</th></tr>"
        "{}</table>".format(rows)
    )


def delete_selected_model(model_name_to_delete):
    """Delete a model."""
    if not model_name_to_delete or model_name_to_delete == "(aucun modele)":
        return "Veuillez selectionner un modele a supprimer.", refresh_models()
    try:
        delete_model(model_name_to_delete)
        return "Modele '{}' supprime.".format(model_name_to_delete), refresh_models()
    except Exception as e:
        return "Erreur : {}".format(e), refresh_models()


# -- Build Gradio UI --

DESCRIPTION = """
# Clone Vocal

Outil de clonage vocal **zero-shot** base sur **Seed-VC** (Diffusion Transformer).

**Comment utiliser :**
1. **Onglet "Ma voix"** : Uploadez un court extrait de votre voix (3-30 sec) pour creer votre profil vocal
2. **Onglet "Convertir"** : Uploadez un morceau de musique, l'outil remplace la voix par la votre
3. **Onglet "Modeles"** : Gerez vos profils vocaux

> **Zero-shot** : pas d'entrainement necessaire ! Juste 3-30 secondes de votre voix suffisent.
"""

with gr.Blocks(
    title="Clone Vocal",
    theme=gr.themes.Soft(),
) as app:

    gr.Markdown(DESCRIPTION)

    with gr.Tabs():
        # Tab 1: Voice Reference
        with gr.TabItem("Ma voix"):
            gr.Markdown("### Enregistrer votre reference vocale")

            with gr.Row():
                with gr.Column(scale=2):
                    train_audio = gr.Audio(
                        label="Extrait de votre voix (WAV ou MP3, 3-30 secondes)",
                        type="filepath",
                        sources=["upload"],
                    )
                    train_model_name = gr.Textbox(
                        label="Nom du profil",
                        placeholder="ex: ma_voix",
                        max_lines=1,
                    )
                    train_btn = gr.Button(
                        "Sauvegarder",
                        variant="primary",
                        size="lg",
                    )

                with gr.Column(scale=1):
                    train_status = gr.Textbox(
                        label="Statut",
                        interactive=False,
                        lines=3,
                    )
                    train_download = gr.File(
                        label="Fichier de reference",
                        interactive=False,
                    )

            gr.Markdown(
                "**Conseils :**\n"
                "- Utilisez un enregistrement propre (pas de bruit de fond, pas de musique)\n"
                "- Parlez ou chantez naturellement pendant 3 a 30 secondes\n"
                "- Plus l'extrait est long et varie, meilleur sera le resultat\n"
                "- Format WAV ou MP3 accepte"
            )

            train_btn.click(
                fn=train_voice_model,
                inputs=[train_audio, train_model_name],
                outputs=[train_status, train_download],
            )

        # Tab 2: Conversion
        with gr.TabItem("Convertir un morceau"):
            gr.Markdown("### Remplacer la voix d'un morceau par la votre")

            with gr.Row():
                with gr.Column(scale=2):
                    convert_model = gr.Dropdown(
                        choices=get_model_choices(),
                        label="Profil vocal",
                        interactive=True,
                    )
                    refresh_btn = gr.Button("Rafraichir la liste", size="sm")
                    convert_audio = gr.Audio(
                        label="Morceau a convertir (WAV ou MP3)",
                        type="filepath",
                        sources=["upload"],
                    )

                    with gr.Accordion("Parametres avances", open=False):
                        convert_pitch = gr.Slider(
                            minimum=-24,
                            maximum=24,
                            value=0,
                            step=1,
                            label="Transposition (demi-tons)",
                        )
                        convert_similarity = gr.Slider(
                            minimum=0.0,
                            maximum=1.0,
                            value=0.7,
                            step=0.05,
                            label="Similarite vocale (0.5=naturel, 0.7=equilibre, 0.9=plus fidele)",
                        )
                        convert_diffusion = gr.Slider(
                            minimum=5,
                            maximum=100,
                            value=25,
                            step=5,
                            label="Qualite (10=rapide, 25=equilibre, 50=haute qualite)",
                        )
                        convert_vocal_vol = gr.Slider(
                            minimum=0.0,
                            maximum=2.0,
                            value=1.0,
                            step=0.1,
                            label="Volume de la voix",
                        )
                        convert_inst_vol = gr.Slider(
                            minimum=0.0,
                            maximum=2.0,
                            value=1.0,
                            step=0.1,
                            label="Volume des instruments",
                        )

                    convert_btn = gr.Button(
                        "Convertir et mixer",
                        variant="primary",
                        size="lg",
                    )

                with gr.Column(scale=1):
                    convert_status = gr.Textbox(
                        label="Statut",
                        interactive=False,
                        lines=3,
                    )
                    gr.Markdown("**Apercu des pistes :**")
                    preview_vocals = gr.Audio(
                        label="Voix originale (separee)",
                        interactive=False,
                    )
                    preview_converted = gr.Audio(
                        label="Voix convertie",
                        interactive=False,
                    )
                    gr.Markdown("**Resultat final :**")
                    final_output = gr.Audio(
                        label="Morceau final (voix + instruments)",
                        interactive=False,
                    )

            refresh_btn.click(
                fn=lambda: gr.Dropdown(choices=get_model_choices()),
                outputs=[convert_model],
            )

            convert_btn.click(
                fn=convert_song,
                inputs=[
                    convert_model,
                    convert_audio,
                    convert_pitch,
                    convert_similarity,
                    convert_diffusion,
                    convert_vocal_vol,
                    convert_inst_vol,
                ],
                outputs=[convert_status, preview_vocals, preview_converted, final_output],
            )

        # Tab 3: Models
        with gr.TabItem("Mes modeles"):
            gr.Markdown("### Gerer vos profils vocaux")

            models_table = gr.HTML(
                value=refresh_models(),
                label="Modeles enregistres",
            )

            with gr.Row():
                models_refresh_btn = gr.Button("Rafraichir", size="sm")
                models_delete_name = gr.Dropdown(
                    choices=get_model_choices(),
                    label="Modele a supprimer",
                    interactive=True,
                )
                models_delete_btn = gr.Button("Supprimer", variant="stop", size="sm")

            models_delete_status = gr.Textbox(label="Statut", interactive=False)

            models_refresh_btn.click(
                fn=refresh_models,
                outputs=[models_table],
            )
            models_refresh_btn.click(
                fn=lambda: gr.Dropdown(choices=get_model_choices()),
                outputs=[models_delete_name],
            )

            models_delete_btn.click(
                fn=delete_selected_model,
                inputs=[models_delete_name],
                outputs=[models_delete_status, models_table],
            )

        # Tab 4: Debug (temporary)
        with gr.TabItem("Debug GPU"):
            gr.Markdown("### Logs GPU Worker (pour diagnostic)")
            debug_output = gr.Textbox(
                label="Derniers logs GPU",
                interactive=False,
                lines=20,
            )
            debug_btn = gr.Button("Lire les logs", size="sm")

            def read_debug_log():
                log_path = "/home/user/app/debug_gpu.log"
                if os.path.exists(log_path):
                    with open(log_path, "r") as f:
                        return f.read()
                return "Aucun log disponible. Lancez d'abord une conversion."

            debug_btn.click(fn=read_debug_log, outputs=[debug_output])


if __name__ == "__main__":
    app.launch(server_name="0.0.0.0")