ghuman7 commited on
Commit
27d66fc
verified
1 Parent(s): 023f6cd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ import requests
3
+
4
+ app = Flask(__name__)
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
+ response = ""
21
+ if neg_rate <= 30.0:
22
+ if neg_rate <= 5.0:
23
+ response = "just chill and relax"
24
+ else:
25
+ response = "*** MILD DEPRESSION ***"
26
+ if neg_rate <= 10.0:
27
+ response += "\nDepression Scale Detected: {:.1f}\nMy Response: Everything is a-okay! There's absolutely nothing wrong 馃榿馃榿".format(round(neg_rate) / 10)
28
+ elif neg_rate > 10.0 and neg_rate <= 20.0:
29
+ response += "\nDepression Scale Detected: {:.1f}\nMy Response: You are a bit frustrated and disappointed\n\tBut you're easily distracted and cheered with little effort 馃榿馃榿".format(round(neg_rate) / 10)
30
+ elif neg_rate > 20.0 and neg_rate <= 30.0:
31
+ response += "\nDepression Scale Detected: {:.1f}\nMy Response: Things are bothering you but you're coping up\n\tYou might be overtired and hungry\n\tThe emotional equivalent of a headache 馃榿馃榿".format(round(neg_rate) / 10)
32
+ elif neg_rate > 30.0 and neg_rate <= 60.0:
33
+ response = "*** MODERATE DEPRESSION ***"
34
+ if neg_rate > 30.0 and neg_rate <= 40.0:
35
+ response += "\nDepression Scale Detected: {:.1f}\nMy Response: Today is slightly a bad day for you.\n\tYou still have the skills to get through it, but be gentle with yourself\n\tUse self-care strategies 馃榿馃榿".format(round(neg_rate) / 10)
36
+ elif neg_rate > 40.0 and neg_rate <= 50.0:
37
+ response += "\nDepression Scale Detected: {:.1f}\nMy Response: Your mental health is starting to impact your everyday life.\n\tEasy things are becoming difficult".format(round(neg_rate) / 10)
38
+ elif neg_rate > 50.0 and neg_rate <= 60.0:
39
+ response += "\nDepression Scale Detected: {:.1f}\nMy Response: You are not able to do things the way you usually do them due to your mental health.\n\tImpulsive and compulsive thoughts might be difficult to cope with".format(round(neg_rate) / 10)
40
+ elif neg_rate > 60.0 and neg_rate <= 100.0:
41
+ response = "*** SEVERE DEPRESSION ***"
42
+ if neg_rate > 60.0 and neg_rate <= 70.0:
43
+ response += "\nDepression Scale Detected: {:.1f}\nMy Response: You are losing interest in activities that used to be enjoyable.\n\tYou should definitely seek help\n\tThis is becoming serious 鈽光樄".format(round(neg_rate) / 10)
44
+ elif neg_rate > 70.0 and neg_rate <= 80.0:
45
+ response += "\nDepression Scale Detected: {:.1f}\nMy Response: You can't ignore your struggles now, It's HIGH time!.\n\tYou may have issues sleeping, eating, having fun, socializing, and work/study\n\tYour mental health is affecting almost all parts of your life 鈽光樄".format(round(neg_rate) / 10)
46
+ elif neg_rate > 80.0 and neg_rate <= 90.0:
47
+ response += "\nDepression Scale Detected: {:.1f}\nMy Response: You are at a critical point!!!.\n\tYou aren't functioning anymore and need urgent help.\n\tYou may be a risk to yourself or others if left untreated 鈽光樄".format(round(neg_rate) / 10)
48
+ elif neg_rate > 90.0 and neg_rate <= 100.0:
49
+ response += "\nDepression Scale Detected: {:.1f}\nMy Response: The worst mental and emotional distress possible.\n\tYou can't imagine things getting any better now and you might think it's all over for you (SUICIDE).\n\tContact a crisis line or get started with treatment immediately. 鈽光樄".format(round(neg_rate) / 10)
50
+ return response
51
+
52
+ @app.route("/analyze", methods=["POST"])
53
+ def analyze():
54
+ audio_file = request.files['file']
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
+ @app.route("/")
61
+ def home():
62
+ return "Emotion Recognition and Depression Analysis API"
63
+
64
+ if __name__ == "__main__":
65
+ app.run(host="0.0.0.0", port=8000)