matatu blog

Brian writes about computing

June 12, 2023

Traveling abroad? Need help learning the language? Wondering how hard it can be to write an app to help translate phrases? Wonder no more, because:

Let's Build a Translation App

using an AI language model and 20 lines of code

Setup

You will need:

  • termux , termux-api , and termux-widget .
  • an account and api token from huggingface
  • a little patience for the setup, but it's worth it
  • Step 1

    Compile the Raku programming language on your Android. There are excellent instructions here, but when I did this I ran into a little issue. This step was easier to do by plugging a real keyboard into the device.

    I like using the Raku programming language for this task for a variety of reasons. One is that the language is so concise I can actually write the program on the phone. Really. Another is that it has great built-in features for interacting with other programs, with functions like shell and the quoting construct qx.

    Step 2

    Write a program like the one to the right. This program basically works like this:

  • Use termux-dialog radio to choose the direction of translation (English to Italian or vice versa). This determines the name of the model.
  • Decide whether we want speech input or keyboard input. Because the speech input only supports English, I use speech if and only if we are starting from English.
  • Send a request using curl
  • Show the result and use termux-dialog confirm to get a yes or no as to whether we should send the request again. This is because the first request might load the model into a cache, in which case you will have to retry it in 20 seconds. You can see this in action by trying it out at the huggingface online interface .
  • When your program is ready, run it with raku ciao.raku and verify that it works. Oh also note that your api token should go into a file called "hugging".

    Step 3

    Make a shell script in ~/.shortcuts/tasks/ciao -- this is a wrapper that calls the above program and is used by termux-widget. This step allows you to add a button to your home screen to call the "app". This can also set up the environment and change to your working directory.

    #!/bin/sh
    exec 2>&1 >> $HOME/ciao.log
    . /data/data/com.termux/files/home/.bashrc
    cd /data/data/com.termux/files/home/ciao
    raku ./ciao.raku
          

    spoiler: here is the program, as seen from an editor on the phone

    That's it! Enjoy your travels! And happy language hacking!

    Below is the program in a wider format, and as text for your copy-paste convenience.

        
    #!/usr/bin/env raku
    
    use JSON::Fast;
    
    my $HF_API_TOKEN="hugging".IO.slurp.trim;
    
    sub j($txt) { from-json($txt) }
    sub jt($txt) { j($txt)<text> }
    
    my $model = jt qx/termux-dialog radio -v en-it,it-en/;
    my $mode is default("") = "speech" if $model ~~ /en '-'/;
    
    my $txt = jt q:w:s:x[termux-dialog $mode];
    
    my $url = <<api-inference.huggingface.co models Helsinki-NLP "opus-mt-$model">>.join: "/";
    
    loop {
      my $proc = shell Q:s:to/SH/, :out;
        curl -s https://$url \
         -X POST \
         -d '{"inputs": "$txt" }' \
         -H "Authorization: Bearer $HF_API_TOKEN"
      SH
    
      my $o = j $proc.out.slurp(:close);
      my $tr = $o[0]<translation_text> || ~$o;
      my $out = "$txt -> $model -> $tr";
      jt qqx[termux-dialog confirm -i "$out"] andthen do { last if $_ eq 'yes' }
    }