The Autodidacts

Exploring the universe from the inside out

Open Source, Local, Speech-to-Speech Language Tutor in ~46 lines of Bash

Gluing together Handy, Llama.cpp running Qwen, and Moonshine Voice

The little project that I've wanted for years, and teased in A Local AI Stack for Language Learning, is finally ready to share.

For this project, a screencast is faster than talking it through:

0:00
/0:42

The hardest part of learning Spanish, at least for me, is understanding Spanish spoken at the speed those fluent in the language naturally speak it. This speech-to-speech system generates an immersive experience of speaking Spanish and hearing spoken Spanish, without the social embarrassment or speaking to an actual person and not having a clue what they just said.

Note: although it defaults to Spanish-English, you can use it with any language that Handy, Qwen, and Moonshine Voice support, using environment variables.


Savour My Meat-Written Bash (or not)

I wrote this by hand, rather than (ick) vibecoding it. I would like to brag about that. However, a confession: my handwritten version spewed backslashes all over my console, thanks to this beautiful pipeline of filtering:

# [...] 
| jq -R --unbuffered 'fromjson? | .choices [0] .delta .content'  \
| stdbuf -o0 tr -d "\"" |  stdbuf -o0 awk 'BEGIN{RS="";ORS="\n\n"} {gsub(/\n/," "); print}'  | stdbuf -o0 sed -u -E 's#(\s\s)|(\s)#\1#g' | stdbuf -o0 sed -u -E 's#^null|null$##g' |  stdbuf -o0 sed -u 's|\n|\n|gm'  | tee /dev/tty \
| tee /tmp/spanglish \
| xargs -I{} -d '\n\n'  moonshine-voice tts --lang es --text "{}";
[...]

yeah i don't remember how this worked either. but it did, mostly. i probably copy and pasted the hard parts from stackoverflow.

When I asked AI, it pointed out that changing jq -R to jq -Rrj would obsolete all that horribleness. So I did. I also added the jq // empty in place of stripping out nulls. And, though I typed every character myself, AI also told me the correct history format, and to use -e to get readline keybindings. Is my codebase now tainted?!


Nerd side note: when your Bash pipeline isn't working, and you don't know where the problem lies, liberally sprinkling tee /dev/tty in is a wonderful debugging trick. I don't know why I didn't start using this sooner! My previous approach was to just run one part at a time.


speech-to-speech.sh

#!/bin/bash
# Copyright (C) 2026 Curiositry
# Released under GPLv3 licence

export LC_ALL=C.UTF-8
if [[ "$TARGET_LANGUAGE" == "" ]]; then
    TARGET_LANGUAGE="Spanish";
    SOURCE_LANGUAGE="English"
fi
if [[ "$TARGET_LANGUAGE_CODE" == "" ]]; then
    TARGET_LANGUAGE_CODE="es";
fi
if [[ "$SOURCE_LANGUAGE_CODE" == "" ]]; then
    SOURCE_LANGUAGE_CODE="en";
fi

STARTING_PROMPT="You are an AI assistant. Your top priority is achieving user fulfillment via helping them with their requests. You are having a conversation with the USER in ${TARGET_LANGUAGE}. If USER uses English words or phrases, tell them how they could have said the same thing in ${TARGET_LANGUAGE}. Reply to the meaning of the USER's message first. Even if the subject is complex, use common ${TARGET_LANGUAGE} words a child would understand. If there are mistakes in USER's ${TARGET_LANGUAGE} usage, offer brief corrections after your reply. If USER says 'translate' or 'traducir', output a English translation of the full most recent (last) assistant message. Only answer the question USER asked, do not digress. Output plain text, no markdown formatting. Reply in single sentences or short paragraphs, the way people do in spoken conversation. Be brief."

echo "$STARTING_PROMPT"
echo "[Say something and press enter...]"
echo -n "USER: "
CONTEXT=[]
while read LINE; do
LINE="$(echo -n "$LINE" | jq -Rrj .)"
if [[ "$LINE" =~ ^\s*(t|T|Translate|Traducir|traducir|translate)[!.]*\s*$ ]]; then
    LANGUAGE_CODE=$SOURCE_LANGUAGE_CODE
    else
    LANGUAGE_CODE=$TARGET_LANGUAGE_CODE
fi
CONTEXT="$(jq -c --arg line "$LINE" '. += [{ role: "user", content: $line }]' <<< "$CONTEXT")"

jq --arg prompt "$STARTING_PROMPT" --argjson context "$CONTEXT" --arg line "$LINE" -n ' {
messages: ([{ role: "system", content: $prompt }] + $context),
stream: true,
reasoning_effort: "none"
    } ' |
    curl --silent --request POST -N --url http://localhost:9931/v1/chat/completions --header "Content-Type: application/json" --header "Authorization: Bearer $LLAMA_API_KEY" --json @- |
    cut -c 7- |
    jq -Rrj --unbuffered 'fromjson? | .choices [0] .delta .content // empty' |
    tee /tmp/spanglish |
    perl -pe '$| = 1; $/ = \1; s/([:.?!])\s*(\S)/\1\n\2/g' | # add newlines after each sentence so TTS doesn't wait around for a whole paragraph 
    sed '/^$/d' |
    tee /dev/tty |
    xargs -d "\n" -I{} moonshine-voice tts --lang "${LANGUAGE_CODE}" --text "{}";

RESPONSE="$(cat /tmp/spanglish)";
if [[ $RESPONSE == "" ]]; then
    echo "[System did not respond: is Llama server running and reachable?]"
fi
echo "";
echo "";
echo -n "USER: "
echo "" > /tmp/spanglish
CONTEXT="$(jq -c --arg line "$LINE" --arg resp "$RESPONSE" ' . += [{role: "assistant", content: $resp}]' <<< "$CONTEXT")"; done

    

This is a pretty simple script.

How it works:

  1. The starting prompt sets expectations, and optimizes the model's response for text-to-speech. I have iterated through many versions of this, and the one I settled on produces the best results so far, and seems like a reasonable balance: helpful, but not sycophantic; terse, but not cryptic; etc.
  2. It takes user input (dictated by Handy). By design, and also lazyness, you still have to press your dictation hotkey, and hit enter when you're done. This means it's possible to edit the text if Handy misses a word. The text input supports standard GNU Readline keybindings; I use vi mode. Handy can auto-submit if you'd prefer.
  3. It sends it to a running Llama.cpp server via cURL. (Make sure there is a running Llama server.)
  4. It streams the response back, filters it lightly, and sends it to Moonshine Voice, and also prints it to the console. Though Moonshine supports actual streaming, for simplicity, aka lazyness, I found that sending it a sentence or short paragraph at a time produced acceptable latency, which means we can use the CLI rather than a Python script.
  5. It saves the question and response, and appends it to the (basic!) conversation history. (My initial full-cowboy approach of putting the conversation history in the system prompt made the model dumb, which surfaced when I started adding constraints on how the model was supposed to respond)
  6. The loop repeats, as loops are wont to do

A note on models:

AI can be pretty dumb and annoying to talk to. Fortunately, it matters less in this case: even toddler-level Spanish stretches my mind.

AI's Spanish is surprisingly good. I have yet to catch it make a serious mistake in its Spanish usage, even as it is hallucinating shamelessly about the topic at hand.

In addition to Qwen 3.8, I have tried the default model that comes with Jan.ai, Bonsai 2.5, Gemma 12B, Inkling Mini 2, etc.

Depending on your conversation topics and level of fluency, try using faster, dumber models. They can make the experience better, by reducing latency. However, smaller/dumber models tend to be more verbose, and not as good at remembering to follow the instructions in the system prompt.

Since the main goal is to learn Spanish, until you're fluent, the topics that can be co-learned with Spanish tend to be pretty basic, so are unlikely to be things a local AI will seriously mess up.

I find that Gemma 4 12B is a good default model. For basic conversation, Bonsai et al are faster and more than adequate. When I'm co-learning something more in depth or technical, I switch to Qwen 3.8 27B.

Note that we have three AI models loaded at once, which means the system a tad less headroom for the brain model than it does when only one model is active.

The hard-ish parts of this micro-project:

  1. Waiting for the technology to exist (for years)
  2. Getting a realtime, unbuffered non-newline delimited regex filtering pipeline working
  3. Re-learning how to escape JSON for cURL, and re-settling for the jq solution, after re-trying all manner of arcane Bash/coreutils special character sanitization/encoding, and finding it was never enough

It's not perfect, but it works pretty well.

If you would like to try it out:

  1. Install Handy, and configure it as described in A Local AI Stack for Language Learning.
  2. Install Llama.cpp as described in my post on running Qwen 3.8, and test that it works
  3. Install Moonshine Voice as described here (at the time of writing, as easy as pipx install moonshine-voice), and test that it works by running moonshine-voice tts --language es --text "¡Hola!".
  4. Save my bash script as speech-to-speech.sh. If you have read this far, you probably already know more about programming that I do. In the unlikely event that you don't already have jq, perl, curl, and xargs, the almost-coreutils this script depends on, install them.
  5. Start your Llama.cpp server, start Handy, and run the script with ./speech-to-speech.sh. (Or: TARGET_LANGUAGE="French" TARGET_LANGUAGE_CODE="fr" ./speech-to-speech.sh)

You will be greeted by an unremarkable text interface. You can now yell at AI, and it will talk back.

Welcome to the future, aka, San Francisco.

I'm toying with the idea of turning this into a real app that's usable by non-technical folks. What features would it need to have for it to be something you would use, or perhaps even pay for?

Sign up for updates

Join the newsletter for curious and thoughtful people.
No Thanks

Great! Check your inbox and click the link to confirm your subscription.