sander-wood commited on
Commit
342a652
1 Parent(s): d237c55

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +104 -0
  2. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import random
4
+ from unidecode import unidecode
5
+ from samplings import top_p_sampling, temperature_sampling
6
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
7
+
8
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
+
10
+ description = """
11
+ <div>
12
+
13
+ <a style="display:inline-block" href='https://github.com/sander-wood/text-to-music'><img src='https://img.shields.io/github/stars/sander-wood/text-to-music?style=social' /></a>
14
+ <a style="display:inline-block" href="https://arxiv.org/pdf/2211.11216.pdf"><img src="https://img.shields.io/badge/arXiv-2211.11216-b31b1b.svg"></a>
15
+ </div>
16
+
17
+ ## ℹ️ How to use this demo?
18
+ 1. Enter a query in the text box.
19
+ 2. You can set the parameters (i.e., number of tunes, maximum length, top-p, temperature, and random seed) for the generation. (optional)
20
+ 3. Click "Submit" and wait for the result.
21
+ 4. The generated ABC notation can be converted to MIDI or PDF using [EasyABC](https://sourceforge.net/projects/easyabc/), you can also use this [online renderer](https://ldzhangyx.github.io/abc/) to render the ABC notation.
22
+
23
+ ## ❕Notice
24
+ - The text box is case-sensitive.
25
+ - The demo is based on BART-base and fine-tuned on the Textune dataset (282,870 text-music pairs).
26
+ - The demo only supports English text as the input.
27
+ - The demo is still in the early stage, and the generated music is not perfect. If you have any suggestions, please feel free to contact me via [email](mailto:shangda@mail.ccom.edu.cn).
28
+ """
29
+
30
+
31
+ examples = [
32
+ ["This is a traditional Irish dance music.\nNote Length-1/8\nMeter-6/8\nKey-D", 3, 1024, 0.9, 1.0, 0],
33
+ ["This is a jazz-swing lead sheet with chord and vocal.", 3, 1024, 0.9, 1.0, 0]
34
+ ]
35
+
36
+
37
+ def generate_abc(text, num_tunes, max_length, top_p, temperature, seed):
38
+
39
+ try:
40
+ seed = int(seed)
41
+ except:
42
+ seed = None
43
+
44
+ text = unidecode(text)
45
+ tokenizer = AutoTokenizer.from_pretrained('sander-wood/text-to-music')
46
+ model = AutoModelForSeq2SeqLM.from_pretrained('sander-wood/text-to-music')
47
+ model = model.to(device)
48
+
49
+ input_ids = tokenizer(text,
50
+ return_tensors='pt',
51
+ truncation=True,
52
+ max_length=max_length)['input_ids'].to(device)
53
+ decoder_start_token_id = model.config.decoder_start_token_id
54
+ eos_token_id = model.config.eos_token_id
55
+ random.seed(seed)
56
+ tunes = ""
57
+
58
+ for n_idx in range(num_tunes):
59
+ print("\nX:"+str(n_idx+1)+"\n", end="")
60
+ tunes += "X:"+str(n_idx+1)+"\n"
61
+ decoder_input_ids = torch.tensor([[decoder_start_token_id]])
62
+
63
+ for t_idx in range(max_length):
64
+
65
+ if seed!=None:
66
+ n_seed = random.randint(0, 1000000)
67
+ random.seed(n_seed)
68
+ else:
69
+ n_seed = None
70
+ outputs = model(input_ids=input_ids,
71
+ decoder_input_ids=decoder_input_ids.to(device))
72
+ probs = outputs.logits[0][-1]
73
+ probs = torch.nn.Softmax(dim=-1)(probs).cpu().detach().numpy()
74
+ sampled_id = temperature_sampling(probs=top_p_sampling(probs,
75
+ top_p=top_p,
76
+ seed=n_seed,
77
+ return_probs=True),
78
+ seed=n_seed,
79
+ temperature=temperature)
80
+ decoder_input_ids = torch.cat((decoder_input_ids, torch.tensor([[sampled_id]])), 1)
81
+ if sampled_id!=eos_token_id:
82
+ sampled_token = tokenizer.decode([sampled_id])
83
+ print(sampled_token, end="")
84
+ tunes += sampled_token
85
+ else:
86
+ tunes += '\n'
87
+ break
88
+
89
+ return tunes
90
+
91
+ input_text = gr.inputs.Textbox(lines=5, label="Input Text", placeholder="Describe the music you want to generate ...")
92
+ input_num_tunes = gr.inputs.Slider(minimum=1, maximum=10, step=1, default=1, label="Number of Tunes")
93
+ input_max_length = gr.inputs.Slider(minimum=10, maximum=1000, step=10, default=500, label="Max Length")
94
+ input_top_p = gr.inputs.Slider(minimum=0.0, maximum=1.0, step=0.05, default=0.9, label="Top P")
95
+ input_temperature = gr.inputs.Slider(minimum=0.0, maximum=2.0, step=0.1, default=1.0, label="Temperature")
96
+ input_seed = gr.inputs.Textbox(lines=1, label="Seed (int)", default="None")
97
+ output_abc = gr.outputs.Textbox(label="Generated Tunes")
98
+
99
+ gr.Interface(fn=generate_abc,
100
+ inputs=[input_text, input_num_tunes, input_max_length, input_top_p, input_temperature, input_seed],
101
+ outputs=output_abc,
102
+ title="Textune: Generating Tune from Text",
103
+ description=description,
104
+ examples=examples).launch(debug=True)
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ transformers==4.18.0
2
+ --find-links https://download.pytorch.org/whl/torch_stable.html
3
+ torch==1.9.1+cu111
4
+ -f https://download.pytorch.org/whl/torch_stable.html
5
+ torchvision==0.10.1+cu111
6
+ unidecode==1.3.4
7
+ samplings==0.1.7