DYI AI Text "Cleaner"
Tags: chatgpt, bash, automation, tutorial
Published: 2024-02-03 05:45:52

I was looking through hacker news an saw [this](https://news.ycombinator.com/item?id=39220777) post about some app. The App will take any highlighted text and "enhance" it using chatgpt after the user does some keybinding. And then lower someone said that they just created a script it do it for them and save $$ a month.

So I said screw it, I'll do it myself as well and started chatting with my personal E2 engineer, ChatGPT.


![In Action](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3lul8n4t4wayzveo53kn.gif)
(I'm clicking a keyboard shortcut to make this generate)




## Setting up the bash script

First off if you don't have jq installed make sure you have it.

```bash
brew install jq
```

Create a folder somewhere easy to access and store your script. For the past of this guide it will be `~/ai_text_cleaner`.

```bash
mkdir ~/ai_text_cleaner
```

Now download my script to that folder.

```bash
wget -O ~/ai_text_cleaner/ai_text_cleaner https://raw.githubusercontent.com/nrgapple/scripts/main/ai_text_cleaner/ai_text_cleaner
```

[The repo is here](https://github.com/nrgapple/scripts)

Next add a `.env` file in the directory and add your OpenAI key to it.

```
OPENAI_API_KEY=[your-key-here]
```

Last we need to make the script executable.

```bash
chmod +x ~/ai_text_cleaner/ai_text_cleaner
```

Lets test if it works.

```bash
~/ai_text_cleaner/ai_text_cleaner "ths is sme text that i wrot poorly and rlly fast to show if you can cln up stuff well."
```

I get this as a response:

```
This is some text that I wrote poorly and really fast to see if you can clean up stuff well.
```

Nice! Lets move on.

## Set up Automator on Mac

Run this command to pull down repo, pull out the automator action folder and remove the repo. (I recommend doing this in your `~/Downloads` directory.

```bash
git clone https://github.com/nrgapple/scripts.git && mv scripts/ai_text_cleaner/automator/AI\ Text\ Clean\ Up.workflow ./ && rm -rf scripts
```

Open up the file `ai_text_cleaner/automator/AI Text Clean Up.workflow/Contents/document.wflow`. `ctr+f` for `ai_text_cleaner`. You should see a line like so:

```xml
<string>$HOME/git/scripts/ai_text_cleaner/ai_text_cleaner "$@"</string>
```

Change it to where you saved your script from above. If you saved it to where I said it would be:

```xml
<string>$HOME/ai_text_cleaner/ai_text_cleaner "$@"</string>
```

Save and Lets move on.

Copy the whole directory (`AI Text Clean Up.workflow`) to your services folder in automator:

```bash
cp -r AI\ Text\ Clean\ Up.workflow ~/Library/Services/AI\ Text\ Clean\ Up.workflow 
```

Nice. Now last is to create a keybinding for running it.

## Set up the keybindings

Go to Settings on Mac and search for `Keyboard`. Click `Keyboard Shortcuts`.


![Keyboard Shortcuts Settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s0l2l1hrn40tbntn0nso.png)


Click `Services` and look for it under the different categories. Mine was under `Text`

![Action in Keybindings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8w5vtxq30kvziwger0lj.png)

Double click it and set your keybinding.

Nice. You're ready to test it out!

## Test it out!

Open a text document and type some sentence or paragraph. Highlight all the text and do your keybinding. It will take a second or two and bam! You should see your highlighted text be replaced by Chatgpt's enhanced cleaning!

## Modify the prompt

If you think what Chatgpt gave you sucked, you can try to change the prompt.

In our bash script from above look for this line:

```bash
# Define the prompt in a variable
PROMPT="You are a helpful assistant. Your task is to correct any grammatical errors, improve sentence structure, and fix spelling mistakes in the following text."
```

You can try and change this to your hearts desire to get better results!

## Conclusion

We have created a tool worth [$6.90](https://app.fixkey.ai/pricing) a month! You'll only have to pay for what you use on OpenAI's api rates. 

Let me know if something doesn't work for you and I'll make some edits. Let me know if you would like me to do some more DYI AI tools in the future. I have already adapted with flow to create a command line command generator is well (Can be found in the same repo).

Thanks! 👋