import os import gradio as gr import tempfile import soundfile as sf from models import Tokenizer, Kokoro # Function to fetch available style vectors dynamically def get_style_vector_choices(directory="voices"): return [file for file in os.listdir(directory) if file.endswith(".pt")] def get_onnx_models(directory="weights"): return [file for file in os.listdir(directory) if file.endswith(".onnx")] # Function to perform TTS using your local model def local_tts( text: str, model_path: str, style_vector: str, output_file_format: str = "wav", speed: float = 1.0 ): if len(text) > 0: try: tokenizer = Tokenizer() style_vector_path = os.path.join("voices", style_vector) model_path = os.path.join("weights", model_path) inference = Kokoro(model_path, style_vector_path, tokenizer=tokenizer, lang='en-us') audio, sample_rate = inference.generate_audio(text, speed=speed) with tempfile.NamedTemporaryFile(suffix=f".{output_file_format}", delete=False) as temp_file: sf.write(temp_file.name, audio, sample_rate) temp_file_path = temp_file.name return temp_file_path except Exception as e: raise gr.Error(f"An error occurred during TTS inference: {str(e)}") else: raise gr.Error("Input text cannot be empty.") # Get the list of available style vectors style_vector_choices = get_style_vector_choices() onnx_models_choices = get_onnx_models() # sample texts and their corresponding audio sample_outputs = [ ("Educational Note", "Machine learning models rely on large datasets and complex algorithms to identify patterns and make predictions.", "assets/edu_note.wav"), ("Fun Fact", "Did you know that honey never spoils? Archaeologists have found pots of honey in ancient Egyptian tombs that are over 3,000 years old and still edible!", "assets/fun_fact.wav"), ("Thanks", "Thank you for listening to this audio. It was generated by the Kokoro TTS model.", "assets/thanks.wav") ] example_texts = [ ["Machine learning models rely on large datasets and complex algorithms to identify patterns and make predictions."], ["Did you know that honey never spoils? Archaeologists have found pots of honey in ancient Egyptian tombs that are over 3,000 years old and still edible!"], ["Thank you for listening to this audio. It was generated by the Kokoro TTS model."] ] # Gradio Interface with gr.Blocks() as demo: gr.Markdown("##