-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempCodeRunnerFile.python
More file actions
114 lines (96 loc) · 3.73 KB
/
tempCodeRunnerFile.python
File metadata and controls
114 lines (96 loc) · 3.73 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import os
import threading
import tkinter as tk
from tkinter import messagebox
from PIL import ImageGrab, Image
import pytesseract
import openai
import pyperclip
import keyboard
import pystray
from pystray import MenuItem as item
# Get API key from environment variable
client = openai.OpenAI(api_key="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")
# ---------- REGION SELECTOR ----------
class ScreenRegionSelector:
def __init__(self):
self.root = tk.Tk()
self.root.attributes("-fullscreen", True)
self.root.attributes("-alpha", 0.3)
self.root.configure(background='black')
self.canvas = tk.Canvas(self.root, cursor="cross", bg="gray", highlightthickness=0)
self.canvas.pack(fill=tk.BOTH, expand=True)
self.canvas.bind("<Button-1>", self.on_click)
self.canvas.bind("<B1-Motion>", self.on_drag)
self.canvas.bind("<ButtonRelease-1>", self.on_release)
self.start_x = self.start_y = 0
self.rect = None
self.bbox = None
def on_click(self, event):
self.start_x, self.start_y = event.x, event.y
self.rect = self.canvas.create_rectangle(self.start_x, self.start_y, self.start_x, self.start_y, outline="red", width=2)
def on_drag(self, event):
self.canvas.coords(self.rect, self.start_x, self.start_y, event.x, event.y)
def on_release(self, event):
self.bbox = (min(self.start_x, event.x), min(self.start_y, event.y), max(self.start_x, event.x), max(self.start_y, event.y))
self.root.destroy()
def get_bbox(self):
self.root.mainloop()
return self.bbox
# ---------- OCR & GPT ----------
def capture_text_from_selected_area():
selector = ScreenRegionSelector()
bbox = selector.get_bbox()
if not bbox:
return None
try:
screenshot = ImageGrab.grab(bbox)
return pytesseract.image_to_string(screenshot).strip()
except Exception as e:
return f"OCR Error: {e}"
def ask_chatgpt(prompt):
try:
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}]
)
return response.choices[0].message.content.strip()
except Exception as e:
return f"OpenAI Error: {e}"
def show_popup(title, content):
popup = tk.Tk()
popup.withdraw()
messagebox.showinfo(title, content)
popup.destroy()
def handle_hotkey():
print("🔹 Select a region on screen...")
text = capture_text_from_selected_area()
if not text or "Error" in text:
show_popup("❌ Error", text or "No text detected.")
return
print(f"📝 OCR Text: {text[:100]}...")
response = ask_chatgpt(text)
pyperclip.copy(response)
show_popup("🤖 GPT Response", response)
print("✅ GPT response copied to clipboard.")
# ---------- TRAY APP ----------
def run_tray_app():
icon_image = Image.new("RGB", (64, 64), "black") # Simple black square icon
icon = pystray.Icon("GPT Assistant", icon_image, "GPT Assistant", menu=pystray.Menu(
item('Activate (Ctrl+Shift+G)', lambda: handle_hotkey()),
item('Quit', lambda icon, item: quit_app(icon))
))
icon.run()
def quit_app(icon):
icon.stop()
os._exit(0)
# ---------- HOTKEY ----------
def start_hotkey_listener():
keyboard.add_hotkey('ctrl+shift+g', handle_hotkey)
print("🟢 Press Ctrl+Shift+G to activate OCR+GPT. Press Quit in tray to exit.")
keyboard.wait() # Keeps thread alive
# ---------- MAIN ----------
if __name__ == "__main__":
hotkey_thread = threading.Thread(target=start_hotkey_listener, daemon=True)
hotkey_thread.start()
run_tray_app()