RecepOnur's picture
Upload 4 files
6640aee verified
import gradio as gr
import pandas as pd
from get_recommend import get_song_recommendation, get_spotify_links, get_custom_recommendation
def recommend_music(input_type, genre, mood, custom_query):
try:
if input_type == "genre_mood":
songs = get_song_recommendation(genre, mood)
else:
songs = get_custom_recommendation(custom_query)
spotify_links = get_spotify_links(songs)
data = []
for recommendation in spotify_links:
if '|' in recommendation:
song_info, description, spotify_url = [part.strip() for part in recommendation.split('|')]
data.append({
"Song & Artist": song_info,
"Description": description,
"Spotify Link": f"<a href='{spotify_url}' target='_blank'>{spotify_url}</a>"
})
df = pd.DataFrame(data)
return df
except Exception as e:
return f"Error: {str(e)}"
MOODS = ["happy", "sad", "energetic", "relaxed", "angry", "romantic", "nostalgic", "melancholic", "dark"]
custom_css = """
a {
color: #1DB954 !important;
text-decoration: none !important;
font-weight: bold;
}
a:hover {
text-decoration: underline !important;
color: #1ED760 !important;
}
textarea{
background-color: #F6F6F6 !important;
border: 2px solid #1DB954 !important;
outline: none !important;
}
"""
with gr.Blocks(theme='JohnSmith9982/small_and_pretty', css=custom_css) as demo:
gr.Markdown(
"""
# 🎵 TuneMate - AI-Powered Music Recommender
Get personalized music recommendations based on your preferences!
"""
)
with gr.Row():
with gr.Column(scale=1):
input_type = gr.Radio(
choices=["genre_mood", "custom_query"],
label="How would you like to get recommendations?",
value="genre_mood"
)
with gr.Group() as genre_mood_group:
genre_input = gr.Textbox(
label="Genre",
placeholder="Enter a music genre (e.g., rock, jazz, pop)",
info="Type any music genre you like",
visible=True
)
mood_input = gr.Dropdown(
choices=MOODS,
label="Mood",
info="Select how you're feeling",
value="happy",
visible=True
)
with gr.Group() as custom_query_group:
custom_query_input = gr.Textbox(
label="Custom Query",
placeholder="e.g., 'upbeat rock songs for workout' or 'relaxing jazz for studying'",
info="Describe what kind of music you're looking for",
visible=False,
lines=3
)
submit_btn = gr.Button("Get Recommendations 🎧", variant="primary")
output = gr.Dataframe(
headers=["Song & Artist", "Description", "Spotify Link"],
datatype=["str", "str", "html"],
col_count=(3, "fixed"),
wrap=True,
row_count=5,
interactive=False
)
def update_input_visibility(choice):
if choice == "genre_mood":
return [
gr.update(visible=True), # genre input
gr.update(visible=True), # mood input
gr.update(visible=False) # custom query input
]
else: # custom_query
return [
gr.update(visible=False), # genre input
gr.update(visible=False), # mood input
gr.update(visible=True) # custom query input
]
input_type.change(
update_input_visibility,
inputs=[input_type],
outputs=[genre_input, mood_input, custom_query_input]
)
submit_btn.click(
recommend_music,
inputs=[input_type, genre_input, mood_input, custom_query_input],
outputs=[output]
)
gr.Markdown(
"""
### How to use:
1. Choose your recommendation method:
- Genre & Mood: Traditional way with specific genre and mood
- Custom Query: Describe exactly what you're looking for
2. Fill in the required information
3. Click 'Get Recommendations' to discover new music!
*Powered by OpenAI and Spotify APIs*
"""
)
demo.launch()