-
Notifications
You must be signed in to change notification settings - Fork 35
/
main.py
90 lines (65 loc) · 1.92 KB
/
main.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
83
84
85
86
87
88
89
90
import time
from string import Template
import httpx
from pynput import keyboard
from pynput.keyboard import Key, Controller
import pyperclip
controller = Controller()
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()