Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -29,18 +29,26 @@ def analyze_text_emotion(text):
|
|
29 |
except Exception as e:
|
30 |
return f"Error: {str(e)}"
|
31 |
|
|
|
32 |
def analyze_voice_emotion(audio):
|
33 |
try:
|
34 |
if audio is None:
|
35 |
return "Please upload an audio file"
|
36 |
-
|
37 |
-
|
38 |
sr = audio[0]
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
pitch = float(librosa.feature.spectral_centroid(y=y, sr=sr).mean())
|
41 |
intensity = float(librosa.feature.rms(y=y).mean())
|
42 |
tempo, _ = librosa.beat.beat_track(y=y, sr=sr)
|
43 |
|
|
|
44 |
if pitch < 150 and intensity < 0.02:
|
45 |
emotion = "sadness"
|
46 |
elif pitch > 200 and intensity > 0.05:
|
@@ -54,6 +62,7 @@ def analyze_voice_emotion(audio):
|
|
54 |
except Exception as e:
|
55 |
return f"Error analyzing audio: {str(e)}"
|
56 |
|
|
|
57 |
def chat_and_tts(message):
|
58 |
try:
|
59 |
if not OPENAI_API_KEY or not ELEVEN_LABS_API_KEY:
|
|
|
29 |
except Exception as e:
|
30 |
return f"Error: {str(e)}"
|
31 |
|
32 |
+
|
33 |
def analyze_voice_emotion(audio):
|
34 |
try:
|
35 |
if audio is None:
|
36 |
return "Please upload an audio file"
|
37 |
+
|
38 |
+
# Ensure audio is loaded with correct format
|
39 |
sr = audio[0]
|
40 |
+
y = audio[1]
|
41 |
+
|
42 |
+
# Check if the audio data is already in float format; if not, convert it
|
43 |
+
if y.dtype != 'float32':
|
44 |
+
y = y.astype('float32')
|
45 |
+
|
46 |
+
# Calculate features
|
47 |
pitch = float(librosa.feature.spectral_centroid(y=y, sr=sr).mean())
|
48 |
intensity = float(librosa.feature.rms(y=y).mean())
|
49 |
tempo, _ = librosa.beat.beat_track(y=y, sr=sr)
|
50 |
|
51 |
+
# Determine emotion based on features
|
52 |
if pitch < 150 and intensity < 0.02:
|
53 |
emotion = "sadness"
|
54 |
elif pitch > 200 and intensity > 0.05:
|
|
|
62 |
except Exception as e:
|
63 |
return f"Error analyzing audio: {str(e)}"
|
64 |
|
65 |
+
|
66 |
def chat_and_tts(message):
|
67 |
try:
|
68 |
if not OPENAI_API_KEY or not ELEVEN_LABS_API_KEY:
|