Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,76 +1,120 @@
|
|
1 |
-
import gradio
|
2 |
from transformers import pipeline
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
with
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
59 |
-
with gr.
|
60 |
-
with gr.
|
|
|
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 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
with gr.Row():
|
75 |
chord_progression = gr.Textbox(
|
76 |
label="Chord Progression (Optional)",
|
@@ -81,13 +125,15 @@ 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')
|
83 |
generated_midi = gr.File(label="Download MIDI", elem_id='generated_midi_output')
|
|
|
84 |
generate_btn.click(
|
85 |
-
fn=
|
86 |
inputs=[midi_file, chord_progression, tempo, temperature, nb_tokens, bar_range],
|
87 |
outputs=[generated_music, generated_midi, info_message]
|
88 |
-
)
|
89 |
-
|
90 |
-
with gr.
|
|
|
91 |
gr.Markdown("## Examples")
|
92 |
gr.Examples(
|
93 |
examples=[["/home/user/app/bach_847.mid", "", 120, 0.95, 512, "0-4"],
|
@@ -100,7 +146,8 @@ with gr.Column():
|
|
100 |
],
|
101 |
inputs=[midi_file, chord_progression, tempo, temperature, nb_tokens, bar_range],
|
102 |
outputs=[generated_music, generated_midi, info_message],
|
103 |
-
fn=
|
104 |
cache_examples=True,
|
105 |
)
|
|
|
106 |
demo.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
+
import tempfile, os
|
4 |
+
from midi2audio import FluidSynth
|
5 |
+
|
6 |
+
# --- Music Generation Logic (API-like Function) ---
|
7 |
+
|
8 |
+
def generate_music_api(midi_data=None, chord_progression=None, tempo=120, temperature=0.95, nb_tokens=512, bar_range="0-4"):
|
9 |
+
try:
|
10 |
+
# Load the MusicLang Predict model (replace with actual loading code)
|
11 |
+
ml = ... # Example: ml = pipeline("music-generation", model="your-musiclang-predict-model")
|
12 |
+
|
13 |
+
# Handle different generation scenarios based on inputs
|
14 |
+
if midi_data is not None and chord_progression.strip() != "":
|
15 |
+
# Continue sequence with chord progression
|
16 |
+
generated_score = ml.continue_sequence(
|
17 |
+
midi_data,
|
18 |
+
chord_progression=chord_progression,
|
19 |
+
nb_tokens=int(nb_tokens),
|
20 |
+
temperature=float(temperature),
|
21 |
+
# ... other parameters
|
22 |
+
)
|
23 |
+
elif midi_data is not None and chord_progression.strip() == "":
|
24 |
+
# Generate using the uploaded MIDI file as a prompt
|
25 |
+
generated_score = ml.predict(
|
26 |
+
midi_data, # Use the uploaded MIDI as the prompt
|
27 |
+
nb_tokens=int(nb_tokens),
|
28 |
+
temperature=float(temperature),
|
29 |
+
# ... other parameters
|
30 |
+
)
|
31 |
+
else:
|
32 |
+
# Generate with specific chord progression
|
33 |
+
generated_score = ml.predict_chords(
|
34 |
+
chord_progression,
|
35 |
+
# ... other parameters
|
36 |
+
)
|
37 |
+
|
38 |
+
# Save generated files to temporary locations
|
39 |
+
temp_midi_file = tempfile.NamedTemporaryFile(suffix=".mid", delete=False)
|
40 |
+
midi_path = temp_midi_file.name
|
41 |
+
generated_score.to_midi(midi_path, tempo=tempo, time_signature=time_signature) # Assuming time_signature is defined
|
42 |
+
|
43 |
+
temp_mp3_file = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
44 |
+
mp3_path = temp_mp3_file.name
|
45 |
+
# ... (convert MIDI to MP3 using FluidSynth and FFmpeg)
|
46 |
+
|
47 |
+
# Read binary data from the temporary files
|
48 |
+
with open(mp3_path, 'rb') as f_mp3:
|
49 |
+
mp3_binary = f_mp3.read()
|
50 |
+
|
51 |
+
with open(midi_path, 'rb') as f_midi:
|
52 |
+
midi_binary = f_midi.read()
|
53 |
+
|
54 |
+
# Remove temporary files
|
55 |
+
os.remove(mp3_path)
|
56 |
+
os.remove(midi_path)
|
57 |
+
|
58 |
+
return {
|
59 |
+
"mp3": mp3_binary,
|
60 |
+
"midi": midi_binary,
|
61 |
+
"chord_repr": chord_repr, # Assuming chord_repr is still needed
|
62 |
+
"tempo_message": tempo_message # Assuming tempo_message is still needed
|
63 |
+
}
|
64 |
+
|
65 |
+
except Exception as e:
|
66 |
+
return {"error": str(e)}
|
67 |
+
|
68 |
+
# --- Gradio Interface ---
|
69 |
+
|
70 |
+
def musiclang_gradio(midi_file, chord_progression, tempo, temperature, nb_tokens, bar_range):
|
71 |
+
midi_data = None
|
72 |
+
if midi_file:
|
73 |
+
with open(midi_file.name, "rb") as f:
|
74 |
+
midi_data = f.read()
|
75 |
+
|
76 |
+
api_response = generate_music_api(midi_data=midi_data, chord_progression=chord_progression, tempo=tempo, temperature=temperature, nb_tokens=nb_tokens, bar_range=bar_range)
|
77 |
+
|
78 |
+
if "error" in api_response:
|
79 |
+
return None, None, api_response["error"]
|
80 |
+
|
81 |
+
# Create temporary files for Gradio
|
82 |
+
mp3_path = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False).name
|
83 |
+
midi_path = tempfile.NamedTemporaryFile(suffix=".mid", delete=False).name
|
84 |
+
|
85 |
+
# Write binary data to temporary files
|
86 |
+
with open(mp3_path, "wb") as f:
|
87 |
+
f.write(api_response["mp3"])
|
88 |
+
|
89 |
+
with open(midi_path, "wb") as f:
|
90 |
+
f.write(api_response["midi"])
|
91 |
+
|
92 |
+
return mp3_path, midi_path, None
|
93 |
+
|
94 |
+
with gr.Blocks() as demo:
|
95 |
+
# Introductory text
|
96 |
gr.Markdown("""
|
97 |
# Controllable Symbolic Music Generation with MusicLang Predict
|
98 |
[MusicLang Predict](https://github.com/musiclang/musiclang_predict) offers advanced controllability features and high-quality music generation by manipulating symbolic music.
|
99 |
You can for example use it to continue your composition with a specific chord progression.
|
100 |
""")
|
101 |
+
|
102 |
+
with gr.Row():
|
103 |
+
with gr.Column():
|
104 |
+
with gr.Row():
|
105 |
midi_file = gr.File(label="Prompt MIDI File (Optional)", type="filepath", file_types=[".mid", ".midi"],
|
106 |
+
elem_id='midi_file_input')
|
107 |
+
with gr.Column():
|
108 |
+
bar_range = gr.Textbox(label="Bar Range of input file (eg: 0-4 for first four bars)", placeholder="0-4",
|
109 |
+
value="0-4", elem_id='bar_range_input')
|
110 |
+
nb_tokens = gr.Number(label="Nb Tokens",
|
111 |
+
value=512, minimum=256, maximum=2048, step=256, elem_id='nb_tokens_input')
|
112 |
+
temperature = gr.Slider(
|
113 |
+
label="Temperature",
|
114 |
+
value=0.95,
|
115 |
+
visible=False,
|
116 |
+
minimum=0.1, maximum=1.0, step=0.1, elem_id='temperature_input')
|
117 |
+
tempo = gr.Slider(label="Tempo", value=120, minimum=60, maximum=240, step=1, elem_id='tempo_input')
|
118 |
with gr.Row():
|
119 |
chord_progression = gr.Textbox(
|
120 |
label="Chord Progression (Optional)",
|
|
|
125 |
info_message = gr.Textbox(label="Info Message", elem_id='info_message_output')
|
126 |
generated_music = gr.Audio(label="Preview generated Music", elem_id='generated_music_output')
|
127 |
generated_midi = gr.File(label="Download MIDI", elem_id='generated_midi_output')
|
128 |
+
|
129 |
generate_btn.click(
|
130 |
+
fn=musiclang_gradio,
|
131 |
inputs=[midi_file, chord_progression, tempo, temperature, nb_tokens, bar_range],
|
132 |
outputs=[generated_music, generated_midi, info_message]
|
133 |
+
)
|
134 |
+
|
135 |
+
with gr.Row():
|
136 |
+
with gr.Column():
|
137 |
gr.Markdown("## Examples")
|
138 |
gr.Examples(
|
139 |
examples=[["/home/user/app/bach_847.mid", "", 120, 0.95, 512, "0-4"],
|
|
|
146 |
],
|
147 |
inputs=[midi_file, chord_progression, tempo, temperature, nb_tokens, bar_range],
|
148 |
outputs=[generated_music, generated_midi, info_message],
|
149 |
+
fn=musiclang_gradio,
|
150 |
cache_examples=True,
|
151 |
)
|
152 |
+
|
153 |
demo.launch()
|