script.py 1.9 KB

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