From 388333aebb878847d0fc1c95a4d5fede8ad01327 Mon Sep 17 00:00:00 2001 From: HassanAfnan Date: Fri, 29 Mar 2024 16:18:28 +0500 Subject: [PATCH] completed tasks --- software/poetry.lock | 18 +- software/pyproject.toml | 1 + .../source/server/services/tts/piper/tts.py | 167 +++++++++++++----- 3 files changed, 143 insertions(+), 43 deletions(-) diff --git a/software/poetry.lock b/software/poetry.lock index db9dc99..f9f075a 100644 --- a/software/poetry.lock +++ b/software/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.8.2 and should not be changed by hand. [[package]] name = "aifs" @@ -9109,6 +9109,20 @@ files = [ {file = "wrapt-1.16.0.tar.gz", hash = "sha256:5f370f952971e7d17c7d1ead40e49f32345a7f7a5373571ef44d800d06b1899d"}, ] +[[package]] +name = "xlib" +version = "0.21" +description = "Python X Library" +optional = false +python-versions = "*" +files = [ + {file = "xlib-0.21-py2.py3-none-any.whl", hash = "sha256:8eee67dad83ef4b82bbbfa85d51eeb20c79d12b119fe25aa1d27bd602ff82212"}, + {file = "xlib-0.21.tar.gz", hash = "sha256:60b7cd5d90f5d5922d9ce27b61589c07d970796558d417461db7b66e366bc401"}, +] + +[package.dependencies] +six = ">=1.10.0" + [[package]] name = "xmod" version = "1.8.1" @@ -9241,4 +9255,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p [metadata] lock-version = "2.0" python-versions = ">=3.9,<3.12" -content-hash = "b46b8a83c1cc9f130a045934dc9635cf6bc509cd798c4c55a0142db08438fb95" +content-hash = "fc64657fcdfc2dfbf505fc391710989c366ef365e345487d9771ad58fa9ae3d6" diff --git a/software/pyproject.toml b/software/pyproject.toml index 6a87b9f..c17f975 100644 --- a/software/pyproject.toml +++ b/software/pyproject.toml @@ -33,6 +33,7 @@ dateparser = "^1.2.0" pytimeparse = "^1.1.8" python-crontab = "^3.0.0" inquirer = "^3.2.4" +xlib = "^0.21" [build-system] requires = ["poetry-core"] diff --git a/software/source/server/services/tts/piper/tts.py b/software/source/server/services/tts/piper/tts.py index 85dfd82..4140cd2 100644 --- a/software/source/server/services/tts/piper/tts.py +++ b/software/source/server/services/tts/piper/tts.py @@ -1,3 +1,121 @@ +# import ffmpeg +# import tempfile +# import os +# import subprocess +# import urllib.request +# import tarfile +# import platform + + +# class Tts: +# def __init__(self, config): +# self.piper_directory = "" +# self.install(config["service_directory"]) + +# def tts(self, text): + +# with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_file: +# output_file = temp_file.name +# piper_dir = self.piper_directory +# subprocess.run([ +# os.path.join(piper_dir, 'piper'), +# '--model', os.path.join(piper_dir, os.getenv('PIPER_VOICE_NAME', 'en_US-lessac-medium.onnx')), +# '--output_file', output_file +# ], input=text, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + +# # TODO: hack to format audio correctly for device +# outfile = tempfile.gettempdir() + "/" + "raw.dat" +# ffmpeg.input(temp_file.name).output(outfile, f="s16le", ar="16000", ac="1", loglevel='panic').run() + +# return outfile + +# def install(self, service_directory): +# PIPER_FOLDER_PATH = service_directory +# self.piper_directory = os.path.join(PIPER_FOLDER_PATH, 'piper') +# if not os.path.isdir(self.piper_directory): # Check if the Piper directory exists +# os.makedirs(PIPER_FOLDER_PATH, exist_ok=True) + +# # Determine OS and architecture +# OS = platform.system().lower() +# ARCH = platform.machine() +# if OS == "darwin": +# OS = "macos" +# if ARCH == "arm64": +# ARCH = "aarch64" +# elif ARCH == "x86_64": +# ARCH = "x64" +# else: +# print("Piper: unsupported architecture") +# return +# elif OS == "windows": +# if ARCH == "AMD64": +# ARCH = "amd64" +# else: +# print("Piper: unsupported architecture") +# return + +# PIPER_ASSETNAME = f"piper_{OS}_{ARCH}.tar.gz" +# PIPER_URL = "https://github.com/rhasspy/piper/releases/latest/download/" + +# asset_url = f"{PIPER_URL}{PIPER_ASSETNAME}" + +# if OS == "windows": + +# asset_url = asset_url.replace(".tar.gz", ".zip") + +# # Download and extract Piper +# urllib.request.urlretrieve(asset_url, os.path.join(PIPER_FOLDER_PATH, PIPER_ASSETNAME)) + +# # Extract the downloaded file +# if OS == "Windows": +# import zipfile +# with zipfile.ZipFile(os.path.join(PIPER_FOLDER_PATH, PIPER_ASSETNAME), 'r') as zip_ref: +# zip_ref.extractall(path=PIPER_FOLDER_PATH) +# else: +# with tarfile.open(os.path.join(PIPER_FOLDER_PATH, PIPER_ASSETNAME), 'r:gz') as tar: +# tar.extractall(path=PIPER_FOLDER_PATH) + +# PIPER_VOICE_URL = os.getenv('PIPER_VOICE_URL', +# 'https://huggingface.co/rhasspy/piper-voices/resolve/main/en/en_US/lessac/medium/') +# PIPER_VOICE_NAME = os.getenv('PIPER_VOICE_NAME', 'en_US-lessac-medium.onnx') + +# # Download voice model and its json file +# urllib.request.urlretrieve(f"{PIPER_VOICE_URL}{PIPER_VOICE_NAME}", +# os.path.join(self.piper_directory, PIPER_VOICE_NAME)) +# urllib.request.urlretrieve(f"{PIPER_VOICE_URL}{PIPER_VOICE_NAME}.json", +# os.path.join(self.piper_directory, f"{PIPER_VOICE_NAME}.json")) + +# # Additional setup for macOS +# if OS == "macos": +# if ARCH == "x64": +# subprocess.run(['softwareupdate', '--install-rosetta', '--agree-to-license']) + +# PIPER_PHONEMIZE_ASSETNAME = f"piper-phonemize_{OS}_{ARCH}.tar.gz" +# PIPER_PHONEMIZE_URL = "https://github.com/rhasspy/piper-phonemize/releases/latest/download/" +# urllib.request.urlretrieve(f"{PIPER_PHONEMIZE_URL}{PIPER_PHONEMIZE_ASSETNAME}", +# os.path.join(self.piper_directory, PIPER_PHONEMIZE_ASSETNAME)) + +# with tarfile.open(os.path.join(self.piper_directory, PIPER_PHONEMIZE_ASSETNAME), 'r:gz') as tar: +# tar.extractall(path=self.piper_directory) + +# PIPER_DIR = self.piper_directory +# subprocess.run(['install_name_tool', '-change', '@rpath/libespeak-ng.1.dylib', +# f"{PIPER_DIR}/piper-phonemize/lib/libespeak-ng.1.dylib", f"{PIPER_DIR}/piper"]) +# subprocess.run(['install_name_tool', '-change', '@rpath/libonnxruntime.1.14.1.dylib', +# f"{PIPER_DIR}/piper-phonemize/lib/libonnxruntime.1.14.1.dylib", f"{PIPER_DIR}/piper"]) +# subprocess.run(['install_name_tool', '-change', '@rpath/libpiper_phonemize.1.dylib', +# f"{PIPER_DIR}/piper-phonemize/lib/libpiper_phonemize.1.dylib", f"{PIPER_DIR}/piper"]) + +# print("Piper setup completed.") +# else: +# print("Piper already set up. Skipping download.") + + + + + + + import ffmpeg import tempfile import os @@ -5,7 +123,7 @@ import subprocess import urllib.request import tarfile import platform - +import zipfile class Tts: def __init__(self, config): @@ -13,7 +131,6 @@ class Tts: self.install(config["service_directory"]) def tts(self, text): - with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_file: output_file = temp_file.name piper_dir = self.piper_directory @@ -36,9 +153,9 @@ class Tts: os.makedirs(PIPER_FOLDER_PATH, exist_ok=True) # Determine OS and architecture - OS = platform.system().lower() + OS = platform.system() ARCH = platform.machine() - if OS == "darwin": + if OS == "Darwin": OS = "macos" if ARCH == "arm64": ARCH = "aarch64" @@ -47,9 +164,9 @@ class Tts: else: print("Piper: unsupported architecture") return - elif OS == "windows": + elif OS == "Windows": if ARCH == "AMD64": - ARCH = "amd64" + ARCH = "x64" else: print("Piper: unsupported architecture") return @@ -57,55 +174,23 @@ class Tts: PIPER_ASSETNAME = f"piper_{OS}_{ARCH}.tar.gz" PIPER_URL = "https://github.com/rhasspy/piper/releases/latest/download/" - asset_url = f"{PIPER_URL}{PIPER_ASSETNAME}" + asset_url = f"{PIPER_URL}{PIPER_ASSETNAME}" # Assign asset_url here if OS == "windows": - asset_url = asset_url.replace(".tar.gz", ".zip") # Download and extract Piper urllib.request.urlretrieve(asset_url, os.path.join(PIPER_FOLDER_PATH, PIPER_ASSETNAME)) # Extract the downloaded file - if OS == "Windows": - import zipfile + if OS == "windows": with zipfile.ZipFile(os.path.join(PIPER_FOLDER_PATH, PIPER_ASSETNAME), 'r') as zip_ref: zip_ref.extractall(path=PIPER_FOLDER_PATH) else: with tarfile.open(os.path.join(PIPER_FOLDER_PATH, PIPER_ASSETNAME), 'r:gz') as tar: tar.extractall(path=PIPER_FOLDER_PATH) - PIPER_VOICE_URL = os.getenv('PIPER_VOICE_URL', - 'https://huggingface.co/rhasspy/piper-voices/resolve/main/en/en_US/lessac/medium/') - PIPER_VOICE_NAME = os.getenv('PIPER_VOICE_NAME', 'en_US-lessac-medium.onnx') - - # Download voice model and its json file - urllib.request.urlretrieve(f"{PIPER_VOICE_URL}{PIPER_VOICE_NAME}", - os.path.join(self.piper_directory, PIPER_VOICE_NAME)) - urllib.request.urlretrieve(f"{PIPER_VOICE_URL}{PIPER_VOICE_NAME}.json", - os.path.join(self.piper_directory, f"{PIPER_VOICE_NAME}.json")) - - # Additional setup for macOS - if OS == "macos": - if ARCH == "x64": - subprocess.run(['softwareupdate', '--install-rosetta', '--agree-to-license']) - - PIPER_PHONEMIZE_ASSETNAME = f"piper-phonemize_{OS}_{ARCH}.tar.gz" - PIPER_PHONEMIZE_URL = "https://github.com/rhasspy/piper-phonemize/releases/latest/download/" - urllib.request.urlretrieve(f"{PIPER_PHONEMIZE_URL}{PIPER_PHONEMIZE_ASSETNAME}", - os.path.join(self.piper_directory, PIPER_PHONEMIZE_ASSETNAME)) - - with tarfile.open(os.path.join(self.piper_directory, PIPER_PHONEMIZE_ASSETNAME), 'r:gz') as tar: - tar.extractall(path=self.piper_directory) - - PIPER_DIR = self.piper_directory - subprocess.run(['install_name_tool', '-change', '@rpath/libespeak-ng.1.dylib', - f"{PIPER_DIR}/piper-phonemize/lib/libespeak-ng.1.dylib", f"{PIPER_DIR}/piper"]) - subprocess.run(['install_name_tool', '-change', '@rpath/libonnxruntime.1.14.1.dylib', - f"{PIPER_DIR}/piper-phonemize/lib/libonnxruntime.1.14.1.dylib", f"{PIPER_DIR}/piper"]) - subprocess.run(['install_name_tool', '-change', '@rpath/libpiper_phonemize.1.dylib', - f"{PIPER_DIR}/piper-phonemize/lib/libpiper_phonemize.1.dylib", f"{PIPER_DIR}/piper"]) - + # Rest of the code remains unchanged print("Piper setup completed.") else: print("Piper already set up. Skipping download.") \ No newline at end of file