ghuman7 commited on
Commit
525afe5
·
verified ·
1 Parent(s): 6a7b9c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -5
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import streamlit as st
2
  import requests
3
  import os
 
4
 
5
  API_URL = "https://api-inference.huggingface.co/models/ehcalabres/wav2vec2-lg-xlsr-en-speech-emotion-recognition"
6
  headers = {"Authorization": f"Bearer {os.getenv('HF_API_TOKEN')}"}
@@ -17,11 +18,20 @@ emotion_to_neg_rate = {
17
  }
18
 
19
  def get_emotion(audio_file):
20
- response = requests.post(API_URL, headers=headers, files={"file": audio_file})
21
- if response.status_code == 200:
22
- return response.json()
23
- else:
24
- return {"error": response.text}
 
 
 
 
 
 
 
 
 
25
 
26
  def get_neg_rate(emotion):
27
  return emotion_to_neg_rate.get(emotion, 30.0)
 
1
  import streamlit as st
2
  import requests
3
  import os
4
+ import time # Import time for sleep
5
 
6
  API_URL = "https://api-inference.huggingface.co/models/ehcalabres/wav2vec2-lg-xlsr-en-speech-emotion-recognition"
7
  headers = {"Authorization": f"Bearer {os.getenv('HF_API_TOKEN')}"}
 
18
  }
19
 
20
  def get_emotion(audio_file):
21
+ for _ in range(5): # Retry up to 5 times
22
+ response = requests.post(API_URL, headers=headers, files={"file": audio_file})
23
+ if response.status_code == 200:
24
+ return response.json()
25
+ elif response.status_code == 503: # Service Unavailable
26
+ error_info = response.json()
27
+ if "error" in error_info and "loading" in error_info["error"]:
28
+ st.write("Model is currently loading. Please wait...")
29
+ time.sleep(10) # Wait for 10 seconds before retrying
30
+ else:
31
+ return {"error": response.text}
32
+ else:
33
+ return {"error": response.text}
34
+ return {"error": "Model is taking too long to load or an unexpected error occurred."}
35
 
36
  def get_neg_rate(emotion):
37
  return emotion_to_neg_rate.get(emotion, 30.0)