ChordPrompter / app.py
awacke1's picture
Update app.py
1442cce verified
raw
history blame
2.48 kB
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):
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 = [process_line(line) for line in chord_sheet.split('\n')]
return '<br>'.join(processed_lines)
def split_text(text, split_ratio=0.5):
lines = text.split('\n')
split_point = int(len(lines) * split_ratio)
return '\n'.join(lines[:split_point]), '\n'.join(lines[split_point:])
def display_chord_sheet_in_two_page_view(chord_sheet):
# Splitting the text into two halves
first_half, second_half = split_text(chord_sheet)
# HTML structure for two-page view
html_content = f"""
<div style="display: flex; justify-content: space-between;">
<div style="width: 49%; overflow: scroll; white-space: pre-wrap; font-size: small;">
{first_half}
</div>
<div style="width: 49%; overflow: scroll; white-space: pre-wrap; font-size: small;">
{second_half}
</div>
</div>
"""
components.html(html_content, height=1200) # Increased height
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)
with col3:
if 'selected_file' in st.session_state and st.session_state.selected_file:
# Display chord sheet in two-page 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()