Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,50 +1,20 @@
|
|
1 |
-
import gradio
|
2 |
-
from
|
3 |
-
from musiclang import Score
|
4 |
-
from midi2audio import FluidSynth
|
5 |
-
import os
|
6 |
-
import tempfile
|
7 |
-
|
8 |
-
|
9 |
-
def inner_loop(midi_file, chord_progression, tempo, temperature, nb_tokens, bar_range):
|
10 |
-
top_p = 0.98
|
11 |
-
seed = 0
|
12 |
-
# Initialize the MusicLangPredictor
|
13 |
-
ml = MusicLangPredictor('musiclang/musiclang-v2')
|
14 |
-
tempo_message = "" # Default message if no MIDI file is uploaded
|
15 |
-
time_signature = (4, 4)
|
16 |
-
if midi_file is not None and midi_file != "":
|
17 |
-
# Load the MIDI file and use it as the score prompt
|
18 |
-
filepath = midi_file
|
19 |
-
start_bar, end_bar = map(int, bar_range.split("-"))
|
20 |
-
score = Score.from_midi(filepath, chord_range=(start_bar, end_bar))
|
21 |
-
tempo = score.config['tempo'] # Use the tempo from the MIDI file and change input
|
22 |
-
time_signature = score.config['time_signature']
|
23 |
-
time_signature = (time_signature[1], time_signature[2])
|
24 |
-
tempo_message = f"Warning : real tempo of file is : {int(tempo)} BPM." # Update message based on MIDI file
|
25 |
-
else:
|
26 |
-
score = None # Default score is None if no MIDI file is uploaded
|
27 |
-
|
28 |
-
# Generate the score based on provided inputs and the uploaded MIDI file if available
|
29 |
-
if chord_progression.strip() == "" and score is None:
|
30 |
-
# Generate without specific chord progression or MIDI prompt
|
31 |
-
generated_score = ml.predict(
|
32 |
-
nb_tokens=int(nb_tokens),
|
33 |
temperature=float(temperature),
|
34 |
topp=top_p,
|
35 |
rng_seed=seed
|
36 |
-
)
|
37 |
-
|
38 |
-
|
39 |
generated_score = ml.predict(
|
40 |
score=score, # Use the uploaded MIDI as the score prompt
|
41 |
nb_tokens=int(nb_tokens),
|
42 |
temperature=float(temperature),
|
43 |
topp=top_p,
|
44 |
rng_seed=seed
|
45 |
-
)
|
46 |
-
|
47 |
-
|
48 |
generated_score = ml.predict_chords(
|
49 |
chord_progression,
|
50 |
score=score, # Use the uploaded MIDI as the score prompt
|
@@ -53,14 +23,11 @@ def inner_loop(midi_file, chord_progression, tempo, temperature, nb_tokens, bar_
|
|
53 |
topp=top_p,
|
54 |
rng_seed=seed
|
55 |
)
|
56 |
-
|
57 |
chord_repr = generated_score.to_chord_repr()
|
58 |
-
|
59 |
# Save the generated score as a MIDI file
|
60 |
temp_midi_file = tempfile.NamedTemporaryFile(suffix=".mid", delete=False)
|
61 |
midi_path = temp_midi_file.name
|
62 |
generated_score.to_midi(midi_path, tempo=tempo, time_signature=time_signature)
|
63 |
-
|
64 |
# Convert MIDI to WAV then WAV to MP3 for playback
|
65 |
temp_wav_file = tempfile.NamedTemporaryFile(suffix=".wav", delete=False)
|
66 |
temp_mp3_file = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
@@ -68,37 +35,32 @@ def inner_loop(midi_file, chord_progression, tempo, temperature, nb_tokens, bar_
|
|
68 |
mp3_path = temp_mp3_file.name
|
69 |
FluidSynth("/usr/share/sounds/sf2/FluidR3_GM.sf2").midi_to_audio(midi_path, wav_path)
|
70 |
os.system(f'ffmpeg -i {wav_path} -acodec libmp3lame -y -loglevel quiet -stats {mp3_path}')
|
71 |
-
|
72 |
# Remove the temporary WAV file
|
73 |
os.remove(wav_path)
|
74 |
-
|
75 |
return mp3_path, midi_path, chord_repr, tempo_message
|
76 |
-
|
77 |
def musiclang(midi_file, chord_progression, tempo, temperature, nb_tokens, bar_range):
|
78 |
exception = None
|
79 |
-
mp3_path, midi_path, chord_repr, tempo_message = None, None, None, ""
|
80 |
-
|
81 |
-
mp3_path, midi_path, chord_repr, tempo_message = inner_loop(midi_file, chord_progression, tempo, temperature, nb_tokens, bar_range)
|
82 |
-
|
83 |
-
exception = "Error : " + e.__class__.__name__ + " " + str(e)
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
# Introductory text
|
90 |
gr.Markdown("""
|
91 |
# Controllable Symbolic Music Generation with MusicLang Predict
|
92 |
[MusicLang Predict](https://github.com/musiclang/musiclang_predict) offers advanced controllability features and high-quality music generation by manipulating symbolic music.
|
93 |
You can for example use it to continue your composition with a specific chord progression.
|
94 |
""")
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
with gr.Row():
|
99 |
midi_file = gr.File(label="Prompt MIDI File (Optional)", type="filepath", file_types=[".mid", ".midi"],
|
100 |
-
elem_id='midi_file_input')
|
101 |
-
|
102 |
bar_range = gr.Textbox(label="Bar Range of input file (eg: 0-4 for first four bars)", placeholder="0-4",
|
103 |
value="0-4", elem_id='bar_range_input')
|
104 |
nb_tokens = gr.Number(label="Nb Tokens",
|
@@ -109,15 +71,12 @@ with gr.Blocks() as demo:
|
|
109 |
visible=False,
|
110 |
minimum=0.1, maximum=1.0, step=0.1, elem_id='temperature_input')
|
111 |
tempo = gr.Slider(label="Tempo", value=120, minimum=60, maximum=240, step=1, elem_id='tempo_input')
|
112 |
-
|
113 |
with gr.Row():
|
114 |
chord_progression = gr.Textbox(
|
115 |
label="Chord Progression (Optional)",
|
116 |
placeholder="Am CM Dm7/F E7 Asus4", lines=2, value="", elem_id='chord_progression_input')
|
117 |
-
|
118 |
with gr.Row():
|
119 |
generate_btn = gr.Button("Generate", elem_id='generate_button')
|
120 |
-
|
121 |
with gr.Column():
|
122 |
info_message = gr.Textbox(label="Info Message", elem_id='info_message_output')
|
123 |
generated_music = gr.Audio(label="Preview generated Music", elem_id='generated_music_output')
|
@@ -126,11 +85,9 @@ with gr.Blocks() as demo:
|
|
126 |
fn=musiclang,
|
127 |
inputs=[midi_file, chord_progression, tempo, temperature, nb_tokens, bar_range],
|
128 |
outputs=[generated_music, generated_midi, info_message]
|
129 |
-
)
|
130 |
-
|
131 |
-
|
132 |
-
with gr.Row():
|
133 |
-
with gr.Column():
|
134 |
gr.Markdown("## Examples")
|
135 |
gr.Examples(
|
136 |
examples=[["/home/user/app/bach_847.mid", "", 120, 0.95, 512, "0-4"],
|
@@ -146,5 +103,4 @@ with gr.Blocks() as demo:
|
|
146 |
fn=musiclang,
|
147 |
cache_examples=True,
|
148 |
)
|
149 |
-
|
150 |
-
demo.launch()
|
|
|
1 |
+
import gradio
|
2 |
+
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
temperature=float(temperature),
|
4 |
topp=top_p,
|
5 |
rng_seed=seed
|
6 |
+
)
|
7 |
+
elif score is not None and chord_progression.strip() == "":
|
8 |
+
# Generate using the uploaded MIDI file as a prompt
|
9 |
generated_score = ml.predict(
|
10 |
score=score, # Use the uploaded MIDI as the score prompt
|
11 |
nb_tokens=int(nb_tokens),
|
12 |
temperature=float(temperature),
|
13 |
topp=top_p,
|
14 |
rng_seed=seed
|
15 |
+
)
|
16 |
+
else:
|
17 |
+
# Generate with specific chord progression
|
18 |
generated_score = ml.predict_chords(
|
19 |
chord_progression,
|
20 |
score=score, # Use the uploaded MIDI as the score prompt
|
|
|
23 |
topp=top_p,
|
24 |
rng_seed=seed
|
25 |
)
|
|
|
26 |
chord_repr = generated_score.to_chord_repr()
|
|
|
27 |
# Save the generated score as a MIDI file
|
28 |
temp_midi_file = tempfile.NamedTemporaryFile(suffix=".mid", delete=False)
|
29 |
midi_path = temp_midi_file.name
|
30 |
generated_score.to_midi(midi_path, tempo=tempo, time_signature=time_signature)
|
|
|
31 |
# Convert MIDI to WAV then WAV to MP3 for playback
|
32 |
temp_wav_file = tempfile.NamedTemporaryFile(suffix=".wav", delete=False)
|
33 |
temp_mp3_file = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
|
|
35 |
mp3_path = temp_mp3_file.name
|
36 |
FluidSynth("/usr/share/sounds/sf2/FluidR3_GM.sf2").midi_to_audio(midi_path, wav_path)
|
37 |
os.system(f'ffmpeg -i {wav_path} -acodec libmp3lame -y -loglevel quiet -stats {mp3_path}')
|
|
|
38 |
# Remove the temporary WAV file
|
39 |
os.remove(wav_path)
|
|
|
40 |
return mp3_path, midi_path, chord_repr, tempo_message
|
|
|
41 |
def musiclang(midi_file, chord_progression, tempo, temperature, nb_tokens, bar_range):
|
42 |
exception = None
|
43 |
+
mp3_path, midi_path, chord_repr, tempo_message = None, None, None, ""
|
44 |
+
try:
|
45 |
+
mp3_path, midi_path, chord_repr, tempo_message = inner_loop(midi_file, chord_progression, tempo, temperature, nb_tokens, bar_range)
|
46 |
+
except Exception as e:
|
47 |
+
exception = "Error : " + e.__class__.__name__ + " " + str(e)
|
48 |
+
# Return the MP3 path for Gradio to display and the MIDI file path for download
|
49 |
+
return mp3_path, midi_path, exception
|
50 |
+
|
51 |
+
with gr.Blocks() as demo:
|
52 |
+
# Introductory text
|
|
|
53 |
gr.Markdown("""
|
54 |
# Controllable Symbolic Music Generation with MusicLang Predict
|
55 |
[MusicLang Predict](https://github.com/musiclang/musiclang_predict) offers advanced controllability features and high-quality music generation by manipulating symbolic music.
|
56 |
You can for example use it to continue your composition with a specific chord progression.
|
57 |
""")
|
58 |
+
with gr.Row():
|
59 |
+
with gr.Column():
|
60 |
+
with gr.Row():
|
|
|
61 |
midi_file = gr.File(label="Prompt MIDI File (Optional)", type="filepath", file_types=[".mid", ".midi"],
|
62 |
+
elem_id='midi_file_input')
|
63 |
+
with gr.Column():
|
64 |
bar_range = gr.Textbox(label="Bar Range of input file (eg: 0-4 for first four bars)", placeholder="0-4",
|
65 |
value="0-4", elem_id='bar_range_input')
|
66 |
nb_tokens = gr.Number(label="Nb Tokens",
|
|
|
71 |
visible=False,
|
72 |
minimum=0.1, maximum=1.0, step=0.1, elem_id='temperature_input')
|
73 |
tempo = gr.Slider(label="Tempo", value=120, minimum=60, maximum=240, step=1, elem_id='tempo_input')
|
|
|
74 |
with gr.Row():
|
75 |
chord_progression = gr.Textbox(
|
76 |
label="Chord Progression (Optional)",
|
77 |
placeholder="Am CM Dm7/F E7 Asus4", lines=2, value="", elem_id='chord_progression_input')
|
|
|
78 |
with gr.Row():
|
79 |
generate_btn = gr.Button("Generate", elem_id='generate_button')
|
|
|
80 |
with gr.Column():
|
81 |
info_message = gr.Textbox(label="Info Message", elem_id='info_message_output')
|
82 |
generated_music = gr.Audio(label="Preview generated Music", elem_id='generated_music_output')
|
|
|
85 |
fn=musiclang,
|
86 |
inputs=[midi_file, chord_progression, tempo, temperature, nb_tokens, bar_range],
|
87 |
outputs=[generated_music, generated_midi, info_message]
|
88 |
+
)
|
89 |
+
with gr.Row():
|
90 |
+
with gr.Column():
|
|
|
|
|
91 |
gr.Markdown("## Examples")
|
92 |
gr.Examples(
|
93 |
examples=[["/home/user/app/bach_847.mid", "", 120, 0.95, 512, "0-4"],
|
|
|
103 |
fn=musiclang,
|
104 |
cache_examples=True,
|
105 |
)
|
106 |
+
demo.launch()
|
|