MusicChallenge / app.py
sahandkh1419's picture
Update app.py
431feaa verified
raw
history blame
5.75 kB
import streamlit as st
import whisper
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import base64
from pydub import AudioSegment
st.set_page_config(
page_title="Sing It Forward App",
page_icon="🎵")
st.markdown(
"""
<style>
body {
background: linear-gradient(to bottom, #0E5AAB, #00ffff);
padding: 20px;
border-radius: 10px;
}
a {
color: #EDA67C !important
}
</style>
""",
unsafe_allow_html=True
)
def load_image(image_file):
with open(image_file, "rb") as f:
return f.read()
image_data = load_image("bcg.jpg")
image_base64 = base64.b64encode(image_data).decode()
st.markdown(
f"""
<style>
.stApp {{
background-image: url(data:image/jpeg;base64,{image_base64});
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}}
</style>
""",
unsafe_allow_html=True
)
st.markdown("<h1 style='text-align: center; margin-bottom: 5px;'>Sing It Forward App🎵</h1>", unsafe_allow_html=True)
description = """
<h5>Welcome to Sing It Forward App!</h5>
<p style="text-align: justify;">
Get ready to test your singing skills and memory! First, listen carefully to the first part of the song, then it’s your turn to shine.
Record yourself singing the next 15 seconds on your own, matching the lyrics and rhythm perfectly. Think you’ve got what it takes to keep the music going?
Let’s see if you can hit the right notes and showcase your talent! Unleash your inner star and take the challenge!
</p>
📌For any questions or contact:
**Name:** <span style="color: #EDA67C;">Sahand Khorsandi</span>
**Email:** <a href="mailto:[email protected]" style="color: #EDA67C;">[email protected]</a>"""
st.markdown(description, unsafe_allow_html=True)
st.write('------')
def cosine_sim(text1, text2):
vectorizer = TfidfVectorizer().fit_transform([text1, text2])
vectors = vectorizer.toarray()
return cosine_similarity(vectors)[0, 1]
model = whisper.load_model("base")
tab1, tab2 = st.tabs(["Take Challenge", "Make Challenge"])
with tab1:
st.write("Listen to music since you have to record 15seconds after that")
st.audio("titanic.mp3")
audio_value = st.experimental_audio_input("Sing Rest of music:🎙️")
lyrics = "Far across the distance And spaces between us You have come to show you go on"
if audio_value:
with open("user_sing.mp3", "wb") as f:
f.write(audio_value.getbuffer())
user_lyrics = model.transcribe("user_sing.mp3")["text"]
st.write(user_lyrics)
similarity_score = cosine_sim(lyrics, user_lyrics)
if similarity_score > 0.85:
st.success('Awsome! You are doing great', icon="✅")
st.markdown('<style>div.stAlert { background-color: rgba(3, 67, 24, 0.9); }</style>', unsafe_allow_html=True)
else:
st.error('Awful! Try harder next time', icon="🚨")
st.markdown('<style>div.stAlert { background-color: rgba(241, 36, 36, 0.9); }</style>', unsafe_allow_html=True)
def take_challenge(music_file, typed_lyrics, key):
st.write('------')
st.write("Listen to music since you have to record 15seconds after that")
st.audio(music_file)
audio_value = st.experimental_audio_input("Sing Rest of music:🎙️", key)
if audio_value:
with open("user_sing.mp3", "wb") as f:
f.write(audio_value.getbuffer())
user_lyrics = model.transcribe("user_sing.mp3")["text"]
st.write(user_lyrics)
similarity_score = cosine_sim(typed_lyrics, user_lyrics)
if similarity_score > 0.85:
st.success('Awsome! You are doing great', icon="✅")
st.markdown('<style>div.stAlert { background-color: rgba(3, 67, 24, 0.9); }</style>', unsafe_allow_html=True)
else:
st.error('Awful! Try harder next time', icon="🚨")
st.markdown('<style>div.stAlert { background-color: rgba(241, 36, 36, 0.9); }</style>', unsafe_allow_html=True)
with tab2:
st.write("Upload music to make challenge:")
uploaded_file = st.file_uploader("Choose a music file", type=["mp3", "wav"])
if uploaded_file is not None:
with open("raw_music.mp3", "wb") as f:
f.write(uploaded_file.getbuffer())
st.audio("raw_music.mp3")
trimm_check = st.checkbox("Trim?")
if trimm_check:
st.write("Specify start and end times for trimming:")
audio = AudioSegment.from_file("raw_music.mp3")
duration = len(audio) // 1000
start_time = st.number_input("Start Time (seconds)", min_value=0, max_value=duration, value=0)
end_time = st.number_input("End Time (seconds)", min_value=0, max_value=duration, value=duration)
if start_time < end_time:
trimmed_audio = audio[start_time * 1000:end_time * 1000]
trimmed_audio.export("trimmed_music.mp3", format="mp3")
st.write("Now type what user should sing:")
typed_lyrics = st.text_area("Lyrics to be singed:")
take_challenge("trimmed_music.mp3", typed_lyrics, "unique_key_1")
else:
st.error('Start Time should be smaller than End Time!', icon="❌")
st.markdown('<style>div.stAlert { background-color: rgba(241, 36, 36, 0.9); }</style>', unsafe_allow_html=True)
else:
st.write("Now type what user should sing:")
typed_lyrics = st.text_area("Lyrics to be singed:")
take_challenge("raw_music.mp3", typed_lyrics, "unique_key_2")