from TTS.api import TTS
import os

model = None

def get_model():
    global model
    if model is None:
        model = TTS(
            model_name="tts_models/multilingual/multi-dataset/xtts_v2"
        )
    return model

def generate_voice(text, speaker_wav, output_file="/tmp/voice.wav"):
    if not speaker_wav or not os.path.exists(speaker_wav):
        print("Please upload a voice sample first using `voice upload-voice <path>`.")
        return None
        
    tts = get_model()

    tts.tts_to_file(
        text=text,
        speaker_wav=speaker_wav,
        language="en",
        file_path=output_file,
    )

    return output_file
