Akshayram1 commited on
Commit
f7c8a78
·
verified ·
1 Parent(s): 711b8a5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -0
app.py CHANGED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ import google.generativeai as genai
4
+ from pytube import Search
5
+ import speech_recognition as sr
6
+
7
+ # Load sentiment analysis model
8
+ mood_classifier = pipeline("sentiment-analysis")
9
+
10
+ # Speech-to-text function
11
+ def speech_to_text():
12
+ recognizer = sr.Recognizer()
13
+ with sr.Microphone() as source:
14
+ st.write("🎤 Speak now...")
15
+ audio = recognizer.listen(source)
16
+ try:
17
+ text = recognizer.recognize_google(audio)
18
+ st.write(f"🎤 You said: **{text}**")
19
+ return text
20
+ except sr.UnknownValueError:
21
+ st.error("Sorry, I could not understand your audio.")
22
+ return None
23
+ except sr.RequestError:
24
+ st.error("Sorry, there was an issue with the speech recognition service.")
25
+ return None
26
+
27
+ # Functions
28
+ def detect_mood(text):
29
+ result = mood_classifier(text)[0]
30
+ if result['label'] == 'POSITIVE':
31
+ return "joyful"
32
+ elif result['label'] == 'NEGATIVE':
33
+ return "sad"
34
+ else:
35
+ return "neutral"
36
+
37
+ def get_song_recommendations(mood, api_key):
38
+ try:
39
+ genai.configure(api_key=api_key)
40
+ model = genai.GenerativeModel('gemini-pro')
41
+ prompt = f"Suggest 3 popular Indian {mood} songs."
42
+ response = model.generate_content(prompt)
43
+ return response.text
44
+ except Exception as e:
45
+ st.error(f"Error using Gemini API: {e}")
46
+ return None
47
+
48
+ def search_youtube(query):
49
+ search = Search(query)
50
+ return search.results[0].watch_url
51
+
52
+ # Streamlit App
53
+ st.title("🎵 Mood-Based Indian Song Player")
54
+
55
+ # Sidebar for user input
56
+ st.sidebar.header("How are you feeling today?")
57
+ mood_options = ["happy", "sad", "energetic", "romantic", "calm"]
58
+
59
+ # Input for Gemini API key
60
+ gemini_api_key = st.sidebar.text_input("Enter your Gemini API Key:", type="password")
61
+
62
+ # Add a button for speech input
63
+ if st.sidebar.button("🎤 Speak your mood"):
64
+ user_mood = speech_to_text()
65
+ else:
66
+ user_mood = st.sidebar.selectbox("Or select your mood:", mood_options)
67
+
68
+ # Playlist
69
+ if 'playlist' not in st.session_state:
70
+ st.session_state.playlist = []
71
+
72
+ # Main content
73
+ if user_mood and gemini_api_key:
74
+ mood = detect_mood(user_mood)
75
+ st.write(f"🎭 Detected Mood: **{mood}**")
76
+
77
+ st.write("🎵 Recommended Songs:")
78
+ recommendations = get_song_recommendations(mood, gemini_api_key)
79
+ if recommendations:
80
+ st.write(recommendations)
81
+
82
+ song_names = recommendations.split("\n")
83
+ for song in song_names:
84
+ if song.strip():
85
+ st.write(f"🔍 Searching for: **{song}**")
86
+ video_url = search_youtube(song)
87
+ st.video(video_url)
88
+
89
+ # Add to playlist button
90
+ if st.button(f"Add '{song}' to Playlist"):
91
+ st.session_state.playlist.append((song, video_url))
92
+ st.success(f"Added '{song}' to your playlist!")
93
+ else:
94
+ st.error("Failed to get song recommendations. Please check your API key.")
95
+ elif not gemini_api_key:
96
+ st.warning("Please enter your Gemini API key to get started.")
97
+
98
+ # Display the playlist
99
+ st.sidebar.header("Your Playlist")
100
+ for idx, (song, url) in enumerate(st.session_state.playlist):
101
+ st.sidebar.write(f"{idx + 1}. {song}")
102
+ if st.sidebar.button(f"Play {song}", key=f"play_{idx}"):
103
+ st.video(url)