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: object

Functions: tts() stt()

  • TTS (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 with lang='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 espeak is supported

  • lang (str) – Not used (espeak default voice)

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).

timeout is 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). If None, it is not saved

  • timeout (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: object

Functions: 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

class openpibo.speech.SpeechToText(model_dir='/home/pi/.model/stt', num_threads=4)[source]

Bases: object

Functions: 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.onnx tokens.txt silero_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).

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 string

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_silence seconds after speech ends. After timeout seconds 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). If None, it is not saved

  • end_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: object

Functions: 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 (load reset ngram diff_ngram get_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()
# 아래의 모든 예제 이전에 위 코드를 먼저 사용합니다.
start_llm(port=50020)[source]

Starts the LLM service. (Chat mode)

example:

dialog.start_llm()
call_llm(prompt=None, system_prompt=None, temperature=0.8, max_tokens=100)[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)

Returns:

The generated text, or the full API response JSON.

stop_llm()[source]

Stops the LLM service.

example:

dialog.stop_llm()