floriangardin commited on
Commit
d878645
1 Parent(s): 4b77a87

add examples with app

Browse files
Files changed (3) hide show
  1. .gitignore +3 -2
  2. app.py +69 -42
  3. flagged/log.csv +0 -2
.gitignore CHANGED
@@ -2,6 +2,7 @@
2
  .idea/
3
  *.mid
4
  venv/
5
- *.mid
6
  *.wav
7
- *.mp3
 
 
 
2
  .idea/
3
  *.mid
4
  venv/
 
5
  *.wav
6
+ !examples/
7
+ *.mp3
8
+ gradio_cached_examples/
app.py CHANGED
@@ -6,16 +6,14 @@ import os
6
  import tempfile
7
 
8
 
9
- def inner_loop(nb_tokens, temperature, chord_progression, tempo, midi_file, bar_range):
10
  top_p = 0.98
11
  seed = 0
12
-
13
- print(midi_file)
14
  # Initialize the MusicLangPredictor
15
  ml = MusicLangPredictor('musiclang/musiclang-v2')
16
  tempo_message = "" # Default message if no MIDI file is uploaded
17
  time_signature = (4, 4)
18
- if midi_file is not None:
19
  # Load the MIDI file and use it as the score prompt
20
  filepath = midi_file
21
  start_bar, end_bar = map(int, bar_range.split("-"))
@@ -76,47 +74,76 @@ def inner_loop(nb_tokens, temperature, chord_progression, tempo, midi_file, bar_
76
 
77
  return mp3_path, midi_path, chord_repr, tempo_message
78
 
79
- def musiclang(nb_tokens, temperature, chord_progression, tempo, midi_file, bar_range):
80
  exception = None
81
  mp3_path, midi_path, chord_repr, tempo_message = None, None, None, ""
82
  try:
83
- mp3_path, midi_path, chord_repr, tempo_message = inner_loop(nb_tokens, temperature, chord_progression, tempo,
84
- midi_file, bar_range)
85
  except Exception as e:
86
  exception = "Error : " + e.__class__.__name__ + " " + str(e)
87
  # Return the MP3 path for Gradio to display and the MIDI file path for download
88
- return mp3_path, midi_path, chord_repr, tempo_message, exception
89
-
90
- # Update Gradio interface to include MIDI file upload and bar range selection
91
- iface = gr.Interface(
92
- fn=musiclang,
93
- inputs=[
94
- gr.Number(label="Number of Tokens", value=1024, minimum=256, maximum=2048, step=256),
95
- gr.Slider(label="Temperature", value=0.95, minimum=0.1, maximum=1.0, step=0.1),
96
- gr.Textbox(label="Chord Progression (Optional)", placeholder="Am CM Dm/F E7 Am", lines=2, value=""),
97
- gr.Slider(label="Tempo", value=120, minimum=60, maximum=240, step=1),
98
- gr.File(label="Upload MIDI File", type="filepath", file_types=[".mid", ".midi"]),
99
- gr.Textbox(label="Bar Range of input file", placeholder="0-4", value="0-4")
100
- ],
101
- outputs=[
102
- gr.Audio(label="Generated Music"),
103
- gr.File(label="Download MIDI"),
104
- gr.Textbox(label="Inferred output Chord Progression", lines=2, value=""),
105
- gr.Textbox(label="Tempo Used", value=""), # Display the tempo used for generation
106
- gr.Textbox(label="Info Message") # Initially hidden, shown only if there's an error
107
- ],
108
- title="Controllable Symbolic Music Generation with MusicLang Predict",
109
- description="""
110
- \n This is the demo for MusicLang Predict, which offers advanced controllability features and high-quality music generation by manipulating symbolic music. Control your music generation by :
111
- \n - Specifying the number of tokens
112
- \n - <b>The temperature</b>: the level of creativity of MusicLang. The higher the temperature, the more creative the generation. We suggest you test different levels to find the one that suits your needs
113
- \n - <b>Chord progression</b> : Available chord qualities include: M, m, 7, m7b5, sus2, sus4, m7, M7, dim, dim0. You can also specify the bass if it belongs to the chord (e.g., Bm/D)
114
- \n - <b>The tempo</b>
115
- \n - <b>Uploading a MIDI</b> file to use as a prompt
116
- \n - Selecting a bar range of the input file (For example 0-4 means first four bars)
117
- \n Note: The model generates a score file, not an audio one. Therefore, the audio rendering in this notebook serves as a quick preview of the generated music. For an optimized experience, we recommend downloading the midi file and opening it in your favorite Digital Audio Workstation (DAW).”
118
- \n If no chord progression or MIDI file is given, it generates a free sample with the specified number of tokens.
119
- \n Need more info ? Visit <a href="https://github.com/musiclang/musiclang_predict">our Github</a>"""
120
- )
121
-
122
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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("-"))
 
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
  try:
81
+ mp3_path, midi_path, chord_repr, tempo_message = inner_loop(midi_file, chord_progression, tempo, temperature, nb_tokens, bar_range)
 
82
  except Exception as e:
83
  exception = "Error : " + e.__class__.__name__ + " " + str(e)
84
  # Return the MP3 path for Gradio to display and the MIDI file path for download
85
+ return mp3_path, midi_path, exception
86
+
87
+
88
+ with gr.Blocks() as demo:
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
+ with gr.Row():
97
+ with gr.Column():
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
+ with gr.Column():
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",
105
+ value=512, minimum=256, maximum=2048, step=256, elem_id='nb_tokens_input')
106
+ temperature = gr.Slider(
107
+ label="Temperature",
108
+ value=0.95,
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')
124
+ generated_midi = gr.File(label="Download MIDI", elem_id='generated_midi_output')
125
+ generate_btn.click(
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
+ with gr.Row():
132
+ with gr.Column():
133
+ gr.Markdown("## Examples")
134
+ gr.Examples(
135
+ examples=[["examples/Bach_847.mid", "", 120, 0.95, 512, "0-4"],
136
+ ["examples/Bach_847.mid", "Cm C7/E Fm F#dim G7", 120, 0.95, 512, "0-4"],
137
+ ["examples/Boney_m_ma_baker.mid", "", 120, 0.95, 512, "0-4"],
138
+ ["examples/Eminem_slim_shady.mid", "Cm AbM BbM G7 Cm", 120, 0.95, 512, "0-4"],
139
+ ["examples/Mozart_alla_turca.mid", "", 120, 0.95, 512, "0-4"],
140
+ ["examples/Mozart_alla_turca.mid", "Am Em CM G7 E7 Am Am E7 Am", 120, 0.95, 512, "0-4"],
141
+ ["examples/Daft_punk_Around_the_world.mid", "", 120, 0.95, 512, "0-4"],
142
+ ],
143
+ inputs=[midi_file, chord_progression, tempo, temperature, nb_tokens, bar_range],
144
+ outputs=[generated_music, generated_midi, info_message],
145
+ fn=musiclang,
146
+ cache_examples=True,
147
+ )
148
+
149
+ demo.launch()
flagged/log.csv DELETED
@@ -1,2 +0,0 @@
1
- Number of Tokens,Temperature,Chord Progression,Tempo,Upload MIDI File,Bar Range,Generated Music,Download MIDI,Inferred output Chord Progression,Info Message,flag,username,timestamp
2
- 1024,0.9,,120,,0-4,"{""path"":""flagged/Generated Music/40e8a82a91668a20d35d/result.mp3"",""url"":""http://127.0.0.1:7860/file=/private/var/folders/52/q1zgqdvj1818brk2d_kw0stw0000gn/T/gradio/3926f3a3196bf0f0bbc8f54a4a464fa74a066dd9/result.mp3"",""size"":null,""orig_name"":""result.mp3"",""mime_type"":null,""is_stream"":false}","{""path"":""flagged/Download MIDI/1f6e813973ef7da5a542/test.mid"",""url"":""http://127.0.0.1:7860/file=/private/var/folders/52/q1zgqdvj1818brk2d_kw0stw0000gn/T/gradio/450e7e4a544b0fb7d114e7d3eadff382988e612b/test.mid"",""size"":2490,""orig_name"":""test.mid"",""mime_type"":null,""is_stream"":false}",Em CM Bm7b5/A Em GM Am Em/B Em GM Am Em/B Em GM Am Em/B Em GM Am Em/B Em GM Am Em/B Em Bm CM Em Em Bm CM Em GM Am Em/B Em GM Am Em/B Em GM Am Em/B,,,,2024-03-04 18:20:18.251715