import gradio as gr from kokoro import KPipeline import soundfile as sf import os # Initialize the pipeline pipeline = KPipeline(lang_code='a') def generate_speech(text, voice): # Ensure text is not empty if not text: return None # Generate speech generator = pipeline(text, voice=voice, speed=1, split_pattern=r'\n+') # Save the output output_path = "output.wav" for _, _, audio in generator: sf.write(output_path, audio, 24000) return output_path return None # Create the Gradio interface # The api_name parameter enables the API endpoint automatically demo = gr.Interface( fn=generate_speech, inputs=[ gr.Textbox(label="Enter Text", placeholder="Type your text here..."), gr.Dropdown(choices=["af_sarah", "am_michael", "af_bella"], label="Select Voice", value="af_sarah") ], outputs=gr.Audio(label="Generated Audio"), title="TTS Generator & API", description="Type text to generate audio. This app also functions as an API.", api_name="generate_audio" ) if __name__ == "__main__": demo.launch()