ghuman7 commited on
Commit
9768d85
verified
1 Parent(s): 1f3e409

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -32
app.py CHANGED
@@ -1,65 +1,86 @@
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)
 
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()