speech · Voice, speech recognition, chat¶
Performs voice generation (TTS), speech recognition (STT) and conversation (LLM) on the device.
Class:
Speech
SpeechOnDevice
SpeechToText
Dialog
- class openpibo.speech.Speech[source]¶
Bases:
objectTTS (Text to Speech)
STT (Speech to Text) — processed on the device (
SpeechToText)
example:
from openpibo.speech import Speech speech = Speech() # 아래의 모든 예제 이전에 위 코드를 먼저 사용합니다.
- tts(text, filename='tts.wav', voice='espeak', lang='ko')[source]¶
TTS(Text to Speech)
Converts Text into Speech and saves it to a file.
Processed on the device with espeak. The server-based voices (main/boy/girl/man1/ woman1) have been removed. For a natural voice, use
SpeechOnDevice— an on-device ONNX model that detects the language automatically withlang='na'.example:
speech.tts('안녕하세요! 만나서 반가워요!', '/home/pi/tts.wav')
- Parameters:
text (str) – Sentence to convert
filename (str) – Path of the converted audio file (wav)
voice (str) – Only
espeakis supportedlang (str) – Not used (espeak default voice)
Raises
RuntimeErrorif espeak fails.
- stt(filename='stream.wav', timeout=5, verbose=True)[source]¶
STT(Speech to Text)
Listens with piBo’s microphone and converts speech to text when it ends. Processed on the device (
SpeechToText, automatic Korean/English detection).timeoutis the maximum wait time. It stops earlier when speech ends (after about 0.8 seconds of silence). The first call takes a few seconds longer to load the model.example:
text = speech.stt(timeout=5)
- Parameters:
filename (str) – Path to save the recorded audio (
wav). IfNone, it is not savedtimeout (int) – Maximum recording time (seconds)
verbose (bool) – Not used (an argument kept to match the shape of older code)
- Returns:
Recognized string. Empty string if nothing was said
- class openpibo.speech.SpeechOnDevice(onnx_dir: str = '/home/pi/.model/tts/assets/onnx', voice_dir: str = '/home/pi/.model/tts/assets/voice_styles', total_step: int = 5, speed: float = 1.05)[source]¶
Bases:
objectFunctions:
tts()TTS (Text to Speech)
example:
from openpibo.speech import SpeechOnDevice speech_od = SpeechOnDevice() # 아래의 모든 예제 이전에 위 코드를 먼저 사용합니다.
- tts(text: str, filename: str = 'tts.wav', voice: str = 'm1', lang: str = 'na') str[source]¶
TTS (Text to Speech) — converts text into an audio file.
example:
tts.tts(text='안녕하세요! 만나서 반가워요!', filename='/home/pi/tts.wav', voice='m1', lang='na')
- Parameters:
text (str) – Sentence to convert
filename (str) – Path of the audio file to save (.wav)
voice (str) – Voice type (m1~m5/f1~f5)
lang (str) – Language code (‘na’=auto/other, ‘ko’, ‘en’, ‘ja’ …)
- Returns str:
Path of the saved file
If
textis empty or only whitespace, nothing is synthesized and a 0.1-second silent wav is saved. CallingAudio.play(filename)right after plays no sound and raises no error.
- class openpibo.speech.SpeechToText(model_dir='/home/pi/.model/stt', num_threads=4)[source]¶
Bases:
objectFunctions:
listen()transcribe()transcribe_file()STT (Speech to Text) — processed on the device. No internet needed
The model is SenseVoiceSmall int8 (Korean, English, Chinese, Japanese, Cantonese, automatic language detection), and where speech starts and ends is found with silero VAD. Both run on sherpa-onnx. Model files are in
/home/pi/.model/stt(model.int8.onnxtokens.txtsilero_vad.onnx).On piBo (Pi 4, 2GB) it transcribes 5 seconds of speech in about 1.3 seconds, and uses about 240MB once the model is loaded.
example:
from openpibo.speech import SpeechToText stt = SpeechToText() # 아래의 모든 예제 이전에 위 코드를 먼저 사용합니다.
- SAMPLE_RATE = 16000¶
- transcribe(audio, sample_rate=16000)[source]¶
Converts sound (a number array) to text.
If longer than 25 seconds, it is split into speech segments, transcribed and joined (feeding it whole mixes up the text). If there is no speech at all (silence or noise), it returns an empty string
''.example:
text = stt.transcribe(audio, 16000)
- Parameters:
audio (numpy.ndarray) – Sound as float between -1 and 1. If two channels, they are averaged
sample_rate (int) – Sample rate (does not have to be 16000)
- Returns:
Recognized text (
''if there was no speech)
- transcribe_file(filename)[source]¶
Converts a sound file to text.
example:
text = stt.transcribe_file('/home/pi/myaudio/hello.wav')
- Parameters:
filename (str) – Sound file path (wav or other formats soundfile can read)
- Returns:
Recognized string
- listen(timeout=5, filename=None, end_silence=0.8)[source]¶
Listens with piBo’s microphone and converts speech to text when it ends.
Stops when it is silent for
end_silenceseconds after speech ends. Aftertimeoutseconds it stops even during speech.example:
text = stt.listen(timeout=5)
- Parameters:
timeout (int) – Maximum recording time (seconds)
filename (str) – Path to save the recorded audio (
wav, 16kHz mono). IfNone, it is not savedend_silence (float) – Silence time (seconds) after which speech is considered ended
- Returns:
Recognized string. Empty string if nothing was said
- class openpibo.speech.Dialog[source]¶
Bases:
objectFunctions:
set_llm_model()start_llm()call_llm()stop_llm()A class for conversation on piBo. It sends questions to an LLM (llama-server) running on the device and receives answers.
The n-gram chatbot (
loadresetngramdiff_ngramget_dialog) was removed in 260914v6. Its data (dialog.csv) was Korean-only so it could not be used in the English edition, and in the Korean edition it is replaced by the LLM.example:
from openpibo.speech import Dialog dialog = Dialog() # 아래의 모든 예제 이전에 위 코드를 먼저 사용합니다.
- LLM_MODELS = ('general', 'themaker-ko', 'themaker-en')¶
Chat models you can choose. The file is
/home/pi/.model/llm/<name>.gguf(a real file put in with the system image; its version is in.model/VERSION)
- LLM_DIR = '/home/pi/.model/llm'¶
- set_llm_model(model)[source]¶
Chooses the model to use before starting the chat server. Without it,
generalstarts. The choice is kept only in thisDialog. If the server is already running, it restarts with the chosen model.example:
dialog.set_llm_model('themaker-ko') dialog.start_llm()
- Parameters:
model (str) –
general(General — currently Gemma3 1B, Korean and English, no tools) ·themaker-ko(TheMaker Korean) ·themaker-en(TheMaker English). Both TheMaker models have tools (light, look, move), butcall_llmis text only — if the model answers with a tool call, that part is removed and only the text is returned- Raises:
ValueError – when the model name is not one of the three
FileNotFoundError – when that model file is not on the robot yet
- start_llm(port=50020, timeout=120)[source]¶
Starts the LLM service and waits until the model is loaded and can answer. (Chat mode)
example:
dialog.start_llm()
- Parameters:
port (int) – llama-server port to check for readiness. The service always listens on 50020, so keep the default
timeout (int) – Maximum waiting time (seconds)
- Returns:
Truewhen ready,Falseif not ready withintimeout
- call_llm(prompt=None, system_prompt=None, temperature=0.8, max_tokens=100, timeout=120)[source]¶
Calls the LLM server (OpenAI-compatible Chat Completions API).
- Example:
dialog.call_llm(prompt=”Hello”, system_prompt=”You are my smart assistant”)
- Parameters:
prompt (str) – The user’s input message.
system_prompt (str) – System prompt. (keeps the default if not given)
timeout (int) – Maximum time to wait for the answer (seconds). Raises an exception if exceeded
- Returns:
The generated text, or the full API response JSON.