File size: 9,107 Bytes
1315cad
 
 
 
 
 
 
 
 
 
2a66429
1315cad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba7fe05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1315cad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba7fe05
 
 
 
 
1315cad
 
 
ba7fe05
1315cad
 
ba7fe05
1315cad
 
 
 
 
 
 
 
 
 
 
2a66429
1315cad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba7fe05
1315cad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
from __future__ import annotations

import contextlib
import io
import os
from pathlib import Path
from typing import List, Tuple

import gradio as gr
import torch
import spaces

from dia2 import Dia2, GenerationConfig, SamplingConfig

DEFAULT_REPO = os.environ.get("DIA2_DEFAULT_REPO", "nari-labs/Dia2-2B")
MAX_TURNS = 10
INITIAL_TURNS = 2

_dia: Dia2 | None = None


def _get_dia() -> Dia2:
    global _dia
    if _dia is None:
        _dia = Dia2.from_repo(DEFAULT_REPO, device="cuda", dtype="bfloat16")
    return _dia


def _concat_script(turn_count: int, turn_values: List[str]) -> str:
    lines: List[str] = []
    for idx in range(min(turn_count, len(turn_values))):
        text = (turn_values[idx] or "").strip()
        if not text:
            continue
        speaker = "[S1]" if idx % 2 == 0 else "[S2]"
        lines.append(f"{speaker} {text}")
    return "\n".join(lines)


EXAMPLES: dict[str, dict[str, List[str] | str | None]] = {
    "Intro": {
        "turns": [
            "Hello Dia2 fans! Today we're unveiling the new open TTS model.",
            "Sounds exciting. Can you show a sample right now?",
            "Absolutely. (laughs) Just press generate.",
        ],
        "voice_s1": "example_prefix1.wav",
        "voice_s2": "example_prefix2.wav",
    },
    "Customer Support": {
        "turns": [
            "Thanks for calling. How can I help you today?",
            "My parcel never arrived and it's been two weeks.",
            "I'm sorry about that. Let me check your tracking number.",
            "Appreciate it. I really need that package soon.",
        ],
        "voice_s1": "example_prefix1.wav",
        "voice_s2": "example_prefix2.wav",
    },
}


def _apply_turn_visibility(count: int) -> List[gr.Update]:
    return [gr.update(visible=i < count) for i in range(MAX_TURNS)]


def _add_turn(count: int):
    count = min(count + 1, MAX_TURNS)
    return (count, *_apply_turn_visibility(count))


def _remove_turn(count: int):
    count = max(1, count - 1)
    return (count, *_apply_turn_visibility(count))


def _load_example(name: str, count: int):
    data = EXAMPLES.get(name)
    if not data:
        return (count, *_apply_turn_visibility(count), None, None)
    turns = data.get("turns", [])
    voice_s1_path = data.get("voice_s1")
    voice_s2_path = data.get("voice_s2")
    new_count = min(len(turns), MAX_TURNS)
    updates: List[gr.Update] = []
    for idx in range(MAX_TURNS):
        if idx < new_count:
            updates.append(gr.update(value=turns[idx], visible=True))
        else:
            updates.append(gr.update(value="", visible=idx < INITIAL_TURNS))
    return (new_count, *updates, voice_s1_path, voice_s2_path)


def _prepare_prefix(file_path: str | None) -> str | None:
    if not file_path:
        return None
    path = Path(file_path)
    if not path.exists():
        return None
    return str(path)


@spaces.GPU(duration=100)
def generate_audio(
    turn_count: int,
    *inputs,
):
    turn_values = list(inputs[:MAX_TURNS])
    voice_s1 = inputs[MAX_TURNS]
    voice_s2 = inputs[MAX_TURNS + 1]
    cfg_scale = float(inputs[MAX_TURNS + 2])
    text_temperature = float(inputs[MAX_TURNS + 3])
    audio_temperature = float(inputs[MAX_TURNS + 4])
    text_top_k = int(inputs[MAX_TURNS + 5])
    audio_top_k = int(inputs[MAX_TURNS + 6])
    include_prefix = bool(inputs[MAX_TURNS + 7])

    script = _concat_script(turn_count, turn_values)
    if not script.strip():
        raise gr.Error("Please enter at least one non-empty speaker turn.")

    dia = _get_dia()
    config = GenerationConfig(
        cfg_scale=cfg_scale,
        text=SamplingConfig(temperature=text_temperature, top_k=text_top_k),
        audio=SamplingConfig(temperature=audio_temperature, top_k=audio_top_k),
        use_cuda_graph=True,
    )
    kwargs = {
        "prefix_speaker_1": _prepare_prefix(voice_s1),
        "prefix_speaker_2": _prepare_prefix(voice_s2),
        "include_prefix": include_prefix,
    }
    buffer = io.StringIO()
    with contextlib.redirect_stdout(buffer):
        result = dia.generate(
            script,
            config=config,
            output_wav=None,
            verbose=True,
            **kwargs,
        )
    waveform = result.waveform.detach().cpu().numpy()
    sample_rate = result.sample_rate
    timestamps = result.timestamps
    log_text = buffer.getvalue().strip()
    table = [[w, round(t, 3)] for w, t in timestamps]
    return (sample_rate, waveform), table, log_text or "Generation finished."


def build_interface() -> gr.Blocks:
    with gr.Blocks(
        title="Dia2 TTS", css=".compact-turn textarea {min-height: 60px}"
    ) as demo:
        gr.Markdown(
            """## Dia2 — Open TTS Model
Compose dialogue, attach optional voice prompts, and generate audio (CUDA graphs enabled by default)."""
        )
        turn_state = gr.State(INITIAL_TURNS)
        with gr.Row(equal_height=True):
            example_dropdown = gr.Dropdown(
                choices=["(select example)"] + list(EXAMPLES.keys()),
                label="Examples",
                value="(select example)",
            )
        with gr.Row(equal_height=True):
            with gr.Column(scale=1):
                with gr.Group():
                    gr.Markdown("### Script")
                    controls = []
                    for idx in range(MAX_TURNS):
                        speaker = "[S1]" if idx % 2 == 0 else "[S2]"
                        box = gr.Textbox(
                            label=f"{speaker} turn {idx + 1}",
                            lines=2,
                            elem_classes=["compact-turn"],
                            placeholder=f"Enter dialogue for {speaker}…",
                            visible=idx < INITIAL_TURNS,
                        )
                        controls.append(box)
                    with gr.Row():
                        add_btn = gr.Button("Add Turn")
                        remove_btn = gr.Button("Remove Turn")
                with gr.Group():
                    gr.Markdown("### Voice Prompts")
                    with gr.Row():
                        voice_s1 = gr.File(
                            label="[S1] voice (wav/mp3)", type="filepath"
                        )
                        voice_s2 = gr.File(
                            label="[S2] voice (wav/mp3)", type="filepath"
                        )
                with gr.Group():
                    gr.Markdown("### Sampling")
                    cfg_scale = gr.Slider(
                        1.0, 8.0, value=6.0, step=0.1, label="CFG Scale"
                    )
                    with gr.Group():
                        gr.Markdown("#### Text Sampling")
                        text_temperature = gr.Slider(
                            0.1, 1.5, value=0.6, step=0.05, label="Text Temperature"
                        )
                        text_top_k = gr.Slider(
                            1, 200, value=50, step=1, label="Text Top-K"
                        )
                    with gr.Group():
                        gr.Markdown("#### Audio Sampling")
                        audio_temperature = gr.Slider(
                            0.1, 1.5, value=0.8, step=0.05, label="Audio Temperature"
                        )
                        audio_top_k = gr.Slider(
                            1, 200, value=50, step=1, label="Audio Top-K"
                        )
                    include_prefix = gr.Checkbox(
                        label="Keep prefix audio in output", value=False
                    )
                    generate_btn = gr.Button("Generate", variant="primary")
            with gr.Column(scale=1):
                gr.Markdown("### Output")
                audio_out = gr.Audio(label="Waveform", interactive=False)
                timestamps = gr.Dataframe(
                    headers=["word", "seconds"], label="Timestamps"
                )
                log_box = gr.Textbox(label="Logs", lines=8)

        add_btn.click(
            lambda c: _add_turn(c),
            inputs=turn_state,
            outputs=[turn_state, *controls],
        )
        remove_btn.click(
            lambda c: _remove_turn(c),
            inputs=turn_state,
            outputs=[turn_state, *controls],
        )
        example_dropdown.change(
            lambda name, c: _load_example(name, c),
            inputs=[example_dropdown, turn_state],
            outputs=[turn_state, *controls, voice_s1, voice_s2],
        )

        generate_btn.click(
            generate_audio,
            inputs=[
                turn_state,
                *controls,
                voice_s1,
                voice_s2,
                cfg_scale,
                text_temperature,
                audio_temperature,
                text_top_k,
                audio_top_k,
                include_prefix,
            ],
            outputs=[audio_out, timestamps, log_box],
        )
    return demo


if __name__ == "__main__":
    app = build_interface()
    app.queue(default_concurrency_limit=1)
    app.launch(share=True)