Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,81 +1,87 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
3 |
-
import torch
|
4 |
-
import numpy as np
|
5 |
-
import io
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
model = Wav2Vec2ForSequenceClassification.from_pretrained(model_name)
|
10 |
-
tokenizer = Wav2Vec2Tokenizer.from_pretrained(model_name)
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
if neg_rate <= 30.0:
|
20 |
if neg_rate <= 5.0:
|
21 |
return "just chill and relax"
|
22 |
-
|
23 |
-
return
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
elif neg_rate <= 20.0:
|
28 |
-
return (
|
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 |
-
elif neg_rate <=
|
59 |
-
return (
|
60 |
-
|
61 |
-
|
62 |
-
f"\t\tYour mental health is affecting almost all parts of your life 鈽光樄")
|
63 |
-
elif neg_rate <= 90.0:
|
64 |
-
return (f"\nDepression Scale Detected : {round(neg_rate) / 10}\n"
|
65 |
-
f"My Response :\tYou are at a critical point !!!\n"
|
66 |
-
f"\t\tYou aren't functioning anymore and need urgent help.\n"
|
67 |
-
f"\t\tYou may be a risk to yourself or others if left untreated 鈽光樄")
|
68 |
-
elif neg_rate <= 100.0:
|
69 |
-
return (f"\nDepression Scale Detected : {round(neg_rate) / 10}\n"
|
70 |
-
f"My Response :\tThe worst mental and emotional distress possible.\n"
|
71 |
-
f"\t\tYou can't imagine things getting any better now and you might think it's all over for you (SUICIDE).\n"
|
72 |
-
f"\t\tContact crisis line or get started for the treatment immediately. 鈽光樄")
|
73 |
|
74 |
-
st.title("Emotion Detection
|
75 |
|
76 |
-
|
77 |
|
78 |
-
if
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import requests
|
|
|
|
|
|
|
3 |
|
4 |
+
API_URL = "https://api-inference.huggingface.co/models/ehcalabres/wav2vec2-lg-xlsr-en-speech-emotion-recognition"
|
5 |
+
headers = {"Authorization": "Bearer hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}
|
|
|
|
|
6 |
|
7 |
+
emotion_to_neg_rate = {
|
8 |
+
'angry': 70.0,
|
9 |
+
'calm': 5.0,
|
10 |
+
'disgust': 60.0,
|
11 |
+
'fearful': 50.0,
|
12 |
+
'happy': 5.0,
|
13 |
+
'neutral': 20.0,
|
14 |
+
'sad': 80.0,
|
15 |
+
'surprised': 10.0
|
16 |
+
}
|
17 |
+
|
18 |
+
def get_emotion(audio_file):
|
19 |
+
response = requests.post(API_URL, headers=headers, files={"file": audio_file})
|
20 |
+
if response.status_code == 200:
|
21 |
+
return response.json()
|
22 |
+
else:
|
23 |
+
return {"error": response.text}
|
24 |
+
|
25 |
+
def get_neg_rate(emotion):
|
26 |
+
return emotion_to_neg_rate.get(emotion, 30.0)
|
27 |
+
|
28 |
+
def evaluate_depression(neg_rate):
|
29 |
if neg_rate <= 30.0:
|
30 |
if neg_rate <= 5.0:
|
31 |
return "just chill and relax"
|
32 |
+
else:
|
33 |
+
return "*** MILD DEPRESSION ***"
|
34 |
+
if neg_rate <= 10.0:
|
35 |
+
return ("Everything is a-okay! There's absolutely nothing wrong 馃榿馃榿\n"
|
36 |
+
"You're probably cuddling a fluffy kitten right now")
|
37 |
+
elif neg_rate > 10.0 and neg_rate <= 20.0:
|
38 |
+
return ("You are a bit frustrated and disappointed\n"
|
39 |
+
"But you're easily distracted and cheered with little effort 馃榿馃榿")
|
40 |
+
elif neg_rate > 20.0 and neg_rate <= 30.0:
|
41 |
+
return ("Things are bothering you but you're coping up\n"
|
42 |
+
"You might be over tired and hungry\n"
|
43 |
+
"The emotional equivalent of a headache 馃榿馃榿")
|
44 |
+
elif neg_rate > 30.0 and neg_rate <= 60.0:
|
45 |
+
if neg_rate > 30.0 and neg_rate <= 40.0:
|
46 |
+
return ("Today is slightly a bad day for you.\n"
|
47 |
+
"You still have the skills to get through it, but be gentle with yourself\n"
|
48 |
+
"Use self-care strategies 馃榿馃榿")
|
49 |
+
elif neg_rate > 40.0 and neg_rate <= 50.0:
|
50 |
+
return ("Your mental health is starting to impact your everyday life.\n"
|
51 |
+
"Easy things are becoming difficult")
|
52 |
+
elif neg_rate > 50.0 and neg_rate <= 60.0:
|
53 |
+
return ("You are not able to do things the way you usually do them due to your mental health.\n"
|
54 |
+
"Impulsive and compulsive thoughts might be difficult to cope with")
|
55 |
+
elif neg_rate > 60.0 and neg_rate <= 100.0:
|
56 |
+
if neg_rate > 60.0 and neg_rate <= 70.0:
|
57 |
+
return ("You are losing interest in the activities that used to be enjoyable.\n"
|
58 |
+
"You should definitely seek help\n"
|
59 |
+
"This is becoming serious 鈽光樄")
|
60 |
+
elif neg_rate > 70.0 and neg_rate <= 80.0:
|
61 |
+
return ("You can't ignore your struggles now; it's HIGH time!\n"
|
62 |
+
"You may have issues sleeping, eating, having fun, socializing, and work/study\n"
|
63 |
+
"Your mental health is affecting almost all parts of your life 鈽光樄")
|
64 |
+
elif neg_rate > 80.0 and neg_rate <= 90.0:
|
65 |
+
return ("You are at a critical point!!!\n"
|
66 |
+
"You aren't functioning anymore and need urgent help.\n"
|
67 |
+
"You may be a risk to yourself or others if left untreated 鈽光樄")
|
68 |
+
elif neg_rate > 90.0 and neg_rate <= 100.0:
|
69 |
+
return ("The worst mental and emotional distress possible.\n"
|
70 |
+
"You can't imagine things getting any better now and you might think it's all over for you (SUICIDE).\n"
|
71 |
+
"Contact a crisis line or get started with treatment immediately. 鈽光樄")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
+
st.title("Emotion Detection and Depression Analysis")
|
74 |
|
75 |
+
audio_file = st.file_uploader("Upload an audio file", type=["wav", "mp3"])
|
76 |
|
77 |
+
if audio_file:
|
78 |
+
st.audio(audio_file, format='audio/wav')
|
79 |
+
emotion_response = get_emotion(audio_file)
|
80 |
+
|
81 |
+
if "error" not in emotion_response:
|
82 |
+
emotion = emotion_response.get("emotion", "neutral")
|
83 |
+
neg_rate = get_neg_rate(emotion)
|
84 |
+
result = evaluate_depression(neg_rate)
|
85 |
+
st.write(result)
|
86 |
+
else:
|
87 |
+
st.error(f"Error fetching emotion from API: {emotion_response.get('error')}")
|