admin commited on
Commit
3c75406
·
1 Parent(s): 127f350
Files changed (2) hide show
  1. app.py +42 -31
  2. utils.py +52 -5
app.py CHANGED
@@ -19,6 +19,8 @@ from utils import (
19
  TEMP_DIR,
20
  TEYVAT,
21
  DEVICE,
 
 
22
  )
23
 
24
 
@@ -179,49 +181,58 @@ def generate_music(args, region: str):
179
  return generate_music(args, region)
180
 
181
 
182
- def infer(p, k, t, region: str):
183
- if os.path.exists(TEMP_DIR):
184
- shutil.rmtree(TEMP_DIR)
 
 
 
 
 
 
 
 
 
 
 
185
 
186
- os.makedirs(TEMP_DIR, exist_ok=True)
187
- parser = argparse.ArgumentParser()
188
- args = get_args(parser)
189
- args.top_p = p
190
- args.top_k = k
191
- args.temperature = t
192
- if region == "Natlan":
193
- region = "Teyvat"
194
 
195
- return generate_music(args, region)
196
 
197
 
198
  if __name__ == "__main__":
199
  warnings.filterwarnings("ignore")
 
200
  gr.Interface(
201
  fn=infer,
202
  inputs=[
203
- gr.Slider(0.01, 1.0, 0.8, step=0.01, label="Top-P sample"),
204
- gr.Slider(0, 80, 8, step=1, label="Top-K sample (0=closed)"),
205
- gr.Slider(0.01, 2.0, 1.2, step=0.01, label="Temperature"),
206
  gr.Dropdown(
207
- choices=TEYVAT,
208
- value="Mondstadt",
209
- label="Region",
210
  ),
211
  ],
212
  outputs=[
213
- gr.Audio(label="Audio", type="filepath"),
214
- gr.File(label="Download MIDI"),
215
- gr.File(label="Download PDF"),
216
- gr.File(label="Download MusicXML"),
217
- gr.File(label="Download MXL"),
218
- gr.Textbox(label="ABC notation", show_copy_button=True),
219
- gr.Image(label="Staff", type="filepath", show_share_button=False),
 
220
  ],
221
  flagging_mode="never",
222
- title="Genshin Music Generation",
223
- description="""
224
- Welcome to this space based on the Tunesformer open source project, which is totally free! The current model is still in debugging, the plan is in the Genshin Impact after the main line is killed, all countries and regions after all the characters are open, the second creation of the concert will be complete and the sample is balanced, at that time to re-fine-tune the model and add the reality of the style of screening to assist in the game of each country's output to strengthen the learning in order to enhance the output differentiation and quality. Note: Data engineering on the Star Rail is in operation, and will hopefully be baselined in the future as well with the mainline kill.<br>
225
- Data source: <a href="https://musescore.org">MuseScore</a> Tags source: <a href="https://genshin-impact.fandom.com/wiki/Genshin_Impact_Wiki">Genshin Impact Wiki | Fandom</a> Model base: <a href="https://github.com/sander-wood/tunesformer">Tunesformer</a>
226
- """,
227
- ).launch()
 
 
 
19
  TEMP_DIR,
20
  TEYVAT,
21
  DEVICE,
22
+ EN_US,
23
+ _L,
24
  )
25
 
26
 
 
181
  return generate_music(args, region)
182
 
183
 
184
+ def infer(p: float, k: int, t: float, region: str):
185
+ status = "Success"
186
+ audio = midi = pdf = xml = mxl = tunes = jpg = None
187
+ try:
188
+ if os.path.exists(TEMP_DIR):
189
+ shutil.rmtree(TEMP_DIR)
190
+
191
+ os.makedirs(TEMP_DIR)
192
+ parser = argparse.ArgumentParser()
193
+ args = get_args(parser)
194
+ args.top_p = p
195
+ args.top_k = k
196
+ args.temperature = t
197
+ audio, midi, pdf, xml, mxl, tunes, jpg = generate_music(args, TEYVAT[region])
198
 
199
+ except Exception as e:
200
+ status = f"{e}"
 
 
 
 
 
 
201
 
202
+ return status, audio, midi, pdf, xml, mxl, tunes, jpg
203
 
204
 
205
  if __name__ == "__main__":
206
  warnings.filterwarnings("ignore")
207
+ opts = list(TEYVAT.values()) if EN_US else list(TEYVAT.keys())
208
  gr.Interface(
209
  fn=infer,
210
  inputs=[
211
+ gr.Slider(0.01, 1.0, 0.8, step=0.01, label=_L("Top-P 采样")),
212
+ gr.Slider(0, 80, 8, step=1, label=_L("Top-K 采样 (0 为关闭)")),
213
+ gr.Slider(0.01, 2.0, 1.2, step=0.01, label=_L("温度参数")),
214
  gr.Dropdown(
215
+ choices=opts,
216
+ value=opts[0],
217
+ label=_L("地区风格"),
218
  ),
219
  ],
220
  outputs=[
221
+ gr.Textbox(label=_L("状态栏"), show_copy_button=True),
222
+ gr.Audio(label=_L("音频"), type="filepath"),
223
+ gr.File(label=_L("下载 MIDI")),
224
+ gr.File(label=_L("下载 PDF 乐谱")),
225
+ gr.File(label=_L("下载 MusicXML")),
226
+ gr.File(label=_L("下载 MXL")),
227
+ gr.Textbox(label=_L("ABC 记谱"), show_copy_button=True),
228
+ gr.Image(label=_L("五线谱"), type="filepath", show_share_button=False),
229
  ],
230
  flagging_mode="never",
231
+ title=_L("原神音乐生成"),
232
+ description=_L(
233
+ """
234
+ 欢迎使用此创空间, 此创空间基于 Tunesformer 开源项目制作,完全免费。当前模型还在调试中,计划在原神主线杀青后,所有国家地区角色全部开放后,二创音乐会齐全且样本均衡,届时重新微调模型并添加现实风格筛选辅助游戏各国家输出强化学习,以提升输出区分度与质量。注:崩铁方面数据工程正在运作中,未来也希望随主线杀青而基线化。<br>
235
+ 数据来源: <a href="https://musescore.org">MuseScore</a> 标签来源: <a href="https://genshin-impact.fandom.com/wiki/Genshin_Impact_Wiki">Genshin Impact Wiki | Fandom</a> 模型基础: <a href="https://github.com/sander-wood/tunesformer">Tunesformer</a>
236
+ """
237
+ ),
238
+ ).launch(ssr_mode=False)
utils.py CHANGED
@@ -4,14 +4,61 @@ import time
4
  import torch
5
  import requests
6
  import subprocess
 
 
7
  from tqdm import tqdm
8
- from huggingface_hub import snapshot_download
9
 
10
- TEYVAT = ["Mondstadt", "Liyue", "Inazuma", "Sumeru", "Fontaine", "Natlan"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  WEIGHTS_PATH = (
12
- snapshot_download("Genius-Society/hoyoMusic", cache_dir="./__pycache__")
13
- + "/weights.pth"
14
- )
 
 
 
 
 
 
 
 
15
  DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
16
  TEMP_DIR = "./__pycache__/tmp"
17
  PATCH_LENGTH = 128 # Patch Length
 
4
  import torch
5
  import requests
6
  import subprocess
7
+ import modelscope
8
+ import huggingface_hub
9
  from tqdm import tqdm
 
10
 
11
+ EN_US = os.getenv("LANG") != "zh_CN.UTF-8"
12
+
13
+ ZH2EN = {
14
+ "Top-P 采样": "Top-P sample",
15
+ "Top-K 采样 (0 为关闭)": "Top-K sample (0=closed)",
16
+ "温度参数": "Temperature",
17
+ "地区风格": "Region",
18
+ "状态栏": "Status",
19
+ "音频": "Audio",
20
+ "下载 MIDI": "Download MIDI",
21
+ "下载 PDF 乐谱": "Download PDF",
22
+ "下载 MusicXML": "Download MusicXML",
23
+ "下载 MXL": "Download MXL",
24
+ "ABC 记谱": "ABC notation",
25
+ "五线谱": "Staff",
26
+ "原神音乐生成": "Genshin Music Generation",
27
+ """
28
+ 欢迎使用此创空间, 此创空间基于 Tunesformer 开源项目制作,完全免费。当前模型还在调试中,计划在原神主线杀青后,所有国家地区角色全部开放后,二创音乐会齐全且样本均衡,届时重新微调模型并添加现实风格筛选辅助游戏各国家输出强化学习,以提升输出区分度与质量。注:崩铁方面数据工程正在运作中,未来也希望随主线杀青而基线化。<br>
29
+ 数据来源: <a href="https://musescore.org">MuseScore</a> 标签来源: <a href="https://genshin-impact.fandom.com/wiki/Genshin_Impact_Wiki">Genshin Impact Wiki | Fandom</a> 模型基础: <a href="https://github.com/sander-wood/tunesformer">Tunesformer</a>
30
+ """: """
31
+ Welcome to this space based on the Tunesformer open source project, which is totally free! The current model is still in debugging, the plan is in the Genshin Impact after the main line is killed, all countries and regions after all the characters are open, the second creation of the concert will be complete and the sample is balanced, at that time to re-fine-tune the model and add the reality of the style of screening to assist in the game of each country's output to strengthen the learning in order to enhance the output differentiation and quality. Note: Data engineering on the Star Rail is in operation, and will hopefully be baselined in the future as well with the mainline kill.<br>
32
+ Data source: <a href="https://musescore.org">MuseScore</a> Tags source: <a href="https://genshin-impact.fandom.com/wiki/Genshin_Impact_Wiki">Genshin Impact Wiki | Fandom</a> Model base: <a href="https://github.com/sander-wood/tunesformer">Tunesformer</a>
33
+ """,
34
+ }
35
+
36
+
37
+ def _L(zh_txt: str):
38
+ return ZH2EN[zh_txt] if EN_US else zh_txt
39
+
40
+
41
+ TEYVAT = {
42
+ "蒙德": "Mondstadt",
43
+ "璃月": "Liyue",
44
+ "稻妻": "Inazuma",
45
+ "须弥": "Sumeru",
46
+ "枫丹": "Fontaine",
47
+ "纳塔": "Teyvat",
48
+ }
49
+
50
  WEIGHTS_PATH = (
51
+ huggingface_hub.snapshot_download(
52
+ "Genius-Society/hoyoMusic",
53
+ cache_dir="./__pycache__",
54
+ )
55
+ if EN_US
56
+ else modelscope.snapshot_download(
57
+ "Genius-Society/hoyoMusic",
58
+ cache_dir="./__pycache__",
59
+ )
60
+ ) + "/weights.pth"
61
+
62
  DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
63
  TEMP_DIR = "./__pycache__/tmp"
64
  PATCH_LENGTH = 128 # Patch Length