File size: 11,664 Bytes
b5e825c
 
 
91394e0
1c7c6fd
91394e0
 
 
 
 
b03522b
 
 
 
 
 
 
 
 
91394e0
4067f95
b03522b
 
 
4067f95
b03522b
 
 
 
4067f95
b03522b
4067f95
b03522b
 
 
4067f95
 
b03522b
4067f95
b03522b
 
 
 
4067f95
 
 
 
b03522b
 
 
 
4067f95
b03522b
 
 
 
 
4067f95
 
 
 
b03522b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91394e0
 
 
 
 
 
 
 
 
 
 
 
f1b8d35
 
91394e0
87f144d
91394e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3cade4
 
 
 
91394e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1b8d35
 
91394e0
 
f1b8d35
91394e0
 
 
 
f1b8d35
91394e0
 
 
 
f1b8d35
91394e0
 
 
 
 
 
 
 
 
 
f1b8d35
91394e0
 
 
 
 
 
f1b8d35
 
91394e0
 
b03522b
91394e0
 
 
05779d3
b03522b
05779d3
 
 
91394e0
 
b03522b
1a42edd
4067f95
1a42edd
05779d3
91394e0
 
 
f1b8d35
 
 
91394e0
 
f1b8d35
91394e0
 
 
b03522b
 
91394e0
 
b03522b
 
91394e0
 
 
f1b8d35
 
 
91394e0
b03522b
 
 
91394e0
 
 
 
f1b8d35
91394e0
f1b8d35
91394e0
 
 
 
 
b03522b
 
91394e0
f1b8d35
b5e825c
f1b8d35
b03522b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import time
import uuid

import gradio as gr
import spaces
import yaml

from characters import CHARACTERS
from pipeline import SingingDialoguePipeline

pipe = None


def _ensure_pipeline(config):
    """Ensure pipeline is initialized in GPU worker context."""
    global pipe
    if pipe is None:
        pipe = SingingDialoguePipeline(config)


@spaces.GPU(duration=120)
def run_pipeline(audio_path, config, svs_model_info, character_prompt, current_voice):
    global pipe

    if not audio_path:
        return gr.update(value=None), gr.update(value=None), None

    _ensure_pipeline(config)

    tmp_file = f"audio_{int(time.time())}_{uuid.uuid4().hex[:8]}.wav"
    results = pipe.run(
        audio_path,
        svs_model_info["lang"],
        character_prompt,
        current_voice,
        output_audio_path=tmp_file,
    )

    formatted_logs = f"ASR: {results['asr_text']}\nLLM: {results['llm_text']}"
    return (
        gr.update(value=formatted_logs),
        gr.update(value=results["output_audio_path"]),
        results,
    )


@spaces.GPU(duration=120)
def update_metrics(audio_path, config, results_data):
    global pipe

    if not audio_path or not results_data:
        return gr.update(value="")

    _ensure_pipeline(config)

    results = pipe.evaluate(audio_path, **results_data)
    results.update(results_data.get("metrics", {}))
    formatted_metrics = "\n".join([f"{k}: {v}" for k, v in results.items()])
    return gr.update(value=formatted_metrics)


@spaces.GPU(duration=120)
def update_asr_model_in_pipeline(config, asr_model):
    _ensure_pipeline(config)
    pipe.set_asr_model(asr_model)
    return gr.update(value=asr_model)


@spaces.GPU(duration=120)
def update_llm_model_in_pipeline(config, llm_model):
    _ensure_pipeline(config)
    pipe.set_llm_model(llm_model)
    return gr.update(value=llm_model)


@spaces.GPU(duration=120)
def update_svs_model_in_pipeline(config, svs_model_path):
    _ensure_pipeline(config)
    pipe.set_svs_model(svs_model_path)
    return gr.update()


@spaces.GPU(duration=120)
def update_melody_source_in_pipeline(config, melody_source):
    _ensure_pipeline(config)
    pipe.set_melody_controller(melody_source)
    return gr.update(value=melody_source)


class GradioInterface:
    def __init__(self, options_config: str, default_config: str):
        self.options = self.load_config(options_config)
        self.svs_model_map = {
            model["id"]: model for model in self.options["svs_models"]
        }
        self.default_config = self.load_config(default_config)
        self.character_info = CHARACTERS
        self.current_character = self.default_config["character"]
        self.current_svs_model = (
            f"{self.default_config['language']}-{self.default_config['svs_model']}"
        )
        self.current_voice = self.svs_model_map[self.current_svs_model]["voices"][
            self.character_info[self.current_character].default_voice
        ]
        self.results = None

    def load_config(self, path: str):
        with open(path, "r") as f:
            return yaml.safe_load(f)

    def create_interface(self) -> gr.Blocks:
        try:
            with gr.Blocks(title="SingingSDS") as demo:
                gr.Markdown("# SingingSDS: Role-Playing Singing Spoken Dialogue System")
                with gr.Row():
                    with gr.Column(scale=1):
                        character_image = gr.Image(
                            self.character_info[self.current_character].image_path,
                            label="Character",
                            show_label=False,
                        )
                    with gr.Column(scale=2):
                        mic_input = gr.Audio(
                            sources=["microphone", "upload"],
                            type="filepath",
                            label="Speak to the character",
                        )
                        interaction_log = gr.Textbox(
                            label="Interaction Log", lines=3, interactive=False
                        )
                        audio_output = gr.Audio(
                            label="Character's Response",
                            type="filepath",
                            autoplay=True,
                            interactive=False,
                        )

                        with gr.Row():
                            metrics_button = gr.Button(
                                "Evaluate Metrics", variant="secondary"
                            )
                            metrics_output = gr.Textbox(
                                label="Evaluation Results", lines=3, interactive=False
                            )

                gr.Markdown("## Configuration")
                with gr.Row():
                    with gr.Column():
                        character_radio = gr.Radio(
                            label="Character Role",
                            choices=list(self.character_info.keys()),
                            value=self.default_config["character"],
                        )
                        with gr.Row():
                            asr_radio = gr.Radio(
                                label="ASR Model",
                                choices=[
                                    (model["name"], model["id"])
                                    for model in self.options["asr_models"]
                                ],
                                value=self.default_config["asr_model"],
                            )
                        with gr.Row():
                            llm_radio = gr.Radio(
                                label="LLM Model",
                                choices=[
                                    (model["name"], model["id"])
                                    for model in self.options["llm_models"]
                                ],
                                value=self.default_config["llm_model"],
                            )
                    with gr.Column():
                        with gr.Row():
                            melody_radio = gr.Radio(
                                label="Melody Source",
                                choices=[
                                    (source["name"], source["id"])
                                    for source in self.options["melody_sources"]
                                ],
                                value=self.default_config["melody_source"],
                            )
                        with gr.Row():
                            svs_radio = gr.Radio(
                                label="SVS Model",
                                choices=[
                                    (model["name"], model["id"])
                                    for model in self.options["svs_models"]
                                ],
                                value=self.current_svs_model,
                            )
                        with gr.Row():
                            voice_radio = gr.Radio(
                                label="Singing voice",
                                choices=list(
                                    self.svs_model_map[self.current_svs_model][
                                        "voices"
                                    ].keys()
                                ),
                                value=self.character_info[
                                    self.current_character
                                ].default_voice,
                            )
                character_radio.change(
                    fn=self.update_character,
                    inputs=character_radio,
                    outputs=[character_image, voice_radio],
                )
                asr_radio.change(
                    fn=self.update_asr_model, inputs=asr_radio, outputs=asr_radio
                )
                llm_radio.change(
                    fn=self.update_llm_model, inputs=llm_radio, outputs=llm_radio
                )
                svs_radio.change(
                    fn=self.update_svs_model,
                    inputs=svs_radio,
                    outputs=[svs_radio, voice_radio],
                )
                melody_radio.change(
                    fn=self.update_melody_source,
                    inputs=melody_radio,
                    outputs=melody_radio,
                )
                voice_radio.change(
                    fn=self.update_voice, inputs=voice_radio, outputs=voice_radio
                )
                mic_input.change(
                    fn=self._run_pipeline_wrapper,
                    inputs=mic_input,
                    outputs=[interaction_log, audio_output],
                )
                metrics_button.click(
                    fn=self._update_metrics_wrapper,
                    inputs=audio_output,
                    outputs=[metrics_output],
                )

            return demo
        except Exception:
            import traceback

            print(traceback.format_exc())
            return gr.Blocks()

    def update_character(self, character):
        self.current_character = character
        character_voice = self.character_info[self.current_character].default_voice
        self.current_voice = self.svs_model_map[self.current_svs_model]["voices"][
            character_voice
        ]
        return gr.update(value=self.character_info[character].image_path), gr.update(
            value=character_voice
        )

    def update_asr_model(self, asr_model):
        self.default_config["asr_model"] = asr_model
        return update_asr_model_in_pipeline(self.default_config, asr_model)

    def update_llm_model(self, llm_model):
        self.default_config["llm_model"] = llm_model
        return update_llm_model_in_pipeline(self.default_config, llm_model)

    def update_svs_model(self, svs_model):
        self.current_svs_model = svs_model
        character_voice = self.character_info[self.current_character].default_voice
        self.current_voice = self.svs_model_map[self.current_svs_model]["voices"][
            character_voice
        ]
        svs_model_path = self.svs_model_map[self.current_svs_model]["model_path"]
        self.default_config["svs_model"] = svs_model_path
        update_svs_model_in_pipeline(self.default_config, svs_model_path)
        return (
            gr.update(value=svs_model),
            gr.update(
                choices=list(
                    self.svs_model_map[self.current_svs_model]["voices"].keys()
                ),
                value=character_voice,
            ),
        )

    def update_melody_source(self, melody_source):
        self.current_melody_source = melody_source
        self.default_config["melody_source"] = melody_source
        return update_melody_source_in_pipeline(self.default_config, melody_source)

    def update_voice(self, voice):
        self.current_voice = self.svs_model_map[self.current_svs_model]["voices"][voice]
        return gr.update(value=voice)

    def _run_pipeline_wrapper(self, audio_path):
        log_update, audio_update, pipeline_results = run_pipeline(
            audio_path,
            self.default_config,
            self.svs_model_map[self.current_svs_model],
            self.character_info[self.current_character].prompt,
            self.current_voice,
        )
        if pipeline_results:
            self.results = pipeline_results
        return log_update, audio_update

    def _update_metrics_wrapper(self, audio_path):
        return update_metrics(audio_path, self.default_config, self.results or {})