-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic.py
More file actions
89 lines (69 loc) · 3.83 KB
/
basic.py
File metadata and controls
89 lines (69 loc) · 3.83 KB
1
2
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import os
import openai
from dotenv import load_dotenv
load_dotenv()
class Skitz:
def __init__(self):
openai.api_key = os.getenv('OPENAI_API_KEY')
with open('/Users/nico/Documents/Quick projects/mk/mk/workspace/Documentations/instructions.md', 'r') as file:
self.instructions = file.read()
def compose_song(self, user_instructions):
prompt = f"# ABC Player Specification\n\nUse the documentation for ABC and these instructions to complete the request. Only respond with ABC format:{user_instructions}"
try:
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[
{"role": "system", "content": f"You are a helpful assistant. You can generate creative and original music based on the input requirements given to you. Use the documentation for ABC to complete the request:\n\n{self.instructions} \n\n Mention generated by skitz at the very botom of the file."},
{"role": "user", "content": prompt},
]
)
except Exception as e:
print(f"\nAn error occurred: {e}")
return None
song = response.choices[0].message['content']
return song
def write_abc_file(self, song, filename):
os.makedirs("Generations", exist_ok=True)
filepath = os.path.join("Generations", filename)
if os.path.exists(filepath):
overwrite = input(f"\nA file with the name {filename} already exists. Do you want to overwrite it? (y/n): ")
if overwrite.lower() != 'y':
return
with open(filepath, 'w') as file:
file.write(song)
def get_user_input(self):
user_instructions = ""
questions = [
"\nPlease enter the genre of the song: ",
"\nPlease enter the tempo of the song: ",
"\nPlease enter any specific lyrics or themes you'd like to include: ",
"\nPlease enter the desired chord progression: ",
"\nPlease enter any additional instructions or preferences: ",
]
for question in questions:
answer = input(question)
user_instructions += answer + "\n"
return user_instructions.strip()
def generate_song(self):
print("""
███████╗██╗ ██╗██╗████████╗████████╗██╗ ██╗███████╗
██╔════╝██║ ██║██║╚══██╔══╝╚══██╔══╝██║ ██║██╔════╝
███████╗███████║██║ ██║ ██║ ███████║█████╗
╚════██║██╔══██║██║ ██║ ██║ ██╔══██║██╔══╝
███████║██║ ██║██║ ██║ ██║ ██║ ██║███████╗
╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝╚══════╝
""")
print("\nWelcome to Skitz! This program will generate a song for you based on your input.")
user_instructions = self.get_user_input()
print("\nGenerating your song. This may take a few moments...")
song = self.compose_song(user_instructions)
if song is None:
print("\nFailed to generate a song. Please try again.")
return
filename = f"{user_instructions[:20].replace(' ', '_')}.abc"
self.write_abc_file(song, filename)
filepath = os.path.join("Generations", filename)
print(f"\nThe song has been written to: {filepath}")
if __name__ == "__main__":
skitz = Skitz()
skitz.generate_song()