|
| 1 | +# Copyright (c) 2025 Amphion. |
| 2 | +# |
| 3 | +# This source code is licensed under the MIT license found in the |
| 4 | +# LICENSE file in the root directory of this source tree. |
| 5 | + |
| 6 | +import argparse |
| 7 | +import os |
| 8 | +import gradio as gr |
| 9 | +from huggingface_hub import snapshot_download |
| 10 | +from models.tts.ints.ints import Ints |
| 11 | +from utils.util import load_config |
| 12 | + |
| 13 | + |
| 14 | +def text_to_speech( |
| 15 | + prompt_text, text, prompt_audio, top_k=20, top_p=0.98, temperature=1.0 |
| 16 | +): |
| 17 | + gen_audio, debug_dict = ins_model( |
| 18 | + text, prompt_audio, prompt_text, top_k, top_p, temperature |
| 19 | + ) |
| 20 | + # (sample rate in Hz, audio data as numpy array) |
| 21 | + output_audio = (24000, gen_audio) |
| 22 | + return output_audio, debug_dict |
| 23 | + |
| 24 | + |
| 25 | +def create_demo(): |
| 26 | + with gr.Blocks() as demo: |
| 27 | + gr.Markdown("# Ints Text-to-Speech") |
| 28 | + |
| 29 | + with gr.Row(): |
| 30 | + with gr.Column(): |
| 31 | + prompt_text = gr.Textbox( |
| 32 | + label="Prompt Text", |
| 33 | + placeholder="Enter the prompt text here...", |
| 34 | + lines=2, |
| 35 | + ) |
| 36 | + text = gr.Textbox( |
| 37 | + label="Text to Synthesize", |
| 38 | + placeholder="Enter the text you want to convert to speech...", |
| 39 | + lines=3, |
| 40 | + ) |
| 41 | + prompt_audio = gr.Audio(label="Prompt Audio", type="filepath") |
| 42 | + |
| 43 | + with gr.Row(): |
| 44 | + top_k = gr.Slider( |
| 45 | + minimum=10, maximum=100, value=20, step=1, label="Top-k" |
| 46 | + ) |
| 47 | + top_p = gr.Slider( |
| 48 | + minimum=0.5, maximum=1.0, value=0.98, step=0.01, label="Top-p" |
| 49 | + ) |
| 50 | + temperature = gr.Slider( |
| 51 | + minimum=0.5, |
| 52 | + maximum=1.5, |
| 53 | + value=1.0, |
| 54 | + step=0.01, |
| 55 | + label="Temperature", |
| 56 | + ) |
| 57 | + |
| 58 | + generate_btn = gr.Button("Generate Speech") |
| 59 | + |
| 60 | + with gr.Column(): |
| 61 | + output_audio = gr.Audio(label="Generated Speech") |
| 62 | + debug_dict = gr.JSON(label="Debug Dict") |
| 63 | + |
| 64 | + generate_btn.click( |
| 65 | + fn=text_to_speech, |
| 66 | + inputs=[ |
| 67 | + prompt_text, |
| 68 | + text, |
| 69 | + prompt_audio, |
| 70 | + top_k, |
| 71 | + top_p, |
| 72 | + temperature, |
| 73 | + ], |
| 74 | + outputs=[output_audio, debug_dict], |
| 75 | + ) |
| 76 | + |
| 77 | + return demo |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + parser = argparse.ArgumentParser() |
| 82 | + parser.add_argument("--port", type=int, default=7860) |
| 83 | + parser.add_argument("--server_name", type=str, default="0.0.0.0") |
| 84 | + parser.add_argument("--use_vllm", action="store_true") |
| 85 | + parser.add_argument("--use_flash_attn", action="store_true") |
| 86 | + parser.add_argument("--device", type=str, default="cuda:0") |
| 87 | + parser.add_argument("--config_path", type=str, default="models/tts/ints/ints.json") |
| 88 | + parser.add_argument("--model_name", type=str, default="ints_v2") |
| 89 | + parser.add_argument("--gpu_memory_utilization", type=float, default=0.4) |
| 90 | + args = parser.parse_args() |
| 91 | + |
| 92 | + ins_cfg = load_config(args.config_path) |
| 93 | + |
| 94 | + base_folder = snapshot_download("amphion/Ints") |
| 95 | + llm_path = os.path.join(base_folder, args.model_name) |
| 96 | + print(f"llm_path: {llm_path}") |
| 97 | + |
| 98 | + ins_model = Ints( |
| 99 | + llm_path=llm_path, |
| 100 | + cfg=ins_cfg, |
| 101 | + device=args.device, |
| 102 | + use_vllm=args.use_vllm, |
| 103 | + use_flash_attn=args.use_flash_attn, |
| 104 | + gpu_memory_utilization=args.gpu_memory_utilization, |
| 105 | + ) |
| 106 | + |
| 107 | + text_to_speech( |
| 108 | + prompt_text="We do not break. We never give in. We never back down.", |
| 109 | + prompt_audio="models/tts/maskgct/wav/prompt.wav", |
| 110 | + text="I will make America great again.", |
| 111 | + ) |
| 112 | + |
| 113 | + demo = create_demo() |
| 114 | + demo.launch( |
| 115 | + server_name=args.server_name, |
| 116 | + server_port=args.port, |
| 117 | + ) |
0 commit comments