Update app.py
Browse files
app.py
CHANGED
|
@@ -1,185 +1,77 @@
|
|
| 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 |
-
import tempfile
|
| 7 |
-
from audio_recorder_streamlit import audio_recorder
|
| 8 |
-
import numpy as np
|
| 9 |
-
import wave
|
| 10 |
-
import io
|
| 11 |
-
import logging
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
# Load sentiment analysis model using PyTorch backend
|
| 17 |
-
mood_classifier = pipeline("sentiment-analysis", framework="pt")
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
with wave.open(wav_buffer, 'wb') as wav_file:
|
| 24 |
-
wav_file.setnchannels(1) # Mono
|
| 25 |
-
wav_file.setsampwidth(2) # 2 bytes per sample
|
| 26 |
-
wav_file.setframerate(44100) # Sample rate
|
| 27 |
-
wav_file.writeframes(audio_bytes)
|
| 28 |
-
|
| 29 |
-
return wav_buffer.getvalue()
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
if result['label'] == 'POSITIVE':
|
| 34 |
-
return "happy"
|
| 35 |
-
elif result['label'] == 'NEGATIVE':
|
| 36 |
-
return "sad"
|
| 37 |
-
else:
|
| 38 |
-
return "calm"
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
r = sr.Recognizer()
|
| 43 |
-
|
| 44 |
-
# Add audio recording widget
|
| 45 |
-
audio_bytes = audio_recorder(
|
| 46 |
-
text="Click to record your mood",
|
| 47 |
-
recording_color="#e8b62c",
|
| 48 |
-
neutral_color="#6aa36f",
|
| 49 |
-
pause_threshold=2.0 # Automatically stop after 2 seconds of silence
|
| 50 |
-
)
|
| 51 |
-
|
| 52 |
-
if audio_bytes:
|
| 53 |
-
try:
|
| 54 |
-
# Create a temporary WAV file
|
| 55 |
-
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_audio:
|
| 56 |
-
# Convert and write audio bytes to WAV format
|
| 57 |
-
wav_bytes = convert_audio_to_wav(audio_bytes)
|
| 58 |
-
temp_audio.write(wav_bytes)
|
| 59 |
-
temp_audio.flush()
|
| 60 |
-
|
| 61 |
-
# Use the temporary file for speech recognition
|
| 62 |
-
with sr.AudioFile(temp_audio.name) as source:
|
| 63 |
-
# Record the audio file
|
| 64 |
-
audio = r.record(source)
|
| 65 |
-
|
| 66 |
-
try:
|
| 67 |
-
# Attempt speech recognition
|
| 68 |
-
text = r.recognize_google(audio)
|
| 69 |
-
st.success("Speech recognized successfully!")
|
| 70 |
-
return text
|
| 71 |
-
except sr.UnknownValueError:
|
| 72 |
-
st.error("Could not understand the audio. Please try speaking clearly and try again.")
|
| 73 |
-
return None
|
| 74 |
-
except sr.RequestError as e:
|
| 75 |
-
st.error(f"Could not request results from speech recognition service; {e}")
|
| 76 |
-
return None
|
| 77 |
-
except Exception as e:
|
| 78 |
-
st.error(f"Error processing audio: {e}")
|
| 79 |
-
return None
|
| 80 |
-
return None
|
| 81 |
|
| 82 |
-
|
|
|
|
| 83 |
try:
|
| 84 |
genai.configure(api_key=api_key)
|
| 85 |
model = genai.GenerativeModel('gemini-pro')
|
| 86 |
-
#
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
- If the mood is "energetic," suggest high-energy dance or workout songs.
|
| 92 |
-
- If the mood is "romantic," suggest romantic Bollywood or Indian love songs.
|
| 93 |
-
- If the mood is "calm," suggest soothing Indian classical or instrumental music.
|
| 94 |
-
Always suggest 3 songs and provide the song title and artist name in the format:
|
| 95 |
1. Song Title - Artist Name
|
| 96 |
2. Song Title - Artist Name
|
| 97 |
3. Song Title - Artist Name
|
| 98 |
"""
|
| 99 |
-
|
| 100 |
-
# User prompt
|
| 101 |
-
user_prompt = f"Suggest 3 popular Indian {mood} songs."
|
| 102 |
-
|
| 103 |
-
# Combine system and user prompts
|
| 104 |
-
response = model.generate_content([system_prompt, user_prompt])
|
| 105 |
return response.text
|
| 106 |
except Exception as e:
|
| 107 |
st.error(f"Error using Gemini API: {e}")
|
| 108 |
return None
|
| 109 |
|
|
|
|
| 110 |
def search_youtube(query):
|
| 111 |
search = Search(query)
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
# Sidebar for user input
|
| 118 |
-
st.sidebar.header("How are you feeling today?")
|
| 119 |
-
mood_options = ["happy", "sad", "energetic", "romantic", "calm"]
|
| 120 |
-
|
| 121 |
-
# Input for Gemini API key
|
| 122 |
-
gemini_api_key = st.sidebar.text_input("Enter your Gemini API Key:", type="password")
|
| 123 |
-
|
| 124 |
-
# Add option to choose between text and speech input
|
| 125 |
-
input_method = st.sidebar.radio("Choose input method:", ["Text", "Speech"])
|
| 126 |
-
|
| 127 |
-
# Initialize user_mood as None
|
| 128 |
-
user_mood = None
|
| 129 |
-
user_text = None
|
| 130 |
-
|
| 131 |
-
if input_method == "Text":
|
| 132 |
-
# Text input
|
| 133 |
-
user_text = st.sidebar.text_input("Enter text describing your mood:")
|
| 134 |
-
if st.sidebar.button("Submit"):
|
| 135 |
-
if user_text:
|
| 136 |
-
user_mood = detect_mood(user_text)
|
| 137 |
-
st.write(f"Based on your text, I detect your mood as: **{user_mood}**")
|
| 138 |
-
else:
|
| 139 |
-
st.sidebar.warning("Please enter your mood description.")
|
| 140 |
-
user_mood = None
|
| 141 |
-
else:
|
| 142 |
-
# Speech input
|
| 143 |
-
st.write("📢 Click the button below and tell me about your day...")
|
| 144 |
-
user_text = speech_to_text()
|
| 145 |
-
|
| 146 |
-
if user_text:
|
| 147 |
-
st.info(f"You said: '{user_text}'")
|
| 148 |
-
user_mood = detect_mood(user_text)
|
| 149 |
-
st.write(f"Based on what you said, I detect your mood as: **{user_mood}**")
|
| 150 |
-
|
| 151 |
-
# Playlist
|
| 152 |
-
if 'playlist' not in st.session_state:
|
| 153 |
-
st.session_state.playlist = []
|
| 154 |
|
| 155 |
# Main content
|
| 156 |
-
if
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
if
|
| 173 |
-
st.
|
| 174 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
else:
|
| 176 |
-
st.
|
|
|
|
|
|
|
| 177 |
elif not gemini_api_key:
|
| 178 |
-
st.warning("Please enter your Gemini API key to get started.")
|
| 179 |
-
|
| 180 |
-
# Display the playlist
|
| 181 |
-
st.sidebar.header("Your Playlist")
|
| 182 |
-
for idx, (song, url) in enumerate(st.session_state.playlist):
|
| 183 |
-
st.sidebar.write(f"{idx + 1}. {song}")
|
| 184 |
-
if st.sidebar.button(f"Play {song}", key=f"play_{idx}"):
|
| 185 |
-
st.video(url)
|
|
|
|
| 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 |
+
input_method = "Text" # Only text input is available
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Input for Gemini API key
|
| 13 |
+
gemini_api_key = st.sidebar.text_input("Enter your Gemini API Key:", type="password")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Text input for user's mood or context
|
| 16 |
+
user_input = st.text_input("Enter your mood or context:")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# Function to get song recommendations using Gemini API
|
| 19 |
+
def get_song_recommendations(user_input, api_key):
|
| 20 |
try:
|
| 21 |
genai.configure(api_key=api_key)
|
| 22 |
model = genai.GenerativeModel('gemini-pro')
|
| 23 |
+
# Prompt to guide the AI
|
| 24 |
+
prompt = f"""
|
| 25 |
+
Based on the input: '{user_input}', determine the mood or context and suggest 3 popular Indian songs that match this mood or context.
|
| 26 |
+
Output the mood followed by the song recommendations in the following format:
|
| 27 |
+
Mood: [mood]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
1. Song Title - Artist Name
|
| 29 |
2. Song Title - Artist Name
|
| 30 |
3. Song Title - Artist Name
|
| 31 |
"""
|
| 32 |
+
response = model.generate_content(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
return response.text
|
| 34 |
except Exception as e:
|
| 35 |
st.error(f"Error using Gemini API: {e}")
|
| 36 |
return None
|
| 37 |
|
| 38 |
+
# Function to search YouTube for a song
|
| 39 |
def search_youtube(query):
|
| 40 |
search = Search(query)
|
| 41 |
+
if search.results:
|
| 42 |
+
return search.results[0].watch_url
|
| 43 |
+
else:
|
| 44 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# Main content
|
| 47 |
+
if user_input and gemini_api_key:
|
| 48 |
+
# Get combined mood detection and song recommendations from Gemini API
|
| 49 |
+
response = get_song_recommendations(user_input, gemini_api_key)
|
| 50 |
+
if response:
|
| 51 |
+
# Parse mood and song recommendations
|
| 52 |
+
lines = response.split('\n')
|
| 53 |
+
if len(lines) > 1 and lines[0].startswith("Mood:"):
|
| 54 |
+
mood = lines[0].replace("Mood: ", "")
|
| 55 |
+
st.write(f"Detected Mood/Context: {mood}")
|
| 56 |
+
songs = [line.strip() for line in lines[1:4] if line.strip()]
|
| 57 |
+
st.write("🎵 Recommended Songs:")
|
| 58 |
+
for song in songs:
|
| 59 |
+
st.write(song)
|
| 60 |
+
# Search YouTube and provide playback options
|
| 61 |
+
query = f"{song} song"
|
| 62 |
+
video_url = search_youtube(query)
|
| 63 |
+
if video_url:
|
| 64 |
+
st.video(video_url)
|
| 65 |
+
if st.button(f"Add '{song}' to Playlist"):
|
| 66 |
+
# Add to playlist logic here
|
| 67 |
+
st.success(f"Added '{song}' to your playlist!")
|
| 68 |
+
else:
|
| 69 |
+
st.warning(f"Could not find '{song}' on YouTube.")
|
| 70 |
+
else:
|
| 71 |
+
st.error("Gemini API response format is incorrect.")
|
| 72 |
else:
|
| 73 |
+
st.warning("Please provide input to get song recommendations.")
|
| 74 |
+
elif not user_input:
|
| 75 |
+
st.warning("Please enter your mood or context.")
|
| 76 |
elif not gemini_api_key:
|
| 77 |
+
st.warning("Please enter your Gemini API key to get started.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|