import os
# os.environ["CUDA_VISIBLE_DEVICES"] = "7"

import wandb
from trainer import Trainer, TrainerArgs

from TTS.tts.configs.shared_configs import BaseDatasetConfig , CharactersConfig
from TTS.config.shared_configs import BaseAudioConfig
from TTS.tts.configs.vits_config import VitsConfig
from TTS.tts.datasets import load_tts_samples
from TTS.tts.models.vits import Vits, VitsAudioConfig, VitsArgs
from TTS.tts.utils.text.tokenizer import TTSTokenizer
from TTS.utils.audio import AudioProcessor
from TTS.tts.utils.speakers import SpeakerManager

 # Start a wandb run with `sync_tensorboard=True`
if wandb.run is None:
    wandb.init(project="persian-tts-vits-grapheme-arman24-FineTuned", group="GPUx8 accel mixed fp16 1x32", sync_tensorboard=True)

# output_path = os.path.dirname(os.path.abspath(__file__))
# output_path = output_path + '/notebook_files/runs'
# output_path = wandb.run.dir  ### PROBABLY better for notebook
output_path = "runs"

print("output path is:")
print(output_path)

cache_path = "cache"
dataset_config = BaseDatasetConfig(
    formatter="mozilla", meta_file_train="metadata-hotfix.csv", path="./datasets/arman-24"
)

character_config=CharactersConfig(
  characters='ءابتثجحخدذرزسشصضطظعغفقلمنهويِپچژکگیآأؤإئًَُّ',
#   characters="!¡'(),-.:;¿?ABCDEFGHIJKLMNOPRSTUVWXYZabcdefghijklmnopqrstuvwxyzáçèéêëìíîïñòóôöùúûü«°±µ»$%&‘’‚“`”„",
  punctuations='!(),-.:;? ̠،؛؟‌<>٫',
  phonemes='ˈˌːˑpbtdʈɖcɟkɡqɢʔɴŋɲɳnɱmʙrʀⱱɾɽɸβfvθðszʃʒʂʐçʝxɣχʁħʕhɦɬɮʋɹɻjɰlɭʎʟaegiouwyɪʊ̩æɑɔəɚɛɝɨ̃ʉʌʍ0123456789"#$%*+/=ABCDEFGHIJKLMNOPRSTUVWXYZ[]^_{}۱۲۳۴۵۶۷۸۹۰',
  pad="<PAD>",
  eos="<EOS>",
  bos="<BOS>",
  blank="<BLNK>",
  characters_class="TTS.tts.models.vits.VitsCharacters",
  )

# # From the coqui multilinguL recipes, will try later
# vitsArgs = VitsArgs(
#     use_language_embedding=True,
#     embedded_language_dim=1,
#     use_speaker_embedding=True,
#     use_sdp=False,
# )

audio_config = BaseAudioConfig(
     sample_rate=22050,
     do_trim_silence=True,
     min_level_db=-1,
    # do_sound_norm=True,
     signal_norm=True,
     clip_norm=True,
     symmetric_norm=True,
     max_norm = 0.9,
     resample=True,
     win_length=1024,
     hop_length=256,
     num_mels=80,
     mel_fmin=0,
     mel_fmax=None 
 )

vits_audio_config = VitsAudioConfig(
    sample_rate=22050,
#    do_sound_norm=True,
    win_length=1024,
    hop_length=256,
    num_mels=80,
    # do_trim_silence=True, #from hugging
    mel_fmin=0,
    mel_fmax=None
)
config = VitsConfig(
    # model_args=vitsArgs,
    audio=vits_audio_config, #from huggingface
    run_name="persian-tts-vits-grapheme-arman24-FINETUNED",
    batch_size=1,
    batch_group_size=0,
    eval_batch_size=1,
    num_loader_workers=16,
    num_eval_loader_workers=16,
    run_eval=True,
    run_eval_steps = 1000,
    print_eval=True,
    test_delay_epochs=-1,
    epochs=2000,
    save_step=1000,
    text_cleaner="basic_cleaners", #from MH
    use_phonemes=False,
    # phonemizer='persian_mh', #from TTS github
    # phoneme_language="fa",
    characters=character_config, #test without as well
    phoneme_cache_path=os.path.join(cache_path, "phoneme_cache_grapheme_azure-2"),
    compute_input_seq_cache=True,
    print_step=30,
    mixed_precision=False, #from TTS - True causes error "Expected reduction dim"
    test_sentences=[
        ["زین همرهان سست عناصر، دلم گرفت."],
        ["بیا تا گل برافشانیم و می در ساغر اندازیم."],
        ["بنی آدم اعضای یک پیکرند, که در آفرینش ز یک گوهرند."],
        ["سهام زندگی به 10 درصد و سهام بیتکوین گوگل به 33 درصد افزایش یافت."],
        ["من بودم و آبجی فوتینا، و حالا رپتی پتینا. این شعر یکی از اشعار معروف رو حوضی است که در کوچه بازار تهران زمزمه می شده است." ],
        ["یه دو دقه هم به حرفم گوش کن، نگو نگوشیدم و نحرفیدی."],
        [ "داستان با توصیف طوفان‌های شدید آغاز می‌شود؛ طوفان‌هایی که مزرعه‌ها را از بین می‌برد و محصولات را زیر شن دفن می‌کند؛ محصولاتی که زندگی افراد بسیاری به آن وابسته است."]
    ],
    output_path=output_path,
    datasets=[dataset_config],
    # use_sdp=False,  ### used in multiligual recipes on coqui
    # use_speaker_embedding=True
)

# INITIALIZE THE AUDIO PROCESSOR
# Audio processor is used for feature extraction and audio I/O.
# It mainly serves to the dataloader and the training loggers.
ap = AudioProcessor.init_from_config(config)

# INITIALIZE THE TOKENIZER
# Tokenizer is used to convert text to sequences of token IDs.
# config is updated with the default characters if not defined in the config.
tokenizer, config = TTSTokenizer.init_from_config(config)

# LOAD DATA SAMPLES
# Each sample is a list of ```[text, audio_file_path, speaker_name]```
# You can define your custom sample loader returning the list of samples.
# Or define your custom formatter and pass it to the `load_tts_samples`.
# Check `TTS.tts.datasets.load_tts_samples` for more details.
train_samples, eval_samples = load_tts_samples(
    dataset_config,
    eval_split=True,
    eval_split_max_size=config.eval_split_max_size,
    eval_split_size=config.eval_split_size,
)

# init speaker manager for multi-speaker training
# it maps speaker-id to speaker-name in the model and data-loader
# speaker_manager = SpeakerManager()
# speaker_manager.set_ids_from_data(train_samples + eval_samples, parse_key="speaker_name")
# config.num_speakers = speaker_manager.num_speakers



# init model
model = Vits(config, ap, tokenizer, speaker_manager=None)

# init the trainer and 🚀

trainer = Trainer(
    TrainerArgs(use_accelerate=True),
    config,
    output_path,
    model=model,
    train_samples=train_samples,
    eval_samples=eval_samples,
)
trainer.fit()