ghuman7 commited on
Commit
b2998d7
verified
1 Parent(s): cf2093e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -72
app.py CHANGED
@@ -1,81 +1,87 @@
1
  import streamlit as st
2
- from transformers import Wav2Vec2ForSequenceClassification, Wav2Vec2Tokenizer
3
- import torch
4
- import numpy as np
5
- import io
6
 
7
- # Load the model and tokenizer
8
- model_name = "harshit345/xlsr-wav2vec-speech-emotion-recognition"
9
- model = Wav2Vec2ForSequenceClassification.from_pretrained(model_name)
10
- tokenizer = Wav2Vec2Tokenizer.from_pretrained(model_name)
11
 
12
- def analyze_emotion(audio_bytes):
13
- audio_input = tokenizer(audio_bytes, return_tensors="pt", padding="longest")
14
- with torch.no_grad():
15
- logits = model(**audio_input).logits
16
- prob = torch.sigmoid(logits).numpy().flatten()
17
- neg_rate = prob[0] * 100 # Assuming the first element is the negative emotion probability
18
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  if neg_rate <= 30.0:
20
  if neg_rate <= 5.0:
21
  return "just chill and relax"
22
- elif neg_rate <= 10.0:
23
- return (f"*** MILD DEPRESSION ***\n"
24
- f"\nDepression Scale Detected : {round(neg_rate) / 10}\n"
25
- f"My Response :\tEverything is a-okay! There's absolutely nothing wrong 馃榿馃榿\n"
26
- f"\t\tYou're probably cuddling a fluffy kitten right now")
27
- elif neg_rate <= 20.0:
28
- return (f"\nDepression Scale Detected : {round(neg_rate) / 10}\n"
29
- f"My Response :\tYou are a bit frustrated and disappointed\n"
30
- f"\t\tBut you're easily distracted and cheered with little effort 馃榿馃榿")
31
- elif neg_rate <= 30.0:
32
- return (f"\nDepression Scale Detected : {round(neg_rate) / 10}\n"
33
- f"My Response :\tThings are bothering you but you're coping up\n"
34
- f"\t\tYou might be over tired and hungry\n"
35
- f"\t\tThe emotional equivalent of a headache 馃榿馃榿")
36
- elif neg_rate <= 60.0:
37
- if neg_rate <= 40.0:
38
- return (f"*** MODERATE DEPRESSION ***\n"
39
- f"\nDepression Scale Detected : {round(neg_rate) / 10}\n"
40
- f"My Response :\tToday is slightly bad for you.\n"
41
- f"\t\tYou still have the skills to get through it, but be gentle with yourself\n"
42
- f"\t\tUse self-care strategies 馃榿馃榿")
43
- elif neg_rate <= 50.0:
44
- return (f"\nDepression Scale Detected : {round(neg_rate) / 10}\n"
45
- f"My Response :\tYour mental health is starting to impact your everyday life.\n"
46
- f"\t\tEasy things are becoming difficult")
47
- elif neg_rate <= 60.0:
48
- return (f"\nDepression Scale Detected : {round(neg_rate) / 10}\n"
49
- f"My Response :\tYou are not able to do things the way usually you do them due to your mental health.\n"
50
- f"\t\tImpulsive and compulsive thoughts might be difficult to cope with")
51
- elif neg_rate <= 100.0:
52
- if neg_rate <= 70.0:
53
- return (f"*** SEVERE DEPRESSION ***\n"
54
- f"\nDepression Scale Detected : {round(neg_rate) / 10}\n"
55
- f"My Response :\tYou are losing interest in the activities that used to be enjoyable.\n"
56
- f"\t\tYou should definitely seek help\n"
57
- f"\t\tThis is becoming serious 鈽光樄")
58
- elif neg_rate <= 80.0:
59
- return (f"\nDepression Scale Detected : {round(neg_rate) / 10}\n"
60
- f"My Response :\tYou can't ignore your struggles now, It's HIGH time!.\n"
61
- f"\t\tYou may have issues sleeping, eating, having fun, socializing, and work/study\n"
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 App")
75
 
76
- uploaded_file = st.file_uploader("Choose an audio file", type=["wav"])
77
 
78
- if uploaded_file is not None:
79
- audio_bytes = uploaded_file.read()
80
- result = analyze_emotion(audio_bytes)
81
- st.write(result)
 
 
 
 
 
 
 
 
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')}")