|
import streamlit as st |
|
import google.generativeai as genai |
|
from pytube import Search |
|
|
|
|
|
st.title("🎶 Mood-Based Indian Song Player") |
|
|
|
|
|
st.sidebar.header("Enter Your Input") |
|
|
|
|
|
try: |
|
gemini_api_key = st.secrets["GEMINI_API_KEY"] |
|
except KeyError: |
|
st.error("Gemini API key not found in secrets. Please configure it in your Streamlit secrets.") |
|
gemini_api_key = None |
|
|
|
|
|
user_input = st.text_input("Enter your mood and context:", |
|
placeholder="e.g., 'I'm feeling happy on a sunny day'") |
|
|
|
|
|
if 'playlist' not in st.session_state: |
|
st.session_state.playlist = [] |
|
|
|
|
|
def get_song_recommendations(user_input, api_key): |
|
try: |
|
genai.configure(api_key=api_key) |
|
model = genai.GenerativeModel('gemini-2.0-flash-lite') |
|
|
|
prompt = f""" |
|
Given the input: '{user_input}', determine the mood or context and suggest 3 popular Indian songs that match this mood or context, considering the specific details provided. |
|
|
|
Examples: |
|
- Input: 'I'm feeling stressed at work' |
|
Mood: stressed |
|
Songs: calming instrumental music |
|
- Input: 'I'm excited about my vacation' |
|
Mood: excited |
|
Songs: upbeat and joyful songs |
|
|
|
Output the mood followed by the song recommendations in the following format: |
|
Mood: [mood] |
|
1. Song Title - Artist Name |
|
2. Song Title - Artist Name |
|
3. Song Title - Artist Name |
|
""" |
|
response = model.generate_content(prompt) |
|
return response.text |
|
except Exception as e: |
|
st.error(f"Error using Gemini API: {e}") |
|
return None |
|
|
|
|
|
def search_youtube(query): |
|
search = Search(query) |
|
if search.results: |
|
return search.results[0].watch_url |
|
else: |
|
return None |
|
|
|
|
|
if user_input and gemini_api_key: |
|
|
|
response = get_song_recommendations(user_input, gemini_api_key) |
|
if response: |
|
|
|
lines = response.split('\n') |
|
if len(lines) > 1 and lines[0].startswith("Mood:"): |
|
mood = lines[0].replace("Mood: ", "") |
|
st.write(f"Detected Mood/Context: {mood}") |
|
songs = [line.strip() for line in lines[1:4] if line.strip()] |
|
st.write("🎵 Recommended Songs:") |
|
for song in songs: |
|
st.write(song) |
|
|
|
query = f"{song} song" |
|
video_url = search_youtube(query) |
|
if video_url: |
|
st.video(video_url) |
|
|
|
if st.button(f"Add '{song}' to Playlist", key=f"add_{song}"): |
|
st.session_state.playlist.append((song, video_url)) |
|
st.success(f"Added '{song}' to your playlist!") |
|
else: |
|
st.warning(f"Could not find '{song}' on YouTube.") |
|
else: |
|
st.error("Gemini API response format is incorrect.") |
|
else: |
|
st.warning("Please provide input to get song recommendations.") |
|
elif not user_input: |
|
st.warning("Please enter your mood and context.") |
|
elif not gemini_api_key: |
|
st.warning("Unable to retrieve Gemini API key. Please check your Streamlit secrets configuration.") |
|
|
|
|
|
st.sidebar.header("Your Playlist") |
|
if st.session_state.playlist: |
|
for idx, (song, url) in enumerate(st.session_state.playlist): |
|
st.sidebar.write(f"{idx + 1}. {song}") |
|
if st.sidebar.button(f"Play {song}", key=f"play_{song}"): |
|
st.video(url) |
|
else: |
|
st.sidebar.write("Your playlist is empty.") |