File size: 5,787 Bytes
a1de66d
ce879dc
a1de66d
fbfdf9e
a830900
29a1d82
 
a830900
29a1d82
4da6726
 
 
 
 
a1de66d
a29f926
 
 
 
 
 
 
4da6726
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a29f926
 
 
 
 
 
 
 
 
 
 
 
a1de66d
3c3ae65
a1de66d
29a1d82
a1de66d
29a1d82
a1de66d
29a1d82
a96c21d
 
 
 
f4065b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e12508c
e333a11
 
 
 
 
 
 
 
 
 
 
 
 
1442cce
e333a11
e12508c
e333a11
 
 
e12508c
 
e333a11
29a1d82
a1de66d
092fa31
29a1d82
4da4ce9
5583fd4
feffd36
 
29a1d82
f4065b2
 
 
 
 
 
b4bf86f
f4065b2
 
 
 
 
 
e12508c
f4065b2
 
 
 
 
 
 
 
 
 
 
 
 
29a1d82
7692c5b
e12508c
e333a11
e12508c
 
 
29a1d82
6f681ea
29a1d82
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import streamlit as st
import streamlit.components.v1 as components
import re
import os
import glob

# Set Streamlit page configuration
st.set_page_config(layout="wide")

import base64

def get_image_base64(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode()
def process_line(line):
    # Define a dictionary for chord-to-image mapping
    chord_images = {
        'Em': 'Em.png',
        'D': 'D.png',
        'C': 'C.png'
    }

    # Function to replace chord with base64 image
    def replace_chord(match):
        chord = match.group(0)
        image_base64 = get_image_base64(chord_images.get(chord, 'default.png'))
        return f"<img src='data:image/png;base64,{image_base64}' style='height:20px;'>"

    # Use regular expression to find and replace chords
    pattern = r'\b(' + '|'.join(re.escape(chord) for chord in chord_images.keys()) + r')\b'
    line = re.sub(pattern, replace_chord, line)
    return line


def process_line_old2(line):
    # Define a dictionary for chord-to-image mapping
    chord_images = {
        'Em': 'Em.png',
        'D': 'D.png',
        'C': 'C.png'
    }

    # Function to replace chord with image
    def replace_chord(match):
        chord = match.group(0)
        return f"<img src='{chord_images.get(chord, 'default.png')}' style='height:20px;'>"

    # Use regular expression to find and replace chords
    pattern = r'\b(' + '|'.join(re.escape(chord) for chord in chord_images.keys()) + r')\b'
    line = re.sub(pattern, replace_chord, line)
    return line


def process_line_old(line):
    if re.search(r'\b[A-G][#b]?m?\b', line):
        line = re.sub(r'\b([A-G][#b]?m?)\b', r"<img src='\Em.png' style='height:20px;'>", line)
    return line

def process_chord_sheet(chord_sheet):
    processed_lines = [process_line(line) for line in chord_sheet.split('\n')]
    return '<br>'.join(processed_lines)

def load_song_file(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        return file.read()

def create_search_url_wikipedia(artist_song):
    base_url = "https://www.wikipedia.org/search-redirect.php?family=wikipedia&language=en&search="
    return base_url + artist_song.replace(' ', '+').replace('โ€“', '%E2%80%93').replace('&', 'and')

def create_search_url_youtube(artist_song):
    base_url = "https://www.youtube.com/results?search_query="
    return base_url + artist_song.replace(' ', '+').replace('โ€“', '%E2%80%93').replace('&', 'and')

def create_search_url_chords(artist_song):
    base_url = "https://www.ultimate-guitar.com/search.php?search_type=title&value="
    return base_url + artist_song.replace(' ', '+').replace('โ€“', '%E2%80%93').replace('&', 'and')

def create_search_url_lyrics(artist_song):
    base_url = "https://www.google.com/search?q="
    return base_url + artist_song.replace(' ', '+').replace('โ€“', '%E2%80%93').replace('&', 'and') + '+lyrics'

def display_chord_sheet_in_two_page_view(chord_sheet):
    # CSS for multi-column layout
    css_content = """
    <style>
    .multi-column {
        column-count: 2;
        column-gap: 1em;
        column-rule: thin solid black;
        overflow: auto;
        white-space: pre-wrap;
        font-size: small;
    }
    </style>
    """

    # HTML structure with multi-column layout
    html_content = f"""
    {css_content}
    <div class="multi-column">
        {chord_sheet}
    </div>
    """
    components.html(html_content, height=1200)

def main():
    col1, col3 = st.columns([3, 5])

    with col1:
        st.markdown('### ๐ŸŽต ๐Ÿ“šPrompt๐ŸŽฅ๐ŸŽธChord Sheet๐ŸŽถ AI Prompt Authoring App')
        with st.expander("Select Song:", expanded=True):
            all_files = [f for f in glob.glob("*.txt") if ' by ' in f]
            selected_file = st.selectbox("Choose: ", all_files, key='selected_file')
            if selected_file:
                song_info = os.path.splitext(selected_file)[0].replace("_", " ")
                st.header("๐ŸŽผ Current Song")
                st.markdown("**" + song_info + "**")
                chord_sheet = load_song_file(selected_file)
                processed_sheet = process_chord_sheet(chord_sheet)
                #st.markdown(processed_sheet, unsafe_allow_html=True)
                table_md = f"""
                | Wikipedia | YouTube | Chords | Lyrics |
                | --------- | ------- | ------ | ------ |
                | [๐Ÿ“š]({create_search_url_wikipedia(song_info)}) | [๐ŸŽฅ]({create_search_url_youtube(song_info)}) | [๐ŸŽธ]({create_search_url_chords(song_info)}) | [๐ŸŽถ]({create_search_url_lyrics(song_info)}) |
                """
                st.markdown(table_md)
        
        st.header("๐ŸŽผ Available Songs")
        for file in all_files:
            song_info = os.path.splitext(file)[0].replace("_", " ")
            icol1, icol2 = st.columns([1, 3])
            with icol1:
                st.markdown("**" + song_info + "**")
            with icol2:
                table_md = f"""
                | Wikipedia | YouTube | Chords | Lyrics |
                | --------- | ------- | ------ | ------ |
                | [๐Ÿ“š]({create_search_url_wikipedia(song_info)}) | [๐ŸŽฅ]({create_search_url_youtube(song_info)}) | [๐ŸŽธ]({create_search_url_chords(song_info)}) | [๐ŸŽถ]({create_search_url_lyrics(song_info)}) |
                """
                st.markdown(table_md)

    with col3:
        if 'selected_file' in st.session_state and st.session_state.selected_file:
            # Display chord sheet in multi-column view
            chord_sheet = load_song_file(st.session_state.selected_file)
            processed_sheet = process_chord_sheet(chord_sheet)
            display_chord_sheet_in_two_page_view(processed_sheet)

if __name__ == '__main__':
    main()