upload all files
Browse files- .gitattributes +1 -0
- README.md +1 -12
- app.py +201 -0
- mapping.json +34 -0
- mel.mid +0 -0
- model.keras +3 -0
- requirements.txt +11 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
model.keras filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
@@ -1,12 +1 @@
|
|
1 |
-
|
2 |
-
title: Tunegenie
|
3 |
-
emoji: 📊
|
4 |
-
colorFrom: gray
|
5 |
-
colorTo: gray
|
6 |
-
sdk: streamlit
|
7 |
-
sdk_version: 1.36.0
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
-
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
+
![image](https://github.com/kumarkse/MusicGen/assets/109473805/f41b7930-22f6-4df6-b67c-45edfd7a35b2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
ADDED
@@ -0,0 +1,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
import numpy as np
|
4 |
+
import music21 as m21
|
5 |
+
import streamlit as st
|
6 |
+
import tensorflow.keras as keras
|
7 |
+
|
8 |
+
|
9 |
+
MAPPING_PATH = "mapping.json"
|
10 |
+
SEQUENCE_LENGTH = 64
|
11 |
+
|
12 |
+
|
13 |
+
|
14 |
+
class MelodyGenerator:
|
15 |
+
|
16 |
+
def __init__( self, model_path = "model.keras" ):
|
17 |
+
|
18 |
+
self.model_path = model_path
|
19 |
+
self.model = keras.models.load_model(model_path)
|
20 |
+
|
21 |
+
|
22 |
+
with open(MAPPING_PATH , "r") as fp:
|
23 |
+
self._mappings = json.load(fp)
|
24 |
+
|
25 |
+
self._start_symbol = ["/"] * SEQUENCE_LENGTH
|
26 |
+
|
27 |
+
|
28 |
+
|
29 |
+
def generate_melody(self, seed, num_steps, max_sequence_length, temperature):
|
30 |
+
|
31 |
+
# create seed with start symbols
|
32 |
+
seed = seed.split()
|
33 |
+
melody = seed
|
34 |
+
seed = self._start_symbol + seed
|
35 |
+
|
36 |
+
# map seed to int
|
37 |
+
seed = [self._mappings[symbol] for symbol in seed]
|
38 |
+
|
39 |
+
|
40 |
+
|
41 |
+
for _ in range( num_steps ):
|
42 |
+
|
43 |
+
# limit the seed to max_sequence_length
|
44 |
+
seed = seed[-max_sequence_length :]
|
45 |
+
|
46 |
+
|
47 |
+
#one-hot encode the seed
|
48 |
+
onehot_seed = keras.utils.to_categorical( seed, num_classes=len(self._mappings)+1)
|
49 |
+
|
50 |
+
# currently the onehot_seed is a 2d array having shape : ( max_sequence_length , num of symbol in the vocabulary )
|
51 |
+
# but generaly model.predict accepts a 3d array beacause it calculates probability for bunch of data not for 1 so . we are adding a extra dimention
|
52 |
+
onehot_seed = onehot_seed[np.newaxis , ...]
|
53 |
+
|
54 |
+
# make a prediction
|
55 |
+
|
56 |
+
probabilities = self.model.predict( onehot_seed )[0]
|
57 |
+
# [0.1, 0.2, 0.1, 0.6] -> 1
|
58 |
+
|
59 |
+
output_int = self._sample_with_temperature(probabilities , temperature)
|
60 |
+
|
61 |
+
#update the seed
|
62 |
+
seed.append(output_int)
|
63 |
+
|
64 |
+
# map int to our encodeing
|
65 |
+
output_symbol = [ k for k,v in self._mappings.items() if v == output_int ][0]
|
66 |
+
|
67 |
+
# check whether we'r at the end of a melody
|
68 |
+
if output_symbol == "/":
|
69 |
+
break
|
70 |
+
|
71 |
+
# update the melody
|
72 |
+
else :
|
73 |
+
melody.append(output_symbol)
|
74 |
+
|
75 |
+
|
76 |
+
|
77 |
+
|
78 |
+
return melody
|
79 |
+
|
80 |
+
|
81 |
+
|
82 |
+
def _sample_with_temperature( self, probabilities , temperature ):
|
83 |
+
|
84 |
+
# temperature -> infinity
|
85 |
+
# temperature -> 0
|
86 |
+
# temperature = 1
|
87 |
+
|
88 |
+
predictions = np.log(probabilities) / temperature
|
89 |
+
probabilities = np.exp( predictions ) / np.sum(np.exp(predictions))
|
90 |
+
|
91 |
+
|
92 |
+
choices = range(len(probabilities))
|
93 |
+
index = np.random.choice(choices , p = probabilities)
|
94 |
+
|
95 |
+
return index
|
96 |
+
|
97 |
+
|
98 |
+
|
99 |
+
def save_melody( self, melody ,step_duration = 0.25, format = "midi" , file_name = "mel.mid") :
|
100 |
+
|
101 |
+
#create a music21 stream
|
102 |
+
stream = m21.stream.Stream()
|
103 |
+
|
104 |
+
|
105 |
+
#parse all the symbol in the melody and create note/rest objects
|
106 |
+
# 60 _ _ _ r _ 62 _
|
107 |
+
start_symbol = None
|
108 |
+
step_counter = 1
|
109 |
+
|
110 |
+
|
111 |
+
for i, symbol in enumerate(melody) :
|
112 |
+
|
113 |
+
#handle case in which we have a note/rest
|
114 |
+
if symbol != "_" or i + 1 == len(melody):
|
115 |
+
|
116 |
+
#ensure we're dealing with note/rest beyond the first one
|
117 |
+
if start_symbol is not None:
|
118 |
+
|
119 |
+
quarter_length_duration = step_duration * step_counter # 0.25 * 4 = 1
|
120 |
+
|
121 |
+
# handle rest
|
122 |
+
if start_symbol == "r":
|
123 |
+
m21_event = m21.note.Rest( quarterLength = quarter_length_duration)
|
124 |
+
|
125 |
+
#hadle notes
|
126 |
+
else :
|
127 |
+
m21_event = m21.note.Note( int(start_symbol) , quaterLegth = quarter_length_duration )
|
128 |
+
|
129 |
+
|
130 |
+
stream.append(m21_event)
|
131 |
+
|
132 |
+
# reset the step counter
|
133 |
+
step_counter = 1
|
134 |
+
|
135 |
+
|
136 |
+
start_symbol = symbol
|
137 |
+
|
138 |
+
|
139 |
+
# handle case in which we have a prolongation sign "_"
|
140 |
+
else :
|
141 |
+
step_counter = step_counter + 1
|
142 |
+
|
143 |
+
|
144 |
+
|
145 |
+
# write the m21 strem to a midi file
|
146 |
+
stream.write( format , file_name )
|
147 |
+
|
148 |
+
|
149 |
+
|
150 |
+
# app initiallize
|
151 |
+
|
152 |
+
# st.title('Melody Craft 1.0')
|
153 |
+
|
154 |
+
st.markdown(
|
155 |
+
"""
|
156 |
+
<style>
|
157 |
+
/* Define keyframes for the animation */
|
158 |
+
@keyframes glow {
|
159 |
+
0% { text-shadow: 0 0 10px #fff, 0 0 20px #fff, 0 0 30px #fff, 0 0 40px #ff00e4, 0 0 70px #ff00e4, 0 0 80px #ff00e4, 0 0 100px #ff00e4, 0 0 150px #ff00e4; }
|
160 |
+
50% { text-shadow: 0 0 20px #fff, 0 0 30px #fff, 0 0 40px #ff00e4, 0 0 50px #ff00e4, 0 0 80px #ff00e4, 0 0 90px #ff00e4, 0 0 110px #ff00e4, 0 0 160px #ff00e4; }
|
161 |
+
100% { text-shadow: 0 0 10px #fff, 0 0 20px #fff, 0 0 30px #fff, 0 0 40px #ff00e4, 0 0 70px #ff00e4, 0 0 80px #ff00e4, 0 0 100px #ff00e4, 0 0 150px #ff00e4; }
|
162 |
+
}
|
163 |
+
|
164 |
+
/* Apply animation to the title */
|
165 |
+
.glowing-title {
|
166 |
+
font-size: 6rem;
|
167 |
+
font-weight: bold;
|
168 |
+
color: #ff00e4;
|
169 |
+
animation: glow 2s ease-in-out infinite;
|
170 |
+
}
|
171 |
+
</style>
|
172 |
+
"""
|
173 |
+
, unsafe_allow_html=True
|
174 |
+
)
|
175 |
+
st.markdown('<h1 class="glowing-title">Tune Genie</h1>', unsafe_allow_html=True)
|
176 |
+
|
177 |
+
|
178 |
+
|
179 |
+
seeds ={ 'seed1' : "60 _ _ _ _ _ _ _ 60 _ _ _ _ _ _ _ 60 _ _ _ _ _ _ _ 62",'seed2' : "69 _ _ _ _ _ _ _",'seed3' : "69 _ _ _ 69 _ _ _ 69 _ _ _ 72 _ _ _ 72 _ _ _ 69",'seed4' : "76 _ _ _ 76 _ _ _ _ _ 76 _ 76 _ _ _ 79 _ _ _ 74 ",'seed5': "71 _ _ _ 69 _" }
|
180 |
+
|
181 |
+
|
182 |
+
seed = st.selectbox( 'choose seed ' , ('seed1','seed2','seed3','seed4','seed5','Try your own seed' ))
|
183 |
+
|
184 |
+
|
185 |
+
if seed == "Try your own seed":
|
186 |
+
seed = st.text_input("Enter your custom seed:")
|
187 |
+
else:
|
188 |
+
seed = seeds[seed]
|
189 |
+
|
190 |
+
|
191 |
+
if st.button('GENERATE'):
|
192 |
+
mg = MelodyGenerator()
|
193 |
+
melody = mg.generate_melody( seed , 200, SEQUENCE_LENGTH , 0.4)
|
194 |
+
print(melody)
|
195 |
+
mg.save_melody(melody)
|
196 |
+
st.download_button(
|
197 |
+
label="Download Midi File",
|
198 |
+
data=open("mel.mid", "rb").read(),
|
199 |
+
file_name="example.midi",
|
200 |
+
mime="audio/midi"
|
201 |
+
)
|
mapping.json
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"75": 0,
|
3 |
+
"78": 1,
|
4 |
+
"59": 2,
|
5 |
+
"84": 3,
|
6 |
+
"_": 4,
|
7 |
+
"57": 5,
|
8 |
+
"69": 6,
|
9 |
+
"66": 7,
|
10 |
+
"64": 8,
|
11 |
+
"55": 9,
|
12 |
+
"83": 10,
|
13 |
+
"72": 11,
|
14 |
+
"63": 12,
|
15 |
+
"77": 13,
|
16 |
+
"76": 14,
|
17 |
+
"79": 15,
|
18 |
+
"70": 16,
|
19 |
+
"73": 17,
|
20 |
+
"80": 18,
|
21 |
+
"68": 19,
|
22 |
+
"74": 20,
|
23 |
+
"62": 21,
|
24 |
+
"/": 22,
|
25 |
+
"52": 23,
|
26 |
+
"67": 24,
|
27 |
+
"58": 25,
|
28 |
+
"53": 26,
|
29 |
+
"r": 27,
|
30 |
+
"81": 28,
|
31 |
+
"60": 29,
|
32 |
+
"71": 30,
|
33 |
+
"65": 31
|
34 |
+
}
|
mel.mid
ADDED
Binary file (108 Bytes). View file
|
|
model.keras
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c475eced0d41753655bb84186fea05bf2a8cf2087d1b4b5dfa62077764416cdd
|
3 |
+
size 3688948
|
requirements.txt
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gitdb==4.0.11
|
2 |
+
GitPython==3.1.43
|
3 |
+
jsonpickle==3.0.4
|
4 |
+
jsonschema==4.22.0
|
5 |
+
jsonschema-specifications==2023.12.1
|
6 |
+
keras==3.3.3
|
7 |
+
music21==9.1.0
|
8 |
+
numpy==1.26.4
|
9 |
+
tensorflow==2.16.1
|
10 |
+
streamlit==1.34.0
|
11 |
+
|