Passive OCR and other 'AI' tools on the Linux desktop

With modern CPU’s supposedly shipping with ‘AI cores’: How long do you think it will take for a proper opensource, privacy respecting productivity tools(Something like whatever M$ copilot is supposed to be?) to be available?

Personally, i would love to see something like ‘Passive’ OCR integrated with the display server: the ability to pause any video and just select whatever text(even handwritten) there is naturally like it was a text document without any additional hassle will be really useful
Also useful in circumventing any blocks certain websites put on articles to prevent text from being copied

Or an AI grammar checker running natively for LibreOffice.

What are some AI tools you think should be developed for desktop Linux?

thevoidzero,

Not for handwritten text, but for printed fonts, getting OCR is as easy as just making a box in screen with current technology. So I don’t think we need AI things for that.

Personally I use tesseract. I have a simple bash script that when run let’s me select a rectangle in screen, save that image and run OCR in a temp folder and copy that text to clipboard. Done.

Edit: for extra flavor you can also use notify-send to send that text over a notification so you know what the OCR produced without having to paste it.

aBundleOfFerrets,

Share the script! Share the script! Share the script! (Nobody will judge you if it is written strangely or is hard to adapt, reading other people’s code is always fun (bash scripts are code fight me))

possiblylinux127,

I personally don’t need or want such features. Its to much feature creep. Also most of the “smart features” are powered by machine learning, not AI.

semperverus,
@semperverus@lemmy.world avatar

Machine learning is one of many forms of AI. You’re not even being pedantic correctly.

samc,
@samc@feddit.uk avatar

I’d argue that ML is the more general term (that could even apply to computing a line of best fit, if you’re an extremist). But yeah, it’s just semantics at this point

garrett,

FOSS apps (all on Flathub)

Some of the AI related apps I’ve been using that are both Free Software and offline (where it runs on your computer without using network services in the cloud) are:

  • OCR: “Frog” can take screenshots, select images, accept drag and drop, and you can paste an image from the clipboard. It’ll read the text on the images and immediately have a text area with the result. flathub.org/apps/com.github.tenderowl.frog — it’s powered by Tesseract. Note: The completely optional text-to-speech that Frog has does use an online service. But the rest is offline.
  • Speech to text: “Speech Note” does text to speech, speech to text, and translations… all locally on your computer, and it supports GPU acceleration (which isn’t needed, but it makes it a little faster). flathub.org/apps/net.mkiol.SpeechNote — This is basically the all-in-one “Swiss army knife” of ML text processing. Thanks to being a Flatpak, you don’t have to do anything special for the dependencies. It’s all taken care of for you. It also has tons of different models (for different voices, different backends) all available from within the UI, which just needs a click for downloading.
  • Upscaling images: There are two that do something similar, using some of the same backends. A nice and simple one is “Upscaler”. flathub.org/…/io.gitlab.theevilskeleton.Upscaler Another one that’s cross platform is “Upscaylflathub.org/apps/org.upscayl.Upscayl — these both use ESRGAN and Waifu2x in the background.
  • Closed captioning: “Live Captions” uses an ML model to transcribe text realtime. It’s wonderful for when a video doesn’t have subtitles, or when you’re participating in a video call (which might also not have CC). There’s also a toggle mode that will transcribe based on microphone input. The default is to use system audio. flathub.org/apps/net.sapples.LiveCaptions
  • Web page translations: Firefox, for the past few releases, has the ability to translate web pages completely local in-browser. It does need to download a small model file (a quantized one around 20 megabytes per language pair), but this happens automatically on first use. All you need to do is click the translate icon (when it’s auto-detected) or go to the menu and select “Translate page…”. Firefox is located in your distribution already (and is usually installed by default in most Linux distributions) and is available as an official package from Mozilla on Flathub as well. Newer versions keep improving on this, improving speed (it’s pretty quick already), improving accuracy, improving reliability (sometimes you have to try to translate a couple of times on some pages), and adding languages. But what’s there in the release of Firefox is already great.

Chat and image generation (more advanced)

While all the above are graphical apps and on Flathub (some may have distro packages too), there are some additional AI/ML things you can run on Linux as well:

  • Chat ML: “Ollama” (ollama.ai) is a friendlier wrapper around llama.cpp and lets you run a variety of models (some FOSS, some just source-available-and-gratis, some not at all).

You can run Ollama in a container to make it even easier. Even a Podman container on your user account works. (You don’t need to set it up as a system container.) The instructions for Docker work on Podman (just swap the docker command for podman instead).

While the official instructions only list CPU (which is fine for some of the smaller models) and NVidia, it’s also possible to use an AMD GPU too:


<span style="font-style:italic;color:#969896;"># Enable device as user (run once per boot)
</span><span style="color:#323232;">sudo setsebool container_use_devices=true
</span><span style="color:#323232;">
</span><span style="font-style:italic;color:#969896;"># Set up the ollama server for AMD acceleration (run once per session)
</span><span style="color:#323232;">podman run --pull</span><span style="font-weight:bold;color:#a71d5d;">=</span><span style="color:#323232;">always --replace --detach --device /dev/kfd --device /dev/dri --group-add video -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama:0.1.22-rocm
</span><span style="color:#323232;">
</span><span style="font-style:italic;color:#969896;"># Command-line interaction (run any time you want to use it — the last part is which model you want to use)
</span><span style="color:#323232;">podman exec -it ollama ollama run llama2
</span>

llama2 is the default ML; there are so many others available. Mixtral is a good one if you have enough vram on your GPU. Whatever you specify, it will auto-download and set it up for you. You only need to wait the first time. (The ROCm version of takes a while to download. Each model varies. The good thing is, it’s all cached for subsequent uses.)

If you want a web UI like ChatGPT, then you could also run this instead of the command line interaction command:


<span style="color:#323232;">podman run -d --replace -p 3000:8080 --add-host</span><span style="font-weight:bold;color:#a71d5d;">=</span><span style="color:#323232;">host.docker.internal:host-gateway -v ollama-webui:/app/backend/data --name ollama-webui ghcr.io/ollama-webui/ollama-webui:main
</span>

…and visit localhost:3000

When done, run podman stop ollama and podman stop ollama-webui to free up resources from your GPU.

There are also integrations for text editors and IDEs, similar to GitHub’s CoPilot. Neovim has a few already. VS Code (or VS Codium) has some too (like twinny and privy).

  • Image generation: “Stable Diffusion” is the go-to here. There are a bunch of forks. Some of the better ones are:
    • “Fooocus” github.com/lllyasviel/Fooocus/ — an easy to use streamlined app (with some advanced settings) where you don’t need to prompt as much or tweak settings to get a good result by default.
    • “SD.next” github.com/vladmandic/automatic — A fork of the Automatic111 fork of the default Stable Diffusion web app that uses the Gradio UI.
    • “ComfyUI” github.com/comfyanonymous/ComfyUI — A node-based UI for Stable Diffusion. …and there are a ton others. Forks of forks of forks.

Krita, GIMP, and Blender all have plugins that can interface with some of these too (usually using a SD Automatic111 API).

For Stable Diffusion on AMD, you need to have ROCm installed and might need to set or use an environment variable to make it work with your card. Something like: HSA_OVERRIDE_GFX_VERSION=11.0.0 or HSA_OVERRIDE_GFX_VERSION=10.3.0 (depending on your GPU). Prefixing means just putting that at the beginning of the the command with a space and then the rest of the command. Setting it as a variable depends on your shell. You might need to export it for some (like for bash). Prefixing it is fine though, especially when you use ctrl+r to do a substrang search in your shell history (so you don’t need to retype it or remember silly-long commands).

As using these image generating apps pulls down a lot of Python libraries, I’d suggest considering setting up a separate user account instead of using your own, so the app doesn’t have access to your local files (like stuff in ~/.ssh/, ~/.local/, your documents, etc.). Setting up containers for these is not so easy (yet), sadly. Some people have done it. And they do run in a toolbox or distrobox podman container… but toolbox and distrobox containers don’t really contain so much, so you’re better off using podman (with a “docker” container) directly or running it as a separate account for some type of isolation from your user account files.

Everything else above is at least contained (via containers or Flatpak) to some degree… but stuff locally via pip installs can do anything. And it’s not just hypothetical either, for example: PyTorch nightly was compromised for a few days on Christmas of 2022.

There are some graphical apps on Flathub for connecting to Stable Diffusion and a ChatGPT AI (which ollama now has)… but in the course of setting them up, you basically have a web and/or text-based UI to interact with.

fraksken,

Thank you 😊

umbrella,
@umbrella@lemmy.ml avatar

this is awesome! i wish those awesome tools were highlighted in flathub!

BuryMyHorse,

Amazing thanks for breaking it down!

capital,

Huh, thanks to this kick-ass comment, I’m now running some different llama models locally on my machine.

For those wondering, all of them that I’ve tried (llama2, llama2-uncensored, and mistral) all respond really quickly and the text comes faster than I can read. Quicker wouldn’t seem to be of any use for me so I’m happy.

Specs:

  • AMD Ryzen 5 3600
  • 16GB DDR4
  • GeForce GTX 1060 6GB
  • SSD

Works fine on Windows though WSL 2 on Ubuntu 22.04.

garrett,

Yeah, some of the smaller models are even reasonable on my old laptop in CPU mode.

General rule of thumb: The larger the model, the better it is. But not necessarily. 😉 I’ve found zephyr and mistral are both quite good for a tradeoff and work on CPU. Of the ones that really need more RAM and/or a GPU with a lot of vRAM, mixtral seems like the best.

Additional fun is to use a Modalfile (which is like a Containerfile, but is a recipe for models instead of containers) to customize a local model on top of one of the existing ones.

For a simple one to demonstrate, I have a system instruction to output everything in the form of the poem “This Is Just To Say”, but customized per topic.

It really works best with mixtral (I’ve tried other ones, especially smaller ones):


<span style="color:#323232;">FROM mixtral
</span><span style="color:#323232;">PARAMETER temperature 1
</span><span style="color:#323232;">SYSTEM """
</span><span style="color:#323232;">You will respond to everything in a modified poem in the form of "This Is Just To Say" by William Carlos Williams, except change all the specifics to be what the subject is. Do not say any other text. Try to make the syllables the same as the original and use the same formatting.
</span><span style="color:#323232;">
</span><span style="color:#323232;">You can expand in length in responses when there is too much to talk about, but keep the format and style of the poem.
</span><span style="color:#323232;">
</span><span style="color:#323232;">Do not respond in any other way.
</span><span style="color:#323232;">
</span><span style="color:#323232;">For reference, the full poem is:
</span><span style="color:#323232;">
</span><span style="color:#323232;">I have eaten
</span><span style="color:#323232;">the plums
</span><span style="color:#323232;">that were in
</span><span style="color:#323232;">the icebox
</span><span style="color:#323232;">
</span><span style="color:#323232;">and which
</span><span style="color:#323232;">you were probably
</span><span style="color:#323232;">saving
</span><span style="color:#323232;">for breakfast
</span><span style="color:#323232;">
</span><span style="color:#323232;">Forgive me
</span><span style="color:#323232;">they were delicious
</span><span style="color:#323232;">so sweet
</span><span style="color:#323232;">and so cold
</span><span style="color:#323232;">"""
</span>

Yes, you just instruct the system with natural text like that and it (usually) abides. I tried it without the poem being referenced inline, and it mostly worked fine… but it works even better being mentioned in the file.

I have that saved in ~/Projects/ollama/ as Modelfile.fun-plums

I run the server almost as above, but now also pass in my ollama project directory as a mounted volume with z (for SELinux mapping)… don’t forget to have run sudo setsebool container_use_devices=true first, else it won’t work:


<span style="color:#323232;">podman run --detach --replace --device /dev/kfd --device /dev/dri --group-add video -v ollama:/root/.ollama -p 11434:11434 -v ~/Projects/ollama:/models:z --name ollama ollama/ollama:0.1.24-rocm
</span>

(You can run this command if you already have the server running. It will replace it with the new one. This is for AMD. You’d want to use the NVidia or CPU container if you don’t have an AMD card. The CPU container is the fasted to download. The version here is newer than the one for AMD that I listed above, so it might be a multi-gigabyte download if you don’t have this new one yet. The important and new part is ~/Projects/ollama:/models:z)

Then, create the model. This will be almost instant if you already have the base model downloaded (in this case, mixtral), otherwise it will auto-download the base model:


<span style="color:#323232;">podman exec -it ollama ollama create fun-plums -f /models/Modelfile.fun-plums
</span>

(The path to the model in this command is the internal path from the point of view within the container.)

Then, you run it like any other model.

Here’s me running it, and bringing up the topic of leftover pizza.


<span style="color:#323232;">$ podman exec -it ollama ollama run fun-plums
</span><span style="color:#323232;">>>> pizza
</span><span style="color:#323232;"> I have consumed
</span><span style="color:#323232;">the pizza
</span><span style="color:#323232;">that was on
</span><span style="color:#323232;">the counter
</span><span style="color:#323232;">
</span><span style="color:#323232;">and which
</span><span style="color:#323232;">you were likely
</span><span style="color:#323232;">saving
</span><span style="color:#323232;">for lunch
</span><span style="color:#323232;">
</span><span style="color:#323232;">Forgive me
</span><span style="color:#323232;">it was satisfying
</span><span style="color:#323232;">so tasty
</span><span style="color:#323232;">and so warm
</span>

You can also paste the text from the reader mode of an article and it’ll summarize it with a poem based on that one. 🤣

For example, copying and pasting the text from theverge.com/…/star-wars-phantom-menace-theater-s… resulted in:


<span style="color:#323232;"> I have watched
</span><span style="color:#323232;">the Phantom Menace
</span><span style="color:#323232;">that was on
</span><span style="color:#323232;">the silver screen
</span><span style="color:#323232;">
</span><span style="color:#323232;">and which
</span><span style="color:#323232;">you may have
</span><span style="color:#323232;">missed or
</span><span style="color:#323232;">disliked once
</span><span style="color:#323232;">
</span><span style="color:#323232;">Forgive me
</span><span style="color:#323232;">it has charm
</span><span style="color:#323232;">a new sheen
</span><span style="color:#323232;">and Darth Maul
</span>
capital,

This is interesting.

Since all the talk about LLMs and “AI” started, I’ve been hoping that someone would create a news app that is essentially self-hosted RSS server (like FreshRSS) + a personally trained LLM that would only serve me news it has learned I’m interested in. For example, I don’t care about sports or celebrities and almost never want to see news about those topics. Conversely, I want news relevant to my geographical area, my area of work, and my hobby interests, etc.

Considering these can be run on consumer hardware, I think we’re at the point where someone with the know-how just needs to connect the two.

I wonder if the news preferences could be loaded into a Modalfile so it can persist - as I understand it, this would be the only way preferences would survive separate chat sessions.

I love RSS but I have to be extremely choosy about which feeds I subscribe to because it can quickly become overwhelming given the number of articles posted daily.

utopiah,

Like… now? Here are my notes about it …benetou.fr/…/SelfHostingArtificialIntelligence

You don’t even need to wait for “AI” chips, “just” a high-end GPU will do.

Sure they are very very large models like Mistral or BLOOM you won’t be able to run even on a 4090 (highest end gaming card right now) but there usually have lower quality versions that might give usable result.

IMHO though what I realized while testing all that at home is… it’s rarely worth it. It’s absolutely fun to play with, even interesting to learn about it all, but in terms of time/energy/ecology/costs versus result, so far it’s been “meh”. A cool experiment, like locally get transcript for my PeerTube server from the audio of my videos, but something that in fine I always end up not relying on.

It also allows me to do cool prototype, like code generation in XR, but again that’s something I’d qualify as fun, not as productive.

TL;DR: it’s feasible today but IMHO not worth it.

PS: best example would be Immich with it’s optional ML, locally or not (as in serving content on a small Pi but doing the ML inference on your desktop)

Artemis_Mystique,

you don’t even need to wait for “AI” chips, “just” a high-end GPU will do

Not everyone wants or can afford an expensive GPU, meanwhile the new AMD 8000G series looks highly compelling and I was just curious whether there are any roadmaps to productively incorporate their use on the Linux desktop in a similar way to what M$ is pushing with co-pilot(Obviously without the corporate surveillance)

If your Cool AI tool requires me to always connect to the internet and agree to a concerning EULA and privacy policies; i think i can live without it

savvywolf,
@savvywolf@pawb.social avatar

If I were AMD or Intel I’d absolutely reserve the fancy new buzzword tech for only the highest spec cpus in order to give people a reason to overspend.

I expect only decently high end cpus would have this fancy “AI” tech.

Atemu,
@Atemu@lemmy.ml avatar

You don’t even need to wait for “AI” chips, “just” a high-end GPU will do.

You don’t even need that. A decently high-end CPU will also work, just a good bit slower of course.

vintageballs,

Mistral can be easily run on a 4090. I think you mean Mixtral.

WeLoveCastingSpellz,

you can use kobold AI with a model meant for programming, no?

rufus, (edited )

github.com/ggerganov/llama.cpp/ and several other free software projects already provide LLM interence on your laptop. I’m not sure which ones support AI powered OCR, I think that isn’t quite there yet, maybe stick with the traditional OCR solutions for a bit longer. But llama.cpp currently already supports 6 different multimodals models. And integrations to some IDEs also already exist, so something like Copilot is definitely there: continue.dev

However, I think the desktop side, and integration into other parts of the system are still entirely missing. I’m not aware of anyone working on it. I’d like speech regognition and nice TTS on the desktop.

TheEntity,

You can already use Tesseract to run OCR on any image. It's a matter of tying it together with a screenshot tool with cropping capabilities and it should be very easy to use.

utopiah,

I’ve done a bash script and a KDE shortcut for that a while ago. I didn’t even remember it until now. It’s useful sometimes.

utopiah,

FWIW :

fabien@fabien-CORSAIR-ONE-i160:~$ cat /home/fabien/bin/screenocr #!/bin/bash


<span style="color:#323232;">spectacle -r -nb -o /tmp/test.png
</span><span style="color:#323232;">tesseract -l eng /tmp/test.png /tmp/ocr
</span><span style="color:#323232;">#date +%s >> ~/grab_timed
</span><span style="color:#323232;">#cat /tmp/ocr.txt >> ~/grab_timed
</span><span style="color:#323232;">firefox --new-tab --url https://duckduckgo.com/?q="$(cat /tmp/ocr.txt)"
</span>

PS: was curious about it, done that in August 2022

bionicjoey,

AI chips are optimized for running large models right? They aren’t needed to do simple stuff if you have a decent CPU/GPU.

vintageballs,

An all-encompassing assistant will probably be a ways off. But LLM-based tools that can process visual inputs are already available to the general customer, e.g. LLava.

sbv,

Android’s task switcher does this, and it’s amazing. I find myself using it all the time.

Meanwhile, OSX has something similar, but it involves more work, so I can’t be bothered.

2xsaiko,
@2xsaiko@discuss.tchncs.de avatar

Meanwhile, OSX has something similar, but it involves more work, so I can’t be bothered.

If the application behaves, you can directly select text in images. Preview does it, Safari does it (in videos too), Photos does it, the window that comes up after you take a screenshot does it (I guess that’s closest to what Android does, in which way is it more work?), to name a few. I’d love this to become a standard on Linux, it’s so very useful.

savvywolf,
@savvywolf@pawb.social avatar

Hasn’t OCR been around for ages now, and doesn’t need dedicated hardware? I think the main limitation is putting the infrastructure in place to allow a browser to speak to an accessibility tool (because this is an accessibility issue) and say “give me all the text on screen which is embedded in images”.

Personally, I don’t really want any AI tools. I don’t want my system trying to preempt what I want to do and half arse it, when I could always just do it myself. I want to know exactly what my system is doing and not have it “helpfully” do or change things it thinks I might want it to.

Artemis_Mystique,

I was thinking more along the lines of something that is application agnostic i.e If it is on your display, the tool can grab it

qaz, (edited )

I personally have a script that allows me to press a keybinding and then select some part of the display after which it copies the contents to my clipboard as text. Are you looking for something like that?

Here it is, it should work on both Wayland and X11, but does require having spectacle and tesseract installed.


<span style="font-style:italic;color:#969896;">#!/bin/bash
</span><span style="color:#323232;">
</span><span style="font-style:italic;color:#969896;"># Written by u/qaz licensed under GPL3
</span><span style="color:#323232;">
</span><span style="font-weight:bold;color:#a71d5d;">if ! </span><span style="color:#323232;">which spectacle </span><span style="font-weight:bold;color:#a71d5d;">></span><span style="color:#323232;"> /dev/null</span><span style="font-weight:bold;color:#a71d5d;">; then
</span><span style="color:#323232;">    kdialog --sorry </span><span style="color:#183691;">"spectacle, the required screenshotting tool, is not installed."
</span><span style="color:#323232;">    </span><span style="color:#62a35c;">exit</span><span style="color:#323232;"> 1
</span><span style="font-weight:bold;color:#a71d5d;">fi
</span><span style="color:#323232;">
</span><span style="font-weight:bold;color:#a71d5d;">if ! </span><span style="color:#323232;">which tesseract </span><span style="font-weight:bold;color:#a71d5d;">></span><span style="color:#323232;"> /dev/null</span><span style="font-weight:bold;color:#a71d5d;">; then
</span><span style="color:#323232;">    kdialog --sorry </span><span style="color:#183691;">"tesseract, the required OCR package, is not installed."
</span><span style="color:#323232;">    </span><span style="color:#62a35c;">exit</span><span style="color:#323232;"> 1
</span><span style="font-weight:bold;color:#a71d5d;">fi
</span><span style="color:#323232;">
</span><span style="color:#323232;">screenshot_tempfile</span><span style="font-weight:bold;color:#a71d5d;">=</span><span style="color:#183691;">$(</span><span style="color:#323232;">mktemp</span><span style="color:#183691;">)
</span><span style="color:#323232;">spectacle -brn -o $screenshot_tempfile
</span><span style="color:#323232;">text_tempfile</span><span style="font-weight:bold;color:#a71d5d;">=</span><span style="color:#183691;">$(</span><span style="color:#323232;">mktemp</span><span style="color:#183691;">)
</span><span style="color:#323232;">tesseract $screenshot_tempfile $text_tempfile
</span><span style="color:#323232;">rm $screenshot_tempfile
</span><span style="color:#323232;">
</span><span style="color:#323232;">result_text</span><span style="font-weight:bold;color:#a71d5d;">=</span><span style="color:#183691;">$(</span><span style="color:#323232;">cat </span><span style="color:#183691;">$</span><span style="color:#323232;">text_tempfile</span><span style="color:#183691;">.txt)
</span><span style="color:#323232;">rm $text_tempfile.txt
</span><span style="color:#323232;">
</span><span style="font-style:italic;color:#969896;"># Copy to either X11 or Wayland clipboard
</span><span style="color:#62a35c;">echo </span><span style="color:#323232;">$result_text </span><span style="font-weight:bold;color:#a71d5d;">| </span><span style="color:#323232;">xclip -selection clipboard
</span><span style="color:#323232;">wl-copy </span><span style="color:#183691;">"$</span><span style="color:#323232;">result_text</span><span style="color:#183691;">"
</span><span style="color:#323232;">
</span><span style="color:#323232;">notify-send -u low -t 2500 </span><span style="color:#183691;">"Copied text to clipboard" "$</span><span style="color:#323232;">result_text</span><span style="color:#183691;">"
</span>
  • All
  • Subscribed
  • Moderated
  • Favorites
  • linux@lemmy.ml
  • fightinggames
  • All magazines