-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibjotalea.py
451 lines (399 loc) · 15.6 KB
/
libjotalea.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# pip install aiohttp
# pip install requests
# pip install discord
GPT_ENDPOINT = "https://api.openai.com/v1/chat/completions"
GPT_KEY = ""
GPT_HEADERS = {"Content-Type": "application/json", "Authorization": f"Bearer {GPT_KEY}"}
GPT_MODEL = "gpt-4"
GPT_TTS_URL = ""
GEMINI_KEY = ""
GEMINI_MEMORY = []
def gemini(prompt, history=[], memory=False, personality:str="You are a helpful AI."):
global GEMINI_KEY, GEMINI_MEMORY
import requests, json
if not history:
# No chat history is provided, create a new one
if not memory:
# Do not remember the conversation
# Make the history with just the prompt
history = [
{
"role": "user",
"parts": [
{
"text": prompt
}
]
}
]
else:
# Remember the conversation
history = GEMINI_MEMORY
history.append(
{
"role": "user",
"parts": [
{
"text": prompt
}
]
}
)
response = requests.post(
f'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={GEMINI_KEY}',
headers = {
'Content-Type': 'application/json',
},
json={
"system_instruction": {"parts": {"text": personality}},
"contents": history
}
)
try:
reply = response.json()["candidates"][0]["content"]["parts"][0]["text"]
if memory:
history.append({"role": "model", "parts": [{"text": reply}]})
GEMINI_MEMORY = history
return reply if reply else None
except Exception as e:
return str(e)
class GeminiChat:
def __init__(self, key):
self.GEMINI_KEY = key
self.GEMINI_MEMORY = []
def gemini(self, prompt, history=[], memory=False, personality:str="You are a helpful AI."):
import requests, json
if not history:
# No chat history is provided, create a new one
if not memory:
# Do not remember the conversation
history = [
{
"role": "user",
"parts": [
{
"text": prompt
}
]
}
]
else:
# Remember the conversation
history = self.GEMINI_MEMORY
history.append(
{
"role": "user",
"parts": [
{
"text": prompt
}
]
}
)
response = requests.post(
f'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={self.GEMINI_KEY}',
headers = {
'Content-Type': 'application/json',
},
json={
"system_instruction": {"parts": {"text": personality}},
"contents": history
}
)
try:
reply = response.json()["candidates"][0]["content"]["parts"][0]["text"]
if memory:
history.append({"role": "model", "parts": [{"text": reply}]})
self.GEMINI_MEMORY = history
return reply if reply else None
except Exception as e:
return str(e)
def chatgpt(prompt:str, history_payload:list):
import requests, json
global GPT_ENDPOINT, GPT_HEADERS, GPT_KEY, GPT_MODEL, GPT_TTS_URL, payload, data
payload = [
{"role": "system", "content": "You are a helpful AI."},
{"role": "user", "content": prompt}
]
data = {
"model": GPT_MODEL,
"messages": history_payload + [{"role": "user", "content": prompt}]
}
try:
if GPT_ENDPOINT == "https://api.openai.com/v1/chat/completions":
response = requests.post(GPT_ENDPOINT, headers=GPT_HEADERS, data=json.dumps(data))
else:
response = requests.post(GPT_ENDPOINT, headers=GPT_HEADERS, json=data)
return response.json()
except Exception as e:
return e
def tts(text, api_key):
import requests
api_domain = "api.shuttleai.app"
url = f"https://{api_domain}/v1/audio/speech"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
data = {
"model": "eleven-labs",
"input": text,
"voice": "james"
}
import random
random_v = random.randint(1000000, 9999999)
response = requests.post(url, headers=headers, json=data)
print(response)
print(response.json())
print(response.content)
response_a = requests.get(response.content)
with open(f"/files/speech_{random_v}.png", "wb") as file:
file.write(response_a)
path = "/files/speech_{random_v}.mp3"
# with open(f"/files/speech{random_v}.mp3", "rb") as file:
# file_send = file.read()
# return file_send
return path
def ttsmp3(text, voice:str="Enrique"):
import requests, json
response = requests.post("https://ttsmp3.com/makemp3_new.php", data={"msg":text,"lang":voice,"source":"ttsmp3"})
mp3 = json.loads(response.text)
return f"https://ttsmp3.com/created_mp3/{mp3['MP3']}"
def webhook(url:str, content:str="Test content", debug:bool=False):
import requests
try:
rs = requests.post(url, json={"content": content})
if rs.status_code == 200:
return True
except Exception as e:
if debug:
return str(e)
else:
return False
async def async_webhook(url: str, content: str = "Test content", debug: bool = False):
import aiohttp, asyncio, json
try:
async with aiohttp.ClientSession() as session:
async with session.post(url, json={"content": content}) as response:
if response.status == 200:
return True
else:
if debug:
return response.status
return False
except Exception as e:
if debug:
return str(e)
return False
log = []
def ansirgb(r:int=0, g:int=0, b:int=0):
return f"\x1b[38;2;{r};{g};{b}m"
def gradient(text:str, mode:str, start_color:list, end_color:list, usenumpy:bool=False):
# If usenumpy is True, the numpy module is required.
# Example usage:
# gradient(text="Text", mode="by-character-diagonal", start_color=[0, 0, 0], end_color=[255, 255, 255])
# Notes:
# text must be a string any content
# mode must be a string its content can only be "by-character", "by-character-diagonal", "line-horizontal" or "line-vertical"
# start_color must be a list its content must be [int>0<255, int>0<255, int>0<255]
# end_color must be a list its content must be [int>0<255, int>0<255, int>0<255]
# usenumpy must be a bool its content must be True (default) or False
class InvalidRGBColor(Exception):
def __init__(self, reason=None):
self.reason = reason
message = "The RGB value must be higher than 0 and lower than 255. Your input doesn't match those requirements."
class InvalidGradientOption(Exception):
def __init__(self, reason=None):
self.reason = reason
message = f"The gradient option you chose ({str(reason)}) is not a valid option."
class InvalidStepsAmount(Exception):
def __init__(self, reason=None):
self.reason = reason
message = "The amount of steps for the gradient must be integer and higher than 0."
def rgb(r:int=0, g:int=0, b:int=0):
return f"\x1b[38;2;{r};{g};{b}m"
def generate_rgb_grad(start_color, end_color, steps, un=usenumpy):
def validate_rgb_color(color):
for channel in color:
if not 0 < channel < 255:
raise InvalidRGBColor("RGB color values should be between 0 and 255")
validate_rgb_color(start_color)
validate_rgb_color(end_color)
if un:
try:
import numpy as np
except ImportError:
import sys
import platform
if platform.system() == "Windows":
python_command = "python" # Windows ussually has Python in PATH
else:
python_command = sys.executable or "python"
print(f"Numpy module is not installed.\nInstall it by running \"{python_command} -m pip install numpy\" or disable it.")
if steps <= 0:
raise InvalidStepsAmount("The amount of steps must be higher than 0")
s_color = np.array(start_color)
e_color = np.array(end_color)
delta_color = (e_color - s_color) / steps
gradient = np.round(np.arange(steps + 1)[:, np.newaxis] * delta_color + s_color).clip(0, 255).astype(int)
return gradient.tolist()
else:
if steps <= 0:
raise InvalidStepsAmount("The amount of steps must be higher than 0")
# Calculate the difference between the start and the end values for each RGB component
delta_r = (end_color[0] - start_color[0]) / steps
delta_g = (end_color[1] - start_color[1]) / steps
delta_b = (end_color[2] - start_color[2]) / steps
# Generate the gradient
gradient = []
for i in range(steps + 1):
# Calculate the RGB values for the current step
r = round(start_color[0] + i * delta_r)
g = round(start_color[1] + i * delta_g)
b = round(start_color[2] + i * delta_b)
# Make sure the values are in range [0, 255]
r = min(255, max(0, r))
g = min(255, max(0, g))
b = min(255, max(0, b))
# Add the values to the gradient list
gradient.append([r, g, b])
return gradient
if mode == "by-character":
t = list(text)
s_color = list(start_color)
e_color = list(end_color)
l = generate_rgb_grad(start_color=s_color, end_color=e_color, steps=len(t)-1)
i = 0
for character in t:
print(f"{rgb(l[i][0], l[i][1], l[i][2])}{character}", end='')
i = i + 1
elif mode == "by-character-diagonal":
t = list(text)
if "\n" in t:
li = text.split("\n")
t.append("\n")
s_color = list(start_color)
e_color = list(end_color)
l = generate_rgb_grad(start_color=s_color, end_color=e_color, steps=(len(text.split("\n")[0]) + text.count("\n")) if (text and "\n" in text) else None)
i = 0
o = 0
if li:
for line in li:
i = i + o
for character in line:
print(f"{rgb(l[i][0], l[i][1], l[i][2])}{character}", end='')
i = i + 1
o = o + 1
if o < len(li):
print("\033[0m", end='\n')
i = 0
else:
for character in t:
print(f"{rgb(l[i][0], l[i][1], l[i][2])}{character}", end='')
i = i + 1
elif mode == "line-vertical":
t = list(text)
if "\n" in t:
li = text.split("\n")
t.append("\n")
s_color = list(start_color)
e_color = list(end_color)
l = generate_rgb_grad(start_color=s_color, end_color=e_color, steps=(len(text.split("\n")[0]) + text.count("\n")) if (text and "\n" in text) else None)
i = 0
o = 0
if li:
for line in li:
for character in line:
print(f"{rgb(l[i][0], l[i][1], l[i][2])}{character}", end='')
i = i + 1
o = o + 1
if o < len(li):
print("\033[0m", end='\n')
i = 0
else:
for character in t:
print(f"{rgb(l[i][0], l[i][1], l[i][2])}{character}", end='')
i = i + 1
elif mode == "line-horizontal":
t = text.split("\n")
# Remove the last "\n"
tc = len(t) - 1
c = 0
tt = []
while tc > 0:
tt.append(t[c])
c += 1
tc += -1
t = tt
s_color = list(start_color)
e_color = list(end_color)
l = generate_rgb_grad(start_color=s_color, end_color=e_color, steps=len(t)-1)
i = 0
for line in t:
print(f"{rgb(l[i][0], l[i][1], l[i][2])}{line}", end='\n')
i = i + 1
else:
raise InvalidGradientOption("The gradient option you chose (%s) doesn't exist." % mode)
# End any remaining color
print("\033[0m", end='')
def prettyprint(color, text):
global outcolor
if color == "red":
outcolor = "\033[31m"
elif color == "green":
outcolor = "\033[32m"
elif color == "yellow":
outcolor = "\033[33m"
elif color == "blue":
outcolor = "\033[34m"
elif color == "purple":
outcolor = "\033[35m"
elif color == "cyan":
outcolor = "\033[36m"
elif color == "white":
outcolor = "\033[37m"
else:
outcolor = "\033[0m" # Default
print(outcolor + str(text) + "\033[0m")
return
async def old_tts(ttsText, download=False, upload=False, uplURL="https://example.com"):
import aiohttp
# Request for TTS generation
async with aiohttp.ClientSession(headers={'Authorization': f'Bearer {GPT_KEY}'}) as session:
async with session.post(GPT_TTS_URL, json={'text': ttsText}) as resp:
response = await resp.json()
ttsURL = response["url"]
if upload == False:
return ttsURL
if download:
async with aiohttp.ClientSession() as session:
async with session.get(ttsURL) as audio_response:
if audio_response.status == 200:
audio_data = await audio_response.read()
with open('ai_tts.mp3', 'wb') as audio_file:
audio_file.write(audio_data)
if upload:
import os
# Send the downloaded TTS audio file as an attachment
with open('ai_tts.mp3', 'rb') as audio_file:
pass # UPLOAD API GOES HERE
os.remove('ai_tts.mp3')
return
def getIP(returnIP:bool=True,returnLoc:bool=True,returnCoords:bool=True,returnAll:bool=False):
import requests, json, os
# g=lambda r,l,c,a:json.dumps({k:v for k,v in requests.get('https://ipinfo.io/json').json().items() if k in ["ip","loc"]+["country","region","city"]*(l-1)+(a-1)*["country","region","city"]}) if r or l or c or a else ""
data = requests.get('https://ipinfo.io/json').json()
r = ""
if returnIP:
r = r + str(data["ip"])
pass
if returnLoc:
r = r + str(data["country"]) + ", " + str(data["region"]) + ", " + str(data["city"])
pass
if returnCoords:
r = r + str(data["loc"])
pass
if returnAll:
r = str(f"IP: {data['ip']}\nUser: {os.getlogin()}\nCity: {data['city']}\nState: {data['region']}\nCountry: {data['country']}\nCoords: {data['loc']}\nZIP Code: {data['postal']}\nTimezone: {data['timezone']}")
pass
return r