awacke1 commited on
Commit
29a1d82
โ€ข
1 Parent(s): 8cc0981

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -91
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
- processed_lines = []
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
- def create_search_url_wikipedia(artist_song):
18
- base_url = "https://www.wikipedia.org/search-redirect.php?family=wikipedia&language=en&search="
 
19
  return base_url + artist_song.replace(' ', '+').replace('โ€“', '%E2%80%93').replace('&', 'and')
20
- def create_search_url_youtube(artist_song):
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
- chord_sheet = file.read()
40
- return chord_sheet
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
- def auto_save():
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, on_change=song_update, key='selected_file')
68
- song_name_input = st.text_input("๐ŸŽต Song:", key='song_name', on_change=auto_save)
69
- artist_name_input = st.text_input("๐ŸŽค Artist:", key='artist_name', on_change=auto_save)
70
- if 'selected_file' in st.session_state and st.session_state.selected_file:
71
- # Update the session state before creating the text area widget
72
- st.session_state['chord_sheet'] = load_song_file(st.session_state.selected_file)
73
- st.header("๐ŸŽผ Current Song")
74
- load_song_file(selected_file)
75
- song_info = os.path.splitext(selected_file)[0].replace("_", " ")
76
- st.markdown("**" + song_info + "**")
77
- table_md = f"""
78
- | Wikipedia | YouTube | Chords | Lyrics |
79
- | --------- | ------- | ------ | ------ |
80
- | [๐Ÿ“š]({create_search_url_wikipedia(song_info)}) | [๐ŸŽฅ]({create_search_url_youtube(song_info)}) | [๐ŸŽธ]({create_search_url_chords(song_info)}) | [๐ŸŽถ]({create_search_url_lyrics(song_info)}) |
81
- """
82
- st.markdown(table_md)
83
- st.header("๐ŸŽผ Available Songs")
84
- for file in all_files:
85
- song_info = os.path.splitext(file)[0].replace("_", " ")
86
- icol1, icol2 = st.columns([1, 3])
87
- with icol1:
88
- st.markdown("**" + song_info + "**")
89
- load_song_file(file)
 
90
  song_info = os.path.splitext(file)[0].replace("_", " ")
91
- with icol2:
92
- # Create a markdown table with links for each song file
93
- table_md = f"""
94
- | Wikipedia | YouTube | Chords | Lyrics |
95
- | --------- | ------- | ------ | ------ |
96
- | [๐Ÿ“š]({create_search_url_wikipedia(song_info)}) | [๐ŸŽฅ]({create_search_url_youtube(song_info)}) | [๐ŸŽธ]({create_search_url_chords(song_info)}) | [๐ŸŽถ]({create_search_url_lyrics(song_info)}) |
97
- """
98
- st.markdown(table_md)
99
  with col3:
100
- subcol1, subcol2 = st.columns([1, 5])
101
- with subcol2:
102
- chord_sheet_area = st.text_area("Chord Sheet", value=st.session_state.get('chord_sheet', ''), height=1600, key='chord_sheet', on_change=auto_save)
103
- with subcol1:
104
- # Save functionality
105
- if st.button("๐Ÿ’พ Save", key="save_song"):
106
- if song_name_input and artist_name_input:
107
- filename = song_name_input + " by " + artist_name_input + ".txt"
108
- with open(filename, "w") as file:
109
- file.write(chord_sheet_area)
110
- st.success("Chord sheet saved to file: " + filename)
111
- else:
112
- st.error("Both Song Name and Artist Name are required.")
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()