File size: 31,029 Bytes
8c06717
 
d85e329
 
9441ab4
7fef4b1
a801789
d1553c7
9441ab4
7fd4bd3
d1553c7
7fef4b1
d9452a6
1ea390e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a6cc6d
1ea390e
 
 
 
 
80ff30d
7ff1066
1ea390e
 
 
 
bc506be
 
 
 
bd2d3be
bc506be
bd2d3be
 
 
 
 
 
bc506be
 
 
7c52cd1
bc506be
e620b13
bc506be
7c52cd1
bc506be
 
 
 
 
 
 
 
 
 
 
 
 
bd2d3be
 
 
bc506be
 
 
 
 
bd2d3be
bc506be
 
 
 
 
 
 
 
bd2d3be
bc506be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ea390e
 
 
 
 
 
 
7f59922
1ea390e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a273093
1ea390e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a273093
1ea390e
 
 
 
 
 
 
 
 
 
 
 
 
 
a90af58
1ea390e
 
 
 
 
 
 
 
 
 
 
 
7f086ae
 
1ea390e
 
 
 
 
 
 
 
 
7d0e0a6
 
 
 
 
 
7f8e3fa
7d0e0a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ea390e
 
 
 
 
 
 
 
 
 
7d0e0a6
 
 
 
 
 
1ea390e
7d0e0a6
1ea390e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d0e0a6
1ea390e
 
 
 
 
7d0e0a6
1ea390e
 
 
 
 
 
7d0e0a6
1ea390e
 
 
 
 
 
7d0e0a6
1ea390e
 
7d0e0a6
1ea390e
 
 
 
 
 
 
 
 
7d0e0a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ea390e
7d0e0a6
 
 
1ea390e
7d0e0a6
 
 
 
 
 
 
1ea390e
7d0e0a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ea390e
7d0e0a6
1ea390e
7d0e0a6
 
1ea390e
7d0e0a6
 
fa5e729
1ea390e
7d0e0a6
 
 
 
 
 
 
1ea390e
7d0e0a6
1ea390e
7d0e0a6
 
 
 
 
 
 
 
 
 
 
 
 
1ea390e
7d0e0a6
1ea390e
 
 
7d0e0a6
1ea390e
 
 
7d0e0a6
 
 
 
 
 
 
 
 
 
 
 
71ab67f
7d0e0a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd1605b
7d0e0a6
 
f3e5cc1
473b810
face690
0dbe183
 
8cfab1d
25942bd
 
 
 
 
 
 
 
b93784b
ed1d65a
 
d5e8ac8
b93784b
8cfab1d
 
 
 
 
 
b93784b
8cfab1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
473b810
7d0e0a6
 
1ea390e
7d0e0a6
f9f38a0
7d0e0a6
 
f9f38a0
fd1605b
f3e5cc1
7d0e0a6
 
473b810
7d0e0a6
4687b44
7d0e0a6
 
 
 
 
 
 
 
749cf45
 
7d0e0a6
e8f1875
 
 
7d0e0a6
 
3232fb7
 
7d0e0a6
 
1ea390e
 
7d0e0a6
1ea390e
 
 
7d0e0a6
1ea390e
7d0e0a6
1ea390e
7d0e0a6
f33e37e
e0dd5e1
36ef269
 
 
 
 
 
9d09e8d
 
1ea390e
d5e8ac8
8cfab1d
7d0e0a6
1ea390e
ed1d65a
 
 
 
 
 
 
 
 
7d0e0a6
ed1d65a
 
8cfab1d
7d0e0a6
8cfab1d
 
 
 
 
 
1ea390e
8cfab1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13d7d81
ed1d65a
 
1ea390e
7d0e0a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b13d3e8
7d0e0a6
 
 
 
 
 
 
 
 
 
1ea390e
 
7d0e0a6
 
1ea390e
7d0e0a6
1ea390e
 
dc93c51
ca2206d
 
 
 
 
d85e329
ca2206d
 
 
609de06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65aee5d
609de06
 
 
 
 
 
 
 
dc93c51
 
 
7ff1066
dc93c51
473b810
 
 
 
 
 
 
 
fa77fbe
f683700
fa77fbe
f683700
b60bf0e
7d0e0a6
71ab67f
4687b44
 
 
7d0e0a6
71ab67f
4687b44
 
 
4986393
7d0e0a6
 
 
 
 
1e1ef94
7d0e0a6
 
473b810
ad16f6e
 
473b810
7d0e0a6
 
 
 
 
d17a96e
7d0e0a6
 
a930a0e
 
7d0e0a6
437c265
 
 
7d0e0a6
fa77fbe
6064962
89a5e52
5d372fb
fa77fbe
bc506be
7d0e0a6
 
 
 
 
 
 
749cf45
7d0e0a6
 
 
 
 
1f1eee1
7d0e0a6
 
 
 
 
 
 
 
 
 
 
 
 
1f1eee1
7d0e0a6
80ff30d
7d0e0a6
6031810
7d0e0a6
 
6031810
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
import spaces

import gradio as gr
GK=0
from transformers import AutoTokenizer
import torch
import os

from VitsModelSplit.vits_model2 import VitsModel,get_state_grad_loss
import VitsModelSplit.monotonic_align  as  monotonic_align
from VitsModelSplit.Vits_models_only_decoder import Vits_models_only_decoder 
token=os.environ.get("key_")
# import VitsModelSplit.monotonic_align as monotonic_align
from IPython.display import clear_output
from transformers import set_seed
import wandb
import logging
import copy
import torch

import numpy as np
import torch
from datasets import DatasetDict,Dataset

import os

from VitsModelSplit.vits_model2 import VitsModel,get_state_grad_loss
from VitsModelSplit.PosteriorDecoderModel import PosteriorDecoderModel
from VitsModelSplit.feature_extraction import VitsFeatureExtractor
from VitsModelSplit.vits_models_only_decoder imports Vits_models_only_decoder 
from transformers import AutoTokenizer, HfArgumentParser, set_seed
from VitsModelSplit.Arguments import DataTrainingArguments, ModelArguments, VITSTrainingArguments
from  VitsModelSplit.dataset_features_collector import FeaturesCollectionDataset
from torch.cuda.amp import autocast, GradScaler
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")


# sgl=get_state_grad_loss(k1=True,#generator=False,
#                         discriminator=False,
#                         duration=False
# )
class model_onxx:
    def __init__(self):
        self.model=None
        self.n_onxx=""
        self.storage_dir = "uploads"  
        pass
     

 


   
    def function_change(self,n_model,token,n_onxx,choice):
        if choice=="decoder":

            V=self.convert_to_onnx_only_decoder(n_model,token,n_onxx)
        elif choice=="all only decoder":
            V=self.convert_to_onnx_only_decoder(n_model,token,n_onxx)
        else:
            V=self.convert_to_onnx_only_decoder(n_model,token,n_onxx)
        return V

    def install_model(self,n_model,token,n_onxx):
        self.n_onxx=n_onxx
        self.model= VitsModel.from_pretrained(n_model,token=token)
        return self.model
    def convert_model_decoder_onxx(self,n_model,token,namemodelonxx):
        self.model= VitsModel.from_pretrained(n_model,token=token)
        x=f"{namemodelonxx}.onnx"
        return x
    def convert_to_onnx_only_decoder(self,n_model,token,namemodelonxx):
          model=VitsModel.from_pretrained(n_model,token=token)
          x=f"{namemodelonxx}.onnx"
          if not os.path.exists("uploads"):
             os.makedirs(storage_dir)
          file_path = os.path.join("uploads",x)
          vocab_size = model.text_encoder.embed_tokens.weight.size(0)
          example_input = torch.randint(0, vocab_size, (1, 100), dtype=torch.long)
          torch.onnx.export(
              model,  # The model to be exported
              example_input,  # Example input for the model
              file_path,  # The filename for the exported ONNX model
              opset_version=11,  # Use an appropriate ONNX opset version
              input_names=['input'],  # Name of the input layer
              output_names=['output'],  # Name of the output layer
              dynamic_axes={
                  'input': {0: 'batch_size', 1: 'sequence_length'},  # Dynamic axes for variable-length inputs
                  'output': {0: 'batch_size'}
              }
           )
          return file_path
    def convert_to_onnx_all(self,n_model,token ,namemodelonxx):

          model=VitsModel.from_pretrained(n_model,token=token)
          x=f"{namemodelonxx}.onnx"

          vocab_size = model.text_encoder.embed_tokens.weight.size(0)
          example_input = torch.randint(0, vocab_size, (1, 100), dtype=torch.long)
          torch.onnx.export(
              model,  # The model to be exported
              example_input,  # Example input for the model
              x,  # The filename for the exported ONNX model
              opset_version=11,  # Use an appropriate ONNX opset version
              input_names=['input'],  # Name of the input layer
              output_names=['output'],  # Name of the output layer
              dynamic_axes={
                  'input': {0: 'batch_size', 1: 'sequence_length'},  # Dynamic axes for variable-length inputs
                  'output': {0: 'batch_size'}
              }
           )
          return x
    def starrt(self):
        #with gr.Blocks() as demo:
            with gr.Row():
                  with gr.Column():
                    text_n_model=gr.Textbox(label="name model")
                    text_n_token=gr.Textbox(label="token")
                    text_n_onxx=gr.Textbox(label="name model onxx")
                    choice = gr.Dropdown(choices=["decoder", "all anoly decoder", "All"], label="My Dropdown")

                  with gr.Column():

                    btn=gr.Button("convert")
                    label=gr.Label("return  name model onxx")
                    btn.click(self.function_change,[text_n_model,text_n_token,text_n_onxx,choice],[label])
                    #choice.change(fn=function_change, inputs=choice, outputs=label)
        #return demo
c=model_onxx()
#cc=c.starrt()
###############################################################
Lst=['input_ids',
 'attention_mask',
 'waveform',
 'labels',
 'labels_attention_mask',
 'mel_scaled_input_features']
def covert_cuda_batch(d):
  return d
  for key in Lst:
      d[key]=d[key].cuda(non_blocking=True)
  # for key in d['text_encoder_output']:
  #   d['text_encoder_output'][key]=d['text_encoder_output'][key].cuda(non_blocking=True)
  # for key in d['posterior_encode_output']:
  #   d['posterior_encode_output'][key]=d['posterior_encode_output'][key].cuda(non_blocking=True)

  return d
def generator_loss(disc_outputs):
    total_loss = 0
    gen_losses = []
    for disc_output in disc_outputs:
        disc_output = disc_output
        loss = torch.mean((1 - disc_output) ** 2)
        gen_losses.append(loss)
        total_loss += loss

    return total_loss, gen_losses

def discriminator_loss(disc_real_outputs, disc_generated_outputs):
    loss = 0
    real_losses = 0
    generated_losses = 0
    for disc_real, disc_generated in zip(disc_real_outputs, disc_generated_outputs):
        real_loss = torch.mean((1 - disc_real) ** 2)
        generated_loss = torch.mean(disc_generated**2)
        loss += real_loss + generated_loss
        real_losses += real_loss
        generated_losses += generated_loss

    return loss, real_losses, generated_losses

def feature_loss(feature_maps_real, feature_maps_generated):
    loss = 0
    for feature_map_real, feature_map_generated in zip(feature_maps_real, feature_maps_generated):
        for real, generated in zip(feature_map_real, feature_map_generated):
            real = real.detach()
            loss += torch.mean(torch.abs(real - generated))

    return loss * 2


def kl_loss(z_p, logs_q, m_p, logs_p, z_mask):
  """
  z_p, logs_q: [b, h, t_t]
  m_p, logs_p: [b, h, t_t]
  """
  z_p = z_p.float()
  logs_q = logs_q.float()
  m_p = m_p.float()
  logs_p = logs_p.float()
  z_mask = z_mask.float()

  kl = logs_p - logs_q - 0.5
  kl += 0.5 * ((z_p - m_p)**2) * torch.exp(-2. * logs_p)
  kl = torch.sum(kl * z_mask)
  l = kl / torch.sum(z_mask)
  return l
#.............................................
# def kl_loss(prior_latents, posterior_log_variance, prior_means, prior_log_variance, labels_mask):


#   kl = prior_log_variance - posterior_log_variance - 0.5
#   kl += 0.5 * ((prior_latents - prior_means) ** 2) * torch.exp(-2.0 * prior_log_variance)
#   kl = torch.sum(kl * labels_mask)
#   loss = kl / torch.sum(labels_mask)
#   return loss

def get_state_grad_loss(k1=True,
                             mel=True,
                             duration=True,
                             generator=True,
                             discriminator=True):
                             return {'k1':k1,'mel':mel,'duration':duration,'generator':generator,'discriminator':discriminator}

@spaces.GPU
def clip_grad_value_(parameters, clip_value, norm_type=2):
  if isinstance(parameters, torch.Tensor):
    parameters = [parameters]
  parameters = list(filter(lambda p: p.grad is not None, parameters))
  norm_type = float(norm_type)
  if clip_value is not None:
    clip_value = float(clip_value)

  total_norm = 0
  for p in parameters:
    param_norm = p.grad.data.norm(norm_type)
    total_norm += param_norm.item() ** norm_type
    if clip_value is not None:
      p.grad.data.clamp_(min=-clip_value, max=clip_value)
  total_norm = total_norm ** (1. / norm_type)
  return total_norm

@spaces.GPU
def get_embed_speaker(self,speaker_id):
     if self.config.num_speakers > 1 and speaker_id is not None:
          if isinstance(speaker_id, int):
              speaker_id = torch.full(size=(1,), fill_value=speaker_id, device=self.device)
          elif isinstance(speaker_id, (list, tuple, np.ndarray)):
              speaker_id = torch.tensor(speaker_id, device=self.device)

          if not ((0 <= speaker_id).all() and (speaker_id < self.config.num_speakers).all()).item():
              raise ValueError(f"Set `speaker_id` in the range 0-{self.config.num_speakers - 1}.")


          return self.embed_speaker(speaker_id).unsqueeze(-1)
     else:
          return None

def  get_data_loader(train_dataset_dirs,eval_dataset_dir,full_generation_dir,device):
     ctrain_datasets=[]
     for  dataset_dir ,id_sp in train_dataset_dirs:
        train_dataset = FeaturesCollectionDataset(dataset_dir = os.path.join(dataset_dir,'train'),
                                                  device = device
                                                  )
        ctrain_datasets.append((train_dataset,id_sp))




     eval_dataset = None
     
     eval_dataset = FeaturesCollectionDataset(dataset_dir = eval_dataset_dir,
                                                  device = device
                                                  )

     full_generation_dataset = FeaturesCollectionDataset(dataset_dir = full_generation_dir,
                                                        device = device)
     return ctrain_datasets,eval_dataset,full_generation_dataset
global_step=0


def  train_step(batch,models=[],optimizers=[], training_args=None,tools=[]):
     self,discriminator=models
     optimizer,disc_optimizer,scaler=optimizers
     feature_extractor,maf,dict_state_grad_loss=tools
     
     with autocast(enabled=training_args.fp16):
          speaker_embeddings=get_embed_speaker(self,batch["speaker_id"])
          waveform,ids_slice,log_duration,prior_latents,posterior_log_variances,prior_means,prior_log_variances,labels_padding_mask = self.forward_train(
              input_ids=batch["input_ids"],
              attention_mask=batch["attention_mask"],
              labels=batch["labels"],
              labels_attention_mask=batch["labels_attention_mask"],
              text_encoder_output =None ,
              posterior_encode_output=None  ,
              return_dict=True,
              monotonic_alignment_function=maf,
              speaker_embeddings=speaker_embeddings
                                          
          )
          mel_scaled_labels = batch["mel_scaled_input_features"]
          mel_scaled_target = self.slice_segments(mel_scaled_labels, ids_slice,self.segment_size)
          mel_scaled_generation = feature_extractor._torch_extract_fbank_features(waveform.squeeze(1))[1]

          target_waveform = batch["waveform"].transpose(1, 2)
          target_waveform = self.slice_segments(
                              target_waveform,
                              ids_slice * feature_extractor.hop_length,
                              self.config.segment_size
                          )

          discriminator_target, fmaps_target = discriminator(target_waveform)
          discriminator_candidate, fmaps_candidate = discriminator(waveform.detach())
          with autocast(enabled=False):
              if dict_state_grad_loss['discriminator']:


                    loss_disc, loss_real_disc, loss_fake_disc = discriminator_loss(
                        discriminator_target, discriminator_candidate
                    )

                    loss_dd = loss_disc# + loss_real_disc + loss_fake_disc

              #  loss_dd.backward()

     disc_optimizer.zero_grad()
     scaler.scale(loss_dd).backward()
     scaler.unscale_(disc_optimizer )
     grad_norm_d = clip_grad_value_(discriminator.parameters(), None)
     scaler.step(disc_optimizer)
     loss_des=grad_norm_d

     with autocast(enabled=training_args.fp16):

              # backpropagate

              discriminator_target, fmaps_target = discriminator(target_waveform)

              discriminator_candidate, fmaps_candidate = discriminator(waveform.detach())
              with autocast(enabled=False):
                if dict_state_grad_loss['k1']:
                  loss_kl = kl_loss(
                      prior_latents,
                     posterior_log_variances,
                      prior_means,
                     prior_log_variances,
                     labels_padding_mask,
                  )
                  loss_kl=loss_kl*training_args.weight_kl
                  loss_klall=loss_kl.detach().item()
                  #if displayloss['loss_kl']>=0:
                    #  loss_kl.backward()

                if dict_state_grad_loss['mel']:
                    loss_mel = torch.nn.functional.l1_loss(mel_scaled_target, mel_scaled_generation)*training_args.weight_mel
                    loss_melall= loss_mel.detach().item()
                    # train_losses_sum = train_losses_sum + displayloss['loss_mel']
                  # if displayloss['loss_mel']>=0:
                      #  loss_mel.backward()

                if dict_state_grad_loss['duration']:
                    loss_duration=torch.sum(log_duration)*training_args.weight_duration
                    loss_durationsall=loss_duration.detach().item()
                  #  if displayloss['loss_duration']>=0:
                    #   loss_duration.backward()
                if dict_state_grad_loss['generator']:
                  loss_fmaps = feature_loss(fmaps_target, fmaps_candidate)
                  loss_gen, losses_gen = generator_loss(discriminator_candidate)
                  loss_gen=loss_gen * training_args.weight_gen
                 
              #   loss_gen.backward(retain_graph=True)
                  loss_fmaps=loss_fmaps * training_args.weight_fmaps
                 
              #   loss_fmaps.backward(retain_graph=True)
                  total_generator_loss = (
                      loss_duration
                      + loss_mel
                      + loss_kl
                      + loss_fmaps
                      + loss_gen
                  )
                  # total_generator_loss.backward()
     optimizer.zero_grad()
     scaler.scale(total_generator_loss).backward()
     scaler.unscale_(optimizer)
     grad_norm_g = clip_grad_value_(self.parameters(), None)
     scaler.step(optimizer)
     scaler.update()
     loss_gen=grad_norm_g

     return loss_gen,loss_des,loss_durationsall,loss_melall,loss_klall



def  train_epoch(obtrainer,index_db=0,epoch=0,idspeakers=[],full_generation_sample_index=-1):
     train_losses_sum = 0
     loss_genall=0
     loss_desall=0
     loss_durationsall=0
     loss_melall=0
     loss_klall=0
     loss_fmapsall=0
     start_speeker,end_speeker=idspeakers
     

     datatrain=obtrainer.DataSets['train'][index_db]
     lr_scheduler,disc_lr_scheduler=obtrainer.lr_schedulers
     lr_scheduler.step()

     disc_lr_scheduler.step()
     train_dataset,speaker_id=datatrain
     print(f"  Num Epochs = {epoch}, speaker_id DB ={speaker_id}")
     num_div_proc=int(len(train_dataset)/10)+1
     print('       -process traning : [',end='')
     full_generation_sample =obtrainer.DataSets['full_generation'][full_generation_sample_index]
     

      
     for step, batch in enumerate(train_dataset):
          loss_gen,loss_des,loss_durationsa,loss_mela,loss_kl=train_step(batch,
                                                                                models=obtrainer.models,
                                                                                optimizers=obtrainer.optimizers,
                                                                                training_args=obtrainer.training_args,
                                                                                tools=obtrainer.tools)
          loss_genall+=loss_gen
          loss_desall+=loss_des
          loss_durationsall+=loss_durationsa
          loss_melall+=loss_mela
          loss_klall+=loss_kl
          
          obtrainer.global_step +=1
          if step%num_div_proc==0:
                   print('==',end='')

          # validation

          do_eval = obtrainer.training_args.do_eval and (obtrainer.global_step % obtrainer.training_args.eval_steps == 0)
          

          if do_eval:
                speaker_id_c=int(torch.randint(start_speeker,end_speeker,size=(1,))[0])
                model=obtrainer.models[0]

                with torch.no_grad():
                   
                    full_generation =model.forward(
                      input_ids =full_generation_sample["input_ids"],
                      attention_mask=full_generation_sample["attention_mask"],
                      speaker_id=speaker_id_c
                      )

                    full_generation_waveform = full_generation.waveform.cpu().numpy()

                wandb.log({
                "full generations samples": [
                    wandb.Audio(w.reshape(-1), caption=f"Full generation sample {epoch}", sample_rate=16000)
                    for w in full_generation_waveform],})
     step+=1
     # wandb.log({"train_losses":loss_melall})
     wandb.log({"loss_gen":loss_genall/step})
     wandb.log({"loss_des":loss_desall/step})
     wandb.log({"loss_duration":loss_durationsall/step})
     wandb.log({"loss_mel":loss_melall/step})
     wandb.log({f"loss_kl_db{speaker_id}":loss_klall/step})
     print(']',end='')
      

     



      



def load_training_args(path):
    parser = HfArgumentParser((ModelArguments, DataTrainingArguments, VITSTrainingArguments))
    json_file = os.path.abspath(path)
    model_args, data_args, training_args = parser.parse_json_file(json_file = json_file)
    return training_args
def load_tools():
    feature_extractor = VitsFeatureExtractor()
    dict_state_grad_loss=get_state_grad_loss()
    return feature_extractor,monotonic_align.maximum_path,dict_state_grad_loss


class TrinerModelVITS:
    KC=0
    def __init__(self,dir_model="",
                 path_training_args="",
                 train_dataset_dirs=[],
                 eval_dataset_dir="",
                 full_generation_dir="",
                 token="",
              

                 device="cpu"):
        self.device=device
        self.dir_model=dir_model
        self.path_training_args=path_training_args
        self.stute_mode=False
        self.token=token
    
        self.load_dataset(train_dataset_dirs,eval_dataset_dir,full_generation_dir)
        self.epoch_count=0
        self.global_step=0
        self.len_dataset=len(self.DataSets['train'])
        self.load_model()
        self.init_wandb()
        # self.training_args=load_training_args(self.path_training_args)
        # training_args= self.training_args
        scaler = GradScaler(enabled=True)
        # for disc in self.model.discriminator.discriminators:
        #     disc.apply_weight_norm()
        # self.model.decoder.apply_weight_norm()
        # # torch.nn.utils.weight_norm(self.decoder.conv_pre)
        # # torch.nn.utils.weight_norm(self.decoder.conv_post)
        # for flow in self.model.flow.flows:
        #     torch.nn.utils.weight_norm(flow.conv_pre)
        #     torch.nn.utils.weight_norm(flow.conv_post)

        discriminator = self.model.discriminator
        self.model.discriminator = None
        self.models=(self.model,discriminator)

        optimizer = torch.optim.AdamW(
            self.model.parameters(),
            2e-4,
            betas=[0.8, 0.99],
           # eps=training_args.adam_epsilon,
        )

        # Hack to be able to train on multiple device
        disc_optimizer = torch.optim.AdamW(
            discriminator.parameters(),
             2e-4,
            betas=[0.8, 0.99],
          #  eps=training_args.adam_epsilon,
        )
        lr_scheduler = torch.optim.lr_scheduler.ExponentialLR(
            optimizer,gamma=0.999875, last_epoch=-1
        )
        disc_lr_scheduler = torch.optim.lr_scheduler.ExponentialLR(
            disc_optimizer, gamma=0.999875,last_epoch=-1
        )
      #  self.models=(self.model,discriminator)
        self.optimizers=(optimizer,disc_optimizer,scaler)
        self.lr_schedulers=(lr_scheduler,disc_lr_scheduler)
        self.tools=load_tools()
        self.stute_mode=True
        print(self.lr_schedulers)
        
       
        

    def init_Starting(self):
        print('init_Starting')
        self.training_args=load_training_args(self.path_training_args)
        self.stute_mode=False
        print('end training_args')
        
        
    def init_training(self):
        
        
        self.initialize_training_components()
    #    self.epoch_count=0
        
  
    def load_model(self):
        self.model=VitsModel.from_pretrained(self.dir_model,token=self.token).to(self.device)
        self.model.setMfA(monotonic_align.maximum_path)
   
    def init_wandb(self):
        wandb.login(key= "782b6a6e82bbb5a5348de0d3c7d40d1e76351e79")
        #config = self.training_args.to_dict()
        wandb.init(project= 'HugfaceTraining')
   
    def load_modell(self,namemodel):
         self.model=VitsModel.from_pretrained(namemodel,token=self.token).to(self.device)
         return "true"
    def load_dataset(self,train_dataset_dirs,eval_dataset_dir,full_generation_dir):
       ctrain_datasets,eval_dataset,full_generation_dataset=get_data_loader(train_dataset_dirs = train_dataset_dirs,
                                                              eval_dataset_dir =eval_dataset_dir ,
                                                            full_generation_dir =full_generation_dir ,
                                                                     device=self.device)
       self.DataSets={'train':ctrain_datasets,'eval':eval_dataset,'full_generation':full_generation_dataset}


    



    

    def initialize_training_components(self):

        
        self.training_args=load_training_args(self.path_training_args)
        training_args= self.training_args
        training_args.weight_kl=1
        training_args.d_learning_rate=2e-4
        training_args.learning_rate=2e-4
        training_args.weight_mel=45
        training_args.num_train_epochs=4
        training_args.eval_steps=1000
        training_args.fp16=True


        set_seed(training_args.seed)
        # scaler = GradScaler(enabled=training_args.fp16)
        

        # # Initialize optimizer, lr_scheduler
        # for disc in self.model.discriminator.discriminators:
        #     disc.apply_weight_norm()
        # self.model.decoder.apply_weight_norm()
        # # torch.nn.utils.weight_norm(self.decoder.conv_pre)
        # # torch.nn.utils.weight_norm(self.decoder.conv_post)
        # for flow in self.model.flow.flows:
        #     torch.nn.utils.weight_norm(flow.conv_pre)
        #     torch.nn.utils.weight_norm(flow.conv_post)

        # discriminator = self.model.discriminator
        # self.model.discriminator = None
        # model,discriminator=self.models

        # optimizer = torch.optim.AdamW(
        #     model.parameters(),
        #     training_args.learning_rate,
        #     betas=[training_args.adam_beta1, training_args.adam_beta2],
        #     eps=training_args.adam_epsilon,
        # )

        # # Hack to be able to train on multiple device
        # disc_optimizer = torch.optim.AdamW(
        #     discriminator.parameters(),
        #     training_args.d_learning_rate,
        #     betas=[training_args.d_adam_beta1, training_args.d_adam_beta2],
        #     eps=training_args.adam_epsilon,
        # )
        # lr_scheduler = torch.optim.lr_scheduler.ExponentialLR(
        #     optimizer, gamma=training_args.lr_decay, last_epoch=-1
        # )
        # disc_lr_scheduler = torch.optim.lr_scheduler.ExponentialLR(
        #     disc_optimizer, gamma=training_args.lr_decay, last_epoch=-1
        # )
        # # self.models=(self.model,discriminator)
        # self.optimizers=(optimizer,disc_optimizer,scaler)
        # self.lr_schedulers=(lr_scheduler,disc_lr_scheduler)
        # self.tools=load_tools()
        # self.stute_mode=True
        # print(self.lr_schedulers)
       

    

    
    def save_pretrained(self,path_save_model):
        
        model,discriminator=self.models
       
        model.discriminator=discriminator
        for disc in model.discriminator.discriminators:
              disc.remove_weight_norm()
        model.decoder.remove_weight_norm()
        # torch.nn.utils.remove_weight_norm(self.decoder.conv_pre)
        # torch.nn.utils.remove_weight_norm(self.decoder.conv_post)
        for flow in model.flow.flows:
                torch.nn.utils.remove_weight_norm(flow.conv_pre)
                torch.nn.utils.remove_weight_norm(flow.conv_post)

        model.push_to_hub(path_save_model,token=self.token)


    def  run_train_epoch(self):
          index_db=self.epoch_count%self.len_dataset
          train_epoch(self,index_db=index_db,epoch=self.epoch_count,idspeakers=(0,1),full_generation_sample_index=-1)
          self.epoch_count+=1
          return f'epoch_count:{self.epoch_count},global_step:{self.global_step},index_db"{index_db}'
          
          
          


        
        # return (self.model,discriminator),(optimizer, disc_optimizer), (lr_scheduler, disc_lr_scheduler)

  


    # logger.info("***** Training / Inference Done *****")
def   modelspeech(texts):
     
    
    
     inputs = tokenizer(texts, return_tensors="pt")#.cuda()

     wav = model_vits(input_ids=inputs["input_ids"]).waveform#.detach()
          # display(Audio(wav, rate=model.config.sampling_rate))
     return  model_vits.config.sampling_rate,wav#remove_noise_nr(wav)

dataset_dir='ABThag-db'
train_dataset_dirs=[
       #   ('/content/drive/MyDrive/vitsM/DATA/fahd_db',0),
          # ('/content/drive/MyDrive/vitsM/DATA/fahd_db',0),
        #   ('/content/drive/MyDrive/vitsM/DB2KKKK',1),
        #  ('/content/drive/MyDrive/vitsM/DATA/Db_Amgd_50_Bitch10',0),
            # ('/content/drive/MyDrive/vitsM/DB2KKKK',1), #
        #  ('/content/drive/MyDrive/vitsM/DATA/Db_Amgd_50_Bitch10',0),
          # ('/content/drive/MyDrive/vitsM/DATA/DBWfaa-Bitch:8-Count:60',0),
      #  ('/content/drive/MyDrive/vitsM/DATA/Wafa/b10r',0),
      #  ('/content/drive/MyDrive/vitsM/DATA/Wafa/b16r',0),
      #  ('/content/drive/MyDrive/vitsM/DATA/Wafa/b4',0),

          # ('/content/drive/MyDrive/vitsM/DATA/fahd_db',None),
          # ('/content/drive/MyDrive/vitsM/DATA/wafa-db',None),
          # ('/content/drive/MyDrive/vitsM/DATA/wafa-db',4),
          # ('/content/drive/MyDrive/vitsM/DATA/DB-ABThag-Bitch:5-Count-37',4),
         # ('/content/drive/MyDrive/vitsM/DB-300-k',6),
         ('databatchs',0),
         #('/content/drive/MyDrive/dataset_ljBatchs',0),





                    ]





dir_model='wasmdashai/vits-ar-huba-fine'
pro=TrinerModelVITS(dir_model=dir_model,
                    path_training_args='VitsModelSplit/finetune_config_ara.json',
                    train_dataset_dirs = train_dataset_dirs,
                                                              eval_dataset_dir = os.path.join(dataset_dir,'eval'),
                                                            full_generation_dir = os.path.join(dataset_dir,'full_generation'),
                                                            token=token,
                                                                     device=device
                    )
def loadd_d():
    token=os.environ.get("key_")
    #model=VitsModel.from_pretrained(n_model,token=token)
    return token
@spaces.GPU(duration=30)
def run_train_epoch(num):
   TrinerModelVITS.KC+=1
   if num >0:
     pro.init_training()
     for i in range(num):
    # model.train(True)
        return  pro.run_train_epoch() +f'- kc={TrinerModelVITS.KC}'
   else:
      pro.save_pretrained(pro.dir_model)
      pro.load_model()
   return 'save model '
       
@spaces.GPU
def init_training():
    pro.init_training()
    return pro.dir_model,'init_training'

@spaces.GPU
def init_Starting():
     
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    
    return 'init_Starting'
@spaces.GPU
def init_wandb():
    pro.init_wandb()
    return 'init_wandb'

def save_pretrained(path):
    pro.save_pretrained(path)
    
    pro.load_model()
    return 'save_pretrained'
def read_modell(n_model):
    #model22=Vits_models_only_decoder.from_pretrained(n_model,token)#.to("cuda")
    return token
with gr.Blocks() as interface:
    with gr.Accordion("get token", open=False):
        btn_init = gr.Button("run")
        label=gr.Label("hhh")
        btn_init.click(loadd_d,[],[label])
    with gr.Accordion("onxx ", open=False):
         c.starrt()
    with gr.Accordion("init_Starting ", open=False):
        btn_init = gr.Button("init start")
        output_init = gr.Textbox(label="init")
        btn_init.click(fn=init_Starting,inputs=[],outputs=[output_init])
    with gr.Accordion("init_wandb ", open=False):
        btn_init_wandb = gr.Button("nit_wandb")
        output_initbtn_init_wandb = gr.Textbox(label="init")
        btn_init_wandb.click(fn=init_wandb,inputs=[],outputs=[output_initbtn_init_wandb])

    with gr.Accordion("init_training ", open=False):
        btn_init_train = gr.Button("init init_train")
        output_btn_init_train = gr.Textbox(label="init")
        # btn_init_train.click(fn=init_training,inputs=[],outputs=[output_btn_init_train])
    
    with gr.Accordion("run_train_epoch ", open=False):
        btn_run_train_epoch = gr.Button("run_train_epoch")
        input_run_train_epoch = gr.Number(label="number _train_epoch")
        output_run_train_epoch = gr.Textbox(label="run_train_epoch")
        btn_run_train_epoch.click(fn=run_train_epoch,inputs=[input_run_train_epoch],outputs=[output_run_train_epoch])

    with gr.Accordion("save_pretrained ", open=False):
        btn_save_pretrained = gr.Button("save_pretrained")
        input_save_pretrained = gr.Textbox(label="save_pretrained")
        output_save_pretrained = gr.Textbox(label="save_pretrained")
        btn_save_pretrained.click(fn=save_pretrained,inputs=[input_save_pretrained],outputs=[output_save_pretrained])

    btn_init_train.click(fn=init_training,inputs=[],outputs=[input_save_pretrained,output_btn_init_train])
    
        

        

interface.launch()
print('loadeed')