File size: 4,018 Bytes
f7c8a78 f4d0320 70cb624 f7c8a78 70cb624 d89bac8 59db83c f7c8a78 1ddcf0f b322b24 70cb624 f7c8a78 212fb7e 1ddcf0f 70cb624 1ddcf0f 70cb624 d1eb12b 70cb624 f7c8a78 70cb624 f7c8a78 70cb624 f7c8a78 70cb624 1ddcf0f 70cb624 f7c8a78 70cb624 1ddcf0f f7c8a78 59db83c 1ddcf0f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
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.") |