import torchaudio
from TTS.tts.configs.xtts_config import XttsConfig
from TTS.tts.models.xtts import Xtts
import torch
import tempfile

XTTS_MODEL = None
model_path = "C:/NeuralNetworks/xtts-finetune-webui-portable-by-neurogen/finetune_models/ready/model.pth"
model_config = "C:/NeuralNetworks/xtts-finetune-webui-portable-by-neurogen/finetune_models/ready/config.json"
model_vocab = "C:/NeuralNetworks/xtts-finetune-webui-portable-by-neurogen/finetune_models/ready/vocab.json "
model_speaker = "C:/NeuralNetworks/xtts-finetune-webui-portable-by-neurogen/finetune_models/ready\speakers_xtts.pth"


def clear_gpu_cache():
    if torch.cuda.is_available():
        torch.cuda.empty_cache()


def load_model(xtts_checkpoint, xtts_config, xtts_vocab, xtts_speaker):
    global XTTS_MODEL
    clear_gpu_cache()
    if not xtts_checkpoint or not xtts_config or not xtts_vocab:
        print(
            "You need to run the previous steps or manually set the `XTTS checkpoint path`, `XTTS config path`, and `XTTS vocab path` fields !!")
        return
    config = XttsConfig()
    config.load_json(xtts_config)
    XTTS_MODEL = Xtts.init_from_config(config)
    print("Loading XTTS model! ")
    XTTS_MODEL.load_checkpoint(config, checkpoint_path=xtts_checkpoint, vocab_path=xtts_vocab,
                               speaker_file_path=xtts_speaker, use_deepspeed=False)
    if torch.cuda.is_available():
        XTTS_MODEL.cuda()
    print("Model Loaded!")


def run_tts(lang, tts_text, speaker_audio_file, temperature, length_penalty, repetition_penalty, top_k, top_p,
            sentence_split, use_config):
    if XTTS_MODEL is None or not speaker_audio_file:
        return "You need to run the previous step to load the model !!", None, None

    gpt_cond_latent, speaker_embedding = XTTS_MODEL.get_conditioning_latents(audio_path=speaker_audio_file,
                                                                             gpt_cond_len=XTTS_MODEL.config.gpt_cond_len,
                                                                             max_ref_length=XTTS_MODEL.config.max_ref_len,
                                                                             sound_norm_refs=XTTS_MODEL.config.sound_norm_refs)

    if use_config:
        out = XTTS_MODEL.inference(
            text=tts_text,
            language=lang,
            gpt_cond_latent=gpt_cond_latent,
            speaker_embedding=speaker_embedding,
            temperature=XTTS_MODEL.config.temperature,  # Add custom parameters here
            length_penalty=XTTS_MODEL.config.length_penalty,
            repetition_penalty=XTTS_MODEL.config.repetition_penalty,
            top_k=XTTS_MODEL.config.top_k,
            top_p=XTTS_MODEL.config.top_p,
            enable_text_splitting=True
        )
    else:
        out = XTTS_MODEL.inference(
            text=tts_text,
            language=lang,
            gpt_cond_latent=gpt_cond_latent,
            speaker_embedding=speaker_embedding,
            temperature=temperature,  # Add custom parameters here
            length_penalty=length_penalty,
            repetition_penalty=float(repetition_penalty),
            top_k=top_k,
            top_p=top_p,
            enable_text_splitting=sentence_split
        )

    with tempfile.NamedTemporaryFile(suffix=".wav", delete=False, dir="xtts/output") as fp:
        out["wav"] = torch.tensor(out["wav"]).unsqueeze(0)
        out_path = fp.name
        torchaudio.save(out_path, out["wav"], 24000)

    print("Speech generated !", out_path)


lang = "ru"
tts_text = "Привет мой мальчик, я так соскучилась по тебе"
speaker_audio_file = "C:/NeuralNetworks/xtts-finetune-webui-portable-by-neurogen/finetune_models/ready/reference.wav"
temperature = 0.9  # 0.75
length_penalty = 1
repetition_penalty = 5
top_k = 50
top_p = 0.85
sentence_split = True
use_config = False

load_model(model_path, model_config, model_vocab, model_speaker)
run_tts(lang, tts_text, speaker_audio_file, temperature, length_penalty, repetition_penalty, top_k, top_p,
        sentence_split, use_config)


Сделай пожалуйста на основе моего кода flask api с методом который будет загружать модель и методом который преобразует текст в голос. Так же я хочу что бы можно было запускать скрипт через командную строку с передачей параметров