Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ import speech_recognition as sr
|
|
6 |
import tempfile
|
7 |
from pydub import AudioSegment
|
8 |
import numpy as np
|
|
|
9 |
|
10 |
# Load sentiment analysis model using PyTorch backend
|
11 |
mood_classifier = pipeline("sentiment-analysis", framework="pt")
|
@@ -20,41 +21,32 @@ def detect_mood(text):
|
|
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 |
-
#
|
30 |
-
|
31 |
-
|
32 |
-
recording_color="#e8b62c",
|
33 |
-
neutral_color="#6aa36f"
|
34 |
-
)
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
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:
|
@@ -107,11 +99,29 @@ if input_method == "Text":
|
|
107 |
else:
|
108 |
# Speech input
|
109 |
st.write("📢 Tell me about your day...")
|
110 |
-
spoken_text = speech_to_text()
|
111 |
|
112 |
-
|
113 |
-
|
114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
else:
|
116 |
user_mood = None
|
117 |
|
|
|
6 |
import tempfile
|
7 |
from pydub import AudioSegment
|
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 |
else:
|
22 |
return "neutral"
|
23 |
|
24 |
+
def speech_to_text(audio_bytes):
|
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 |
+
# Save audio bytes to temporary file
|
31 |
+
fp.write(audio_bytes)
|
32 |
+
temp_filename = fp.name
|
|
|
|
|
|
|
33 |
|
34 |
+
# Read the audio file
|
35 |
+
with sr.AudioFile(temp_filename) as source:
|
36 |
+
# Adjust for ambient noise and record
|
37 |
+
r.adjust_for_ambient_noise(source)
|
38 |
+
audio = r.record(source)
|
39 |
|
40 |
+
try:
|
41 |
+
# Use Google Speech Recognition
|
42 |
+
text = r.recognize_google(audio)
|
43 |
+
return text
|
44 |
+
except sr.UnknownValueError:
|
45 |
+
st.error("Could not understand the audio")
|
46 |
+
return None
|
47 |
+
except sr.RequestError:
|
48 |
+
st.error("Could not request results from speech recognition service")
|
49 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
def get_song_recommendations(mood, api_key):
|
52 |
try:
|
|
|
99 |
else:
|
100 |
# Speech input
|
101 |
st.write("📢 Tell me about your day...")
|
|
|
102 |
|
103 |
+
# Use streamlit-webrtc for audio recording
|
104 |
+
webrtc_ctx = webrtc_streamer(
|
105 |
+
key="speech-to-text",
|
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 |
|