Spaces:
Running
Running
import streamlit as st | |
import streamlit.components.v1 as components | |
import re | |
import os | |
import glob | |
import base64 | |
st.set_page_config(layout="wide") | |
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): | |
chord_images = { | |
'Em': 'Em.png', | |
'D': 'D.png', | |
'C': 'C.png' | |
} | |
def replace_chord(match): | |
chord = match.group(0) | |
image_base64 = get_image_base64(chord_images.get(chord, 'default.png')) | |
return f"<strong>{chord}</strong><img src='data:image/png;base64,{image_base64}' style='height:20px;'>" | |
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_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_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_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('''### 🎵🎥ChordPrompter🎸AI Prompt Authoring🎶Wiki, YouTube, Chords, Lyrics''') | |
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]({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)}) | | |
""" | |
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,1]) | |
#with icol1: | |
st.markdown("**" + song_info + "**") | |
#with icol2: | |
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)}) | """ | |
st.markdown(table_md) | |
with col3: | |
if 'selected_file' in st.session_state and st.session_state.selected_file: | |
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() |