Spaces:
Sleeping
Sleeping
import streamlit as st | |
import re | |
import os | |
# Function to process each line of the chord sheet | |
def process_line(line): | |
# Check if the line is a chord line (contains chord symbols) | |
if re.search(r'\b[A-G][#b]?m?\b', line): | |
# Replace chord symbols with image tags | |
line = re.sub(r'\b([A-G][#b]?m?)\b', r"<img src='\1.png' style='height:20px;'>", line) | |
return line | |
# Function to process the entire chord sheet | |
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) | |
# Functions to create search URLs | |
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' | |
# Streamlit app | |
def main(): | |
st.title('Chord Sheet Processor') | |
# Song name and artist input | |
song_name_artist = st.text_input("Song Name by Music Artist", value="Hold On, Hold On by Neko Case") | |
# File operations | |
filename = song_name_artist.replace(", ", "_").replace(" ", "_").lower() + ".txt" | |
if st.button('Open Chord Sheet'): | |
if os.path.exists(filename): | |
with open(filename, "r") as file: | |
chord_sheet = file.read() | |
st.text_area("Chord Sheet", chord_sheet, height=300) | |
processed_sheet = process_chord_sheet(chord_sheet) | |
st.markdown(processed_sheet, unsafe_allow_html=True) | |
else: | |
st.error("File not found.") | |
# Text area for user to input the chord sheet | |
chord_sheet_input = st.text_area("Enter your chord sheet here:", height=300) | |
if st.button('Save Chord Sheet'): | |
with open(filename, "w") as file: | |
file.write(chord_sheet_input) | |
st.success("Chord sheet saved.") | |
if st.button('Process Chord Sheet'): | |
if chord_sheet_input: | |
# Processing the chord sheet | |
processed_sheet = process_chord_sheet(chord_sheet_input) | |
# Displaying the processed chord sheet | |
st.markdown(processed_sheet, unsafe_allow_html=True) | |
else: | |
st.error("Please input a chord sheet to process.") | |
# Displaying links | |
st.markdown(f"[📚Wikipedia]({create_search_url_wikipedia(song_name_artist)})") | |
st.markdown(f"[🎥YouTube]({create_search_url_youtube(song_name_artist)})") | |
st.markdown(f"[🎸Chords]({create_search_url_chords(song_name_artist)})") | |
st.markdown(f"[🎶Lyrics]({create_search_url_lyrics(song_name_artist)})") | |
if __name__ == '__main__': | |
main() | |