|
from flask import Flask, request, jsonify |
|
import requests |
|
|
|
app = Flask(__name__) |
|
|
|
API_URL = "https://api-inference.huggingface.co/models/ehcalabres/wav2vec2-lg-xlsr-en-speech-emotion-recognition" |
|
headers = {"Authorization": "Bearer hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"} |
|
|
|
@app.route("/") |
|
def home(): |
|
return render("index.html") |
|
|
|
|
|
def query_api(audio_file): |
|
with open(audio_file, "rb") as f: |
|
response = requests.post(API_URL, headers=headers, data=f) |
|
return response.json() |
|
|
|
def compute_neg_rate(emotion_data): |
|
neg_emotions = ["anger", "disgust", "fear", "sadness"] |
|
neg_rate = sum([emotion['score'] for emotion in emotion_data if emotion['label'].lower() in neg_emotions]) |
|
return neg_rate * 100 |
|
|
|
def analyze_depression(neg_rate): |
|
response = "" |
|
if neg_rate <= 30.0: |
|
if neg_rate <= 5.0: |
|
response = "just chill and relax" |
|
else: |
|
response = "*** MILD DEPRESSION ***" |
|
if neg_rate <= 10.0: |
|
response += "\nDepression Scale Detected: {:.1f}\nMy Response: Everything is a-okay! There's absolutely nothing wrong 馃榿馃榿".format(round(neg_rate) / 10) |
|
elif neg_rate > 10.0 and neg_rate <= 20.0: |
|
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) |
|
elif neg_rate > 20.0 and neg_rate <= 30.0: |
|
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) |
|
elif neg_rate > 30.0 and neg_rate <= 60.0: |
|
response = "*** MODERATE DEPRESSION ***" |
|
if neg_rate > 30.0 and neg_rate <= 40.0: |
|
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) |
|
elif neg_rate > 40.0 and neg_rate <= 50.0: |
|
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) |
|
elif neg_rate > 50.0 and neg_rate <= 60.0: |
|
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) |
|
elif neg_rate > 60.0 and neg_rate <= 100.0: |
|
response = "*** SEVERE DEPRESSION ***" |
|
if neg_rate > 60.0 and neg_rate <= 70.0: |
|
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) |
|
elif neg_rate > 70.0 and neg_rate <= 80.0: |
|
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) |
|
elif neg_rate > 80.0 and neg_rate <= 90.0: |
|
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) |
|
elif neg_rate > 90.0 and neg_rate <= 100.0: |
|
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) |
|
return response |
|
|
|
@app.route("/analyze", methods=["POST"]) |
|
def analyze(): |
|
audio_file = request.files['file'] |
|
emotion_data = query_api(audio_file) |
|
neg_rate = compute_neg_rate(emotion_data) |
|
response = analyze_depression(neg_rate) |
|
return jsonify({"response": response}) |
|
|
|
@app.route("/") |
|
def home(): |
|
return "Emotion Recognition and Depression Analysis API" |
|
|
|
if __name__ == "__main__": |
|
app.run(host="0.0.0.0", port=8000) |