Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,117 +3,89 @@ import streamlit.components.v1 as components
|
|
3 |
import re
|
4 |
import os
|
5 |
import glob
|
|
|
|
|
6 |
st.set_page_config(layout="wide")
|
|
|
7 |
def process_line(line):
|
|
|
8 |
if re.search(r'\b[A-G][#b]?m?\b', line):
|
9 |
line = re.sub(r'\b([A-G][#b]?m?)\b', r"<img src='\1.png' style='height:20px;'>", line)
|
10 |
return line
|
|
|
11 |
def process_chord_sheet(chord_sheet):
|
12 |
-
|
13 |
-
for line in chord_sheet.split('\n')
|
14 |
-
processed_line = process_line(line)
|
15 |
-
processed_lines.append(processed_line)
|
16 |
return '<br>'.join(processed_lines)
|
17 |
-
|
18 |
-
|
|
|
19 |
return base_url + artist_song.replace(' ', '+').replace('โ', '%E2%80%93').replace('&', 'and')
|
20 |
-
|
21 |
-
base_url = "https://www.youtube.com/results?search_query="
|
22 |
-
return base_url + artist_song.replace(' ', '+').replace('โ', '%E2%80%93').replace('&', 'and')
|
23 |
-
def create_search_url_chords(artist_song):
|
24 |
-
base_url = "https://www.ultimate-guitar.com/search.php?search_type=title&value="
|
25 |
-
return base_url + artist_song.replace(' ', '+').replace('โ', '%E2%80%93').replace('&', 'and')
|
26 |
-
def create_search_url_lyrics(artist_song):
|
27 |
-
base_url = "https://www.google.com/search?q="
|
28 |
-
return base_url + artist_song.replace(' ', '+').replace('โ', '%E2%80%93').replace('&', 'and') + '+lyrics'
|
29 |
-
def songupdate():
|
30 |
-
st.write(st.session_state.EnhancedChordSheet)
|
31 |
-
def load_song_file2(filename):
|
32 |
-
with open(filename, "r") as file:
|
33 |
-
chord_sheet = file.read()
|
34 |
-
st.session_state['chord_sheet'] = chord_sheet
|
35 |
-
processed_sheet = process_chord_sheet(chord_sheet)
|
36 |
-
st.markdown(processed_sheet, unsafe_allow_html=True)
|
37 |
def load_song_file(file_path):
|
|
|
38 |
with open(file_path, 'r', encoding='utf-8') as file:
|
39 |
-
|
40 |
-
|
41 |
-
def song_update():
|
42 |
-
if 'selected_file' in st.session_state:
|
43 |
-
song_name, artist_name = parse_filename(st.session_state.selected_file)
|
44 |
-
st.session_state.song_name = song_name
|
45 |
-
st.session_state.artist_name = artist_name
|
46 |
def parse_filename(filename):
|
|
|
47 |
base_name = os.path.splitext(filename)[0]
|
48 |
song_name, artist_name = base_name.split(' by ')
|
49 |
return song_name.replace("_", " "), artist_name.replace("_", " ")
|
50 |
-
|
51 |
-
song_name = st.session_state.get('song_name', '')
|
52 |
-
artist_name = st.session_state.get('artist_name', '')
|
53 |
-
chord_sheet = st.session_state.get('chord_sheet', '')
|
54 |
-
if song_name and artist_name and chord_sheet:
|
55 |
-
filename = song_name + " by " + artist_name + ".txt"
|
56 |
-
with open(filename, "w") as file:
|
57 |
-
chord_sheet_text = st.session_state.get('chord_sheet', '')
|
58 |
-
file.write(chord_sheet_text)
|
59 |
-
st.session_state['char_count'] = len(chord_sheet)
|
60 |
-
st.success(f"Auto-saved to {filename}")
|
61 |
def main():
|
62 |
col1, col3 = st.columns([3, 5])
|
|
|
63 |
with col1:
|
64 |
st.markdown('### ๐ต ๐Prompt๐ฅ๐ธChord Sheet๐ถ AI Prompt Authoring App')
|
|
|
|
|
65 |
with st.expander("Select Song:", expanded=True):
|
66 |
all_files = [f for f in glob.glob("*.txt") if ' by ' in f]
|
67 |
-
selected_file = st.selectbox("Choose: ", all_files,
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
90 |
song_info = os.path.splitext(file)[0].replace("_", " ")
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
"""
|
98 |
-
st.markdown(table_md)
|
99 |
with col3:
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
if
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
char_count_msg = f"Character Count: {st.session_state.get('char_count', 0)}"
|
114 |
-
st.write(char_count_msg)
|
115 |
-
# Load chord sheet from selected file into the text area
|
116 |
-
if 'selected_file' in st.session_state and st.session_state.selected_file:
|
117 |
-
load_song_file(st.session_state.selected_file)
|
118 |
if __name__ == '__main__':
|
119 |
-
main()
|
|
|
3 |
import re
|
4 |
import os
|
5 |
import glob
|
6 |
+
|
7 |
+
# Set Streamlit page configuration
|
8 |
st.set_page_config(layout="wide")
|
9 |
+
|
10 |
def process_line(line):
|
11 |
+
# Process each line to replace chords with images
|
12 |
if re.search(r'\b[A-G][#b]?m?\b', line):
|
13 |
line = re.sub(r'\b([A-G][#b]?m?)\b', r"<img src='\1.png' style='height:20px;'>", line)
|
14 |
return line
|
15 |
+
|
16 |
def process_chord_sheet(chord_sheet):
|
17 |
+
# Process the entire chord sheet
|
18 |
+
processed_lines = [process_line(line) for line in chord_sheet.split('\n')]
|
|
|
|
|
19 |
return '<br>'.join(processed_lines)
|
20 |
+
|
21 |
+
def create_search_url(base_url, artist_song):
|
22 |
+
# Create search URL with appropriate character replacements
|
23 |
return base_url + artist_song.replace(' ', '+').replace('โ', '%E2%80%93').replace('&', 'and')
|
24 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
def load_song_file(file_path):
|
26 |
+
# Load a song file and return its content
|
27 |
with open(file_path, 'r', encoding='utf-8') as file:
|
28 |
+
return file.read()
|
29 |
+
|
|
|
|
|
|
|
|
|
|
|
30 |
def parse_filename(filename):
|
31 |
+
# Parse the filename to extract song and artist names
|
32 |
base_name = os.path.splitext(filename)[0]
|
33 |
song_name, artist_name = base_name.split(' by ')
|
34 |
return song_name.replace("_", " "), artist_name.replace("_", " ")
|
35 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
def main():
|
37 |
col1, col3 = st.columns([3, 5])
|
38 |
+
|
39 |
with col1:
|
40 |
st.markdown('### ๐ต ๐Prompt๐ฅ๐ธChord Sheet๐ถ AI Prompt Authoring App')
|
41 |
+
|
42 |
+
# Expander for selecting the song
|
43 |
with st.expander("Select Song:", expanded=True):
|
44 |
all_files = [f for f in glob.glob("*.txt") if ' by ' in f]
|
45 |
+
selected_file = st.selectbox("Choose: ", all_files, key='selected_file')
|
46 |
+
|
47 |
+
# Input fields for song and artist names
|
48 |
+
song_name_input = st.text_input("๐ต Song:", key='song_name')
|
49 |
+
artist_name_input = st.text_input("๐ค Artist:", key='artist_name')
|
50 |
+
|
51 |
+
if selected_file:
|
52 |
+
# Display the current song
|
53 |
+
song_info = os.path.splitext(selected_file)[0].replace("_", " ")
|
54 |
+
st.header("๐ผ Current Song")
|
55 |
+
st.markdown("**" + song_info + "**")
|
56 |
+
processed_sheet = process_chord_sheet(load_song_file(selected_file))
|
57 |
+
components.html(processed_sheet, height=600) # Using HTML component for chord sheet
|
58 |
+
|
59 |
+
# Markdown table with links
|
60 |
+
table_md = f"""
|
61 |
+
| Wikipedia | YouTube | Chords | Lyrics |
|
62 |
+
| --------- | ------- | ------ | ------ |
|
63 |
+
| [๐]({create_search_url("https://www.wikipedia.org/search-redirect.php?family=wikipedia&language=en&search=", song_info)}) | [๐ฅ]({create_search_url("https://www.youtube.com/results?search_query=", song_info)}) | [๐ธ]({create_search_url("https://www.ultimate-guitar.com/search.php?search_type=title&value=", song_info)}) | [๐ถ]({create_search_url("https://www.google.com/search?q=", song_info + '+lyrics')}) |
|
64 |
+
"""
|
65 |
+
st.markdown(table_md)
|
66 |
+
|
67 |
+
st.header("๐ผ Available Songs")
|
68 |
+
for file in all_files:
|
69 |
song_info = os.path.splitext(file)[0].replace("_", " ")
|
70 |
+
icol1, icol2 = st.columns([1, 3])
|
71 |
+
with icol1:
|
72 |
+
st.markdown("**" + song_info + "**")
|
73 |
+
processed_sheet = process_chord_sheet(load_song_file(file))
|
74 |
+
components.html(processed_sheet, height=150) # Displaying processed chord sheets
|
75 |
+
|
|
|
|
|
76 |
with col3:
|
77 |
+
# Chord sheet text area
|
78 |
+
chord_sheet_area = st.text_area("Chord Sheet", value=load_song_file(selected_file) if selected_file else '', height=1600, key='chord_sheet')
|
79 |
+
|
80 |
+
# Save functionality
|
81 |
+
if st.button("๐พ Save", key="save_song"):
|
82 |
+
if song_name_input and artist_name_input:
|
83 |
+
filename = song_name_input + " by " + artist_name_input + ".txt"
|
84 |
+
with open(filename, "w") as file:
|
85 |
+
file.write(chord_sheet_area)
|
86 |
+
st.success("Chord sheet saved to file: " + filename)
|
87 |
+
else:
|
88 |
+
st.error("Both Song Name and Artist Name are required.")
|
89 |
+
|
|
|
|
|
|
|
|
|
|
|
90 |
if __name__ == '__main__':
|
91 |
+
main()
|