Spaces:
Running
Running
init
Browse files- app.py +80 -0
- output.mid +0 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModel
|
3 |
+
from miditok import MusicTokenizer
|
4 |
+
import torch
|
5 |
+
|
6 |
+
import numpy as np
|
7 |
+
import pretty_midi
|
8 |
+
from io import BytesIO
|
9 |
+
import soundfile as sf
|
10 |
+
|
11 |
+
import requests
|
12 |
+
|
13 |
+
# URL of the file to download
|
14 |
+
url = "https://raw.githubusercontent.com/urish/cinto/refs/heads/master/media/FluidR3%20GM.sf2"
|
15 |
+
|
16 |
+
# Local filename to save the file
|
17 |
+
filename = "FluidR3_GM.sf2"
|
18 |
+
|
19 |
+
|
20 |
+
response = requests.get(url)
|
21 |
+
response.raise_for_status()
|
22 |
+
|
23 |
+
with open(filename, "wb") as file:
|
24 |
+
file.write(response.content)
|
25 |
+
|
26 |
+
|
27 |
+
def score_to_audio(score, sample_rate: int = 44100) -> tuple[int, np.ndarray]:
|
28 |
+
"""
|
29 |
+
Convert a symusic Score to integer PCM audio data.
|
30 |
+
|
31 |
+
Args:
|
32 |
+
score (Score): symusic Score object
|
33 |
+
sample_rate (int): desired sample rate in Hz, defaults to 44100
|
34 |
+
|
35 |
+
Returns:
|
36 |
+
tuple[int, np.ndarray]: Tuple of (sample_rate, audio_data as int16)
|
37 |
+
"""
|
38 |
+
# Get MIDI bytes and create MIDI object
|
39 |
+
midi_data = BytesIO(score.dumps_midi())
|
40 |
+
pm = pretty_midi.PrettyMIDI(midi_data)
|
41 |
+
|
42 |
+
# Synthesize to float array first
|
43 |
+
# float_audio = pm.synthesize(fs=sample_rate)
|
44 |
+
float_audio = pm.fluidsynth(
|
45 |
+
fs=sample_rate, sf2_path="./FluidR3_GM.sf2"
|
46 |
+
)
|
47 |
+
|
48 |
+
# Convert to 16-bit integer PCM
|
49 |
+
# Scale to full int16 range (-32768 to 32767)
|
50 |
+
int_audio = (float_audio * 32767).astype(np.int16)
|
51 |
+
|
52 |
+
return sample_rate, float_audio
|
53 |
+
|
54 |
+
|
55 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
56 |
+
|
57 |
+
tokenizer = MusicTokenizer.from_pretrained("shikhr/music_maker")
|
58 |
+
|
59 |
+
model = AutoModel.from_pretrained("shikhr/music_maker", trust_remote_code=True)
|
60 |
+
model.to(device)
|
61 |
+
|
62 |
+
|
63 |
+
def generate_music():
|
64 |
+
# Generate some music
|
65 |
+
out = model.generate(
|
66 |
+
torch.tensor([[1]]).to(device), max_new_tokens=400, temperature=1.0, top_k=None
|
67 |
+
)
|
68 |
+
|
69 |
+
# Save the generated MIDI
|
70 |
+
res = tokenizer(out[0].tolist())
|
71 |
+
res.dump_midi("output.mid")
|
72 |
+
nx = score_to_audio(res)
|
73 |
+
# print(nx)
|
74 |
+
return "Generated" , nx
|
75 |
+
|
76 |
+
|
77 |
+
demo = gr.Interface(generate_music, inputs=[], outputs=["text", "audio"], flagging_mode="never")
|
78 |
+
|
79 |
+
if __name__ == "__main__":
|
80 |
+
demo.launch()
|
output.mid
ADDED
Binary file (1.51 kB). View file
|
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
miditok
|
3 |
+
torch
|
4 |
+
pretty_midi
|