I am using this library to create a small home assistant like alexa. I record the text, process it with a small parsing function, and then it spits out the answer. I am using gTTS for that part and playdub to "say" it. However, after it creates the audio file I get an error that completely shuts down the whole program: BrokenPipeError. I used Claude and ChatGPT to try and fix as well as going through the documentation but no luck. Here is the attached code and error:
Error:
C:\Users\User\Documents\Gecko>ERROR:root:Error receiving data from connection: [WinError 109] The pipe has been ended
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\RealtimeSTT\audio_recorder.py", line 127, in poll_connection
if self.conn.poll(0.01): # Increased from 0.01 to 0.5 seconds
^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\multiprocessing\connection.py", line 256, in poll
return self._poll(timeout)
^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\multiprocessing\connection.py", line 327, in _poll
_winapi.PeekNamedPipe(self._handle)[0] != 0):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
BrokenPipeError: [WinError 109] The pipe has been ended
main.py:
if __name__ == '__main__':
try:
threading.Thread(target=other.alarm.check_alarms, daemon=True).start()
print("Alarm thread started successfully.")
except Exception as e:
print(f"Failed to start alarm thread: {e}")
print("Starting STT...")
with AudioToTextRecorder(
wake_words="computer",
wake_words_sensitivity=0.6,
wakeword_backend="pvporcupine",
post_speech_silence_duration=0.4
) as recorder:
while True:
try:
text = recorder.text()
answer, parsed = initparse.parse_text(text)
threading.Thread(target=stt.say, args=(answer,), daemon=True).start()
log(text, parsed, answer)
except BrokenPipeError: # useless claude code
time.sleep(0.1)
continue
except Exception as e:
print("Error", e)
break
speech/stt.py
from gtts import gTTS
from pydub import AudioSegment
import os
import time
import random
import pyttsx3
import threading
from pydub.playback import play
from music import songplayer
def seay(text): # first attempt at tts, not used
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
def say(text):
random_id = random.randint(10**15, 10**16 - 1)
filename = f"speech_{random_id}.mp3"
tts = gTTS(text=text, lang='en')
tts.save(filename)
audio = AudioSegment.from_file(filename)
play(audio)
os.remove(filename)
speech/initparse.py
import random
import time
from music import songplayer
from ai import ask_ai
import other.alarm
from speech import stt
def parse_text(text):
print("Non AI Parsing text:", text)
atext = text.strip().lower()
text = atext.strip(".?!'")
text = text.replace("computer", "").strip()
text = text.replace(", ", "").strip()
print("Cleaned text:", text)
parsed = text
if text == "hello" or text == "hi" or text == "hey" or text == "hey there" or text == "hi there" or text == "hello there":
return random.choice([
"Hello! How can I assist you today?",
"Hi there! What can I do for you?",
"Hey! Need any help?",
"Hello! What can I do for you?",
"Hi! How can I help you today?",
"Hey there! What can I assist you with?"
], parsed)
elif text == "what is your name" or text == "whats your name" or text == "who are you" or text == "what are you" or text == "identify yourself":
return random.choice([
"I'm Gecko, your personal assistant.",
"My name is Gecko. How can I assist you?",
"You can call me Gecko. What can I do for you?",
"I'm Gecko, here to help you with anything you need.",
"My name is Gecko. How can I assist you today?",
"You can call me Gecko. What can I do for you?"
], parsed)
elif "what time is it" in text or "whats the time" in text or "tell me the time" in text or "current time" in text:
return "The current time is " + time.strftime("%I:%M %p") + ".", parsed
elif (
"wake me up" in text or
"set an alarm" in text or
"alarm for" in text or
"set alarm for" in text or
"wake me at" in text or
"wake me" in text or
"get me up" in text or
"i need to wake up at" in text or
"make sure i wake up at" in text or
"start an alarm" in text or
"turn on an alarm" in text or
"schedule an alarm" in text or
"alarm at" in text or
"alarm for" in text or
"alarm" in text or
"along" in text or
"wait me up" in text
):
alarm_time = ask_ai.extract_alarm_time(text)
if alarm_time and alarm_time != "null":
other.alarm.set_alarm(alarm_time)
return f"Alarm set for {alarm_time}.", parsed
else:
return "Sorry, I couldn't understand the time for the alarm. Please try again.", parsed
elif text.startswith("play "):
song = text[5:].strip()
og = song
song = song + " lyrics"
songplayer.play_song(song)
return f"Now playing {og}.", parsed
else:
response = ask_ai.question(text)
return response, parsed
Thank you in advance for any help :^)
I am using this library to create a small home assistant like alexa. I record the text, process it with a small parsing function, and then it spits out the answer. I am using gTTS for that part and playdub to "say" it. However, after it creates the audio file I get an error that completely shuts down the whole program: BrokenPipeError. I used Claude and ChatGPT to try and fix as well as going through the documentation but no luck. Here is the attached code and error:
Error:
main.py:
speech/stt.py
speech/initparse.py
Thank you in advance for any help :^)