Taf2023 commited on
Commit
246d69a
·
verified ·
1 Parent(s): 0b75620

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +118 -71
app.py CHANGED
@@ -1,76 +1,120 @@
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
21
- time_signature=time_signature,
22
- temperature=temperature,
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)
34
- wav_path = temp_wav_file.name
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",
67
- value=512, minimum=256, maximum=2048, step=256, elem_id='nb_tokens_input')
68
- temperature = gr.Slider(
69
- label="Temperature",
70
- value=0.95,
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)",
@@ -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=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"],
@@ -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=musiclang,
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()