CoolFace
Apppublic

awacke1/CardGameAI

sourceHugging Facemitupdated 3y agoView on Hugging Face
1likes
ChordPrompter.py111 linesDownload Raw Back to root
1import streamlit as st2import streamlit.components.v1 as components3import re4import os5import glob6import base647 8st.set_page_config(layout="wide")9 10def get_image_base64(image_path):11    with open(image_path, "rb") as image_file:12        return base64.b64encode(image_file.read()).decode()13 14def 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 line27 28def 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 32def load_song_file(file_path):33    with open(file_path, 'r', encoding='utf-8') as file:34        return file.read()35 36def 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 40def 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 44def 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 48def 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 52def 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 73def main():74    col1, col3 = st.columns([3, 5])75    with col1:76        st.markdown('### 🎵🎥 ChordPrompter 🎸 AI Prompt Authoring - Wiki, YouTube, Chords, Lyrics📚🎶')77        with st.expander("Select Song:", expanded=True):78            all_files = [f for f in glob.glob("*.txt") if ' by ' in f]79            selected_file = st.selectbox("Choose: ", all_files, key='selected_file')80            if selected_file:81                song_info = os.path.splitext(selected_file)[0].replace("_", " ")82                st.header("🎼 Current Song")83                st.markdown("**" + song_info + "**")84                chord_sheet = load_song_file(selected_file)85                processed_sheet = process_chord_sheet(chord_sheet)86                #st.markdown(processed_sheet, unsafe_allow_html=True)87                table_md = f"""88                | [📚Wikipedia]({create_search_url_wikipedia(song_info)}) | [🎥YouTube]({create_search_url_youtube(song_info)})  89                | [🎸Chords]({create_search_url_chords(song_info)}) | [🎶Lyrics]({create_search_url_lyrics(song_info)}) |90                """91                st.markdown(table_md)92        st.header("🎼 Available Songs")93        for file in all_files:94            song_info = os.path.splitext(file)[0].replace("_", " ")95            icol1, icol2 = st.columns([1, 2])96            with icol1:97                st.markdown("**" + song_info + "**")98            with icol2:99                table_md = f"""100                | [📚Wikipedia]({create_search_url_wikipedia(song_info)}) | [🎥YouTube]({create_search_url_youtube(song_info)})  101                | [🎸Chords]({create_search_url_chords(song_info)}) | [🎶Lyrics]({create_search_url_lyrics(song_info)}) |102                """103                st.markdown(table_md)104    with col3:105        if 'selected_file' in st.session_state and st.session_state.selected_file:106            chord_sheet = load_song_file(st.session_state.selected_file)107            processed_sheet = process_chord_sheet(chord_sheet)108            display_chord_sheet_in_two_page_view(processed_sheet)109 110if __name__ == '__main__':111    main()