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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
 #!/usr/bin/env python
# Copyright (c) Meta Platforms, Inc. and affiliates
# All rights reserved.
#
# This source code is licensed under the license found in the
# MIT_LICENSE file in the root directory of this source tree.

import os
import pathlib
import tempfile
from pydub import AudioSegment, silence
import gradio as gr
import torch
import torchaudio
from fairseq2.assets import InProcAssetMetadataProvider, asset_store
from fairseq2.data import Collater, SequenceData, VocabularyInfo
from fairseq2.data.audio import (
    AudioDecoder,
    WaveformToFbankConverter,
    WaveformToFbankOutput,
)

from seamless_communication.inference import SequenceGeneratorOptions
from fairseq2.generation import NGramRepeatBlockProcessor
from fairseq2.memory import MemoryBlock
from fairseq2.typing import DataType, Device
from huggingface_hub import snapshot_download
from seamless_communication.inference import BatchedSpeechOutput, Translator, SequenceGeneratorOptions
from seamless_communication.models.generator.loader import load_pretssel_vocoder_model
from seamless_communication.models.unity import (
    UnitTokenizer,
    load_gcmvn_stats,
    load_unity_text_tokenizer,
    load_unity_unit_tokenizer,
)
from torch.nn import Module
from seamless_communication.cli.expressivity.evaluate.pretssel_inference_helper import PretsselGenerator

from utils import LANGUAGE_CODE_TO_NAME

DESCRIPTION = """\
# Seamless Expressive
[SeamlessExpressive](https://github.com/facebookresearch/seamless_communication) is a speech-to-speech translation model that captures certain underexplored aspects of prosody such as speech rate and pauses, while preserving the style of one's voice and high content translation quality.
"""

CACHE_EXAMPLES = os.getenv("CACHE_EXAMPLES") == "1" and torch.cuda.is_available()

CHECKPOINTS_PATH = pathlib.Path(os.getenv("CHECKPOINTS_PATH", "/workspace/seamless_communication/demo/expressive/models"))
if not CHECKPOINTS_PATH.exists():
    snapshot_download(repo_id="facebook/seamless-expressive", repo_type="model", local_dir=CHECKPOINTS_PATH)
    snapshot_download(repo_id="facebook/seamless-m4t-v2-large", repo_type="model", local_dir=CHECKPOINTS_PATH)

# Ensure that we do not have any other environment resolvers and always return
# "demo" for demo purposes.
asset_store.env_resolvers.clear()
asset_store.env_resolvers.append(lambda: "demo")

# Construct an `InProcAssetMetadataProvider` with environment-specific metadata
# that just overrides the regular metadata for "demo" environment. Note the "@demo" suffix.
demo_metadata = [
    {
        "name": "seamless_expressivity@demo",
        "checkpoint": f"file://{CHECKPOINTS_PATH}/m2m_expressive_unity.pt",
        "char_tokenizer": f"file://{CHECKPOINTS_PATH}/spm_char_lang38_tc.model",
    },
    {
        "name": "vocoder_pretssel@demo",
        "checkpoint": f"file://{CHECKPOINTS_PATH}/pretssel_melhifigan_wm-final.pt",
    },
    {
        "name": "seamlessM4T_v2_large@demo",
        "checkpoint": f"file://{CHECKPOINTS_PATH}/seamlessM4T_v2_large.pt",
        "char_tokenizer": f"file://{CHECKPOINTS_PATH}/spm_char_lang38_tc.model",
    },
]

asset_store.metadata_providers.append(InProcAssetMetadataProvider(demo_metadata))

LANGUAGE_NAME_TO_CODE = {v: k for k, v in LANGUAGE_CODE_TO_NAME.items()}


if torch.cuda.is_available():
    device = torch.device("cuda:0")
    dtype = torch.float16
else:
    device = torch.device("cpu")
    dtype = torch.float32


MODEL_NAME = "seamless_expressivity"
VOCODER_NAME = "vocoder_pretssel"

# used for ASR for toxicity
m4t_translator = Translator(
    model_name_or_card="seamlessM4T_v2_large",
    vocoder_name_or_card=None,
    device=device,
    dtype=dtype,
)
unit_tokenizer = load_unity_unit_tokenizer(MODEL_NAME)

_gcmvn_mean, _gcmvn_std = load_gcmvn_stats(VOCODER_NAME)
gcmvn_mean = torch.tensor(_gcmvn_mean, device=device, dtype=dtype)
gcmvn_std = torch.tensor(_gcmvn_std, device=device, dtype=dtype)

translator = Translator(
    MODEL_NAME,
    vocoder_name_or_card=None,
    device=device,
    dtype=dtype,
    apply_mintox=False,
)

text_generation_opts = SequenceGeneratorOptions(
    beam_size=5,
    unk_penalty=torch.inf,
    soft_max_seq_len=(0, 200),
    step_processor=NGramRepeatBlockProcessor(
        ngram_size=10,
    ),
)
m4t_text_generation_opts = SequenceGeneratorOptions(
    beam_size=5,
    unk_penalty=torch.inf,
    soft_max_seq_len=(1, 200),
    step_processor=NGramRepeatBlockProcessor(
        ngram_size=10,
    ),
)

pretssel_generator = PretsselGenerator(
    VOCODER_NAME,
    vocab_info=unit_tokenizer.vocab_info,
    device=device,
    dtype=dtype,
)

decode_audio = AudioDecoder(dtype=torch.float32, device=device)

convert_to_fbank = WaveformToFbankConverter(
    num_mel_bins=80,
    waveform_scale=2**15,
    channel_last=True,
    standardize=False,
    device=device,
    dtype=dtype,
)


def normalize_fbank(data: WaveformToFbankOutput) -> WaveformToFbankOutput:
    fbank = data["fbank"]
    std, mean = torch.std_mean(fbank, dim=0)
    data["fbank"] = fbank.subtract(mean).divide(std)
    data["gcmvn_fbank"] = fbank.subtract(gcmvn_mean).divide(gcmvn_std)
    return data


collate = Collater(pad_value=0, pad_to_multiple=1)


AUDIO_SAMPLE_RATE = 16000
MAX_INPUT_AUDIO_LENGTH = 10  # in seconds


from pydub import AudioSegment

def adjust_audio_duration(input_audio_path, output_audio_path):
    input_audio = AudioSegment.from_file(input_audio_path)
    output_audio = AudioSegment.from_file(output_audio_path)

    input_duration = len(input_audio)
    output_duration = len(output_audio)

    # Calcul de la différence de durée
    duration_diff = input_duration - output_duration

    # Ajout de silence à la fin si l'audio de sortie est plus court
    if duration_diff > 0:
        print("Duration diff : ",duration_diff)
        silence = AudioSegment.silent(duration=duration_diff)
        output_audio += silence

        # Enregistrer l'audio ajusté
        output_audio.export(output_audio_path, format='wav')

    return output_audio_path




import yt_dlp
def dowloadYoutubeAudio(url):
    print("Téléchargement de l'audio YouTube en cours...")
    ydl_opts = {
          'format': 'm4a/bestaudio/best',
        'outtmpl': os.getcwd() + "/audio",  # Mise à jour du chemin de sortie
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'wav',  # Utilisation du format WAV
        }]
    }
    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        error_code = ydl.download([url])

    if error_code == 0:
        print("Sauvegarde du fichier audio...")
        print("download_finished : ", os.getcwd() + "/audio.wav")
    else:
        print("error : Échec du téléchargement...")

    return os.getcwd() + "/audio.wav"


def split_audio(input_audio_path):
    print("Start Split Audio")
    audio = AudioSegment.from_file(input_audio_path)
    silence_thresh = -20  # Seuil de silence
    min_silence_len = 300  # Durée minimale de silence en ms

    chunks = []
    current_chunk = AudioSegment.silent(duration=0)
    for ms in range(0, len(audio), 10):  # Incrément de 10 ms
        segment = audio[ms:ms + 10]
        current_chunk += segment

        if len(current_chunk) >= 8000:  # Si la durée actuelle dépasse 8 secondes
            # Vérifier s'il y a un silence
            if silence.detect_silence(current_chunk[-min_silence_len:], min_silence_len=min_silence_len, silence_thresh=silence_thresh):
                # Couper au silence
                print("Silence détecté, découpage du segment")
                chunks.append(current_chunk)
                current_chunk = AudioSegment.silent(duration=0)

        if len(current_chunk) >= 8900:  # Si la durée dépasse 9,89 secondes
            print("Durée maximale atteinte, découpage du segment")
            chunks.append(current_chunk)
            current_chunk = AudioSegment.silent(duration=0)

    # Ajouter le dernier segment s'il n'est pas vide
    if len(current_chunk) > 0:
        chunks.append(current_chunk)

    print('Nombre de segments valides:', len(chunks))
    return chunks




def remove_prosody_tokens_from_text(text):
    # filter out prosody tokens, there is only emphasis '*', and pause '='
    text = text.replace("*", "").replace("=", "")
    text = " ".join(text.split())
    return text






import torchaudio

AUDIO_SAMPLE_RATE = 16000  # Taux d'échantillonnage standard

def preprocess_audio(input_audio_path: str):
    print("preprocess_audio start")
    print("Audio Path :", input_audio_path)
    audio_segments = split_audio(input_audio_path)
    temp_folder = os.path.join(os.getcwd(), "path_to_temp_folder")
    os.makedirs(temp_folder, exist_ok=True)
    segment_paths = []

    for i, segment in enumerate(audio_segments):
        segment_path = os.path.join(temp_folder, f"segment_{i}.wav")
        segment_audio = segment.get_array_of_samples()
        segment_tensor = torch.tensor(segment_audio).unsqueeze(0).float()
        
        # Rééchantillonnage
        segment_tensor = torchaudio.functional.resample(segment_tensor, orig_freq=segment.frame_rate, new_freq=AUDIO_SAMPLE_RATE)

        torchaudio.save(segment_path, segment_tensor, sample_rate=AUDIO_SAMPLE_RATE)
        segment_paths.append(segment_path)
        print("path for :", segment_path)

    return segment_paths



import os
import torchaudio

# Constante pour le taux d'échantillonnage
AUDIO_SAMPLE_RATE = 16000

def preprocess_audio22(input_audio_path: str):
    print("preprocess_audio start")
    print("Audio Path :", input_audio_path)

    # Appeler split_audio et obtenir les segments
    audio_segments = split_audio(input_audio_path)

    # Créer un dossier temporaire pour stocker les segments
    temp_folder = os.path.join(os.getcwd(), "path_to_temp_folder")
    os.makedirs(temp_folder, exist_ok=True)

    segment_paths = []
    for i, segment in enumerate(audio_segments):
        # Exporter chaque segment dans un fichier temporaire
        temp_segment_path = os.path.join(temp_folder, f"temp_segment_{i}.wav")
        segment.export(temp_segment_path, format="wav")

        # Charger et traiter le segment audio
        arr, org_sr = torchaudio.load(temp_segment_path)
        new_arr = torchaudio.functional.resample(arr, orig_freq=org_sr, new_freq=AUDIO_SAMPLE_RATE)

        # Enregistrer le segment traité
        segment_path = os.path.join(temp_folder, f"segment_{i}.wav")
        torchaudio.save(segment_path, new_arr, sample_rate=AUDIO_SAMPLE_RATE)

        # Ajouter le chemin du segment traité à la liste
        segment_paths.append(segment_path)
        print("Path for :", segment_path)

    return segment_paths


def preprocess_audio222(input_audio_path: str):
    # Appeler split_audio et obtenir les segments
    print("preprocess_audio start")
    print("Audio Path :",input_audio_path)
    audio_segments = split_audio(input_audio_path)
    temp_folder = os.getcwd()+"/path_to_temp_folder"
    os.makedirs(temp_folder, exist_ok=True)
    segment_paths = []
    for i, segment in enumerate(audio_segments):
        segment_path = os.path.join(temp_folder, f"segment_{i}.wav")
        segment.export(segment_path, format="wav")
        segment_paths.append(segment_path)
        print("path for : ",segment_path)
    
    return segment_paths




def process_segment(segment_path, source_language_code, target_language_code):
    # preprocess_audio(segment_path) - cette ligne peut ne pas être nécessaire si le segment est déjà prétraité

    with pathlib.Path(segment_path).open("rb") as fb:
        block = MemoryBlock(fb.read())
        example = decode_audio(block)

    example = convert_to_fbank(example)
    example = normalize_fbank(example)
    example = collate(example)

    # Transcription pour mintox
    source_sentences, _ = m4t_translator.predict(
        input=example["fbank"],
        task_str="S2TT",
        tgt_lang=source_language_code,
        text_generation_opts=m4t_text_generation_opts,
    )
    source_text = str(source_sentences[0])

    prosody_encoder_input = example["gcmvn_fbank"]
    text_output, unit_output = translator.predict(
        example["fbank"],
        "S2ST",
        tgt_lang=target_language_code,
        src_lang=source_language_code,
        text_generation_opts=text_generation_opts,
        unit_generation_ngram_filtering=False,
        duration_factor=1.0,
        prosody_encoder_input=prosody_encoder_input,
        src_text=source_text,
    )
    speech_output = pretssel_generator.predict(
        unit_output.units,
        tgt_lang=target_language_code,
        prosody_encoder_input=prosody_encoder_input,
    )

    # Chemin pour enregistrer l'audio du segment
    segment_output_audio_path = os.path.join(os.getcwd(), "result", f"segment_audio_{os.path.basename(segment_path)}")
    os.makedirs(os.path.dirname(segment_output_audio_path), exist_ok=True)
    
    # Enregistrer l'audio du segment
    torchaudio.save(
        segment_output_audio_path,
        speech_output.audio_wavs[0][0].to(torch.float32).cpu(),
        sample_rate=speech_output.sample_rate,
    )
    segment_output_audio_path = adjust_audio_duration(segment_path, segment_output_audio_path)

    
    text_out = remove_prosody_tokens_from_text(str(text_output[0]))
    print("Audio ici : ",segment_output_audio_path)
    return segment_output_audio_path, text_out


#---------------------------_#


from typing import Tuple

def run2(
    input_audio_path: str,
    source_language: str,
    target_language: str,
) -> Tuple[str, str]:
    target_language_code = LANGUAGE_NAME_TO_CODE[target_language]
    source_language_code = LANGUAGE_NAME_TO_CODE[source_language]

    preprocess_audio(input_audio_path)

    with pathlib.Path(input_audio_path).open("rb") as fb:
        block = MemoryBlock(fb.read())
        example = decode_audio(block)

    example = convert_to_fbank(example)
    example = normalize_fbank(example)
    example = collate(example)

    # get transcription for mintox
    source_sentences, _ = m4t_translator.predict(
        input=example["fbank"],
        task_str="S2TT",  # get source text
        tgt_lang=source_language_code,
        text_generation_opts=m4t_text_generation_opts,
    )
    source_text = str(source_sentences[0])

    prosody_encoder_input = example["gcmvn_fbank"]
    text_output, unit_output = translator.predict(
        example["fbank"],
        "S2ST",
        tgt_lang=target_language_code,
        src_lang=source_language_code,
        text_generation_opts=text_generation_opts,
        unit_generation_ngram_filtering=False,
        duration_factor=1.0,
        prosody_encoder_input=prosody_encoder_input,
        src_text=source_text,  # for mintox check
    )
    speech_output = pretssel_generator.predict(
        unit_output.units,
        tgt_lang=target_language_code,
        prosody_encoder_input=prosody_encoder_input,
    )

    with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as f:
        torchaudio.save(
            f.name,
            speech_output.audio_wavs[0][0].to(torch.float32).cpu(),
            sample_rate=speech_output.sample_rate,
        )

    text_out = remove_prosody_tokens_from_text(str(text_output[0]))

    return f.name, text_out










#---------------------------------------------------------_#
#----------------------------------------------------------#









#----------------------------------------------__#------










#-----------------------#


def run(input_audio_path: str, source_language: str, target_language: str) -> tuple[str, str]:
    target_language_code = LANGUAGE_NAME_TO_CODE[target_language]
    source_language_code = LANGUAGE_NAME_TO_CODE[source_language]
    
    segment_paths = preprocess_audio22(input_audio_path)
    print("preprocess_audio end")
    final_text = ""
    final_audio = AudioSegment.silent(duration=0)


    for segment_path in segment_paths:
        segment_audio_path, segment_text = process_segment(segment_path, source_language_code, target_language_code)
        final_text += segment_text + " "
        segment_audio = AudioSegment.from_file(segment_audio_path)
        final_audio += segment_audio

    output_audio_path = os.path.join(os.getcwd(), "result", "audio.wav")
    os.makedirs(os.path.dirname(output_audio_path), exist_ok=True)
    final_audio.export(output_audio_path, format="wav")

    text_out = remove_prosody_tokens_from_text(final_text.strip())
    
    return output_audio_path, text_out





TARGET_LANGUAGE_NAMES = [
    "English",
    "French",
    "German",
    "Spanish",
]


from flask import Flask, request, jsonify
import torch
import torchaudio

app = Flask(__name__)
# Fonction run adaptée pour Flask
@app.route('/translate', methods=['POST'])
def translate():
    # Récupérer les données de la requête
    data = request.json
    input_audio_path = data['input_audio_path']
    source_language = data['source_language']
    target_language = data['target_language']

    # Exécution de la fonction de traduction
    output_audio_path, output_text = run(input_audio_path, source_language, target_language)

    # Retourner la réponse
    return jsonify({
        'output_audio_path': output_audio_path,
        'output_text': output_text
    })


import os

url = "https://youtu.be/qb_tHWGJOp8?si=10qB2JApy0q3XY76"
input_audio_path = dowloadYoutubeAudio(url)

#input_audio_path = os.getcwd()+"/au1min_Vocals_finale.wav"
source_language = "French"
target_language = "English"
print("Audio à traiter : ",input_audio_path)
output_audio_path, output_text = run(input_audio_path, source_language, target_language)

print("output_audio_path : ",output_audio_path)