import streamlit as st import google.generativeai as genai from pytube import Search # Streamlit App st.title("🎶 Mood-Based Indian Song Player") # Sidebar for user input st.sidebar.header("Enter Your Input") # Retrieve Gemini API key from secrets 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 # Text input for user's mood and context user_input = st.text_input("Enter your mood and context:", placeholder="e.g., 'I'm feeling happy on a sunny day'") # Initialize playlist in session state if 'playlist' not in st.session_state: st.session_state.playlist = [] # Function to get song recommendations using Gemini API def get_song_recommendations(user_input, api_key): try: genai.configure(api_key=api_key) model = genai.GenerativeModel('gemini-2.0-flash-lite') # Enhanced prompt with examples 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 # Function to search YouTube for a song def search_youtube(query): search = Search(query) if search.results: return search.results[0].watch_url else: return None # Main content if user_input and gemini_api_key: # Get combined mood detection and song recommendations from Gemini API response = get_song_recommendations(user_input, gemini_api_key) if response: # Parse mood and song recommendations 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) # Search YouTube and provide playback options query = f"{song} song" video_url = search_youtube(query) if video_url: st.video(video_url) # Add to playlist button 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.") # Display the playlist 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.")