|
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") |
|
input_method = "Text" |
|
|
|
|
|
gemini_api_key = st.sidebar.text_input("Enter your Gemini API Key:", type="password") |
|
|
|
|
|
user_input = st.text_input("Enter your mood or context:") |
|
|
|
|
|
def get_song_recommendations(user_input, api_key): |
|
try: |
|
genai.configure(api_key=api_key) |
|
model = genai.GenerativeModel('gemini-pro') |
|
|
|
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 |
|
|
|
|
|
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"): |
|
|
|
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.") |