codewithjarair/Chatterbox_tts
3
1import os2import gradio as gr3from engine import VoiceCloningEngine4 5# Initialize the Voice Cloning Engine6engine = VoiceCloningEngine()7 8def process_tts(text, ref_audio, exaggeration, cfg_weight, temperature, seed, progress=gr.Progress()):9 """10 Main TTS processing function connecting the UI with the VoiceCloningEngine.11 """12 if not text.strip():13 return None, "Error: Please enter a script."14 if ref_audio is None:15 return None, "Error: Please upload a reference audio clip."16 17 try:18 # Call the engine with the Gradio Progress callback19 output_path, used_seed = engine.generate(20 text=text,21 ref_audio=ref_audio,22 exaggeration=exaggeration,23 cfg_weight=cfg_weight,24 temperature=temperature,25 seed=seed,26 progress_callback=progress27 )28 return output_path, f"Successfully generated audio with seed {used_seed}."29 except Exception as e:30 import traceback31 traceback.print_exc()32 return None, f"Error: {str(e)}"33 34# UI Layout and Configuration35def create_ui():36 with gr.Blocks(theme=gr.themes.Soft(), title="Voice Cloning TTS Chatterbox") as demo:37 gr.Markdown("# ๐ฃ๏ธ Voice Cloning TTS Engine")38 gr.Markdown("""39 **A high-performance voice cloning application powered by Chatterbox TTS.** 40 Optimized for long scripts with intelligent chunking, context preservation, and smooth concatenation.41 """)42 43 with gr.Row():44 # Configuration Column45 with gr.Column(scale=1):46 text_input = gr.Textbox(47 label="Script", 48 placeholder="Paste your long script here. The engine automatically splits it at sentence boundaries for smooth narration...", 49 lines=10,50 value="Welcome to the modular voice cloning application. By separating the core processing engine into its own file, we ensure cleaner code and better scalability. This tool automatically handles long texts, ensuring that your narration is smooth and continuous across multiple sentences."51 )52 ref_audio = gr.Audio(53 label="Reference Voice (Voice to Clone)", 54 type="filepath",55 sources=["upload", "microphone"]56 )57 58 with gr.Row():59 exaggeration = gr.Slider(60 0.1, 1.0, value=0.5, step=0.05, 61 label="Exaggeration", 62 info="Intensity of cloned voice traits. Default 0.5. Warning: >0.8 can be unstable."63 )64 cfg_weight = gr.Slider(65 0.0, 1.0, value=0.5, step=0.05, 66 label="CFG/Pace", 67 info="Balance between text adherence and reference voice speed."68 )69 70 with gr.Accordion("Advanced Options", open=False):71 seed = gr.Number(72 label="Seed", 73 value=0, 74 precision=0, 75 info="Set to 0 for a random seed each time."76 )77 temperature = gr.Slider(78 0.1, 2.0, value=1.0, step=0.05, 79 label="Temperature", 80 info="Higher values increase expressiveness and randomness."81 )82 83 generate_btn = gr.Button("Generate Speech", variant="primary")84 85 # Result Column86 with gr.Column(scale=1):87 audio_output = gr.Audio(label="Generated Speech", type="filepath")88 status_msg = gr.Textbox(label="Status", interactive=False)89 90 gr.Markdown("### ๐ Documentation")91 gr.Markdown("""92 ### Features93 - **Modular Engine**: The `VoiceCloningEngine` in `engine.py` handles all core processing, making the app easier to maintain.94 - **Intelligent Chunking**: Scripts are automatically split at sentence boundaries (~250 chars) for stability.95 - **Context Preservation**: Audio segments are concatenated smoothly for long-form narration.96 97 ### Deployment & Secrets98 - **Secrets Management**: If your app requires API keys, set them in the **Hugging Face Space Secrets** and access them via `os.getenv()`.99 - **GPU Recommended**: This app runs best on a T4 or L4 GPU Space.100 """)101 102 # Connect UI events103 generate_btn.click(104 fn=process_tts,105 inputs=[106 text_input, 107 ref_audio, 108 exaggeration, 109 cfg_weight, 110 temperature, 111 seed112 ],113 outputs=[audio_output, status_msg]114 )115 116 return demo117 118if __name__ == "__main__":119 ui = create_ui()120 # Ensure server_name is set for Hugging Face compatibility121 ui.launch(server_name="0.0.0.0")122 