-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
78 lines (60 loc) · 2.55 KB
/
Copy pathmain.py
File metadata and controls
78 lines (60 loc) · 2.55 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
import streamlit as st
import music21 as m21
import tempfile
from abcsonification.sonifyabc import sonify_all_files
import os
import subprocess
from enum import Enum, IntEnum
from pydantic import BaseModel, ValidationError
class KeyEnum(str, Enum):
Aminor = 'Amin'
banana = 'banana'
def get_transformer_prompt():
with open('tunesformer/prompt.txt') as f:
return f.read()
def get_generated_abc():
with open('sounds_current/placeholder.abc') as f:
return f.read()
def display_sheet_music(score):
# Save the sheet music as an image
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as img_file:
img_path = score.write('musicxml.png', fp=img_file.name)
# Display the sheet music image
st.image(str(img_path), caption="Sheet Music")
def run_transformer():
## Run the transformer
os.chdir('./tunesformer')
command1 = [
'python',
'generate.py'
]
subprocess.run(command1)
os.chdir('../')
st.session_state.trans_prompt = get_transformer_prompt()
st.session_state.gen_abc = get_generated_abc()
if ["inter_prompt", "abc_string", "old_score"] not in st.session_state:
st.session_state.trans_prompt = get_transformer_prompt()
st.session_state.gen_abc = get_generated_abc()
st.session_state.old_score = []
# Main page Headers
st.title("🔤 Irish Music Generator")
# Side Bar
nl_title = "Make me a song with three sections of bar length 4 each with sections being somewhat similar to each other. The metre of the song should be 4/4 and the key Dminor."
nl_prompt_box = st.sidebar.text_area("NL Prompt:", value=nl_title, height=300)
trans_prompt_box = st.sidebar.text_area("Transformer Prompt:", value=st.session_state.trans_prompt, height=300)
st.session_state.trans_prompt = trans_prompt_box
generate_music_button = st.sidebar.button("Generate Music", on_click=run_transformer)
with open('tunesformer/prompt.txt', 'w') as f:
f.write(trans_prompt_box)
# Main Page
gen_abc_box = st.text_area("# Generated 🔤 notation:", value=st.session_state.gen_abc, height=300)
st.session_state.gen_abc = gen_abc_box
with open('sounds_current/placeholder.abc', 'w') as f:
f.write(gen_abc_box)
## Display the sheet music
score = m21.converter.parse(gen_abc_box, format='abc')
if st.session_state.old_score!= score:
sonify_all_files('sounds_current', 'sounds_current', 'sounds_current', './abcsonification/SGM-v2.01-NicePianosGuitarsBass-V1.2.sf2')
st.session_state.old_score = score
display_sheet_music(score)
st.audio('sounds_current/placeholder.wav', format='audio/wav')