File size: 6,559 Bytes
f7c8a78 b322b24 d897b8a b322b24 d89bac8 f4d0320 f7c8a78 0f1be6d b322b24 f7c8a78 d89bac8 f7c8a78 d897b8a b322b24 d89bac8 d897b8a d89bac8 d897b8a b322b24 f7c8a78 d1eb12b f7c8a78 b322b24 d897b8a b322b24 d897b8a b322b24 d89bac8 d897b8a b322b24 d897b8a d89bac8 d897b8a d89bac8 f7c8a78 d897b8a f7c8a78 d897b8a f7c8a78 |
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
import streamlit as st
from transformers import pipeline
import google.generativeai as genai
from pytube import Search
import speech_recognition as sr
import tempfile
from audio_recorder_streamlit import audio_recorder
import numpy as np
import wave
import io
import logging
# Set up logging
logging.basicConfig(level=logging.DEBUG)
# Load sentiment analysis model using PyTorch backend
mood_classifier = pipeline("sentiment-analysis", framework="pt")
def convert_audio_to_wav(audio_bytes):
# Create a wave file in memory
wav_buffer = io.BytesIO()
with wave.open(wav_buffer, 'wb') as wav_file:
wav_file.setnchannels(1) # Mono
wav_file.setsampwidth(2) # 2 bytes per sample
wav_file.setframerate(44100) # Sample rate
wav_file.writeframes(audio_bytes)
return wav_buffer.getvalue()
def detect_mood(text):
result = mood_classifier(text)[0]
if result['label'] == 'POSITIVE':
return "joyful"
elif result['label'] == 'NEGATIVE':
return "sad"
else:
return "neutral"
def speech_to_text():
# Initialize recognizer
r = sr.Recognizer()
# Add audio recording widget
audio_bytes = audio_recorder(
text="Click to record your mood",
recording_color="#e8b62c",
neutral_color="#6aa36f",
pause_threshold=2.0 # Automatically stop after 2 seconds of silence
)
if audio_bytes:
try:
# Create a temporary WAV file
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_audio:
# Convert and write audio bytes to WAV format
wav_bytes = convert_audio_to_wav(audio_bytes)
temp_audio.write(wav_bytes)
temp_audio.flush()
# Use the temporary file for speech recognition
with sr.AudioFile(temp_audio.name) as source:
# Record the audio file
audio = r.record(source)
try:
# Attempt speech recognition
text = r.recognize_google(audio)
st.success("Speech recognized successfully!")
return text
except sr.UnknownValueError:
st.error("Could not understand the audio. Please try speaking clearly and try again.")
return None
except sr.RequestError as e:
st.error(f"Could not request results from speech recognition service; {e}")
return None
except Exception as e:
st.error(f"Error processing audio: {e}")
return None
return None
def get_song_recommendations(mood, api_key):
try:
genai.configure(api_key=api_key)
model = genai.GenerativeModel('gemini-pro')
# System prompt to guide the AI
system_prompt = """
You are a music recommendation assistant specialized in Indian songs. Your task is to suggest popular Indian songs based on the user's mood.
- If the mood is "happy," suggest upbeat and joyful Bollywood or Indian pop songs.
- If the mood is "sad," suggest emotional and soulful Indian songs.
- If the mood is "energetic," suggest high-energy dance or workout songs.
- If the mood is "romantic," suggest romantic Bollywood or Indian love songs.
- If the mood is "calm," suggest soothing Indian classical or instrumental music.
Always suggest 3 songs and provide the song title and artist name in the format:
1. Song Title - Artist Name
2. Song Title - Artist Name
3. Song Title - Artist Name
"""
# User prompt
user_prompt = f"Suggest 3 popular Indian {mood} songs."
# Combine system and user prompts
response = model.generate_content([system_prompt, user_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)
return search.results[0].watch_url
# Streamlit App
st.title("๐ต Mood-Based Indian Song Player")
# Sidebar for user input
st.sidebar.header("How are you feeling today?")
mood_options = ["happy", "sad", "energetic", "romantic", "calm"]
# Input for Gemini API key
gemini_api_key = st.sidebar.text_input("Enter your Gemini API Key:", type="password")
# Add option to choose between text and speech input
input_method = st.sidebar.radio("Choose input method:", ["Text", "Speech"])
# Initialize user_mood as None
user_mood = None
user_text = None
if input_method == "Text":
# Text input
user_mood = st.sidebar.selectbox("Select your mood:", mood_options)
user_text = user_mood
else:
# Speech input
st.write("๐ข Click the button below and tell me about your day...")
user_text = speech_to_text()
if user_text:
st.info(f"You said: '{user_text}'")
user_mood = detect_mood(user_text)
st.write(f"Based on what you said, I detect your mood as: **{user_mood}**")
# Playlist
if 'playlist' not in st.session_state:
st.session_state.playlist = []
# Main content
if user_text and gemini_api_key:
detected_mood = detect_mood(user_text)
st.write(f"๐ญ Detected Mood: **{detected_mood}**")
st.write("๐ต Recommended Songs:")
recommendations = get_song_recommendations(detected_mood, gemini_api_key)
if recommendations:
st.write(recommendations)
song_names = recommendations.split("\n")
for song in song_names:
if song.strip():
st.write(f"๐ Searching for: **{song}**")
video_url = search_youtube(song)
st.video(video_url)
# Add to playlist button
if st.button(f"Add '{song}' to Playlist"):
st.session_state.playlist.append((song, video_url))
st.success(f"Added '{song}' to your playlist!")
else:
st.error("Failed to get song recommendations. Please check your API key.")
elif not gemini_api_key:
st.warning("Please enter your Gemini API key to get started.")
# Display the playlist
st.sidebar.header("Your Playlist")
for idx, (song, url) in enumerate(st.session_state.playlist):
st.sidebar.write(f"{idx + 1}. {song}")
if st.sidebar.button(f"Play {song}", key=f"play_{idx}"):
st.video(url) |