File size: 2,950 Bytes
f7c8a78 f4d0320 70cb624 f7c8a78 70cb624 d89bac8 70cb624 f7c8a78 70cb624 b322b24 70cb624 f7c8a78 70cb624 d1eb12b 70cb624 f7c8a78 70cb624 f7c8a78 70cb624 f7c8a78 70cb624 f7c8a78 70cb624 f7c8a78 70cb624 |
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 |
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")
input_method = "Text" # Only text input is available
# Input for Gemini API key
gemini_api_key = st.sidebar.text_input("Enter your Gemini API Key:", type="password")
# Text input for user's mood or context
user_input = st.text_input("Enter your mood or context:")
# 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-pro')
# Prompt to guide the AI
prompt = f"""
Based on the input: '{user_input}', determine the mood or context and suggest 3 popular Indian songs that match this mood or context.
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)
if st.button(f"Add '{song}' to Playlist"):
# Add to playlist logic here
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 or context.")
elif not gemini_api_key:
st.warning("Please enter your Gemini API key to get started.") |