chinmayc3 commited on
Commit
c10a203
·
1 Parent(s): 16d67be

Added retry logic in fetch audio

Browse files
Files changed (1) hide show
  1. app.py +21 -10
app.py CHANGED
@@ -160,11 +160,19 @@ def send_task(payload):
160
  return response["text"]
161
 
162
  def fetch_audio():
163
- filepath = random.choice(random_paths)
164
- with fs.open(f"{ARENA_PATH}/{filepath}", 'rb') as f:
165
- audio,sr = torchaudio.load(f)
166
- audio = audio.numpy()
167
- return audio,sr,filepath
 
 
 
 
 
 
 
 
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
- st.session_state.audio = {"data":array,"sample_rate":sampling_rate,"format":"audio/wav"}
395
- st.session_state.has_audio = True
396
- st.session_state.current_audio_type = "random"
397
- st.session_state.audio_path = filepath
398
- st.session_state.option_selected = None
 
 
 
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()