-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TTS #27
Open
Abhrant
wants to merge
24
commits into
main
Choose a base branch
from
tts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
TTS #27
Changes from 2 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
4fd96ec
✨feat : async tts using piper, 20 tokens per phrase, ffmpeg to stream…
Abhrant c87b592
✨feat: send sentences to TTS based on delimiters
Abhrant 158daad
✨feat: voice as a config param from yaml, thread monitoring for tts
Abhrant e71d989
update: add tts params to config.yaml
Abhrant b88ede8
📝time compare plots
Abhrant 7c9bb92
🐛bugfix: last sentence not being sent to tts bug fix
Abhrant cc7e416
✨feat: run e2e on android
Abhrant 6fe934e
📝android recipe and running instrctions
Abhrant 79b8c6f
📝docs: detailed instructions to run on android
Abhrant 014ac85
Update run_on_android.md
Abhrant aa9ec8c
✨feat: continously look for audio file and run the code once it finds…
Abhrant b32cc8c
✨feat: receive audio via wifi and save it, then run the wisper-llama …
Abhrant da49521
📝update: llama -> qwen model path update
Abhrant 399dac3
updated paths
Arnav0400 ad5e93a
fixed audio saving
Arnav0400 fb8ee80
receive_audio updated to take save output dir and return the audio bytes
e09842d
fixed blocking calls
Arnav0400 c0df6d0
fixed path for audio dump
Arnav0400 eaa9b4b
fixed speed of audio
787fd8a
Revert "fixed path for audio dump"
ishanpandeynyunai cda12b7
🔥remove: reduce delimiter list to only coordinating conjunctions
Abhrant b576cda
added device comp and remmoed processing
Arnav0400 a7f7d5e
✨update: adding qwen prompt to default prompt
Abhrant 694eb98
✨feat: stopping LLM when STT sends empty string
Abhrant File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,9 +13,12 @@ | |
import requests | ||
import logging | ||
import time | ||
import os | ||
import re | ||
import psutil | ||
|
||
LOGGER = None | ||
DEFAULT_CONFIG = "recipe/default.yaml" | ||
DEFAULT_CONFIG = "/home/piuser/voice/nyuntam/examples/experimentals/voice_engine/recipe/rpi5.yaml" | ||
|
||
|
||
def set_logger(*args, **kwargs): | ||
|
@@ -200,6 +203,11 @@ def parse_args(): | |
################################################## | ||
|
||
|
||
class EnvironmentTypes(StrEnum): | ||
STT = "stt" | ||
LLM = "llm" | ||
|
||
|
||
@dataclass | ||
class STTInput: | ||
environment_config: STTEnvironmentConfig | ||
|
@@ -356,14 +364,24 @@ def call(self, input: STTInput) -> EngineResponse: | |
llm_input = LLMInput.from_stt_response(self.config.llm, stt_response) | ||
if llm_input.stream: | ||
# implement stream response handling | ||
tts_processing_queue = queue.Queue() | ||
decoded_streams = queue.Queue() | ||
stream_queue = queue.Queue() | ||
stop_event = threading.Event() | ||
|
||
decode_thread = threading.Thread( | ||
target=decode_stream, | ||
args=(stop_event, stream_queue, decoded_streams, True), | ||
args=(stop_event, stream_queue, decoded_streams, tts_processing_queue, True), | ||
) | ||
decode_thread.start() | ||
|
||
tts_processing_thread = threading.Thread( | ||
target=create_tts_wav, | ||
args=(stop_event, tts_processing_queue) | ||
) | ||
tts_processing_thread.start() | ||
|
||
|
||
llm_input.data["stream"] = True | ||
ttfs = None | ||
response = call_llm_environment(llm_input) | ||
|
@@ -379,7 +397,10 @@ def call(self, input: STTInput) -> EngineResponse: | |
stream_queue.put(line) | ||
tock = time.time() | ||
stop_event.set() | ||
|
||
decode_thread.join() | ||
tts_processing_thread.join() | ||
|
||
llm_response = LLMResponse( | ||
text=decoded_streams_to_text(list(decoded_streams.queue)), | ||
streams=list(decoded_streams.queue), | ||
|
@@ -436,6 +457,7 @@ def initialize_environment(config: EnvironmentConfig): | |
+ config.get_options().split() | ||
+ config.get_model_option().split() | ||
) | ||
|
||
LOGGER.info(f"Initializing environment with command: {' '.join(cmd)}") | ||
return subprocess.Popen(cmd) | ||
|
||
|
@@ -473,7 +495,9 @@ def decode_stream( | |
stop_event: threading.Event, | ||
stream_queue: queue.Queue, | ||
decoded_streams: queue.Queue, | ||
tts_processing_queue: queue.Queue, | ||
decode_and_print: bool = False, | ||
decode_and_talk: bool = True | ||
): | ||
while not stop_event.is_set() or not stream_queue.empty(): | ||
try: | ||
|
@@ -486,9 +510,120 @@ def decode_stream( | |
# if decode_and_print: | ||
# # print decoded stream continously with flush | ||
# print(json_response["content"], end="", flush=True) | ||
if decode_and_talk: | ||
#use piper to start saying after delay of n tokens | ||
tts_processing_queue.put(json_response["content"]) | ||
|
||
except queue.Empty: | ||
pass # No data to process yet, continue | ||
|
||
def create_tts_wav( | ||
stop_event: threading.Event, | ||
tts_processing_queue: queue.Queue, | ||
output_dir: str = "/home/piuser/voice/core/test-output" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lets have a relative path as default |
||
): | ||
# Ensure output directory exists | ||
os.makedirs(output_dir, exist_ok=True) | ||
|
||
piper_process = subprocess.Popen( | ||
[ | ||
"piper", "--model", "en_US-lessac-medium", "--length-scale" , "1.5" , "--output_raw" | ||
], | ||
stdin=subprocess.PIPE, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
universal_newlines=True | ||
) | ||
|
||
piper_proc = psutil.Process(piper_process.pid) | ||
|
||
# Define FFmpeg command to stream the audio over HTTP | ||
ffmpeg_command = [ | ||
"ffmpeg", | ||
"-f", "s16le", # Input format (16-bit PCM, little-endian) | ||
"-ar", "22050", # Sample rate | ||
"-ac", "1", # Number of audio channels (mono) | ||
"-i", "-", # Input from stdin (output from Piper) | ||
"-acodec", "aac", # Audio codec (AAC) | ||
"-ab", "128k", # Audio bitrate | ||
"-f", "adts", # Output format | ||
"-content_type", "audio/aac", # Content type for the HTTP stream | ||
"-listen", "1", # Make FFmpeg act as a server | ||
"http://0.0.0.0:8090/feed.aac", # Output URL | ||
"-acodec", "pcm_s16le", # Audio codec for WAV | ||
# os.path.join(output_dir, "output.wav") # Output WAV file path | ||
] | ||
|
||
ffmpeg_process = subprocess.Popen( | ||
ffmpeg_command, | ||
stdin=piper_process.stdout, # Take input from Piper process | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE | ||
) | ||
|
||
buffer = "" | ||
|
||
try: | ||
while not (stop_event.is_set() and tts_processing_queue.empty()): | ||
if tts_processing_queue.qsize() > 1: | ||
try: | ||
# Get one item from the queue | ||
text_part = tts_processing_queue.get(timeout=0.001) | ||
buffer += text_part | ||
print("BUFFER : " , buffer) | ||
|
||
# Check if the buffer contains a full sentence | ||
if any(delimiter in buffer for delimiter in ['.', '!', '?' , ":" , ";"]): | ||
# Split the buffer into sentences | ||
sentences = re.split(r'(?<=[.!?])\s+', buffer) | ||
|
||
# Keep the last partial sentence in the buffer | ||
buffer = sentences.pop() if not re.search(r'[.!?]$', buffer) and len(sentences) > 1 else "" | ||
|
||
# print(sentences) | ||
|
||
# Join the complete sentences and send to Piper | ||
text = " ".join(sentences) | ||
|
||
if text: | ||
# Check if the Piper process is still running before writing | ||
try: | ||
print("Sending ---->", text) | ||
# Write the text to Piper's stdin | ||
piper_process.stdin.write(f"{text}\n") | ||
piper_process.stdin.flush() | ||
|
||
except BrokenPipeError: | ||
LOGGER.error("BrokenPipeError: Piper process terminated unexpectedly.") | ||
break | ||
|
||
except queue.Empty: | ||
pass # No data to process yet, continue | ||
|
||
finally: | ||
|
||
# Measure peak memory usage of Piper process | ||
try: | ||
peak_memory = piper_proc.memory_info().rss / (1024 * 1024) # Convert to MB | ||
print(f"Peak memory usage of Piper process: {peak_memory:.2f} MB") | ||
except psutil.NoSuchProcess: | ||
LOGGER.error("Piper process not found for memory measurement.") | ||
|
||
# Stop Piper subprocess if it's still running | ||
if piper_process.poll() is None: | ||
piper_process.stdin.close() | ||
piper_process.wait() | ||
ffmpeg_process.terminate() | ||
ffmpeg_process.wait() | ||
|
||
# Print any errors from Piper | ||
stderr_output = piper_process.stderr.read() | ||
if stderr_output: | ||
print("Piper stderr:", stderr_output) | ||
|
||
|
||
|
||
|
||
|
||
def decoded_streams_to_text(decoded_streams: tp.List[tp.Dict[str, tp.Any]]) -> str: | ||
return " ".join([stream["content"] for stream in decoded_streams]) | ||
|
@@ -539,4 +674,4 @@ def print_dict(d: dict, indent: int = 0): | |
print(f"-" * 50) | ||
except Exception as e: | ||
engine.terminate() | ||
raise e | ||
raise e |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we have a relative path?