Added retry logic in fetch audio
Browse files
app.py
CHANGED
@@ -160,11 +160,19 @@ def send_task(payload):
|
|
160 |
return response["text"]
|
161 |
|
162 |
def fetch_audio():
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
|
169 |
def encode_audio_array(audio_array):
|
170 |
buffer = io.BytesIO()
|
@@ -391,11 +399,14 @@ def on_random_click():
|
|
391 |
reset_state()
|
392 |
with st.spinner("Fetching random audio... please wait"):
|
393 |
array, sampling_rate, filepath = fetch_audio()
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
|
|
|
|
|
|
|
399 |
|
400 |
def on_reset_click():
|
401 |
reset_state()
|
|
|
160 |
return response["text"]
|
161 |
|
162 |
def fetch_audio():
|
163 |
+
num_tries = 3
|
164 |
+
iter_count = 0
|
165 |
+
while iter_count <= num_tries:
|
166 |
+
try:
|
167 |
+
filepath = random.choice(random_paths)
|
168 |
+
with fs.open(f"{ARENA_PATH}/{filepath}", 'rb') as f:
|
169 |
+
audio,sr = torchaudio.load(f)
|
170 |
+
audio = audio.numpy()
|
171 |
+
return audio,sr,filepath
|
172 |
+
except Exception:
|
173 |
+
iter_count += 1
|
174 |
+
|
175 |
+
return None,None,None
|
176 |
|
177 |
def encode_audio_array(audio_array):
|
178 |
buffer = io.BytesIO()
|
|
|
399 |
reset_state()
|
400 |
with st.spinner("Fetching random audio... please wait"):
|
401 |
array, sampling_rate, filepath = fetch_audio()
|
402 |
+
if filepath is None:
|
403 |
+
st.error("Error in fetching random audio please try uploading an audio or using the mic")
|
404 |
+
else:
|
405 |
+
st.session_state.audio = {"data":array,"sample_rate":sampling_rate,"format":"audio/wav"}
|
406 |
+
st.session_state.has_audio = True
|
407 |
+
st.session_state.current_audio_type = "random"
|
408 |
+
st.session_state.audio_path = filepath
|
409 |
+
st.session_state.option_selected = None
|
410 |
|
411 |
def on_reset_click():
|
412 |
reset_state()
|