Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,65 +1,86 @@
|
|
1 |
-
|
2 |
import requests
|
3 |
|
4 |
-
|
5 |
-
|
6 |
API_URL = "https://api-inference.huggingface.co/models/ehcalabres/wav2vec2-lg-xlsr-en-speech-emotion-recognition"
|
7 |
headers = {"Authorization": "Bearer hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}
|
8 |
|
|
|
9 |
def query_api(audio_file):
|
10 |
with open(audio_file, "rb") as f:
|
11 |
response = requests.post(API_URL, headers=headers, data=f)
|
12 |
return response.json()
|
13 |
|
|
|
14 |
def compute_neg_rate(emotion_data):
|
15 |
-
neg_emotions = ["anger", "disgust", "fear", "sadness"]
|
16 |
neg_rate = sum([emotion['score'] for emotion in emotion_data if emotion['label'].lower() in neg_emotions])
|
17 |
-
return neg_rate * 100
|
18 |
|
|
|
19 |
def analyze_depression(neg_rate):
|
20 |
-
|
21 |
if neg_rate <= 30.0:
|
22 |
if neg_rate <= 5.0:
|
23 |
-
|
24 |
else:
|
25 |
-
|
26 |
if neg_rate <= 10.0:
|
27 |
-
|
|
|
28 |
elif neg_rate > 10.0 and neg_rate <= 20.0:
|
29 |
-
|
|
|
30 |
elif neg_rate > 20.0 and neg_rate <= 30.0:
|
31 |
-
|
|
|
32 |
elif neg_rate > 30.0 and neg_rate <= 60.0:
|
33 |
-
|
34 |
if neg_rate > 30.0 and neg_rate <= 40.0:
|
35 |
-
|
|
|
36 |
elif neg_rate > 40.0 and neg_rate <= 50.0:
|
37 |
-
|
|
|
38 |
elif neg_rate > 50.0 and neg_rate <= 60.0:
|
39 |
-
|
|
|
40 |
elif neg_rate > 60.0 and neg_rate <= 100.0:
|
41 |
-
|
42 |
if neg_rate > 60.0 and neg_rate <= 70.0:
|
43 |
-
|
|
|
44 |
elif neg_rate > 70.0 and neg_rate <= 80.0:
|
45 |
-
|
|
|
46 |
elif neg_rate > 80.0 and neg_rate <= 90.0:
|
47 |
-
|
|
|
48 |
elif neg_rate > 90.0 and neg_rate <= 100.0:
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
emotion_data = query_api(audio_file)
|
56 |
-
neg_rate = compute_neg_rate(emotion_data)
|
57 |
-
response = analyze_depression(neg_rate)
|
58 |
-
return jsonify({"response": response})
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
|
64 |
if __name__ == "__main__":
|
65 |
-
|
|
|
1 |
+
import streamlit as st
|
2 |
import requests
|
3 |
|
4 |
+
# Define API details
|
|
|
5 |
API_URL = "https://api-inference.huggingface.co/models/ehcalabres/wav2vec2-lg-xlsr-en-speech-emotion-recognition"
|
6 |
headers = {"Authorization": "Bearer hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}
|
7 |
|
8 |
+
# Function to query the Hugging Face API
|
9 |
def query_api(audio_file):
|
10 |
with open(audio_file, "rb") as f:
|
11 |
response = requests.post(API_URL, headers=headers, data=f)
|
12 |
return response.json()
|
13 |
|
14 |
+
# Function to compute the neg_rate from API response
|
15 |
def compute_neg_rate(emotion_data):
|
16 |
+
neg_emotions = ["anger", "disgust", "fear", "sadness"] # Example negative emotions
|
17 |
neg_rate = sum([emotion['score'] for emotion in emotion_data if emotion['label'].lower() in neg_emotions])
|
18 |
+
return neg_rate * 100 # Scale to a percentage
|
19 |
|
20 |
+
# Depression scale logic
|
21 |
def analyze_depression(neg_rate):
|
22 |
+
responses = []
|
23 |
if neg_rate <= 30.0:
|
24 |
if neg_rate <= 5.0:
|
25 |
+
responses.append("just chill and relax")
|
26 |
else:
|
27 |
+
responses.append("*** MILD DEPRESSION ***")
|
28 |
if neg_rate <= 10.0:
|
29 |
+
responses.append(f"\nDepression Scale Detected: {round(neg_rate) / 10}\n")
|
30 |
+
responses.append("My Response: Everything is a-okay! There's absolutely nothing wrong 馃榿馃榿\n\t\tYou're probably cuddling a fluffy kitten right now")
|
31 |
elif neg_rate > 10.0 and neg_rate <= 20.0:
|
32 |
+
responses.append(f"\nDepression Scale Detected: {round(neg_rate) / 10}\n")
|
33 |
+
responses.append("My Response: You are a bit frustrated and disappointed\n\t\tBut you're easily distracted and cheered with little effort 馃榿馃榿")
|
34 |
elif neg_rate > 20.0 and neg_rate <= 30.0:
|
35 |
+
responses.append(f"\nDepression Scale Detected: {round(neg_rate) / 10}\n")
|
36 |
+
responses.append("My Response: Things are bothering you but you're coping up\n\t\tYou might be overtired and hungry\n\t\tThe emotional equivalent of a headache 馃榿馃榿")
|
37 |
elif neg_rate > 30.0 and neg_rate <= 60.0:
|
38 |
+
responses.append("*** MODERATE DEPRESSION ***")
|
39 |
if neg_rate > 30.0 and neg_rate <= 40.0:
|
40 |
+
responses.append(f"\nDepression Scale Detected: {round(neg_rate) / 10}\n")
|
41 |
+
responses.append("My Response: Today is slightly a bad day for you.\n\t\tYou still have the skills to get through it, but be gentle with yourself\n\t\tUse self-care strategies 馃榿馃榿")
|
42 |
elif neg_rate > 40.0 and neg_rate <= 50.0:
|
43 |
+
responses.append(f"\nDepression Scale Detected: {round(neg_rate) / 10}\n")
|
44 |
+
responses.append("My Response: Your mental health is starting to impact your everyday life.\n\t\tEasy things are becoming difficult")
|
45 |
elif neg_rate > 50.0 and neg_rate <= 60.0:
|
46 |
+
responses.append(f"\nDepression Scale Detected: {round(neg_rate) / 10}\n")
|
47 |
+
responses.append("My Response: You are not able to do things the way you usually do them due to your mental health.\n\t\tImpulsive and compulsive thoughts might be difficult to cope with")
|
48 |
elif neg_rate > 60.0 and neg_rate <= 100.0:
|
49 |
+
responses.append("*** SEVERE DEPRESSION ***")
|
50 |
if neg_rate > 60.0 and neg_rate <= 70.0:
|
51 |
+
responses.append(f"\nDepression Scale Detected: {round(neg_rate) / 10}\n")
|
52 |
+
responses.append("My Response: You are losing interest in activities that used to be enjoyable.\n\t\tYou should definitely seek help\n\t\tThis is becoming serious 鈽光樄")
|
53 |
elif neg_rate > 70.0 and neg_rate <= 80.0:
|
54 |
+
responses.append(f"\nDepression Scale Detected: {round(neg_rate) / 10}\n")
|
55 |
+
responses.append("My Response: You can't ignore your struggles now, It's HIGH time!.\n\t\tYou may have issues sleeping, eating, having fun, socializing, and work/study\n\t\tYour mental health is affecting almost all parts of your life 鈽光樄")
|
56 |
elif neg_rate > 80.0 and neg_rate <= 90.0:
|
57 |
+
responses.append(f"\nDepression Scale Detected: {round(neg_rate) / 10}\n")
|
58 |
+
responses.append("My Response: You are at a critical point!!!.\n\t\tYou aren't functioning anymore and need urgent help.\n\t\tYou may be a risk to yourself or others if left untreated 鈽光樄")
|
59 |
elif neg_rate > 90.0 and neg_rate <= 100.0:
|
60 |
+
responses.append(f"\nDepression Scale Detected: {round(neg_rate) / 10}\n")
|
61 |
+
responses.append("My Response: The worst mental and emotional distress possible.\n\t\tYou can't imagine things getting any better now and you might think it's all over for you (SUICIDE).\n\t\tContact a crisis line or get started with treatment immediately. 鈽光樄")
|
62 |
+
return "\n".join(responses)
|
63 |
+
|
64 |
+
# Streamlit app
|
65 |
+
def main():
|
66 |
+
st.title("Emotion Detection and Depression Scale Analysis")
|
67 |
+
|
68 |
+
audio_file = st.file_uploader("Upload an audio file", type=["wav"])
|
69 |
+
if audio_file:
|
70 |
+
st.audio(audio_file, format="audio/wav")
|
71 |
+
|
72 |
+
if st.button("Analyze"):
|
73 |
+
# Save uploaded file to a temporary file
|
74 |
+
with open("temp_audio.wav", "wb") as f:
|
75 |
+
f.write(audio_file.read())
|
76 |
|
77 |
+
# Query API and compute neg_rate
|
78 |
+
emotion_data = query_api("temp_audio.wav")
|
79 |
+
neg_rate = compute_neg_rate(emotion_data)
|
|
|
|
|
|
|
|
|
80 |
|
81 |
+
# Analyze and display results
|
82 |
+
result = analyze_depression(neg_rate)
|
83 |
+
st.text(result)
|
84 |
|
85 |
if __name__ == "__main__":
|
86 |
+
main()
|