script.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import asyncio
  2. from pathlib import Path
  3. import torch
  4. torch._C._jit_set_profiling_mode(False)
  5. params = {
  6. 'activate': True,
  7. 'speaker': 'en_56',
  8. 'language': 'en',
  9. 'model_id': 'v3_en',
  10. 'sample_rate': 48000,
  11. 'device': 'cpu',
  12. }
  13. current_params = params.copy()
  14. wav_idx = 0
  15. def load_model():
  16. model, example_text = torch.hub.load(repo_or_dir='snakers4/silero-models', model='silero_tts', language=params['language'], speaker=params['model_id'])
  17. model.to(params['device'])
  18. return model
  19. model = load_model()
  20. def remove_surrounded_chars(string):
  21. new_string = ""
  22. in_star = False
  23. for char in string:
  24. if char == '*':
  25. in_star = not in_star
  26. elif not in_star:
  27. new_string += char
  28. return new_string
  29. def input_modifier(string):
  30. """
  31. This function is applied to your text inputs before
  32. they are fed into the model.
  33. """
  34. return string
  35. def output_modifier(string):
  36. """
  37. This function is applied to the model outputs.
  38. """
  39. global wav_idx, model, current_params
  40. for i in params:
  41. if params[i] != current_params[i]:
  42. model = load_model()
  43. current_params = params.copy()
  44. break
  45. if params['activate'] == False:
  46. return string
  47. string = remove_surrounded_chars(string)
  48. string = string.replace('"', '')
  49. string = string.replace('“', '')
  50. string = string.replace('\n', ' ')
  51. string = string.strip()
  52. if string == '':
  53. string = 'empty reply, try regenerating'
  54. output_file = Path(f'extensions/silero_tts/outputs/{wav_idx:06d}.wav')
  55. audio = model.save_wav(text=string, speaker=params['speaker'], sample_rate=int(params['sample_rate']), audio_path=str(output_file))
  56. string = f'<audio src="file/{output_file.as_posix()}" controls></audio>'
  57. wav_idx += 1
  58. return string
  59. def bot_prefix_modifier(string):
  60. """
  61. This function is only applied in chat mode. It modifies
  62. the prefix text for the Bot and can be used to bias its
  63. behavior.
  64. """
  65. return string