awacke1 commited on
Commit
b8baa22
·
verified ·
1 Parent(s): 7c825d0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -0
app.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import streamlit.components.v1 as components
3
+ import re
4
+ import os
5
+ import glob
6
+ import base64
7
+
8
+ st.set_page_config(layout="wide")
9
+
10
+ def get_image_base64(image_path):
11
+ with open(image_path, "rb") as image_file:
12
+ return base64.b64encode(image_file.read()).decode()
13
+
14
+ def process_line(line):
15
+ chord_images = {
16
+ 'Em': 'Em.png',
17
+ 'D': 'D.png',
18
+ 'C': 'C.png'
19
+ }
20
+ def replace_chord(match):
21
+ chord = match.group(0)
22
+ image_base64 = get_image_base64(chord_images.get(chord, 'default.png'))
23
+ return f"<strong>{chord}</strong><img src='data:image/png;base64,{image_base64}' style='height:20px;'>"
24
+ pattern = r'\b(' + '|'.join(re.escape(chord) for chord in chord_images.keys()) + r')\b'
25
+ line = re.sub(pattern, replace_chord, line)
26
+ return line
27
+
28
+ def process_chord_sheet(chord_sheet):
29
+ processed_lines = [process_line(line) for line in chord_sheet.split('\n')]
30
+ return '<br>'.join(processed_lines)
31
+
32
+ def load_song_file(file_path):
33
+ with open(file_path, 'r', encoding='utf-8') as file:
34
+ return file.read()
35
+
36
+ def create_search_url_wikipedia(artist_song):
37
+ base_url = "https://www.wikipedia.org/search-redirect.php?family=wikipedia&language=en&search="
38
+ return base_url + artist_song.replace(' ', '+').replace('–', '%E2%80%93').replace('&', 'and')
39
+
40
+ def create_search_url_youtube(artist_song):
41
+ base_url = "https://www.youtube.com/results?search_query="
42
+ return base_url + artist_song.replace(' ', '+').replace('–', '%E2%80%93').replace('&', 'and')
43
+
44
+ def create_search_url_chords(artist_song):
45
+ base_url = "https://www.ultimate-guitar.com/search.php?search_type=title&value="
46
+ return base_url + artist_song.replace(' ', '+').replace('–', '%E2%80%93').replace('&', 'and')
47
+
48
+ def create_search_url_lyrics(artist_song):
49
+ base_url = "https://www.google.com/search?q="
50
+ return base_url + artist_song.replace(' ', '+').replace('–', '%E2%80%93').replace('&', 'and') + '+lyrics'
51
+
52
+ def display_chord_sheet_in_two_page_view(chord_sheet):
53
+ css_content = """
54
+ <style>
55
+ .multi-column {
56
+ column-count: 2;
57
+ column-gap: 1em;
58
+ column-rule: thin solid black;
59
+ overflow: auto;
60
+ white-space: pre-wrap;
61
+ font-size: small;
62
+ }
63
+ </style>
64
+ """
65
+ html_content = f"""
66
+ {css_content}
67
+ <div class="multi-column">
68
+ {chord_sheet}
69
+ </div>
70
+ """
71
+ components.html(html_content, height=1200)
72
+
73
+ def main():
74
+ col1, col3 = st.columns([3, 5])
75
+ with col1:
76
+ st.markdown('''### 🎵🎥ChordPrompter🎸AI Prompt Authoring🎶Wiki, YouTube, Chords, Lyrics''')
77
+
78
+ with st.expander("🎶 **Select Song:**", expanded=True):
79
+ all_files = [f for f in glob.glob("*.txt") if ' by ' in f]
80
+ selected_file = st.selectbox("🎶 **Choose your Song:**", all_files, key='selected_file')
81
+
82
+ if selected_file:
83
+ song_info = os.path.splitext(selected_file)[0].replace("_", " ")
84
+ st.header(f"🎼 **Current Song:**")
85
+ st.markdown(f"🎸 **{song_info}**")
86
+ chord_sheet = load_song_file(selected_file)
87
+ processed_sheet = process_chord_sheet(chord_sheet)
88
+
89
+ table_md = f"""
90
+ | [📚Wikipedia]({create_search_url_wikipedia(song_info)}) | [🎥YouTube]({create_search_url_youtube(song_info)})
91
+ | [🎸Chords]({create_search_url_chords(song_info)}) | [🎶Lyrics]({create_search_url_lyrics(song_info)}) |
92
+ """
93
+ st.markdown(table_md)
94
+
95
+ st.header("🎼 **Available Songs**")
96
+ for file in all_files:
97
+ song_info = os.path.splitext(file)[0].replace("_", " ")
98
+ st.markdown(f"🎵 **{song_info}**")
99
+ table_md = f"""| [📚Wiki]({create_search_url_wikipedia(song_info)}) | [🎥YouTube]({create_search_url_youtube(song_info)}) | [🎸Chords]({create_search_url_chords(song_info)}) | [🎶Lyrics]({create_search_url_lyrics(song_info)}) | """
100
+ st.markdown(table_md)
101
+
102
+ with col3:
103
+ if 'selected_file' in st.session_state and st.session_state.selected_file:
104
+ chord_sheet = load_song_file(st.session_state.selected_file)
105
+ processed_sheet = process_chord_sheet(chord_sheet)
106
+ display_chord_sheet_in_two_page_view(processed_sheet)
107
+
108
+ if __name__ == '__main__':
109
+ main()