import os from pathlib import Path import gradio as gr import tempfile import soundfile as sf from models import Tokenizer, Kokoro from fastapi import FastAPI, Request from fastapi.responses import FileResponse import uvicorn # --- EXISTING LOGIC (UNCHANGED) --- BASE_DIR = Path(__file__).resolve().parent tokenizer_cache = None kokoro_cache = {} model_status = "not_loaded" model_error = None def get_style_vector_choices(directory="voices"): directory_path = BASE_DIR / directory return [file.name for file in directory_path.iterdir() if file.suffix == ".pt"] def get_onnx_models(directory="weights"): directory_path = BASE_DIR / directory return [file.name for file in directory_path.iterdir() if file.suffix == ".onnx"] def local_tts( text: str, model_path: str, style_vector: str, output_file_format: str = "wav", speed: float = 1.0 ): global tokenizer_cache, model_status, model_error if len(text) > 0: try: style_vector_path = str(BASE_DIR / "voices" / style_vector) model_path_full = str(BASE_DIR / "weights" / model_path) cache_key = (model_path_full, style_vector_path) if tokenizer_cache is None: tokenizer_cache = Tokenizer() if cache_key not in kokoro_cache: model_status = "loading" model_error = None kokoro_cache[cache_key] = Kokoro(model_path_full, style_vector_path, tokenizer=tokenizer_cache, lang='en-us') model_status = "ready" inference = kokoro_cache[cache_key] 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: model_status = "failed" model_error = str(e) raise gr.Error(f"An error occurred during TTS inference: {str(e)}") else: raise gr.Error("Input text cannot be empty.") style_vector_choices = get_style_vector_choices() onnx_models_choices = get_onnx_models() sample_outputs = [ ("Educational Note", "Machine learning models rely on large datasets and complex algorithms to identify patterns and make predictions.", str(BASE_DIR / "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!", str(BASE_DIR / "assets" / "fun_fact.wav")), ("Thanks", "Thank you for listening to this audio. It was generated by the Kokoro TTS model.", str(BASE_DIR / "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 (UNCHANGED) --- with gr.Blocks() as demo: gr.Markdown("##