From 6768b1809f92e6a46fbe2c221ca3a55fb7599175 Mon Sep 17 00:00:00 2001 From: isakcodes <61279302+isakcodes@users.noreply.github.com> Date: Tue, 7 May 2024 18:46:57 +0200 Subject: [PATCH 1/5] changed for compatibility with OpenAI API v1.0.0 and supporting env variable as default --- g2m.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/g2m.py b/g2m.py index 5a6eac4..da1dbc3 100644 --- a/g2m.py +++ b/g2m.py @@ -1,10 +1,10 @@ import os, requests, argparse, re from fractions import Fraction from midiutil.MidiFile import MIDIFile -import openai, mido +import mido +from openai import OpenAI #settings -openaiKey = '' system = 'You are MusicGPT, a music creation and completion chat bot that. When a user gives you a prompt,' \ ' you return them a song showing the notes, durations, and times that they occur. Respond with just the music.' \ '\n\nNotation looks like this:\n(Note-duration-time in beats)\nC4-1/4-0, Eb4-1/8-2.5, D4-1/4-3, F4-1/4-3 etc.' @@ -22,9 +22,11 @@ parser.add_argument('-l', '--load', help='load a MIDI file to be appended to your prompt') parser.add_argument('-v', '--verbose', help='display GPT-4 output', action='store_true') parser.add_argument('-o', '--output', help='specify output directory (default: current)', default=path) -parser.add_argument('-a', '--auth', help='specify openai api key (edit this script file to set a default)', default=openaiKey) +parser.add_argument('-a', '--auth', help='specify openai api key (edit this script file to set a default)', default=os.getenv("OPENAI_API_KEY")) args = parser.parse_args() +client = OpenAI(api_key=args.auth) + #other vars n functions notes = [['C'], ['Db', 'C#'], ['D'], ['Eb', 'D#'], ['E'], ['F'], ['Gb', 'F#'], ['G'], ['Ab', 'G#'], ['A'], ['Bb', 'A#'], ['B']] @@ -73,12 +75,11 @@ def midiToStr(mPath): while 1: #openai request print('[*] Making request to OpenAI API') - openai.api_key = args.auth - r = openai.ChatCompletion.create( - model = 'gpt-4', - messages = history + r = client.chat.completions.create( + model='gpt-4', + messages=history ) - response = r['choices'][0]['message']['content'] + response = r.choices[0].message.content if args.verbose: print('\n'+response+'\n') history.append({'role': 'assistant', 'content': response}) From ea0340ea648c183c3fbf111766e1c00e02c2da2b Mon Sep 17 00:00:00 2001 From: isakcodes <61279302+isakcodes@users.noreply.github.com> Date: Tue, 7 May 2024 18:47:36 +0200 Subject: [PATCH 2/5] added assert to avoid an expensive request if output dir does not exist --- g2m.py | 1 + 1 file changed, 1 insertion(+) diff --git a/g2m.py b/g2m.py index da1dbc3..85b00f6 100644 --- a/g2m.py +++ b/g2m.py @@ -24,6 +24,7 @@ parser.add_argument('-o', '--output', help='specify output directory (default: current)', default=path) parser.add_argument('-a', '--auth', help='specify openai api key (edit this script file to set a default)', default=os.getenv("OPENAI_API_KEY")) args = parser.parse_args() +assert os.path.exists(args.output), "[!] The output directory does not exist. Please run $mkdir -p " client = OpenAI(api_key=args.auth) From 72f90ef78a14d75e861e6fa5812ca7814a835cfd Mon Sep 17 00:00:00 2001 From: isakcodes <61279302+isakcodes@users.noreply.github.com> Date: Tue, 7 May 2024 18:48:31 +0200 Subject: [PATCH 3/5] added saving of the response as well --- g2m.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/g2m.py b/g2m.py index 85b00f6..3b56966 100644 --- a/g2m.py +++ b/g2m.py @@ -81,6 +81,8 @@ def midiToStr(mPath): messages=history ) response = r.choices[0].message.content + with open(os.path.join(args.output, f'response.txt'), 'w') as f: + f.write(response) if args.verbose: print('\n'+response+'\n') history.append({'role': 'assistant', 'content': response}) From c4ed95f8c908ecac6cfe6ebc8c99cf892a1f89aa Mon Sep 17 00:00:00 2001 From: isakcodes <61279302+isakcodes@users.noreply.github.com> Date: Tue, 7 May 2024 18:49:39 +0200 Subject: [PATCH 4/5] added index to output filenames to not overwrite using --chat --- g2m.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/g2m.py b/g2m.py index 3b56966..35f3b39 100644 --- a/g2m.py +++ b/g2m.py @@ -73,7 +73,7 @@ def midiToStr(mPath): history = [{'role': 'system', 'content': system}, {'role': 'user', 'content': prompt}] # main loop -while 1: +for i in range(100): #openai request print('[*] Making request to OpenAI API') r = client.chat.completions.create( @@ -81,7 +81,7 @@ def midiToStr(mPath): messages=history ) response = r.choices[0].message.content - with open(os.path.join(args.output, f'response.txt'), 'w') as f: + with open(os.path.join(args.output, f'response{i}.txt'), 'w') as f: f.write(response) if args.verbose: print('\n'+response+'\n') @@ -101,7 +101,7 @@ def midiToStr(mPath): for i in noteInfo: pitch, dur, time = i melody.addNote(0, 0, pitch, time, dur, 100) - with open(os.path.join(args.output, 'output.mid'), 'wb') as f: + with open(os.path.join(args.output, 'output{i}.mid'), 'wb') as f: melody.writeFile(f) print('[*] Wrote the MIDI file.') From 49eae3f24667410664a59ea2f370a6c9d3762670 Mon Sep 17 00:00:00 2001 From: isakcodes <61279302+isakcodes@users.noreply.github.com> Date: Tue, 21 May 2024 15:30:14 +0200 Subject: [PATCH 5/5] fix typo fstring --- g2m.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/g2m.py b/g2m.py index 35f3b39..68739f5 100644 --- a/g2m.py +++ b/g2m.py @@ -101,7 +101,7 @@ def midiToStr(mPath): for i in noteInfo: pitch, dur, time = i melody.addNote(0, 0, pitch, time, dur, 100) - with open(os.path.join(args.output, 'output{i}.mid'), 'wb') as f: + with open(os.path.join(args.output, f'output{i}.mid'), 'wb') as f: melody.writeFile(f) print('[*] Wrote the MIDI file.')