Advertisement
voidmesmer

piper to AllTalk

Apr 23rd, 2024 (edited)
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. import os
  2. import soundfile as sf
  3. import sounddevice as sd
  4. from openai import OpenAI
  5. from dotenv import load_dotenv
  6. import subprocess
  7. import threading
  8. import queue
  9. import config
  10. import tempfile
  11. import utils
  12. import requests
  13. import shutil
  14.  
  15. ...
  16. ...
  17.  
  18. def TTS_piper(self, text_to_speak, output_file):
  19. """
  20. Generate speech from text using the Piper TTS engine and save it to an output file.
  21.  
  22. Args:
  23. text_to_speak (str): The text to be converted to speech.
  24. output_file (str): The file path where the audio will be saved.
  25. """
  26. # Sanitize the input text by removing unsuitable characters
  27. text_to_speak = utils.sanitize_text(text_to_speak)
  28.  
  29. # If there is no text left after sanitization, return "failed"
  30. if not text_to_speak.strip():
  31. return "failed"
  32. try:
  33. # Define the API endpoint
  34. api_url = "http://127.0.0.1:7851/api/tts-generate"
  35.  
  36. # Prepare the data payload for the POST request
  37. data = {
  38. "text_input": text_to_speak,
  39. "text_filtering": "none",
  40. "character_voice_gen": "female_03.wav",
  41. "narrator_enabled": "false",
  42. "narrator_voice_gen": "arnold.wav",
  43. "text_not_inside": "character",
  44. "language": "en",
  45. "output_file_name": "output",
  46. "output_file_timestamp": "true",
  47. "autoplay": "false",
  48. "autoplay_volume": "0.8"
  49. }
  50. response = requests.post(api_url, data=data)
  51. print(response.content)
  52. response.raise_for_status() # Raise an exception for HTTP error codes
  53. response_data = response.json()
  54. if response_data["status"] == "generate-success":
  55. # Use the local path of the generated audio file
  56. local_audio_path = response_data["output_file_path"]
  57.  
  58. # Copy the file from the local path to the desired output file
  59.  
  60. shutil.copyfile(local_audio_path, output_file)
  61.  
  62.  
  63. return "success"
  64. except requests.RequestException as e:
  65. print(f"Error calling TTS API: {e}")
  66. return "failed"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement