Spaces:
Running
Running
import streamlit as st | |
import re | |
import os | |
import glob | |
st.set_page_config(layout="wide") | |
def process_line(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='\1.png' style='height:20px;'>", line) | |
return line | |
def process_chord_sheet(chord_sheet): | |
processed_lines = [] | |
for line in chord_sheet.split('\n'): | |
processed_line = process_line(line) | |
processed_lines.append(processed_line) | |
return '<br>'.join(processed_lines) | |
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 songupdate(): | |
st.write(st.session_state.EnhancedChordSheet) | |
def load_song_file(filename): | |
with open(filename, "r") as file: | |
chord_sheet = file.read() | |
st.text_area(label="Enhanced Chord Sheet", value=chord_sheet, height=300, key="EnhancedChordSheet", help="This text can be read due to alternating chord lines and lyric lines.", label_visibility="visible", on_change=songupdate ) | |
processed_sheet = process_chord_sheet(chord_sheet) | |
st.markdown(processed_sheet, unsafe_allow_html=True) | |
# Automatic save function | |
def auto_save(): | |
song_name = st.session_state['song_name'] | |
artist_name = st.session_state['artist_name'] | |
chord_sheet = st.session_state['chord_sheet'] | |
# Display the character count | |
st.sidebar.write("Character Count:", len(chord_sheet)) | |
if song_name and artist_name: | |
filename = f"{song_name} by {artist_name}.txt".replace(" ", "_") | |
with open(filename, "w") as file: | |
file.write(chord_sheet) | |
st.sidebar.success("Chord sheet auto-saved.") | |
def main(): | |
st.title('🎵 Song Files') | |
with st.expander("Select Song File", expanded=True): | |
all_files = [f for f in glob.glob("*.txt") if ' by ' in f] | |
selected_file = st.selectbox("Choose a file", all_files) | |
if selected_file: | |
song_name, artist_name = os.path.splitext(selected_file)[0].split(' by ') | |
song_name = song_name.replace("_", " ") | |
artist_name = artist_name.replace("_", " ") | |
else: | |
song_name, artist_name = "", "" | |
col1, col2 = st.columns([4, 1]) | |
with col1: | |
song_name_input = st.text_input("🎵 Song Name", key='song_name') | |
artist_name_input = st.text_input("🎤 Artist Name", key='artist_name') | |
# Load chord sheet from selected file into the text area | |
if selected_file: | |
with open(selected_file, "r") as file: | |
chord_sheet_input = file.read() | |
st.session_state['chord_sheet'] = chord_sheet_input | |
chord_sheet_area = st.text_area("Chord Sheet", value=chord_sheet_input, height=300, key='chord_sheet', on_change=auto_save) | |
else: | |
chord_sheet_area = st.text_area("Chord Sheet", height=300, key='chord_sheet', on_change=auto_save) | |
# Save functionality | |
if st.button("💾 Save", key="save_song"): | |
if song_name_input and artist_name_input: | |
filename = f"{song_name_input} by {artist_name_input}.txt".replace(" ", "_") | |
with open(filename, "w") as file: | |
file.write(chord_sheet_area) | |
st.success("Chord sheet saved.") | |
else: | |
st.error("Both Song Name and Artist Name are required.") | |
st.header("🎼 Available Songs") | |
for file in all_files: | |
song_info = os.path.splitext(file)[0].replace("_", " ") | |
icol1, icol2 = st.columns([4, 1]) | |
with icol1: | |
st.markdown(f"* {song_info}") | |
with icol2: | |
st.markdown(f"[📚Wikipedia]({create_search_url_wikipedia(song_info)})") | |
st.markdown(f"[🎥YouTube]({create_search_url_youtube(song_info)})") | |
st.markdown(f"[🎸Chords]({create_search_url_chords(song_info)})") | |
st.markdown(f"[🎶Lyrics]({create_search_url_lyrics(song_info)})") | |
if selected_file: | |
load_song_file(selected_file) | |
song_info = os.path.splitext(selected_file)[0].replace("_", " ") | |
with col2: | |
if selected_file: | |
st.markdown(f"**Selected Song:** {song_info}") | |
st.markdown(f"[📚Wikipedia]({create_search_url_wikipedia(song_info)})") | |
st.markdown(f"[🎥YouTube]({create_search_url_youtube(song_info)})") | |
st.markdown(f"[🎸Chords]({create_search_url_chords(song_info)})") | |
st.markdown(f"[🎶Lyrics]({create_search_url_lyrics(song_info)})") | |
if __name__ == '__main__': | |
main() |