Update app.py
Browse files
app.py
CHANGED
|
@@ -4,9 +4,8 @@ import google.generativeai as genai
|
|
| 4 |
from pytube import Search
|
| 5 |
import speech_recognition as sr
|
| 6 |
import tempfile
|
| 7 |
-
from
|
| 8 |
import numpy as np
|
| 9 |
-
from streamlit_webrtc import webrtc_streamer, WebRtcMode, AudioProcessorBase
|
| 10 |
|
| 11 |
# Load sentiment analysis model using PyTorch backend
|
| 12 |
mood_classifier = pipeline("sentiment-analysis", framework="pt")
|
|
@@ -21,32 +20,41 @@ def detect_mood(text):
|
|
| 21 |
else:
|
| 22 |
return "neutral"
|
| 23 |
|
| 24 |
-
def speech_to_text(
|
| 25 |
# Initialize recognizer
|
| 26 |
r = sr.Recognizer()
|
| 27 |
|
| 28 |
# Create a temporary file to store the recorded audio
|
| 29 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as fp:
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
audio = r.record(source)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
def get_song_recommendations(mood, api_key):
|
| 52 |
try:
|
|
@@ -93,49 +101,34 @@ gemini_api_key = st.sidebar.text_input("Enter your Gemini API Key:", type="passw
|
|
| 93 |
# Add option to choose between text and speech input
|
| 94 |
input_method = st.sidebar.radio("Choose input method:", ["Text", "Speech"])
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
if input_method == "Text":
|
| 97 |
# Text input
|
| 98 |
user_mood = st.sidebar.selectbox("Select your mood:", mood_options)
|
|
|
|
| 99 |
else:
|
| 100 |
# Speech input
|
| 101 |
st.write("📢 Tell me about your day...")
|
|
|
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
mode=WebRtcMode.SENDONLY,
|
| 107 |
-
audio_receiver_size=1024,
|
| 108 |
-
media_stream_constraints={"audio": True, "video": False},
|
| 109 |
-
)
|
| 110 |
-
|
| 111 |
-
if webrtc_ctx.audio_receiver:
|
| 112 |
-
audio_frames = webrtc_ctx.audio_receiver.get_frames(timeout=5)
|
| 113 |
-
if audio_frames:
|
| 114 |
-
audio_bytes = b"".join([frame.to_ndarray().tobytes() for frame in audio_frames])
|
| 115 |
-
spoken_text = speech_to_text(audio_bytes)
|
| 116 |
-
|
| 117 |
-
if spoken_text:
|
| 118 |
-
st.write(f"You said: {spoken_text}")
|
| 119 |
-
user_mood = detect_mood(spoken_text)
|
| 120 |
-
else:
|
| 121 |
-
user_mood = None
|
| 122 |
-
else:
|
| 123 |
-
st.warning("No audio frames received. Please try again.")
|
| 124 |
-
user_mood = None
|
| 125 |
-
else:
|
| 126 |
-
user_mood = None
|
| 127 |
|
| 128 |
# Playlist
|
| 129 |
if 'playlist' not in st.session_state:
|
| 130 |
st.session_state.playlist = []
|
| 131 |
|
| 132 |
# Main content
|
| 133 |
-
if
|
| 134 |
-
|
| 135 |
-
st.write(f"🎭 Detected Mood: **{
|
| 136 |
|
| 137 |
st.write("🎵 Recommended Songs:")
|
| 138 |
-
recommendations = get_song_recommendations(
|
| 139 |
if recommendations:
|
| 140 |
st.write(recommendations)
|
| 141 |
|
|
|
|
| 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 |
|
| 10 |
# Load sentiment analysis model using PyTorch backend
|
| 11 |
mood_classifier = pipeline("sentiment-analysis", framework="pt")
|
|
|
|
| 20 |
else:
|
| 21 |
return "neutral"
|
| 22 |
|
| 23 |
+
def speech_to_text():
|
| 24 |
# Initialize recognizer
|
| 25 |
r = sr.Recognizer()
|
| 26 |
|
| 27 |
# Create a temporary file to store the recorded audio
|
| 28 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as fp:
|
| 29 |
+
# Add audio recording widget
|
| 30 |
+
audio_bytes = audio_recorder(
|
| 31 |
+
text="Click to record",
|
| 32 |
+
recording_color="#e8b62c",
|
| 33 |
+
neutral_color="#6aa36f"
|
| 34 |
+
)
|
| 35 |
|
| 36 |
+
if audio_bytes:
|
| 37 |
+
# Save audio bytes to temporary file
|
| 38 |
+
fp.write(audio_bytes)
|
| 39 |
+
temp_filename = fp.name
|
|
|
|
| 40 |
|
| 41 |
+
# Read the audio file
|
| 42 |
+
with sr.AudioFile(temp_filename) as source:
|
| 43 |
+
# Adjust for ambient noise and record
|
| 44 |
+
r.adjust_for_ambient_noise(source)
|
| 45 |
+
audio = r.record(source)
|
| 46 |
+
|
| 47 |
+
try:
|
| 48 |
+
# Use Google Speech Recognition
|
| 49 |
+
text = r.recognize_google(audio)
|
| 50 |
+
return text
|
| 51 |
+
except sr.UnknownValueError:
|
| 52 |
+
st.error("Could not understand the audio")
|
| 53 |
+
return None
|
| 54 |
+
except sr.RequestError:
|
| 55 |
+
st.error("Could not request results from speech recognition service")
|
| 56 |
+
return None
|
| 57 |
+
return None
|
| 58 |
|
| 59 |
def get_song_recommendations(mood, api_key):
|
| 60 |
try:
|
|
|
|
| 101 |
# Add option to choose between text and speech input
|
| 102 |
input_method = st.sidebar.radio("Choose input method:", ["Text", "Speech"])
|
| 103 |
|
| 104 |
+
# Initialize user_mood as None
|
| 105 |
+
user_mood = None
|
| 106 |
+
user_text = None
|
| 107 |
+
|
| 108 |
if input_method == "Text":
|
| 109 |
# Text input
|
| 110 |
user_mood = st.sidebar.selectbox("Select your mood:", mood_options)
|
| 111 |
+
user_text = user_mood
|
| 112 |
else:
|
| 113 |
# Speech input
|
| 114 |
st.write("📢 Tell me about your day...")
|
| 115 |
+
user_text = speech_to_text()
|
| 116 |
|
| 117 |
+
if user_text:
|
| 118 |
+
st.write(f"You said: {user_text}")
|
| 119 |
+
user_mood = detect_mood(user_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
# Playlist
|
| 122 |
if 'playlist' not in st.session_state:
|
| 123 |
st.session_state.playlist = []
|
| 124 |
|
| 125 |
# Main content
|
| 126 |
+
if user_text and gemini_api_key:
|
| 127 |
+
detected_mood = detect_mood(user_text)
|
| 128 |
+
st.write(f"🎭 Detected Mood: **{detected_mood}**")
|
| 129 |
|
| 130 |
st.write("🎵 Recommended Songs:")
|
| 131 |
+
recommendations = get_song_recommendations(detected_mood, gemini_api_key)
|
| 132 |
if recommendations:
|
| 133 |
st.write(recommendations)
|
| 134 |
|