Pendrokar commited on
Commit
502ca44
·
verified ·
1 Parent(s): 37e3892

Attempt connection to TTS model up to 3 times

Browse files
Files changed (1) hide show
  1. app.py +50 -44
app.py CHANGED
@@ -805,51 +805,56 @@ def synthandreturn(text):
805
  print("[debug] Using", mdl1, mdl2)
806
  def predict_and_update_result(text, model, result_storage):
807
  print(model)
808
- try:
809
- if model in AVAILABLE_MODELS:
810
- if '/' in model:
811
- # Use public HF Space
812
- mdl_space = Client(model, hf_token=hf_token)
813
- # assume the index is one of the first 9 return params
814
- return_audio_index = int(HF_SPACES[model]['return_audio_index'])
815
- endpoints = mdl_space.view_api(all_endpoints=True, print_info=False, return_format='dict')
816
-
817
- api_name = None
818
- fn_index = None
819
- end_parameters = None
820
- # has named endpoint
821
- if '/' == HF_SPACES[model]['function'][0]:
822
- # audio sync function name
823
- api_name = HF_SPACES[model]['function']
824
-
825
- end_parameters = _get_param_examples(
826
- endpoints['named_endpoints'][api_name]['parameters']
827
- )
828
- # has unnamed endpoint
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
829
  else:
830
- # endpoint index is the first character
831
- fn_index = int(HF_SPACES[model]['function'])
832
-
833
- end_parameters = _get_param_examples(
834
- endpoints['unnamed_endpoints'][str(fn_index)]['parameters']
835
- )
836
-
837
- space_inputs = _override_params(end_parameters, model)
838
-
839
- # force text
840
- space_inputs[HF_SPACES[model]['text_param_index']] = text
841
-
842
- results = mdl_space.predict(*space_inputs, api_name=api_name, fn_index=fn_index)
843
-
844
- # return path to audio
845
- result = results[return_audio_index] if (not isinstance(results, str)) else results
846
  else:
847
- # Use the private HF Space
848
- result = router.predict(text, AVAILABLE_MODELS[model].lower(), api_name="/synthesize")
849
- else:
850
- result = router.predict(text, model.lower(), api_name="/synthesize")
851
- except:
852
- raise gr.Error('Unable to call API, please try again :)')
853
  print('Done with', model)
854
  # try:
855
  # doresample(result)
@@ -860,9 +865,10 @@ def synthandreturn(text):
860
  audio = AudioSegment.from_file(result)
861
  current_sr = audio.frame_rate
862
  if current_sr > 24000:
 
863
  audio = audio.set_frame_rate(24000)
864
  try:
865
- print('Trying to normalize audio')
866
  audio = match_target_amplitude(audio, -20)
867
  except:
868
  print('[WARN] Unable to normalize audio')
 
805
  print("[debug] Using", mdl1, mdl2)
806
  def predict_and_update_result(text, model, result_storage):
807
  print(model)
808
+ # 3 attempts
809
+ attempt_count = 0
810
+ while attempt_count < 3:
811
+ try:
812
+ if model in AVAILABLE_MODELS:
813
+ if '/' in model:
814
+ # Use public HF Space
815
+ mdl_space = Client(model, hf_token=hf_token)
816
+ # assume the index is one of the first 9 return params
817
+ return_audio_index = int(HF_SPACES[model]['return_audio_index'])
818
+ endpoints = mdl_space.view_api(all_endpoints=True, print_info=False, return_format='dict')
819
+
820
+ api_name = None
821
+ fn_index = None
822
+ end_parameters = None
823
+ # has named endpoint
824
+ if '/' == HF_SPACES[model]['function'][0]:
825
+ # audio sync function name
826
+ api_name = HF_SPACES[model]['function']
827
+
828
+ end_parameters = _get_param_examples(
829
+ endpoints['named_endpoints'][api_name]['parameters']
830
+ )
831
+ # has unnamed endpoint
832
+ else:
833
+ # endpoint index is the first character
834
+ fn_index = int(HF_SPACES[model]['function'])
835
+
836
+ end_parameters = _get_param_examples(
837
+ endpoints['unnamed_endpoints'][str(fn_index)]['parameters']
838
+ )
839
+
840
+ space_inputs = _override_params(end_parameters, model)
841
+
842
+ # force text
843
+ space_inputs[HF_SPACES[model]['text_param_index']] = text
844
+
845
+ results = mdl_space.predict(*space_inputs, api_name=api_name, fn_index=fn_index)
846
+
847
+ # return path to audio
848
+ result = results[return_audio_index] if (not isinstance(results, str)) else results
849
  else:
850
+ # Use the private HF Space
851
+ result = router.predict(text, AVAILABLE_MODELS[model].lower(), api_name="/synthesize")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
852
  else:
853
+ result = router.predict(text, model.lower(), api_name="/synthesize")
854
+ break
855
+ except:
856
+ attempt_count++
857
+ raise gr.Error('Unable to call API, please try again')
 
858
  print('Done with', model)
859
  # try:
860
  # doresample(result)
 
865
  audio = AudioSegment.from_file(result)
866
  current_sr = audio.frame_rate
867
  if current_sr > 24000:
868
+ print('Resampling', model)
869
  audio = audio.set_frame_rate(24000)
870
  try:
871
+ print('Trying to normalize audio', model)
872
  audio = match_target_amplitude(audio, -20)
873
  except:
874
  print('[WARN] Unable to normalize audio')