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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -6
app.py CHANGED
@@ -1,5 +1,6 @@
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"
@@ -9,13 +10,22 @@ headers = {"Authorization": "Bearer hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}
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):
@@ -76,9 +86,8 @@ def main():
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
 
 
1
  import streamlit as st
2
  import requests
3
+ import json
4
 
5
  # Define API details
6
  API_URL = "https://api-inference.huggingface.co/models/ehcalabres/wav2vec2-lg-xlsr-en-speech-emotion-recognition"
 
10
  def query_api(audio_file):
11
  with open(audio_file, "rb") as f:
12
  response = requests.post(API_URL, headers=headers, data=f)
13
+ try:
14
+ response_json = response.json()
15
+ except json.JSONDecodeError:
16
+ st.error("Error decoding JSON from API response.")
17
+ response_json = {}
18
+ return response_json
19
 
20
  # Function to compute the neg_rate from API response
21
  def compute_neg_rate(emotion_data):
22
+ if isinstance(emotion_data, list): # Ensure the data is a list
23
+ neg_emotions = ["anger", "disgust", "fear", "sadness"] # Example negative emotions
24
+ neg_rate = sum([emotion['score'] for emotion in emotion_data if emotion['label'].lower() in neg_emotions])
25
+ return neg_rate * 100 # Scale to a percentage
26
+ else:
27
+ st.error("Unexpected API response format.")
28
+ return 0
29
 
30
  # Depression scale logic
31
  def analyze_depression(neg_rate):
 
86
 
87
  # Query API and compute neg_rate
88
  emotion_data = query_api("temp_audio.wav")
89
+ st.write("API Response:", emotion_data) # Debugging line to inspect response
90
  neg_rate = compute_neg_rate(emotion_data)
 
 
91
  result = analyze_depression(neg_rate)
92
  st.text(result)
93