How to use AI as a local translate engine on my Arch Linux.
install ollama.
You probably need install CUDA drivers for nvidia GPUs or ROCm drivers for AMD GPUs for better performance.
I'm using an AMD 7840HS computer (with the integrated 780M iGPU), and you can install it by following these steps:
- Install GPU drivers
$: sudo pacman -S amdvlk lib32-amdvlk
- install ROCm packages
$: sudo pacman -S rocm-hip-sdk rocm-opencl-sdk
- Install tools for monitor GPU usage (optional)
pacman -S radeontop
# OR
pacman -S nvtop
[Unit]
Description=Ollama Service
After=network-online.target
[Service]
Environment="HSA_OVERRIDE_GFX_VERSION=11.0.0"
Environment="OLLAMA_KEEP_ALIVE=-1"
ExecStart=/home/zw963/utils/llms/bin/ollama serve
Restart=always
RestartSec=3
[Install]
WantedBy=default.target
systemctl --user enable ollama systemctl --user start ollama
$: ollama run qwen2:7b
#!/usr/bin/env bash
model=qwen2
if [ $# == 0 ]; then
content=$(cat /proc/$$/fd/0);
else
content="$1"
fi
if echo "$content" |grep -P '[\p{Han}]' >/dev/null; then
tmpfile=/tmp/ai_translater.txt
ollama run $model "Translate Simplified Chinese into English: $content" | tee $tmpfile
cat $tmpfile |sed '/^[[:space:]]*$/d' |sed 's/[ \t]*$//'|tr -d '\n' |xclip -selection clipboard
else
ollama run $model "Translate English into Simplified Chinese: $content"
fi
Then, You can select a piece of text with your mouse, then use a shortcut key to translate it.
It can recognize Chinese and translate it to English, or vice versa.