ChordPrompter / app.py
awacke1's picture
Update app.py
29a1d82 verified
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")
def process_line(line):
# Process each line to replace chords with images
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):
# Process the entire chord sheet
processed_lines = [process_line(line) for line in chord_sheet.split('\n')]
return '<br>'.join(processed_lines)
def create_search_url(base_url, artist_song):
# Create search URL with appropriate character replacements
return base_url + artist_song.replace(' ', '+').replace('โ€“', '%E2%80%93').replace('&', 'and')
def load_song_file(file_path):
# Load a song file and return its content
with open(file_path, 'r', encoding='utf-8') as file:
return file.read()
def parse_filename(filename):
# Parse the filename to extract song and artist names
base_name = os.path.splitext(filename)[0]
song_name, artist_name = base_name.split(' by ')
return song_name.replace("_", " "), artist_name.replace("_", " ")
def main():
col1, col3 = st.columns([3, 5])
with col1:
st.markdown('### ๐ŸŽต ๐Ÿ“šPrompt๐ŸŽฅ๐ŸŽธChord Sheet๐ŸŽถ AI Prompt Authoring App')
# Expander for selecting the song
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')
# Input fields for song and artist names
song_name_input = st.text_input("๐ŸŽต Song:", key='song_name')
artist_name_input = st.text_input("๐ŸŽค Artist:", key='artist_name')
if selected_file:
# Display the current song
song_info = os.path.splitext(selected_file)[0].replace("_", " ")
st.header("๐ŸŽผ Current Song")
st.markdown("**" + song_info + "**")
processed_sheet = process_chord_sheet(load_song_file(selected_file))
components.html(processed_sheet, height=600) # Using HTML component for chord sheet
# Markdown table with links
table_md = f"""
| Wikipedia | YouTube | Chords | Lyrics |
| --------- | ------- | ------ | ------ |
| [๐Ÿ“š]({create_search_url("https://www.wikipedia.org/search-redirect.php?family=wikipedia&language=en&search=", song_info)}) | [๐ŸŽฅ]({create_search_url("https://www.youtube.com/results?search_query=", song_info)}) | [๐ŸŽธ]({create_search_url("https://www.ultimate-guitar.com/search.php?search_type=title&value=", song_info)}) | [๐ŸŽถ]({create_search_url("https://www.google.com/search?q=", song_info + '+lyrics')}) |
"""
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 + "**")
processed_sheet = process_chord_sheet(load_song_file(file))
components.html(processed_sheet, height=150) # Displaying processed chord sheets
with col3:
# Chord sheet text area
chord_sheet_area = st.text_area("Chord Sheet", value=load_song_file(selected_file) if selected_file else '', height=1600, key='chord_sheet')
# Save functionality
if st.button("๐Ÿ’พ Save", key="save_song"):
if song_name_input and artist_name_input:
filename = song_name_input + " by " + artist_name_input + ".txt"
with open(filename, "w") as file:
file.write(chord_sheet_area)
st.success("Chord sheet saved to file: " + filename)
else:
st.error("Both Song Name and Artist Name are required.")
if __name__ == '__main__':
main()