Build a free writing assistant with Python and Ollama

Patrick Loeber · Intermediate ·🧠 Large Language Models ·2y ago

Key Takeaways

Build a free writing assistant with Python and Ollama, utilizing Mistral 7B and implementing hotkey listeners and keyboard controllers

Full Transcript

hi everyone I'm Patrick and welcome back on my channel in this tutorial we build a typing assistant that lets you fix typos with a large language model that's running locally on your machine so this is going to be a free typing assistant so here I can select for example multiple lines with multiple typos and now I press a shortcut and watch what happens it fixes all the typos in an instant how cool is that and now I can for example type super fast without caring about the text python is so cool and I don't even have to select the line I can press another shortcut and now Watch What Happens python is so cool so this is the application we're going to build for this we use olama and you also learn about two different libraries that lets you control the keyboard so let's get started so before we get started I want to mention this tweet that inspired this project from this user Boris he said how to 5ix your typing speed and be in a flow State while you type and then he uses a custom GPT for this and Andre karpathy even retweeted this saying GPT is surprisingly good at correcting minor typos so you can write really really fast ignore the mistakes and it comes out just fine so I thought we don't have to use jet GPT for this we can build our own free system with olama and about 100 lines of python code so let's do this so let's start a new project and here we start with a new vertual environment so let's create a new virtual environment and then we want to activate this and now we want to install a few libraries the first one is pi input this Library allows you to control and monitor input devices and we want to use this to listen to hotkey so basically for example whenever I press F9 it should fix the current line so for this we going to install this by saying pip install Pi input and then we can go to handling the keyboard so this is the official documentation and if you scroll down then here you find this code snippet about global hot keys so let's grab everything here and then let's open our editor and let's create a new file main. pi and let's insert this so here we import the keyboard and then here we can register our Global hot keys and then activate a function or call a function whenever this is pressed and then this is run endlessly in a new threat so here we want to Define our own um hot keys and you can for example use combinations like control alt and H I want to do F9 and F10 so for this we can um say from PI input. keyboard input Port key and then we can print key. F9 Dov value and also key. F10 Dov value and then let's remove this for a second and save this and now run this and by the way the first time you run this you might get asked to give this app permission so let's say Python main.py and now we get the values for F9 and F10 so let's insert this again and here we want to say and this is 101 and this is 109 so only on F9 and on F10 and then we can call this for example on F9 and on F10 and change the name here on F9 and then on F10 and now um let's also change the print text first we only print this on F9 pressed on F10 pressed and now let's save this and let's um open our terminal again and run Python main.py and now you will notice this will not return back here in your terminal so now this will run endlessly until you hit control c um and now if I hit F9 then it prints F10 so it also print something else because I have the terminal selected but for example if I select this window here I can still press F10 um and F9 and it will work so this is the first step so now we have our hotkey listener now let's define two functions the first one we call fix current line and the second one uh we call fix the selection and then we call this so on F9 we want to fix the fix the current line and on F10 we want to fix the selection so let's start with the fix selection so how do we do this uh we do this in five steps so Step One is we copy everything to the clipboard so copy to clip board then step two is um get the text from the clipboard then step three is we fix the text somehow with our large language model and the fourth step is we copy back to the clipboard and then we have the last step so now we simply have to insert the text again so basically this is contrl C and this is contrl V so to copy the text to the clipboard we can also use the library um the pi input library and we need this snippet so let's copy and paste this from PI input keyboard import key and controller then we set up a controller I actually want to call this controller and then we can for example do this with keyboard pressed and then the first key and then the second key so let's let's copy this one here so with keyboard and I call this controller so with controller pressed and then here I need um command I'm not sure if there is a control maybe on Windows you need control um I'm on a Mac so I need command and then we want to um press C so we could do control uh controller press a controller press uh release release a so here we have press c um but there's also a utility function we can just say controller T C so now we have everything in the clipboard now we need to get the text from the clipboard and for this we need another short uh small library that is called Piper clip so this is super simple so let's also install this so let's stop the script and say pip install pip per clip and then here you see all the codes that you need so we import Piper clip and then we can say Piper clip um paste and this will give us the text so here we say text equals um Piper clip paste and then we can also already um copy the second one when we want to copy this back so later we want to um copy this back so let's comment this out for now um and then one thing so while testing I noticed that I also need a very short de delay here so we import time and then here we say um time do sleep for .1 seconds otherwise um yeah I got some issues with these combinations so maybe you don't even need this um but let's do this here so now we have this in memory so let's actually try this out so let's again run the file and now let's go back and let's select this line and now if I hit F10 um actually nothing happens sorry so we didn't do anything so here let's print the text first so let's again save this and let's run this and now let's select this and now let's hit f 10 and if we go back um we see python is so cool so it had exactly the selection so now this is working so now let's continue here so we want to fix the text so let's define another function define fix text which gets the text as an input and and to test this first let's return the Reversed text like this so here we can say fixed text equals the equals fix text so here we call this function with the text as an input then we want to copy this back to the clipboard so for this we um already have this so now we say Piper clip copy and then we copy the fixed text then um here again I want to say time do sleep1 seconds and now we want to copy this back so here we say with controller so basically here we want to say contrl V like this and let's save this and we don't even need to print this anymore more um so let's again run our script and then again select this and now hit um F10 again and I think we get an arror ah yeah okay so what do we do um fix oh here is the typo fixed text so let's run it again sorry about that and now let's select this and say f 10 and You See It reversed the string and inserted it here so um let's stop this and now this is working so now the only functionality we need is um to fix the text and also to select the current line so let's implement the fix current line one so here basically we have to select the current line and on Mac there is a shortcut command shift and the left Arrow key so for example if I go into any application and press command shift and left then it selects the current line so this is exactly what we need and we can easily do this combination so by saying controller and then press T do command so again maybe on Windows you need control here and then let's copy and paste this command and then shift and then left and then of course we also want to release everything again so let's copy and paste this and then instead of press we say release and then release all the three keys again so now we have the current line selected and when we have our selection we can simply call fix selection so now we have two um functionalities we can fix fix the current line and we can fix the current selection so now only thing left to do is to fix the text and to fix the text we're going to use a large language model and that's running locally on our machine and one of the best Frameworks for this is olama so I'm not going into detail here you find plenty of other very good tutorials on YouTube but olama is crossplatform and it's lets you run a lot of different large language models with only one command so just download this and install this and then you find different models and I want to use the mistal 7b1 feel free to use a different one as well and then you also find different tags and I recommend doing the or going with the instruct one which is fine tuned to follow instructions and you can even choose a quantized version which comes with a trade-off that it's not as accurate but faster and for this task the quantized version should work well enough so I want to choose this one the instruct version2 Q4 KS so copy this in your terminal and the first time it will download the models I already have it so I can now give it a request why is the sky blue or a question here and then it will stream the response back so now we have olama running on our machine and what's really cool is that they have a rest API out of the box so we can send a post request let me uh quickly copy this for a moment and modify this and we want to use um this model name that we just downloaded so the model is this the prompt is this and then we can also say we don't want to stream the response we want to get it in one um action and then copy this and see if it works and it's working so here we get the response with the response key the color of the sky appears blue so now we have olama running on our machine so now we need to basically just send the post request with python so for this let's install a library to send uh requests so you can either say pip install requests or here I want to use httpx and by the way I have a tutorial about this on my channel as well so feel free to check this out and now let's define this globally so the olama end point equals and now this is the end point where the model um will generate the response and then the let's call the rest olama config and this is going to be a dictionary with the model name and it's this one then we don't want the prompt here but we also want to say stream equals false and I want to show you one more thing so you can also say keep alive and and this will keep the model alive in memory so it doesn't have to reload it every time and the default is 5 minutes so I want to leave this uh just to show you that this is available as well and now we have the olama config so um now let's let me invent this correctly and now here we basically just want to send the uh request so we Define our prompt prompt prompt equals and then let's leave a empty one for now and then we say response equals httpx so of course we have to import this import HTT X and then we send a post request to to the ol Lama end point and then as Chason data we say we create a new one where we also include the prompt and the prompt is the prompt that we need to set up and then for the rest of the values we unpack the olama config so now it puts all of those fields into this one here and then we also give it a header so we say headers equals and this is again a dictionary where we say content type and the type is um application SL Jason and we can also say time out equals 10 second for example and then we um this is all we need so now we have the response so now let's do some error checking if response. status code um not equals uh 200 we want to here you can print an error I want to Simply return uh none maybe and otherwise we want to return the response and then we can um convert this to Chason and then we will find this in the response field if we have a look you can find the response field here and then let's also call strip to remove um whes space in the beginning and in the end so now we only need the prompt so let's define a prompt template up here and for this we're going to use the buil-in template class so from string we import template this allows us to define a template where we can later insert the text so let's define a multi-line string and now let's say fix all typos and the casing and also the punctuation in this text but um preserve all new line characters and then a colon and then as a placeholder we have to use the dollar sign and give it a name so I call it text and then let's also give the model some instructions for the output format so let's say return only the corrected text don't include a preamble and this is the prompt we want to use again feel free to tweak this a little bit um and play around with this and then here down here we say um prompt template substitute and then we have to use the same name that we use here as placeholder text equals text and now this is all we need so here we get our prompt then we call olama and return this and this is the fixed text so let's run our script again and go to our notepad and for example go to this line and say F9 and see if it works and it works so it corrected this one so let me see if I find the old text so let me also uh try F10 and it fixes the selection awesome so now you have your own typing assistant running locally on your machine with olama let me know in the comments if you try out other models that also work well and yeah I hope you have fun with it and then I hope to see you in the next video bye

Original Description

In this Python tutorial, we'll build a typing assistant with Mistral 7B and Ollama that's running locally. You'll also learn how to implement a hotkey listener and keyboard controller with Python. Follow along in this step-by-step coding tutorial. Code: https://github.com/patrickloeber/ai-typing-assistant Blog post: https://patloeber.com/typing-assistant-llm/ Get my Free NumPy Handbook: https://www.python-engineer.com/numpybook ✅ Write cleaner code with Sourcery, instant refactoring suggestions in VS Code & PyCharm: https://sourcery.ai/?utm_source=youtube&utm_campaign=pythonengineer * ⭐ Join Our Discord : https://discord.gg/FHMg9tKFSN 📓 ML Notebooks available on Patreon: https://www.patreon.com/patrickloeber If you enjoyed this video, please subscribe to the channel: ▶️ : https://www.youtube.com/channel/UCbXgNpp0jedKWcQiULLbDTA?sub_confirmation=1 ~~~~~~~~~~~~~~~ CONNECT ~~~~~~~~~~~~~~~ 🖥️ Website: https://www.python-engineer.com 🐦 Twitter - https://twitter.com/patloeber ✉️ Newsletter - https://www.python-engineer.com/newsletter 📸 Instagram - https://www.instagram.com/patloeber 🦾 Discord: https://discord.gg/FHMg9tKFSN ▶️ Subscribe: https://www.youtube.com/channel/UCbXgNpp0jedKWcQiULLbDTA?sub_confirmation=1 ~~~~~~~~~~~~~~ SUPPORT ME ~~~~~~~~~~~~~~ 🅿 Patreon - https://www.patreon.com/patrickloeber #Python #ollama Timeline: 00:00 - Intro and demo 01:25 - Hotkey listener 05:58 - Keyboard controller 12:37 - Ollama setup ---------------------------------------------------------------------------------------------------------- * This is an affiliate link. By clicking on it you will not have any additional costs, instead you will support me and my project. Thank you so much for the support! 🙏
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Patrick Loeber · Patrick Loeber · 0 of 60

← Previous Next →
1 Lists in Python - Advanced Python 01 - Programming Tutorial
Lists in Python - Advanced Python 01 - Programming Tutorial
Patrick Loeber
2 Tuples in Python - Advanced Python 02 - Programming Tutorial
Tuples in Python - Advanced Python 02 - Programming Tutorial
Patrick Loeber
3 Dictionaries in Python - Advanced Python 03 - Programming Tutorial
Dictionaries in Python - Advanced Python 03 - Programming Tutorial
Patrick Loeber
4 Sets in Python - Advanced Python 04 - Programming Tutorial
Sets in Python - Advanced Python 04 - Programming Tutorial
Patrick Loeber
5 Strings in Python - Advanced Python 05 - Programming Tutorial
Strings in Python - Advanced Python 05 - Programming Tutorial
Patrick Loeber
6 Collections in Python - Advanced Python 06 - Programming Tutorial
Collections in Python - Advanced Python 06 - Programming Tutorial
Patrick Loeber
7 Itertools in Python - Advanced Python 07 - Programming Tutorial
Itertools in Python - Advanced Python 07 - Programming Tutorial
Patrick Loeber
8 Lambda in Python - Advanced Python 08 - Programming Tutorial - Map Filter Reduce
Lambda in Python - Advanced Python 08 - Programming Tutorial - Map Filter Reduce
Patrick Loeber
9 Exceptions in Python - Advanced Python 09 - Programming Tutorial
Exceptions in Python - Advanced Python 09 - Programming Tutorial
Patrick Loeber
10 Logging in Python - Advanced Python 10 - Programming Tutorial
Logging in Python - Advanced Python 10 - Programming Tutorial
Patrick Loeber
11 JSON in Python - Advanced Python 11 - Programming Tutorial
JSON in Python - Advanced Python 11 - Programming Tutorial
Patrick Loeber
12 Random Numbers in Python - Advanced Python 12 - Programming Tutorial
Random Numbers in Python - Advanced Python 12 - Programming Tutorial
Patrick Loeber
13 Decorators in Python - Advanced Python 13 - Programming Tutorial
Decorators in Python - Advanced Python 13 - Programming Tutorial
Patrick Loeber
14 Generators in Python - Advanced Python 14 - Programming Tutorial
Generators in Python - Advanced Python 14 - Programming Tutorial
Patrick Loeber
15 Threading vs Multiprocessing in Python - Advanced Python 15 - Programming Tutorial
Threading vs Multiprocessing in Python - Advanced Python 15 - Programming Tutorial
Patrick Loeber
16 Threading in Python - Advanced Python 16 - Programming Tutorial
Threading in Python - Advanced Python 16 - Programming Tutorial
Patrick Loeber
17 Multiprocessing in Python - Advanced Python 17 - Programming Tutorial
Multiprocessing in Python - Advanced Python 17 - Programming Tutorial
Patrick Loeber
18 Function arguments in detail - Advanced Python 18 - Programming Tutorial
Function arguments in detail - Advanced Python 18 - Programming Tutorial
Patrick Loeber
19 The asterisk (*) operator in Python - Advanced Python 19 - Programming Tutorial
The asterisk (*) operator in Python - Advanced Python 19 - Programming Tutorial
Patrick Loeber
20 Shallow vs Deep Copying in Python - Advanced Python 20 - Programming Tutorial
Shallow vs Deep Copying in Python - Advanced Python 20 - Programming Tutorial
Patrick Loeber
21 Context Managers in Python - Advanced Python 21 - Programming Tutorial
Context Managers in Python - Advanced Python 21 - Programming Tutorial
Patrick Loeber
22 KNN (K Nearest Neighbors) in Python - Machine Learning From Scratch 01 - Python Tutorial
KNN (K Nearest Neighbors) in Python - Machine Learning From Scratch 01 - Python Tutorial
Patrick Loeber
23 Linear Regression in Python - Machine Learning From Scratch 02 - Python Tutorial
Linear Regression in Python - Machine Learning From Scratch 02 - Python Tutorial
Patrick Loeber
24 Logistic Regression in Python - Machine Learning From Scratch 03 - Python Tutorial
Logistic Regression in Python - Machine Learning From Scratch 03 - Python Tutorial
Patrick Loeber
25 Linear and Logistic Regression in 60 lines of Python - Machine Learning From Scratch 04
Linear and Logistic Regression in 60 lines of Python - Machine Learning From Scratch 04
Patrick Loeber
26 Naive Bayes in Python - Machine Learning From Scratch 05 - Python Tutorial
Naive Bayes in Python - Machine Learning From Scratch 05 - Python Tutorial
Patrick Loeber
27 Perceptron in Python - Machine Learning From Scratch 06 - Python Tutorial
Perceptron in Python - Machine Learning From Scratch 06 - Python Tutorial
Patrick Loeber
28 SVM (Support Vector Machine) in Python - Machine Learning From Scratch 07 - Python Tutorial
SVM (Support Vector Machine) in Python - Machine Learning From Scratch 07 - Python Tutorial
Patrick Loeber
29 Decision Tree in Python Part 1/2 - Machine Learning From Scratch 08 - Python Tutorial
Decision Tree in Python Part 1/2 - Machine Learning From Scratch 08 - Python Tutorial
Patrick Loeber
30 Decision Tree in Python Part 2/2 - Machine Learning From Scratch 09 - Python Tutorial
Decision Tree in Python Part 2/2 - Machine Learning From Scratch 09 - Python Tutorial
Patrick Loeber
31 Random Forest in Python - Machine Learning From Scratch 10 - Python Tutorial
Random Forest in Python - Machine Learning From Scratch 10 - Python Tutorial
Patrick Loeber
32 PCA (Principal Component Analysis) in Python - Machine Learning From Scratch 11 - Python Tutorial
PCA (Principal Component Analysis) in Python - Machine Learning From Scratch 11 - Python Tutorial
Patrick Loeber
33 K-Means Clustering in Python - Machine Learning From Scratch 12 - Python Tutorial
K-Means Clustering in Python - Machine Learning From Scratch 12 - Python Tutorial
Patrick Loeber
34 Anaconda Tutorial - Installation and Basic Commands
Anaconda Tutorial - Installation and Basic Commands
Patrick Loeber
35 PyTorch Tutorial 01 - Installation
PyTorch Tutorial 01 - Installation
Patrick Loeber
36 PyTorch Tutorial 02 - Tensor Basics
PyTorch Tutorial 02 - Tensor Basics
Patrick Loeber
37 PyTorch Tutorial 03 - Gradient Calculation With Autograd
PyTorch Tutorial 03 - Gradient Calculation With Autograd
Patrick Loeber
38 PyTorch Tutorial 04 - Backpropagation - Theory With Example
PyTorch Tutorial 04 - Backpropagation - Theory With Example
Patrick Loeber
39 PyTorch Tutorial 05 - Gradient Descent with Autograd and Backpropagation
PyTorch Tutorial 05 - Gradient Descent with Autograd and Backpropagation
Patrick Loeber
40 PyTorch Tutorial 06 - Training Pipeline: Model, Loss, and Optimizer
PyTorch Tutorial 06 - Training Pipeline: Model, Loss, and Optimizer
Patrick Loeber
41 PyTorch Tutorial 07 - Linear Regression
PyTorch Tutorial 07 - Linear Regression
Patrick Loeber
42 PyTorch Tutorial 08 - Logistic Regression
PyTorch Tutorial 08 - Logistic Regression
Patrick Loeber
43 PyTorch Tutorial 09 - Dataset and DataLoader - Batch Training
PyTorch Tutorial 09 - Dataset and DataLoader - Batch Training
Patrick Loeber
44 PyTorch Tutorial 10 - Dataset Transforms
PyTorch Tutorial 10 - Dataset Transforms
Patrick Loeber
45 Download Images With Python Automatically - Python Web Scraping Tutorial
Download Images With Python Automatically - Python Web Scraping Tutorial
Patrick Loeber
46 PyTorch Tutorial 11 - Softmax and Cross Entropy
PyTorch Tutorial 11 - Softmax and Cross Entropy
Patrick Loeber
47 Select Movies with Python - Web Scraping Tutorial
Select Movies with Python - Web Scraping Tutorial
Patrick Loeber
48 PyTorch Tutorial 12 - Activation Functions
PyTorch Tutorial 12 - Activation Functions
Patrick Loeber
49 List Comprehension in Python - A Python Feature You MUST KNOW - Python Tutorial
List Comprehension in Python - A Python Feature You MUST KNOW - Python Tutorial
Patrick Loeber
50 PyTorch Tutorial 13 - Feed-Forward Neural Network
PyTorch Tutorial 13 - Feed-Forward Neural Network
Patrick Loeber
51 How To Add A Progress Bar In Python With Just One Line - Python Tutorial
How To Add A Progress Bar In Python With Just One Line - Python Tutorial
Patrick Loeber
52 PyTorch Tutorial 14 - Convolutional Neural Network (CNN)
PyTorch Tutorial 14 - Convolutional Neural Network (CNN)
Patrick Loeber
53 The Walrus Operator - New in Python 3.8 - Python Tutorial
The Walrus Operator - New in Python 3.8 - Python Tutorial
Patrick Loeber
54 PyTorch Tutorial 15 - Transfer Learning
PyTorch Tutorial 15 - Transfer Learning
Patrick Loeber
55 YouTube Data API Tutorial with Python - Analyze Channel Statistics - Part 1
YouTube Data API Tutorial with Python - Analyze Channel Statistics - Part 1
Patrick Loeber
56 YouTube Data API Tutorial with Python - Find Channel Videos - Part 2
YouTube Data API Tutorial with Python - Find Channel Videos - Part 2
Patrick Loeber
57 YouTube Data API Tutorial with Python - Get Video Statistics - Part 3
YouTube Data API Tutorial with Python - Get Video Statistics - Part 3
Patrick Loeber
58 YouTube Data API Tutorial with Python - Analyze the Data - Part 4
YouTube Data API Tutorial with Python - Analyze the Data - Part 4
Patrick Loeber
59 AdaBoost in Python - Machine Learning From Scratch 13 - Python Tutorial
AdaBoost in Python - Machine Learning From Scratch 13 - Python Tutorial
Patrick Loeber
60 Ultimate FREE Study Guide for Machine Learning and Deep Learning
Ultimate FREE Study Guide for Machine Learning and Deep Learning
Patrick Loeber

This tutorial teaches you how to build a free writing assistant with Python and Ollama, covering hotkey listeners, keyboard controllers, and local LLM deployment. By following along, you'll gain hands-on experience with LLM development and automation techniques.

Key Takeaways
  1. Install required libraries and dependencies
  2. Set up Ollama for LLM development
  3. Implement hotkey listeners with Python
  4. Configure keyboard controllers
  5. Deploy the typing assistant locally
💡 Utilizing Ollama and Mistral 7B allows for efficient and effective LLM development, enabling the creation of powerful typing assistants

Related Reads

📰
What is the Model Context Protocol (MCP)?
Learn about the Model Context Protocol (MCP), an open standard for connecting language models to tools and data through a consistent interface
Dev.to AI
📰
Green Is Not Correct — field notes from an AI orchestrator, end of a long shift
Learn from an AI orchestrator's field notes at the end of a long shift, exploring the complexities of AI operations
Medium · LLM
📰
LLM Security Guide: Prompt Injection and API Exploitation
Learn to detect and mitigate LLM vulnerabilities and secure your web applications against prompt injection and API exploitation
Medium · Cybersecurity
📰
LLM Security Guide: Prompt Injection and API Exploitation
Learn to detect and mitigate LLM vulnerabilities and secure your web applications against prompt injection and API exploitation
Medium · LLM

Chapters (4)

Intro and demo
1:25 Hotkey listener
5:58 Keyboard controller
12:37 Ollama setup
Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →