Spaces:
Runtime error
Runtime error
Commit ·
6b83d53
1
Parent(s): 7d39a65
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,46 @@
|
|
| 1 |
-
!pip install
|
| 2 |
-
import
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
!pip install openai mido pretty_midi
|
| 2 |
+
import openai
|
| 3 |
+
import mido
|
| 4 |
+
import pretty_midi
|
| 5 |
|
| 6 |
+
# Set up your OpenAI API key
|
| 7 |
+
openai.api_key = "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"
|
| 8 |
|
| 9 |
+
# Load the MIDI file
|
| 10 |
+
midi_file = 'example.mid'
|
| 11 |
+
midi_data = mido.MidiFile(midi_file)
|
| 12 |
+
midi_pretty = pretty_midi.PrettyMIDI(midi_file)
|
| 13 |
|
| 14 |
+
# Convert MIDI data to a prompt for Musenet
|
| 15 |
+
tempo = midi_pretty.get_tempo_changes()[0][0]
|
| 16 |
+
key_signature = midi_pretty.key_signature_changes[0].key_number
|
| 17 |
+
prompt = f"extend this {tempo:.0f}bpm {pretty_midi.key_name(key_signature)} MIDI file"
|
| 18 |
|
| 19 |
+
# Define the parameters for the Musenet generation
|
| 20 |
+
model = "openai/musenet"
|
| 21 |
+
length = 32 # Length of the generated audio in bars
|
| 22 |
+
temperature = 0.7 # Controls the randomness of the generation
|
| 23 |
+
|
| 24 |
+
# Generate the MIDI with Musenet
|
| 25 |
+
response = openai.Completion.create(
|
| 26 |
+
engine=model,
|
| 27 |
+
prompt=prompt,
|
| 28 |
+
max_tokens=1024,
|
| 29 |
+
n=1,
|
| 30 |
+
stop=None,
|
| 31 |
+
temperature=temperature,
|
| 32 |
+
timeout=60,
|
| 33 |
+
)
|
| 34 |
+
midi_data.extend(mido.MidiFile(type=1, ticks_per_beat=480))
|
| 35 |
+
for note in response.choices[0].text.strip().split():
|
| 36 |
+
pitch, start, duration, velocity = note.split(',')
|
| 37 |
+
start = float(start) * 480
|
| 38 |
+
duration = float(duration) * 480
|
| 39 |
+
velocity = int(float(velocity) * 127)
|
| 40 |
+
midi_data.add_track([
|
| 41 |
+
mido.Message('note_on', note=int(pitch), velocity=velocity, time=int(start)),
|
| 42 |
+
mido.Message('note_off', note=int(pitch), velocity=velocity, time=int(start + duration))
|
| 43 |
+
])
|
| 44 |
+
|
| 45 |
+
# Save the extended MIDI file
|
| 46 |
+
midi_data.save('extended.mid')
|