-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUDE.py
273 lines (219 loc) · 7.39 KB
/
UDE.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import os
import configparser
import json
import pyperclip
import keyboard
from functools import partial
import requests
import ctypes
import threading
import subprocess
current_version = "v1.3"
repo_url = "https://api.github.com/repos/Wy477-P/UDE"
# DEBUG #
def test(code):
try:
exec(code)
except Exception as e:
print(f"An error occurred: {e}")
input()
# DEBUG #
# DIRECTORIES #
workdir = os.getcwd()
def folfind(fo):
return os.path.join(workdir, fo)
def filfind(fo, fi):
return os.path.join(fo, fi)
logdir = folfind("logs")
dlldir = folfind("dlls")
tesseractdir = folfind("tesseract")
configini = filfind(workdir,"config.ini")
gcontextjson = filfind(logdir,"gcontext.json")
goutput = filfind(logdir,"goutput.txt")
udpdll = filfind(dlldir, "UDP.dll")
ttsdll = filfind(dlldir, "TTS.dll")
autoupdate = filfind(workdir, "autoupdate.py")
# AUTOUPDATE #
def check_for_updates():
response = requests.get(f"{repo_url}/releases/latest")
response.raise_for_status()
latest_release = response.json()
latest_version = latest_release['tag_name']
if current_version != latest_version:
subprocess.Popen(["python", autoupdate])
os._exit(0)
check_for_updates()
# EXIT FUNCTION #
def closeude():
keyboard.unhook_all_hotkeys()
os._exit(0)
# UPDATE CONTEXT #
def ac(t, r):
with open(gcontextjson, 'r') as f:
con = json.load(f)
e = {'role': r, 'parts': [{'text': t}]}
con.append(e)
with open(gcontextjson, 'w') as f:
json.dump(con, f)
# CLEAR CONTEXT #
def clearcon():
if os.path.isfile(gcontextjson):
os.remove(gcontextjson)
with open(gcontextjson, 'w') as f:
json.dump([], f)
# UDG MAIN #
def UDG(key, mode, inst, temper, toppp, topkk):
if not os.path.isfile(gcontextjson):
with open(gcontextjson, 'w') as f:
json.dump([], f)
userin = pyperclip.paste()
user = "user"
ac(userin, user)
with open(gcontextjson, 'r') as f:
con = json.load(f)
SN = {
"system_instruction": {
"parts": [{
"text": inst
}]
},
"safetySettings": [
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"}
],
"generationConfig": {
"temperature": temper,
"topP": toppp,
"topK": topkk
},
"contents": con
}
url = f"https://generativelanguage.googleapis.com/v1beta/models/{mode}:generateContent?key={key}"
response = requests.post(url, json=SN, headers={'Content-Type': 'application/json'})
RP = response.json()
OP = RP.get('candidates', [{}])[0].get('content', {}).get('parts', [{}])[0].get('text', '')
pyperclip.copy(OP)
ac(OP, "model")
with open(goutput, 'w') as file:
json.dump(RP, file)
# UDPDLL SETUP #
udp_dll = ctypes.CDLL(udpdll)
udp_dll.start_udp.argtypes = []
udp_dll.stop_udp.argtypes = []
udp_dll.udp.argtypes = [ctypes.c_char_p, ctypes.c_float, ctypes.c_float, ctypes.c_float]
udp_thread = None
def run_udp(text, delay_per_key, word_delay_min, word_delay_max):
udp_dll.start_udp()
udp_dll.udp(text.encode('utf-8'), delay_per_key, word_delay_min, word_delay_max)
# UDP MAIN #
def UDP(delay_per_key, word_delay_min, word_delay_max):
global udp_thread
text = pyperclip.paste()
text = text.lstrip()
text = text.replace('’', '\'')
threading.Event().wait(0.4)
udp_thread = threading.Thread(target=run_udp, args=(text, delay_per_key, word_delay_min, word_delay_max))
udp_thread.start()
# UDP KILLSWITCH #
def kill_switch():
global udp_thread
if udp_thread is not None and udp_thread.is_alive():
udp_dll.stop_udp()
udp_thread.join()
udp_thread = None
# TTS DLL SETUP #
tts_dll = ctypes.CDLL(ttsdll)
CreateTTS = tts_dll.CreateTTS
SpeakText = tts_dll.SpeakText
StopSpeech = tts_dll.StopSpeech
DestroyTTS = tts_dll.DestroyTTS
CreateTTS.restype = ctypes.c_void_p
SpeakText.argtypes = [ctypes.c_void_p, ctypes.c_wchar_p, ctypes.c_float, ctypes.c_float, ctypes.c_float, ctypes.c_wchar_p]
StopSpeech.argtypes = [ctypes.c_void_p]
DestroyTTS.argtypes = [ctypes.c_void_p]
tts_thread = None
# TTS MAIN
tts_instance = None
def ttsgo(txt, spd, pit, vol, voi):
global tts_instance
tts_instance = CreateTTS()
SpeakText(tts_instance, txt, spd, pit, vol, voi)
def starttts(sped, pitc, volme, voic):
global tts_thread
txet = pyperclip.paste()
tts_thread = threading.Thread(target=ttsgo, args=(txet, sped, pitc, volme, voic))
tts_thread.start()
def stopts():
global tts_thread, tts_instance
DestroyTTS(tts_instance)
tts_thread.join()
tts_thread = None
# CHECK & IMPORT CONFIGS & UPDATE HOTKEYS #
apikey = None
gmodel = None
sysin = None
temp = None
topp = None
topk = None
dpk = None
wdmin = None
wdmax = None
ttsspeed = None
ttspitch = None
ttsvolume = None
tts_model = None
EXIT = None
CALLUDG = None
CALLUDP = None
CLEARCONTEXT = None
UPDATECONFIG = None
KILLSWITCH = None
PLAYTTS = None
STOPTTS = None
def updatecfg(relod):
global apikey, gmodel, sysin, temp, topp, topk, dpk, wdmin, wdmax, ttsspeed, ttspitch, ttsvolume, tts_model, EXIT, CALLUDG, CALLUDP, CLEARCONTEXT, UPDATECONFIG, PLAYTTS, STOPTTS
config = configparser.ConfigParser()
config.read(configini)
apikey = config['UDG']['api_key']
gmodel = config['UDG']['gemini_model']
sysin = config['UDG']['system_instructions']
temp = float(config['UDG']['temperature'])
topp = float(config['UDG']['top_p'])
topk = float(config['UDG']['top_k'])
dpk = float(config['UDP']['delay_per_key'])
wdmin = float(config['UDP']['word_delay_min'])
wdmax = float(config['UDP']['word_delay_max'])
ttsspeed = float(config['TTS']['speech_speed'])
ttspitch = float(config['TTS']['speech_pitch'])
ttsvolume = float(config['TTS']['speech_volume'])
tts_model = config['TTS']['speech_model']
EXIT = config['HOTKEYS']['exit_hotkey']
CALLUDG = config['HOTKEYS']['udg_hotkey']
CALLUDP = config['HOTKEYS']['udp_hotkey']
CLEARCONTEXT = config['HOTKEYS']['clear_context']
UPDATECONFIG = config['HOTKEYS']['update_config']
KILLSWITCH = config['HOTKEYS']['udp_killswitch']
PLAYTTS = config['HOTKEYS']['play_tts']
STOPTTS = config['HOTKEYS']['stop_tts']
if relod:
keyboard.remove_hotkey(EXIT)
keyboard.remove_hotkey(CALLUDG)
keyboard.remove_hotkey(CALLUDP)
keyboard.remove_hotkey(CLEARCONTEXT)
keyboard.remove_hotkey(UPDATECONFIG)
keyboard.remove_hotkey(KILLSWITCH)
keyboard.remove_hotkey(PLAYTTS)
keyboard.remove_hotkey(STOPTTS)
keyboard.add_hotkey(EXIT, closeude)
keyboard.add_hotkey(CALLUDG, partial(UDG, apikey, gmodel, sysin, temp, topp, topk))
keyboard.add_hotkey(CALLUDP, partial(UDP, dpk, wdmin, wdmax))
keyboard.add_hotkey(CLEARCONTEXT, clearcon)
keyboard.add_hotkey(UPDATECONFIG, partial(updatecfg, True))
keyboard.add_hotkey(KILLSWITCH, kill_switch)
keyboard.add_hotkey(PLAYTTS, partial(starttts, ttsspeed, ttspitch, ttsvolume, tts_model))
keyboard.add_hotkey(STOPTTS, stopts)
updatecfg(False)
print(f"It's ready now you impatient fuck.")
keyboard.wait()