Akshayram1 commited on
Commit
46485b8
·
verified ·
1 Parent(s): 4e9f983

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -0
app.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import google.generativeai as genai
3
+ from pytube import Search
4
+
5
+ # Streamlit App
6
+ st.title("🎶 Mood-Based Indian Song Player")
7
+
8
+ # Sidebar for user input
9
+ st.sidebar.header("Enter Your Input")
10
+
11
+ # Input for Gemini API key
12
+ gemini_api_key = st.sidebar.text_input("Enter your Gemini API Key:", type="password")
13
+
14
+ # Text input for user's mood and context
15
+ user_input = st.text_input("Enter your mood and context:",
16
+ placeholder="e.g., 'I'm feeling happy on a sunny day'")
17
+
18
+ # Initialize playlist in session state
19
+ if 'playlist' not in st.session_state:
20
+ st.session_state.playlist = []
21
+
22
+ # Function to get song recommendations using Gemini API
23
+ def get_song_recommendations(user_input, api_key):
24
+ try:
25
+ genai.configure(api_key=api_key)
26
+ model = genai.GenerativeModel('gemini-pro')
27
+ # Enhanced prompt with examples
28
+ prompt = f"""
29
+ 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.
30
+
31
+ Examples:
32
+ - Input: 'I'm feeling stressed at work'
33
+ Mood: stressed
34
+ Songs: calming instrumental music
35
+ - Input: 'I'm excited about my vacation'
36
+ Mood: excited
37
+ Songs: upbeat and joyful songs
38
+
39
+ Output the mood followed by the song recommendations in the following format:
40
+ Mood: [mood]
41
+ 1. Song Title - Artist Name
42
+ 2. Song Title - Artist Name
43
+ 3. Song Title - Artist Name
44
+ """
45
+ response = model.generate_content(prompt)
46
+ return response.text
47
+ except Exception as e:
48
+ st.error(f"Error using Gemini API: {e}")
49
+ return None
50
+
51
+ # Function to search YouTube for a song
52
+ def search_youtube(query):
53
+ search = Search(query)
54
+ if search.results:
55
+ return search.results[0].watch_url
56
+ else:
57
+ return None
58
+
59
+ # Main content
60
+ if user_input and gemini_api_key:
61
+ # Get combined mood detection and song recommendations from Gemini API
62
+ response = get_song_recommendations(user_input, gemini_api_key)
63
+ if response:
64
+ # Parse mood and song recommendations
65
+ lines = response.split('\n')
66
+ if len(lines) > 1 and lines[0].startswith("Mood:"):
67
+ mood = lines[0].replace("Mood: ", "")
68
+ st.write(f"Detected Mood/Context: {mood}")
69
+ songs = [line.strip() for line in lines[1:4] if line.strip()]
70
+ st.write("🎵 Recommended Songs:")
71
+ for song in songs:
72
+ st.write(song)
73
+ # Search YouTube and provide playback options
74
+ query = f"{song} song"
75
+ video_url = search_youtube(query)
76
+ if video_url:
77
+ st.video(video_url)
78
+ # Add to playlist button
79
+ if st.button(f"Add '{song}' to Playlist", key=f"add_{song}"):
80
+ st.session_state.playlist.append((song, video_url))
81
+ st.success(f"Added '{song}' to your playlist!")
82
+ else:
83
+ st.warning(f"Could not find '{song}' on YouTube.")
84
+ else:
85
+ st.error("Gemini API response format is incorrect.")
86
+ else:
87
+ st.warning("Please provide input to get song recommendations.")
88
+ elif not user_input:
89
+ st.warning("Please enter your mood and context.")
90
+ elif not gemini_api_key:
91
+ st.warning("Please enter your Gemini API key to get started.")
92
+
93
+ # Display the playlist
94
+ st.sidebar.header("Your Playlist")
95
+ if st.session_state.playlist:
96
+ for idx, (song, url) in enumerate(st.session_state.playlist):
97
+ st.sidebar.write(f"{idx + 1}. {song}")
98
+ if st.sidebar.button(f"Play {song}", key=f"play_{song}"):
99
+ st.video(url)
100
+ else:
101
+ st.sidebar.write("Your playlist is empty.")