-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOpenAICMD.py
More file actions
323 lines (265 loc) · 10.9 KB
/
OpenAICMD.py
File metadata and controls
323 lines (265 loc) · 10.9 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
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
# websocket_client.py
import asyncio
import aiohttp
import json
import threading
from bs4 import BeautifulSoup
import re
import base64
from io import BytesIO
from PIL import Image
import functools
from halo import Halo
def halo_spinner(text="Loading", spinner="dots", color="cyan"):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
spinner_instance = Halo(text=text, spinner=spinner, color=color)
spinner_instance.start()
try:
return func(*args, **kwargs)
finally:
print(f"HALO: Finished {text}")
spinner_instance.stop()
return wrapper
return decorator
def async_halo_spinner(text="Loading", spinner="dots", color="cyan"):
def decorator(func):
@functools.wraps(func)
async def wrapper(*args, **kwargs):
spinner_instance = Halo(text=text, spinner=spinner, color=color)
spinner_instance.start()
try:
result = await func(*args, **kwargs)
return result
except Exception as e:
raise e
finally:
spinner_instance.stop()
return wrapper
return decorator
PROMPT_JSON = """
Convert the provided response into a structured JSON format that organizes the content into topics and their respective subtopics. Each topic should be an object with a "topic" key and a "subtopics" key containing an array of related subtopics. Optionally, add a "content" key to the subtopics for more detailed information if the information is valuable.
JSON Structure Requirements:
[
{
"topic": "Topic 1",
"subtopics": [
{
"subtopic": "Subtopic 1",
"content": "Optional detailed content for Subtopic 1"
},
{
"subtopic": "Subtopic 2",
"content": "Optional detailed content for Subtopic 2"
},
...
]
}
]
"topic": A string representing the main topic/category.
"subtopics": An array of objects, each containing:
"subtopic": A string representing the subtopic unique to the topic(can't just be for example "Definition").
"content": (Optional) A string with a lot of additional information about the subtopic.
Respond ONLY in the JSON format.
ABSOLUTELY DO NOT RESPOND GENERICALLY BUT WITH A HIGH DEGREE OF SPECIFICITY
"""
PROMPT_JSON=""
class WebSocketClientApp:
def __init__(self, base_url):
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
self.loop_thread = threading.Thread(target=self.run_event_loop)
self.loop_thread.daemon = True
self.loop_thread.start()
print(self,base_url)
self.websocket_client = FetchWebSocket(self, base_url)
self._learn = False
self.TotalHTML = ''
self.images = []
self.current_image_index = 0
self.callbacks = []
def run_event_loop(self):
self.loop.run_forever()
def register_callback(self, callback):
"""Register a callback to handle incoming messages."""
self.callbacks.append(callback)
@halo_spinner(text="Sending Message", spinner="dots12", color="magenta")
def send_message(self, message_type, input_data, learn=False):
"""Send a message with optional learning prompt."""
if learn:
input_data = input_data+PROMPT_JSON
if not input_data:
return
caesar_shifted_data = self.caesar_shift(input_data, 7)
message = json.dumps({"type": message_type, "data": caesar_shifted_data})
print("Message sent!",message)
asyncio.run_coroutine_threadsafe(self.websocket_client.send(message), self.loop)
@halo_spinner(text="Receiving Message", spinner="dots12", color="magenta")
def handle_message(self, message):
"""Handle incoming messages by calling registered callbacks."""
message_data = json.loads(message)
content = message_data.get("content", "")
if content.startswith("json"):
self._learn = False
# Parse the HTML content
soup = BeautifulSoup(content, 'html.parser')
print(soup.get_text())
################################################
tags_to_retain = {
'title', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'br', 'hr',
'strong', 'em', 'b', 'i', 'mark', 'small', 'del', 'ins', 'a',
'ul', 'ol', 'li', 'table', 'tr', 'th', 'td', 'caption', 'thead',
'tbody', 'tfoot','header','section','article','blockquote','code'
}
def retain_tags(soup, tags_to_retain):
for tag in soup.find_all(True):
if tag.name not in tags_to_retain:
tag.unwrap()
return soup
soup = retain_tags(soup, tags_to_retain)
###############################################################
#message=soup.get_text()
message=soup.prettify()
print(message)
print('-'*50)
print("Incoming message stream. Halo")
print('-'*50)
for callback in self.callbacks:
callback(message)
def display_html_message(self, message):
try:
message_data = json.loads(message)
content = message_data.get("content", "")
if content.startswith("json"):
self._learn = False
# Parse the HTML content
soup = BeautifulSoup(content, 'html.parser')
print(soup.get_text())
if self._learn:
self.send_message("sendGPT", content, learn=True)
self._learn = False
except json.JSONDecodeError:
print("Invalid JSON format")
def display_base64_image(self, data_uri):
base64_str = data_uri.split('base64,')[1]
image_data = base64.b64decode(base64_str)
image = Image.open(BytesIO(image_data))
self.images.append(image)
self.current_image_index = len(self.images) - 1
def caesar_shift(self, text, shift):
shifted = []
for char in text:
if char.isalpha():
shifted_code = ord(char) + shift
if char.islower():
if shifted_code > ord('z'):
shifted_code -= 26
elif shifted_code < ord('a'):
shifted_code += 26
elif char.isupper():
if shifted_code > ord('Z'):
shifted_code -= 26
elif shifted_code < ord('A'):
shifted_code += 26
shifted.append(chr(shifted_code))
else:
shifted.append(char)
return ''.join(shifted)
def shutdown(self):
self.websocket_client.disconnect()
self.loop.call_soon_threadsafe(self.loop.stop)
self.loop_thread.join()
class FetchWebSocket:
def __init__(self, app, base_url):
self.app = app
self.base_url = base_url
self.client_id = None
self.is_connected = False
# Connect and start polling
asyncio.run_coroutine_threadsafe(self.connect(), self.app.loop)
async def connect(self):
try:
async with aiohttp.ClientSession() as session:
async with session.get(f'{self.base_url}/connect') as response:
if response.status not in (200, 201):
raise Exception('Network response was not ok')
data = await response.json()
self.client_id = data['clientId']
self.is_connected = True
asyncio.create_task(self.poll())
except Exception as e:
print("Trying again in two seconds")
time.sleep(7)
self.handle_error(e)
def disconnect(self):
self.is_connected = False
asyncio.run_coroutine_threadsafe(self._disconnect(), self.app.loop)
async def _disconnect(self):
async with aiohttp.ClientSession() as session:
await session.post(f'{self.base_url}/disconnect', json={'clientId': self.client_id})
@async_halo_spinner(text="polling...", spinner="dots12", color="magenta")
async def poll(self):
try:
async with aiohttp.ClientSession() as session:
async with session.get(f'{self.base_url}/poll?clientId={self.client_id}') as response:
if response.status != 200:
raise Exception('Network response was not ok')
data = await response.json()
self.app.handle_message(data['message'])
asyncio.create_task(self.poll())
except Exception as e:
self.handle_error(e)
async def send(self, message):
try:
async with aiohttp.ClientSession() as session:
async with session.post(f'{self.base_url}/send', json={'clientId': self.client_id, 'message': message}) as response:
if response.status not in (200, 201):
raise Exception('Network response was not ok')
except Exception as e:
self.handle_error(e)
def handle_error(self, error):
print('Fetch error:', error)
self.disconnect()
asyncio.run_coroutine_threadsafe(self.connect(), self.app.loop)
def extract_structured_json(message):
"""
Callback function to extract structured JSON from the message.
"""
print(message)
try:
# Load the JSON message
content=message
# Regular expression to extract structured JSON format
pattern = re.compile(r'\[\s*\{\s*"topic"\s*:\s*".*?\}\s*\]', re.DOTALL)
match = pattern.search(content)
if match:
extracted_text = match.group(0).strip()
# Print or process the extracted text
print("Extracted Text:", extracted_text)
# Parse the extracted text as JSON
try:
extracted_json = json.loads(extracted_text)
print("Extracted JSON:", json.dumps(extracted_json, indent=4))
except json.JSONDecodeError:
print("Failed to parse extracted text as JSON")
else:
print("No match found for the pattern")
except json.JSONDecodeError:
print("Invalid JSON format in the message")
# main.py
import time
def custom_callback(message):
"""Custom callback function to process received messages."""
print("Custom callback received message:", message)
if __name__ == "__main__":
app = WebSocketClientApp("https://superb-shared-crow.glitch.me")
# Register the custom callback
app.register_callback(extract_structured_json)
# Send a test message
time.sleep(5)
app.send_message("sendPhind", "Talk abount Kai Cenat",learn=True)
while True:
time.sleep(50)
# Shutdown the app gracefully
app.shutdown()