Spaces:
Running
Running
File size: 12,455 Bytes
284b9fd 1aa8b04 4674a36 284b9fd c74f66c 1aa8b04 284b9fd 4674a36 284b9fd 4674a36 284b9fd 4674a36 284b9fd 1aa8b04 284b9fd c74f66c 284b9fd 1aa8b04 284b9fd 4674a36 284b9fd 4674a36 284b9fd 4674a36 284b9fd 1aa8b04 284b9fd 1aa8b04 284b9fd 1aa8b04 284b9fd 1aa8b04 284b9fd 1aa8b04 4674a36 1aa8b04 284b9fd 1aa8b04 284b9fd 4674a36 284b9fd db34083 4674a36 db34083 284b9fd 4674a36 284b9fd 4674a36 1aa8b04 4674a36 1aa8b04 4674a36 1aa8b04 4674a36 1aa8b04 4674a36 1aa8b04 4674a36 1aa8b04 4674a36 1aa8b04 4674a36 1aa8b04 4674a36 1aa8b04 4674a36 1aa8b04 4674a36 1aa8b04 4674a36 1aa8b04 4674a36 1aa8b04 4674a36 1aa8b04 4674a36 284b9fd 4674a36 284b9fd 1aa8b04 4674a36 1aa8b04 4674a36 db34083 4674a36 284b9fd 1aa8b04 4674a36 1aa8b04 4674a36 1aa8b04 816d023 c74f66c 284b9fd 1aa8b04 284b9fd c74f66c 284b9fd 1aa8b04 284b9fd 1aa8b04 284b9fd 1aa8b04 284b9fd |
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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
import os
import json
import shutil
import argparse
import warnings
import gradio as gr
from generate import generate_music, get_args
from utils import WEIGHTS_DIR, TEMP_DIR, LANG
EN2ZH = {
"Cite": "引用",
"Submit": "提交",
"Feedback: the emotion you believe the generated result should belong to": "反馈:你所认为的生成结果该所属的情感",
"Status": "状态栏",
"Staff": "五线谱",
"ABC notation": "ABC 记谱",
"Download MXL": "下载 MXL",
"Download MusicXML": "下载 MusicXML",
"Download PDF score": "下载 PDF 乐谱",
"Download MIDI": "下载 MIDI",
"Audio": "音频",
"Download template": "下载模板",
"Save template": "保存模板",
"The emotion to which the current template belongs": "当前模板所属情感",
"Generate": "生成",
"Generate chords coming soon": "生成和声控制暂不可用",
"Volume in dB": "dB 音量调节",
"±12 octave": "±12 八度上下移",
"BPM tempo": "BPM 速度",
"Minor": "小调",
"Major": "大调",
"Mode": "大小调",
"Pitch SD": "音高标准差",
"Low": "低",
"High": "高",
"By feature control": "通过特征控制生成",
"By template": "通过模板生成",
"Arousal: reflects the calmness-intensity of the emotion": "唤醒度 反映情绪的 平静-激烈 程度",
"Valence: reflects negative-positive levels of emotion": "愉悦度 反映情绪的 消极-积极 程度",
"Video demo": "视频教程",
"Dataset": "数据集",
"Status": "状态栏",
}
def _L(en_txt: str):
return en_txt if LANG else f"{en_txt} ({EN2ZH[en_txt]})"
def infer_by_template(dataset: str, v: str, a: str, add_chord: bool):
status = "Success"
audio = midi = pdf = xml = mxl = tunes = jpg = None
emotion = "Q1"
if v == _L("Low") and a == _L("High"):
emotion = "Q2"
elif v == _L("Low") and a == _L("Low"):
emotion = "Q3"
elif v == _L("High") and a == _L("Low"):
emotion = "Q4"
try:
parser = argparse.ArgumentParser()
args = get_args(parser)
args.template = True
audio, midi, pdf, xml, mxl, tunes, jpg = generate_music(
args,
emo=emotion,
weights=f"{WEIGHTS_DIR}/{dataset.lower()}/weights.pth",
)
except Exception as e:
status = f"{e}"
return status, audio, midi, pdf, xml, mxl, tunes, jpg
def infer_by_features(
dataset: str,
pitch_std: str,
mode: str,
tempo: int,
octave: int,
rms: int,
add_chord: bool,
):
status = "Success"
audio = midi = pdf = xml = mxl = tunes = jpg = None
emotion = "Q1"
if mode == _L("Minor") and pitch_std == _L("High"):
emotion = "Q2"
elif mode == _L("Minor") and pitch_std == _L("Low"):
emotion = "Q3"
elif mode == _L("Major") and pitch_std == _L("Low"):
emotion = "Q4"
try:
parser = argparse.ArgumentParser()
args = get_args(parser)
args.template = False
audio, midi, pdf, xml, mxl, tunes, jpg = generate_music(
args,
emo=emotion,
weights=f"{WEIGHTS_DIR}/{dataset.lower()}/weights.pth",
fix_tempo=tempo,
fix_pitch=octave,
fix_volume=rms,
)
except Exception as e:
status = f"{e}"
return status, audio, midi, pdf, xml, mxl, tunes, jpg
def feedback(
fixed_emo: str,
source_dir=f"./{TEMP_DIR}/output",
target_dir=f"./{TEMP_DIR}/feedback",
):
try:
if not fixed_emo:
raise ValueError("Please select feedback before submitting! ")
os.makedirs(target_dir, exist_ok=True)
for root, _, files in os.walk(source_dir):
for file in files:
if file.endswith(".mxl"):
prompt_emo = file.split("]")[0][1:]
if prompt_emo != fixed_emo:
file_path = os.path.join(root, file)
target_path = os.path.join(
target_dir, file.replace(".mxl", f"_{fixed_emo}.mxl")
)
shutil.copy(file_path, target_path)
return f"Copied {file_path} to {target_path}"
else:
return "Thanks for your feedback!"
return "No .mxl files found in the source directory."
except Exception as e:
return f"{e}"
def save_template(label: str, pitch_std: str, mode: str, tempo: int, octave: int, rms):
status = "Success"
template = None
try:
if (
label
and pitch_std
and mode
and tempo != None
and octave != None
and rms != None
):
json_str = json.dumps(
{
"label": label,
"pitch_std": pitch_std == _L("High"),
"mode": mode == _L("Major"),
"tempo": tempo,
"octave": octave,
"volume": rms,
}
)
with open(
f"./{TEMP_DIR}/feedback/templates.jsonl",
"a",
encoding="utf-8",
) as file:
file.write(json_str + "\n")
template = f"./{TEMP_DIR}/feedback/templates.jsonl"
else:
raise ValueError("Please check features")
except Exception as e:
status = f"{e}"
return status, template
if __name__ == "__main__":
warnings.filterwarnings("ignore")
with gr.Blocks() as demo:
if LANG:
gr.Markdown(
"## The current CPU-based version on HuggingFace has slow inference, you can access the GPU-based mirror on [ModelScope](https://www.modelscope.cn/studios/monetjoe/EMelodyGen)"
)
with gr.Row():
with gr.Column():
gr.Video(
"./demo.mp4" if LANG else "./src/tutorial.mp4",
label=_L("Video demo"),
show_download_button=False,
show_share_button=False,
)
dataset_option = gr.Dropdown(
["VGMIDI", "EMOPIA", "Rough4Q"],
label=_L("Dataset"),
value="Rough4Q",
)
with gr.Tab(_L("By template")):
gr.Image(
"https://www.modelscope.cn/studio/monetjoe/EMelodyGen/resolve/master/src/4q.jpg",
show_label=False,
show_download_button=False,
show_fullscreen_button=False,
show_share_button=False,
)
valence_radio = gr.Radio(
[_L("Low"), _L("High")],
label=_L(
"Valence: reflects negative-positive levels of emotion"
),
value=_L("High"),
)
arousal_radio = gr.Radio(
[_L("Low"), _L("High")],
label=_L(
"Arousal: reflects the calmness-intensity of the emotion"
),
value=_L("High"),
)
chord_check = gr.Checkbox(
label=_L("Generate chords coming soon"),
value=False,
)
gen_btn_1 = gr.Button(_L("Generate"))
with gr.Tab(_L("By feature control")):
std_option = gr.Radio(
[_L("Low"), _L("High")], label=_L("Pitch SD"), value=_L("High")
)
mode_option = gr.Radio(
[_L("Minor"), _L("Major")], label=_L("Mode"), value=_L("Major")
)
tempo_option = gr.Slider(
minimum=40,
maximum=228,
step=1,
value=120,
label=_L("BPM tempo"),
)
octave_option = gr.Slider(
minimum=-24,
maximum=24,
step=12,
value=0,
label=_L("±12 octave"),
)
volume_option = gr.Slider(
minimum=-5,
maximum=10,
step=5,
value=0,
label=_L("Volume in dB"),
)
chord_check_2 = gr.Checkbox(
label=_L("Generate chords coming soon"),
value=False,
)
gen_btn_2 = gr.Button(_L("Generate"))
template_radio = gr.Radio(
["Q1", "Q2", "Q3", "Q4"],
label=_L("The emotion to which the current template belongs"),
)
save_btn = gr.Button(_L("Save template"))
dld_template = gr.File(label=_L("Download template"))
with gr.Column():
wav_audio = gr.Audio(label=_L("Audio"), type="filepath")
midi_file = gr.File(label=_L("Download MIDI"))
pdf_file = gr.File(label=_L("Download PDF score"))
xml_file = gr.File(label=_L("Download MusicXML"))
mxl_file = gr.File(label=_L("Download MXL"))
abc_textbox = gr.Textbox(
label=_L("ABC notation"), show_copy_button=True
)
staff_img = gr.Image(label=_L("Staff"), type="filepath")
with gr.Column():
status_bar = gr.Textbox(label=_L("Status"), show_copy_button=True)
fdb_radio = gr.Radio(
["Q1", "Q2", "Q3", "Q4"],
label=_L(
"Feedback: the emotion you believe the generated result should belong to"
),
)
fdb_btn = gr.Button(_L("Submit"))
gr.Markdown(
f"""## {_L("Cite")}
```bibtex
@inproceedings{{Zhou2025EMelodyGen,
title = {{EMelodyGen: Emotion-Conditioned Melody Generation in ABC Notation with the Musical Feature Template}},
author = {{Monan Zhou and Xiaobing Li and Feng Yu and Wei Li}},
month = {{Mar}},
year = {{2025}},
publisher = {{GitHub}},
version = {{0.1}},
url = {{https://github.com/monetjoe/EMelodyGen}}
}}
```"""
)
# actions
gen_btn_1.click(
fn=infer_by_template,
inputs=[dataset_option, valence_radio, arousal_radio, chord_check],
outputs=[
status_bar,
wav_audio,
midi_file,
pdf_file,
xml_file,
mxl_file,
abc_textbox,
staff_img,
],
)
gen_btn_2.click(
fn=infer_by_features,
inputs=[
dataset_option,
std_option,
mode_option,
tempo_option,
octave_option,
volume_option,
chord_check,
],
outputs=[
status_bar,
wav_audio,
midi_file,
pdf_file,
xml_file,
mxl_file,
abc_textbox,
staff_img,
],
)
save_btn.click(
fn=save_template,
inputs=[
template_radio,
std_option,
mode_option,
tempo_option,
octave_option,
volume_option,
],
outputs=[status_bar, dld_template],
)
fdb_btn.click(fn=feedback, inputs=fdb_radio, outputs=status_bar)
demo.launch()
|