Correct Typos While Typing – AI Typing Assistant

Say goodbye to tedious editing with the AI Typing Assistant. This Python script corrects your text as you type, saving you time and effort. Give it a try!

Struggling with typos and grammatical errors while you type? Check out the AI Typing Assistant: Your New Writing Partner.

AI Typing Assistant is a free, lightweight, open-source Python script that runs locally and listens for hotkeys.

When a hotkey is pressed, it uses a Large Language Model (like Llama 2, Mistral, or Gemma) to fix the text you’ve typed.

This script allows you to quickly and easily correct typos, fix casing and punctuation errors, and improve the overall quality of your written text.

Ideal for writers, students, professionals, or anyone looking to enhance their writing quality without disrupting their workflow.

How to use it:

1. Begin by installing and setting up Ollama to run Large Language Models locally.

ollama run llama2

2. Install necessary dependencies using the following command:

  • pynput:This library allows you to control and monitor input devices.
  • pyperclip: A Python module for cross-platform clipboard functions.
  • httpx: A next generation HTTP client for Python.
pip install pynput pyperclip httpx

3. Download the AI Typing Assistant script and run it on your device. If you encounter an accessibility error, ensure the script or terminal is authorized for input monitoring and accessibility on your Mac.

4. Utilize F9 to correct the current line or F10 for the selected text.

5. Adjust the hotkeys, prompt templates, and Ollama configuration according to your needs to optimize the assistant’s functionality.

OLLAMA_ENDPOINT = "http://localhost:11434/api/generate"
OLLAMA_CONFIG = {
    "model": "mistral:7b-instruct-v0.2-q4_K_S",
    "keep_alive": "5m",
    "stream": False,
}
PROMPT_TEMPLATE = Template(
    """Fix all typos and casing and punctuation in this text, but preserve all new line characters:
$text
Return only the corrected text, don't include a preamble.
"""
)

def fix_text(text):
    prompt = PROMPT_TEMPLATE.substitute(text=text)
    response = httpx.post(
        OLLAMA_ENDPOINT,
        json={"prompt": prompt, **OLLAMA_CONFIG},
        headers={"Content-Type": "application/json"},
        timeout=10,
    )
    if response.status_code != 200:
        print("Error", response.status_code)
        return None
    return response.json()["response"].strip()

def fix_current_line():
    # macOS short cut to select current line: Cmd+Shift+Left
    controller.press(Key.cmd)
    controller.press(Key.shift)
    controller.press(Key.left)
    controller.release(Key.cmd)
    controller.release(Key.shift)
    controller.release(Key.left)
    fix_selection()

def fix_selection():
    # 1. Copy selection to clipboard
    with controller.pressed(Key.cmd):
        controller.tap("c")
    # 2. Get the clipboard string
    time.sleep(0.1)
    text = pyperclip.paste()
    # 3. Fix string
    if not text:
        return
    fixed_text = fix_text(text)
    if not fixed_text:
        return
    # 4. Paste the fixed string to the clipboard
    pyperclip.copy(fixed_text)
    time.sleep(0.1)
    # 5. Paste the clipboard and replace the selected text
    with controller.pressed(Key.cmd):
        controller.tap("v")

def on_f9():
    fix_current_line()

def on_f10():
    fix_selection()

with keyboard.GlobalHotKeys({"<101>": on_f9, "<109>": on_f10}) as h:
    h.join()

6. Watch the YouTube video provided for a step-by-step explanation on how to build and use this AI Typing Assistant.

Leave a Reply

Your email address will not be published. Required fields are marked *