-
Notifications
You must be signed in to change notification settings - Fork 0
/
no_more_typo.py
82 lines (67 loc) · 7.61 KB
/
no_more_typo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# pip install --upgrade pyperclip pynput langchain langchain-openai langchain-community langchain-core
import pyperclip
from pynput import keyboard
from langchain_community.llms.openai import OpenAI
from langchain_core.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain.schema.runnable import RunnableLambda
import sys
import os
import warnings
print("""
╔══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗
║ ║
║ ▄▄ ▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄ ▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄ ▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ║
║▐░░▌ ▐░▌▐░░░░░░░░░░░▌ ▐░░▌ ▐░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌ ▐░░░░░░░░░░░▌▐░▌ ▐░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌ ║
║▐░▌░▌ ▐░▌▐░█▀▀▀▀▀▀▀█░▌ ▐░▌░▌ ▐░▐░▌▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀▀▀ ▀▀▀▀█░█▀▀▀▀ ▐░▌ ▐░▌▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀█░▌ ║
║▐░▌▐░▌ ▐░▌▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌▐░▌▐░▌ ▐░▌▐░▌ ▐░▌▐░▌ ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌▐░▌ ▐░▌ ║
║▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌ ▐░▌ ▐░▐░▌ ▐░▌▐░▌ ▐░▌▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄▄▄ ▐░▌ ▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄█░▌▐░▌ ▐░▌ ║
║▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌ ▐░▌ ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░▌ ▐░▌ ║
║▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌ ▐░▌ ▀ ▐░▌▐░▌ ▐░▌▐░█▀▀▀▀█░█▀▀ ▐░█▀▀▀▀▀▀▀▀▀ ▐░▌ ▀▀▀▀█░█▀▀▀▀ ▐░█▀▀▀▀▀▀▀▀▀ ▐░▌ ▐░▌ ║
║▐░▌ ▐░▌▐░▌▐░▌ ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌ ║
║▐░▌ ▐░▐░▌▐░█▄▄▄▄▄▄▄█░▌ ▐░▌ ▐░▌▐░█▄▄▄▄▄▄▄█░▌▐░▌ ▐░▌ ▐░█▄▄▄▄▄▄▄▄▄ ▐░▌ ▐░▌ ▐░▌ ▐░█▄▄▄▄▄▄▄█░▌ ║
║▐░▌ ▐░░▌▐░░░░░░░░░░░▌ ▐░▌ ▐░▌▐░░░░░░░░░░░▌▐░▌ ▐░▌▐░░░░░░░░░░░▌ ▐░▌ ▐░▌ ▐░▌ ▐░░░░░░░░░░░▌ ║
║ ▀ ▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀ ▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀ ▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀ ▀ ▀ ▀▀▀▀▀▀▀▀▀▀▀ ║
║ ║
║ ║
║ Small utility that uses LLM to fix typos and syntax on the text stored in the clipboard. ║
║ ║
║ LLM api_key should be available in the OPENAI_API_KEY environment variable. ║
║ To use custom prompt template export it to the NO_MORE_TYPO_PROMPT_TEMPLATE environment variable ║
║ The default value is: "Fix the syntax and typos text:\\n\\n{text}\\n\\nThe correct string is:" ║
║ ║
║ Activate fix: [ctrl]+[shift]+[z] ║
║ Exit app: [ctrl]+[shift]+[x] ║
║ ║
║ 2024 devquasar.com ║
║ ║
╚══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝
""")
# Suppress all warnings
warnings.filterwarnings("ignore")
llm = OpenAI()
# Load the prompt template from an environment variable or use the default
default_prompt = "Fix the syntax and typos text:\n\n{text}\n\nThe correct string is:"
custom_prompt = os.getenv("NO_MORE_TYPO_PROMPT_TEMPLATE", default_prompt)
# Ensure the template includes "{text}"
if "{text}" not in custom_prompt:
custom_prompt += "\n CONTEXT: \n {text}"
prompt = PromptTemplate.from_template(custom_prompt)
cleanup = RunnableLambda(
lambda x: x.strip().strip('"').strip("'")
)
no_typo_chain = prompt | llm | cleanup
def on_activate():
original_clipboard_content = pyperclip.paste()
processed_content = no_typo_chain.invoke(original_clipboard_content)
pyperclip.copy(processed_content)
def on_exit():
print("Exit no_more_typo app...")
sys.exit(0)
with keyboard.GlobalHotKeys(
{
'<ctrl>+<shift>+z': on_activate,
'<ctrl>+<shift>+x': on_exit
}
) as h:
h.join()