main-gui.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from audio import combine_two_files
  2. from models import SpeakerModel, Device, SampleRate
  3. import easygui
  4. import os
  5. from text import get_separated_text_from_file
  6. from push import push
  7. tts = SpeakerModel()
  8. # Can provide language, device, sample rate and model id
  9. # Default 'ru' 'cpu' 48000 v3_1_ru
  10. tts.specify_model()
  11. info = tts.get_model_info()
  12. def get_files() -> list:
  13. choice = easygui.buttonbox("Pick an item", "", ["File", "Folder"])
  14. if choice == "File":
  15. p = easygui.fileopenbox(filetypes=['*.txt'])
  16. files = [p]
  17. else:
  18. p = easygui.diropenbox(msg="Hello", title="Filessss")
  19. dir_listing = os.listdir(p)
  20. files = [f"{p}/{f}" for f in dir_listing if f.endswith(".txt")]
  21. return files
  22. files = get_files()
  23. output_audio_filename = easygui.enterbox("Specify outpyt file name") or ""
  24. speaker = easygui.choicebox("Select voice", "", info.speakers)
  25. for i, file in enumerate(files):
  26. print(file)
  27. try:
  28. paragraphs = get_separated_text_from_file(file)
  29. except Exception as e:
  30. print("Aborting program cause file can't be opened")
  31. exit()
  32. if len(files) == 1 and output_audio_filename:
  33. audio_filename = f"{output_audio_filename}.wav"
  34. else:
  35. audio_filename = f"{''.join(file.split('/')[-1].split('.')[:-1])}-{output_audio_filename}.wav"
  36. for j, p in enumerate(paragraphs):
  37. # push("Processing...", f"File {i+1}/{len(files)} paragraph {j+1}/{len(paragraphs)}")
  38. print("Processing...", f"File {i+1}/{len(files)} paragraph {j+1}/{len(paragraphs)}")
  39. try:
  40. tts.text2speech(p, "temp.wav", speaker=speaker)
  41. except Exception as e:
  42. print(e)
  43. combine_two_files([audio_filename, "temp.wav"], audio_filename)
  44. easygui.msgbox("Process was finished", "Text-to-speech")