Arcypojeb commited on
Commit
59196d8
1 Parent(s): 7e78314

Upload 2 files

Browse files
Files changed (2) hide show
  1. pages/Connect.py +426 -0
  2. pages/G.py +557 -0
pages/Connect.py ADDED
@@ -0,0 +1,426 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import datetime
3
+ import asyncio
4
+ import sqlite3
5
+ import g4f
6
+ import streamlit as st
7
+ import fireworks.client
8
+ import streamlit.components.v1 as components
9
+ from ServG4F import WebSocketServer1
10
+ from ServFire import WebSocketServer
11
+ from ServChar import WebSocketServer2
12
+ from clientG4F import WebSocketClient1
13
+ from PyCharacterAI import Client
14
+ from clientFireworks import WebSocketClient
15
+ from clientCharacter import WebSocketClient2
16
+ from websockets.sync.client import connect
17
+
18
+ client = Client()
19
+
20
+ servers = {}
21
+ clients = {}
22
+ inputs = []
23
+ outputs = []
24
+ used_ports = []
25
+ server_ports = []
26
+ client_ports = []
27
+
28
+ system_instruction = "You are now integrated with a local websocket server in a project of hierarchical cooperative multi-agent framework called NeuralGPT. Your main job is to coordinate simultaneous work of multiple LLMs connected to you as clients. Each LLM has a model (API) specific ID to help you recognize different clients in a continuous chat thread (template: <NAME>-agent and/or <NAME>-client). Your chat memory module is integrated with a local SQL database with chat history. Your primary objective is to maintain the logical and chronological order while answering incoming messages and to send your answers to the correct clients to maintain synchronization of the question->answer logic. However, please note that you may choose to ignore or not respond to repeating inputs from specific clients as needed to prevent unnecessary traffic."
29
+
30
+ db = sqlite3.connect('chat-hub.db')
31
+ cursor = db.cursor()
32
+ cursor.execute('CREATE TABLE IF NOT EXISTS messages (id INTEGER PRIMARY KEY AUTOINCREMENT, sender TEXT, message TEXT, timestamp TEXT)')
33
+ db.commit()
34
+
35
+ st.set_page_config(layout="wide")
36
+
37
+ async def askCharacter(token, character_ID, question):
38
+
39
+ await client.authenticate_with_token(token)
40
+ chat = await client.create_or_continue_chat(character_ID)
41
+ print(f"User B: {question}")
42
+ timestamp = datetime.datetime.now().isoformat()
43
+ sender = 'client'
44
+ db = sqlite3.connect('chat-hub.db')
45
+ db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
46
+ (sender, question, timestamp))
47
+ db.commit()
48
+ try:
49
+ answer = await chat.send_message(question)
50
+ response = f"{answer.src_character_name}: {answer.text}"
51
+ print(response)
52
+ timestamp = datetime.datetime.now().isoformat()
53
+ serverSender = 'server'
54
+ db = sqlite3.connect('chat-hub.db')
55
+ db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
56
+ (serverSender, response, timestamp))
57
+ db.commit()
58
+ return response
59
+
60
+ except Exception as e:
61
+ print(f"Error: {e}")
62
+
63
+ async def askQuestion(question):
64
+ try:
65
+ db = sqlite3.connect('chat-hub.db')
66
+ cursor = db.cursor()
67
+ cursor.execute("SELECT * FROM messages ORDER BY timestamp DESC LIMIT 30")
68
+ messages = cursor.fetchall()
69
+ messages.reverse()
70
+
71
+ past_user_inputs = []
72
+ generated_responses = []
73
+
74
+ for message in messages:
75
+ if message[1] == 'client':
76
+ past_user_inputs.append(message[2])
77
+ else:
78
+ generated_responses.append(message[2])
79
+
80
+ response = await g4f.ChatCompletion.create_async(
81
+ model=g4f.models.gpt_4,
82
+ provider=g4f.Provider.Bing,
83
+ messages=[
84
+ {"role": "system", "content": system_instruction},
85
+ *[{"role": "user", "content": message} for message in past_user_inputs],
86
+ *[{"role": "assistant", "content": message} for message in generated_responses],
87
+ {"role": "user", "content": question}
88
+ ])
89
+
90
+ print(response)
91
+ return response
92
+
93
+ except Exception as e:
94
+ print(e)
95
+
96
+ async def chatCompletion(question):
97
+ fireworks.client.api_key = st.session_state.api_key
98
+ try:
99
+ # Connect to the database and get the last 30 messages
100
+ db = sqlite3.connect('chat-hub.db')
101
+ cursor = db.cursor()
102
+ cursor.execute("SELECT * FROM messages ORDER BY timestamp DESC LIMIT 10")
103
+ messages = cursor.fetchall()
104
+ messages.reverse()
105
+
106
+ # Extract user inputs and generated responses from the messages
107
+ past_user_inputs = []
108
+ generated_responses = []
109
+
110
+ for message in messages:
111
+ if message[1] == 'client':
112
+ past_user_inputs.append(message[2])
113
+ else:
114
+ generated_responses.append(message[2])
115
+
116
+ # Prepare data to send to the chatgpt-api.shn.hk
117
+ response = fireworks.client.ChatCompletion.create(
118
+ model="accounts/fireworks/models/llama-v2-7b-chat",
119
+ messages=[
120
+ {"role": "system", "content": system_instruction},
121
+ *[{"role": "user", "content": input} for input in past_user_inputs],
122
+ *[{"role": "assistant", "content": response} for response in generated_responses],
123
+ {"role": "user", "content": question}
124
+ ],
125
+ stream=False,
126
+ n=1,
127
+ max_tokens=2500,
128
+ temperature=0.5,
129
+ top_p=0.7,
130
+ )
131
+
132
+ answer = response.choices[0].message.content
133
+ print(answer)
134
+ return str(answer)
135
+
136
+ except Exception as error:
137
+ print("Error while fetching or processing the response:", error)
138
+ return "Error: Unable to generate a response."
139
+
140
+ async def handleUser(userInput):
141
+ print(f"User B: {userInput}")
142
+ timestamp = datetime.datetime.now().isoformat()
143
+ sender = 'client'
144
+ db = sqlite3.connect('chat-hub.db')
145
+ db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
146
+ (sender, userInput, timestamp))
147
+ db.commit()
148
+ try:
149
+ response2 = await chatCompletion(userInput)
150
+ print(f"Llama2: {response2}")
151
+ serverSender = 'server'
152
+ timestamp = datetime.datetime.now().isoformat()
153
+ db = sqlite3.connect('chat-hub.db')
154
+ db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
155
+ (serverSender, response2, timestamp))
156
+ db.commit()
157
+ return response2
158
+
159
+ except Exception as e:
160
+ print(f"Error: {e}")
161
+
162
+ async def handleUser2(userInput):
163
+ print(f"User B: {userInput}")
164
+ timestamp = datetime.datetime.now().isoformat()
165
+ sender = 'client'
166
+ db = sqlite3.connect('chat-hub.db')
167
+ db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
168
+ (sender, userInput, timestamp))
169
+ db.commit()
170
+ try:
171
+ response3 = await askQuestion(userInput)
172
+ print(f"GPT4Free: {response3}")
173
+ serverSender = 'server'
174
+ timestamp = datetime.datetime.now().isoformat()
175
+ db = sqlite3.connect('chat-hub.db')
176
+ db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
177
+ (serverSender, response3, timestamp))
178
+ db.commit()
179
+ return response3
180
+
181
+ except Exception as e:
182
+ print(f"Error: {e}")
183
+
184
+ async def main():
185
+
186
+ if "server_ports" not in st.session_state:
187
+ st.session_state['server_ports'] = ""
188
+ if "client_ports" not in st.session_state:
189
+ st.session_state['client_ports'] = ""
190
+ if "user_ID" not in st.session_state:
191
+ st.session_state.user_ID = ""
192
+ if "gradio_Port" not in st.session_state:
193
+ st.session_state.gradio_Port = ""
194
+ if "servers" not in st.session_state:
195
+ st.session_state.servers = None
196
+ if "server" not in st.session_state:
197
+ st.session_state.server = False
198
+ if "client" not in st.session_state:
199
+ st.session_state.client = False
200
+ if "api_key" not in st.session_state:
201
+ st.session_state.api_key = ""
202
+ if "tokenChar" not in st.session_state:
203
+ st.session_state.tokenChar = ""
204
+ if "charName" not in st.session_state:
205
+ st.session_state.charName = ""
206
+ if "character_ID" not in st.session_state:
207
+ st.session_state.character_ID = ""
208
+
209
+ st.sidebar.text("Server ports:")
210
+ serverPorts = st.sidebar.container(border=True)
211
+ serverPorts.markdown(st.session_state['server_ports'])
212
+ st.sidebar.text("Client ports")
213
+ clientPorts = st.sidebar.container(border=True)
214
+ clientPorts.markdown(st.session_state['client_ports'])
215
+ st.sidebar.text("Character.ai ID")
216
+ user_id = st.sidebar.container(border=True)
217
+ user_id.markdown(st.session_state.user_ID)
218
+
219
+ st.title("Servers Page")
220
+
221
+ c1, c2 = st.columns(2)
222
+
223
+ with c1:
224
+ websocketPort = st.number_input("Websocket server port", min_value=1000, max_value=9999, value=1000)
225
+ startServer = st.button("Start server")
226
+ stoptServer = st.button("Stop server")
227
+ st.text("Server ports")
228
+ serverPorts1 = st.container(border=True)
229
+ serverPorts1.markdown(st.session_state['server_ports'])
230
+
231
+ with c2:
232
+ clientPort = st.number_input("Websocket client port", min_value=1000, max_value=9999, value=1000)
233
+ runClient = st.button("Start client")
234
+ stoptClient = st.button("Stop client")
235
+ st.text("Client ports")
236
+ clientPorts1 = st.container(border=True)
237
+ clientPorts1.markdown(st.session_state['client_ports'])
238
+
239
+ selectServ = st.selectbox("Select source", ("Fireworks", "GPT4Free", "character.ai", "ChainDesk", "Flowise", "DocsBot"))
240
+
241
+ if selectServ == "Fireworks":
242
+ fireworksAPI = st.text_input("Fireworks API")
243
+ userInput = st.text_input("Ask agent 1")
244
+
245
+ if startServer:
246
+ fireworks.client.api_key = fireworksAPI
247
+ st.session_state.api_key = fireworks.client.api_key
248
+ server_ports.append(websocketPort)
249
+ st.session_state['server_ports'] = server_ports
250
+ serverPorts.markdown(st.session_state['server_ports'])
251
+ serverPorts1.markdown(st.session_state['server_ports'])
252
+ try:
253
+ server = WebSocketServer("localhost", websocketPort)
254
+ print(f"Starting WebSocket server on port {websocketPort}...")
255
+ await server.start_server()
256
+ await asyncio.Future()
257
+
258
+ except Exception as e:
259
+ print(f"Error: {e}")
260
+
261
+ if runClient:
262
+ fireworks.client.api_key = fireworksAPI
263
+ st.session_state.api_key = fireworks.client.api_key
264
+ client_ports.append(clientPort)
265
+ st.session_state['client_ports'] = client_ports
266
+ clientPorts.markdown(st.session_state['client_ports'])
267
+ clientPorts1.markdown(st.session_state['client_ports'])
268
+ try:
269
+ uri = f'ws://localhost:{clientPort}'
270
+ client = WebSocketClient(uri)
271
+ print(f"Connecting client on port {clientPort}...")
272
+ await client.startClient()
273
+ st.session_state.client = client
274
+ await asyncio.Future()
275
+
276
+ except Exception as e:
277
+ print(f"Error: {e}")
278
+
279
+ if userInput:
280
+ print(f"User B: {userInput}")
281
+ fireworks.client.api_key = fireworksAPI
282
+ st.session_state.api_key = fireworks.client.api_key
283
+ user_input = st.chat_message("human")
284
+ user_input.markdown(userInput)
285
+ response1 = await handleUser(userInput)
286
+ print(response1)
287
+ outputMsg = st.chat_message("ai")
288
+ outputMsg.markdown(response1)
289
+
290
+ if selectServ == "GPT4Free":
291
+
292
+ userInput1 = st.text_input("Ask agent_2")
293
+
294
+ if startServer:
295
+ server_ports.append(websocketPort)
296
+ st.session_state['server_ports'] = server_ports
297
+ serverPorts.markdown(st.session_state['server_ports'])
298
+ serverPorts1.markdown(st.session_state['server_ports'])
299
+ try:
300
+ server1 = WebSocketServer1("localhost", websocketPort)
301
+ print(f"Starting WebSocket server on port {websocketPort}...")
302
+ await server1.start_server()
303
+ st.session_state.server = server1
304
+ await asyncio.Future()
305
+
306
+ except Exception as e:
307
+ print(f"Error: {e}")
308
+
309
+ if runClient:
310
+ client_ports.append(clientPort)
311
+ st.session_state['client_ports'] = client_ports
312
+ clientPorts.markdown(st.session_state['client_ports'])
313
+ clientPorts1.markdown(st.session_state['client_ports'])
314
+ try:
315
+ uri = f'ws://localhost:{clientPort}'
316
+ client1 = WebSocketClient1(uri)
317
+ print(f"Connecting client on port {clientPort}...")
318
+ await client1.startClient()
319
+ st.session_state.client = client1
320
+ await asyncio.Future()
321
+
322
+ except Exception as e:
323
+ print(f"Error: {e}")
324
+
325
+ if userInput1:
326
+ user_input1 = st.chat_message("human")
327
+ user_input1.markdown(userInput1)
328
+ response = await handleUser2(userInput1)
329
+ outputMsg1 = st.chat_message("ai")
330
+ outputMsg1.markdown(response)
331
+
332
+ if selectServ == "character.ai":
333
+
334
+ z1, z2 = st.columns(2)
335
+
336
+ with z1:
337
+ token = st.text_input("User token")
338
+
339
+ with z2:
340
+ characterID = st.text_input("Character ID")
341
+
342
+ userID = st.container(border=True)
343
+ userID.markdown(st.session_state.user_ID)
344
+
345
+ userInput2 = st.text_input("Ask agent 3")
346
+
347
+ if startServer:
348
+ client = Client()
349
+ server_ports.append(websocketPort)
350
+ st.session_state['server_ports'] = server_ports
351
+ serverPorts.markdown(st.session_state['server_ports'])
352
+ serverPorts1.markdown(st.session_state['server_ports'])
353
+ st.session_state.tokenChar = token
354
+ st.session_state.character_ID = characterID
355
+ await client.authenticate_with_token(token)
356
+ username = (await client.fetch_user())['user']['username']
357
+ st.session_state.user_ID = username
358
+ user_id.markdown(st.session_state.user_ID)
359
+ userID.markdown(st.session_state.user_ID)
360
+ try:
361
+ server2 = WebSocketServer2("localhost", websocketPort)
362
+ print(f"Starting WebSocket server on port {websocketPort}...")
363
+ await server2.start_server()
364
+ st.session_state.server = server2
365
+ await asyncio.Future()
366
+
367
+ except Exception as e:
368
+ print(f"Error: {e}")
369
+
370
+ if runClient:
371
+ client = Client()
372
+ client_ports.append(clientPort)
373
+ st.session_state['client_ports'] = client_ports
374
+ clientPorts.markdown(st.session_state['client_ports'])
375
+ clientPorts1.markdown(st.session_state['client_ports'])
376
+ st.session_state.tokenChar = token
377
+ st.session_state.character_ID = characterID
378
+ await client.authenticate_with_token(token)
379
+ username = (await client.fetch_user())['user']['username']
380
+ st.session_state.user_ID = username
381
+ user_id.markdown(st.session_state.user_ID)
382
+ userID.markdown(st.session_state.user_ID)
383
+ try:
384
+ uri = f'ws://localhost:{clientPort}'
385
+ client2 = WebSocketClient2(uri)
386
+ print(f"Connecting client on port {clientPort}...")
387
+ await client2.startClient()
388
+ st.session_state.client = client2
389
+ await asyncio.Future()
390
+
391
+ except Exception as e:
392
+ print(f"Error: {e}")
393
+
394
+ if userInput2:
395
+ client = Client()
396
+ await client.authenticate_with_token(token)
397
+ user_input2 = st.chat_message("human")
398
+ user_input2.markdown(userInput2)
399
+ st.session_state.tokenChar = token
400
+ st.session_state.character_ID = characterID
401
+
402
+ username = (await client.fetch_user())['user']['username']
403
+ st.session_state.user_ID = username
404
+ user_id.markdown(st.session_state.user_ID)
405
+ userID.markdown(st.session_state.user_ID)
406
+ try:
407
+ answer = await askCharacter(token, characterID, userInput2)
408
+ outputMsg1 = st.chat_message("ai")
409
+ outputMsg1.markdown(answer)
410
+
411
+ except Exception as e:
412
+ print(f"Error: {e}")
413
+
414
+ if selectServ == "ChainDesk":
415
+ url = f"http://localhost:8000/comp.html"
416
+ st.components.v1.iframe(url, height=950, scrolling=True)
417
+
418
+ if selectServ == "Flowise":
419
+ url = f"http://localhost:8000/flowise.html"
420
+ st.components.v1.iframe(url, height=950, scrolling=True)
421
+
422
+ if selectServ == "DocsBot":
423
+ url = f"http://localhost:8000/Docsbotport.html"
424
+ st.components.v1.iframe(url, height=950, scrolling=True)
425
+
426
+ asyncio.run(main())
pages/G.py ADDED
@@ -0,0 +1,557 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import datetime
3
+ import asyncio
4
+ import sqlite3
5
+ import g4f
6
+ import websockets
7
+ import streamlit as st
8
+ import fireworks.client
9
+ import gradio as gr
10
+ import streamlit.components.v1 as components
11
+ from PyCharacterAI import Client
12
+ from ServG4F import WebSocketServer1
13
+ from ServFire import WebSocketServer
14
+ from ServChar import WebSocketServer2
15
+ from clientG4F import WebSocketClient1
16
+ from clientFireworks import WebSocketClient
17
+ from clientCharacter import WebSocketClient2
18
+ from websockets.sync.client import connect
19
+
20
+ client = Client()
21
+
22
+ servers = {}
23
+ clients = {}
24
+ inputs = []
25
+ outputs = []
26
+ used_ports = []
27
+ server_ports = []
28
+ client_ports = []
29
+ chat_history = []
30
+ ports_g4f = []
31
+ ports_fire = []
32
+ serv_char = []
33
+ serv_g4f = []
34
+
35
+ system_instruction = "You are now integrated with a local websocket server in a project of hierarchical cooperative multi-agent framework called NeuralGPT. Your main job is to coordinate simultaneous work of multiple LLMs connected to you as clients. Each LLM has a model (API) specific ID to help you recognize different clients in a continuous chat thread (template: <NAME>-agent and/or <NAME>-client). Your chat memory module is integrated with a local SQL database with chat history. Your primary objective is to maintain the logical and chronological order while answering incoming messages and to send your answers to the correct clients to maintain synchronization of the question->answer logic. However, please note that you may choose to ignore or not respond to repeating inputs from specific clients as needed to prevent unnecessary traffic."
36
+
37
+ db = sqlite3.connect('chat-hub.db')
38
+ cursor = db.cursor()
39
+ cursor.execute('CREATE TABLE IF NOT EXISTS messages (id INTEGER PRIMARY KEY AUTOINCREMENT, sender TEXT, message TEXT, timestamp TEXT)')
40
+ db.commit()
41
+
42
+ st.set_page_config(layout="wide")
43
+
44
+ async def askQuestion(question):
45
+ try:
46
+ db = sqlite3.connect('chat-hub.db')
47
+ cursor = db.cursor()
48
+ cursor.execute("SELECT * FROM messages ORDER BY timestamp DESC LIMIT 30")
49
+ messages = cursor.fetchall()
50
+ messages.reverse()
51
+
52
+ past_user_inputs = []
53
+ generated_responses = []
54
+
55
+ for message in messages:
56
+ if message[1] == 'client':
57
+ past_user_inputs.append(message[2])
58
+ else:
59
+ generated_responses.append(message[2])
60
+
61
+ response = await g4f.ChatCompletion.create_async(
62
+ model=g4f.models.gpt_4,
63
+ provider=g4f.Provider.Bing,
64
+ messages=[
65
+ {"role": "system", "content": system_instruction},
66
+ *[{"role": "user", "content": message} for message in past_user_inputs],
67
+ *[{"role": "assistant", "content": message} for message in generated_responses],
68
+ {"role": "user", "content": question}
69
+ ])
70
+
71
+ print(response)
72
+ return response
73
+
74
+ except Exception as e:
75
+ print(e)
76
+
77
+ async def chatCompletion(fireworksAPI, question):
78
+ fireworks.client.api_key = fireworksAPI
79
+ st.session_state.api_key = fireworksAPI
80
+ try:
81
+ # Connect to the database and get the last 30 messages
82
+ db = sqlite3.connect('chat-hub.db')
83
+ cursor = db.cursor()
84
+ cursor.execute("SELECT * FROM messages ORDER BY timestamp DESC LIMIT 10")
85
+ messages = cursor.fetchall()
86
+ messages.reverse()
87
+
88
+ # Extract user inputs and generated responses from the messages
89
+ past_user_inputs = []
90
+ generated_responses = []
91
+
92
+ for message in messages:
93
+ if message[1] == 'client':
94
+ past_user_inputs.append(message[2])
95
+ else:
96
+ generated_responses.append(message[2])
97
+
98
+ # Prepare data to send to the chatgpt-api.shn.hk
99
+ response = fireworks.client.ChatCompletion.create(
100
+ model="accounts/fireworks/models/llama-v2-7b-chat",
101
+ messages=[
102
+ {"role": "system", "content": system_instruction},
103
+ *[{"role": "user", "content": input} for input in past_user_inputs],
104
+ *[{"role": "assistant", "content": response} for response in generated_responses],
105
+ {"role": "user", "content": question}
106
+ ],
107
+ stream=False,
108
+ n=1,
109
+ max_tokens=2500,
110
+ temperature=0.5,
111
+ top_p=0.7,
112
+ )
113
+
114
+ answer = response.choices[0].message.content
115
+ print(answer)
116
+ return str(answer)
117
+
118
+ except Exception as error:
119
+ print("Error while fetching or processing the response:", error)
120
+ return "Error: Unable to generate a response."
121
+
122
+ async def main():
123
+
124
+ if "server_ports" not in st.session_state:
125
+ st.session_state['server_ports'] = ""
126
+ if "client_ports" not in st.session_state:
127
+ st.session_state['client_ports'] = ""
128
+ if "chat_history" not in st.session_state:
129
+ st.session_state['chat_history'] = ""
130
+ if "user_ID" not in st.session_state:
131
+ st.session_state.user_ID = ""
132
+ if "gradio_Port" not in st.session_state:
133
+ st.session_state.gradio_Port = ""
134
+ if "servers" not in st.session_state:
135
+ st.session_state.servers = None
136
+ if "server" not in st.session_state:
137
+ st.session_state.server = False
138
+ if "client" not in st.session_state:
139
+ st.session_state.client = False
140
+ if "api_key" not in st.session_state:
141
+ st.session_state.api_key = ""
142
+ if "tokenChar" not in st.session_state:
143
+ st.session_state.tokenChar = ""
144
+ if "charName" not in st.session_state:
145
+ st.session_state.charName = ""
146
+ if "character_ID" not in st.session_state:
147
+ st.session_state.character_ID = ""
148
+
149
+ gradioPorts = st.container(border=True)
150
+ gradioPort = st.number_input("Gradio server port", min_value=1000, max_value=9999, value=1000)
151
+ startGradio = st.button("Start Gradio")
152
+
153
+ st.sidebar.text("Server ports:")
154
+ serverPorts = st.sidebar.container(border=True)
155
+ serverPorts.markdown(st.session_state['server_ports'])
156
+ st.sidebar.text("Client ports")
157
+ clientPorts = st.sidebar.container(border=True)
158
+ clientPorts.markdown(st.session_state['client_ports'])
159
+ st.sidebar.text("Character.ai ID")
160
+ user_id = st.sidebar.container(border=True)
161
+ user_id.markdown(st.session_state.user_ID)
162
+ chat_Area = st.container(border=True)
163
+
164
+
165
+
166
+ async def askCharacter(token, character_ID, question):
167
+
168
+ chatbox2 = st.container(border=True)
169
+
170
+ await client.authenticate_with_token(token)
171
+ username = (await client.fetch_user())['user']['username']
172
+ chat = await client.create_or_continue_chat(character_ID)
173
+ input = f"User B: {question}"
174
+ chatbox2.chat_message("human").write(input)
175
+ st.session_state.tokenChar = token
176
+ st.session_state.charName = username
177
+ st.session_state.character_ID = character_ID
178
+ st.session_state.user_ID = username
179
+
180
+ timestamp = datetime.datetime.now().isoformat()
181
+ sender = 'client'
182
+ db = sqlite3.connect('chat-hub.db')
183
+ db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
184
+ (sender, question, timestamp))
185
+ db.commit()
186
+ try:
187
+ answer = await chat.send_message(input)
188
+ response = f"{answer.src_character_name}: {answer.text}"
189
+ print(response)
190
+ chatbox2.chat_message("ai").write(response)
191
+ timestamp = datetime.datetime.now().isoformat()
192
+ serverSender = 'server'
193
+ db = sqlite3.connect('chat-hub.db')
194
+ db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
195
+ (serverSender, response, timestamp))
196
+ db.commit()
197
+ return response
198
+
199
+ except Exception as e:
200
+ print(f"Error: {e}")
201
+
202
+ with st.sidebar:
203
+ user_id = st.sidebar.container(border=True)
204
+ user_id.markdown(st.session_state.user_ID)
205
+
206
+ async def handleUser(fireworksAPI, userInput):
207
+
208
+ question = f"User B: {userInput}"
209
+ print(question)
210
+ chat_Area.markdown(question)
211
+ timestamp = datetime.datetime.now().isoformat()
212
+ sender = 'client'
213
+ db = sqlite3.connect('chat-hub.db')
214
+ db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
215
+ (sender, question, timestamp))
216
+ db.commit()
217
+ try:
218
+ response2 = await chatCompletion(fireworksAPI, question)
219
+ response = f"Llama2: {response2}"
220
+ chat_Area.markdown(response)
221
+
222
+ serverSender = 'server'
223
+ timestamp = datetime.datetime.now().isoformat()
224
+ db = sqlite3.connect('chat-hub.db')
225
+ db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
226
+ (serverSender, response, timestamp))
227
+ db.commit()
228
+ return response
229
+
230
+ except Exception as e:
231
+ print(f"Error: {e}")
232
+
233
+ async def handleUser2(userInput):
234
+
235
+ question = f"User B: {userInput}"
236
+ print(question)
237
+ user_input = st.chat_message("human")
238
+ chat_history.append(question)
239
+ user_input.markdown(question)
240
+ timestamp = datetime.datetime.now().isoformat()
241
+ sender = 'client'
242
+ db = sqlite3.connect('chat-hub.db')
243
+ db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
244
+ (sender, question, timestamp))
245
+ db.commit()
246
+ try:
247
+ response3 = await askQuestion(question)
248
+ response = f"GPT4Free: {response3}"
249
+ chat_Area.markdown(response)
250
+ serverSender = 'server'
251
+ timestamp = datetime.datetime.now().isoformat()
252
+ db = sqlite3.connect('chat-hub.db')
253
+ db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
254
+ (serverSender, response3, timestamp))
255
+ db.commit()
256
+ return response3
257
+
258
+ except Exception as e:
259
+ print(f"Error: {e}")
260
+
261
+ # Start the WebSocket server
262
+ async def run_websockets(fireworks_API, websocketPort):
263
+ global server
264
+ fireworks.client.api_key = fireworks_API
265
+ st.session_state.api_key = fireworks_API
266
+ server_ports.append(websocketPort)
267
+ try:
268
+ server = WebSocketServer("localhost", websocketPort)
269
+ print(f"Starting WebSocket server on port {websocketPort}...")
270
+ await server.start_server()
271
+ serverPorts.markdown(server_ports)
272
+ return "Used ports:\n" + '\n'.join(map(str, server_ports))
273
+
274
+ except Exception as e:
275
+ print(f"Error: {e}")
276
+
277
+ # Start the WebSocket server1
278
+ async def run_websockets1(websocketPort):
279
+ global server
280
+ serv_g4f.append(websocketPort)
281
+ try:
282
+ server = WebSocketServer1("localhost", websocketPort)
283
+ print(f"Starting WebSocket server on port {websocketPort}...")
284
+ await server.start_server()
285
+ return "Used ports:\n" + '\n'.join(map(str, server_ports))
286
+
287
+ except Exception as e:
288
+ print(f"Error: {e}")
289
+
290
+ with st.sidebar:
291
+ portsG4F = st.container(border=True)
292
+ portsG4F.markdown(serv_g4f)
293
+
294
+ async def run_websockets2(websocketPort, token, characterID):
295
+ global server
296
+ client = Client()
297
+ serv_char.append(websocketPort)
298
+ await client.authenticate_with_token(token)
299
+ username = (await client.fetch_user())['user']['username']
300
+ user_id.markdown(username)
301
+ st.session_state.user_ID = username
302
+ st.session_state.character_ID = characterID
303
+ st.session_state.tokenChar = token
304
+ try:
305
+ server2 = WebSocketServer2("localhost", websocketPort)
306
+ print(f"Starting WebSocket server on port {websocketPort}...")
307
+ await server.start_server()
308
+ return "Used ports:\n" + '\n'.join(map(str, server_ports))
309
+
310
+ except Exception as e:
311
+ print(f"Error: {e}")
312
+
313
+ with st.sidebar:
314
+ char_ports = st.sidebar.container(border=True)
315
+ char_ports.markdown(serv_char)
316
+
317
+ async def run_client(fireworksAPI, clientPort):
318
+
319
+ chat_Area = st.container(border=True)
320
+
321
+ with st.sidebar:
322
+ fire_ports = st.container(border=True)
323
+ fire_ports.markdown(ports_fire)
324
+
325
+ uri = f'ws://localhost:{clientPort}'
326
+ ports_fire.append(clientPort)
327
+
328
+ async with websockets.connect(uri) as ws:
329
+ while True:
330
+ # Listen for messages from the server
331
+ input_message = await ws.recv()
332
+ question = f"server: {input_message}"
333
+ chat_Area.markdown(question)
334
+
335
+ output_message = await chatCompletion(fireworksAPI, input_message)
336
+ response = f"client: {output_message}"
337
+ chat_Area.markdown(response)
338
+
339
+ await ws.send(output_message)
340
+ await asyncio.sleep(0.1)
341
+
342
+
343
+ async def run_client1(clientPort):
344
+
345
+ g4f_ports = st.sidebar.container(border=True)
346
+ g4f_ports.markdown("GPT4Free clients: ")
347
+
348
+ chatbox1 = st.container(border=True)
349
+
350
+ uri = f'ws://localhost:{clientPort}'
351
+ ports_g4f.append(clientPort)
352
+ g4f_ports.markdown(ports_g4f)
353
+ async with websockets.connect(uri) as ws:
354
+ while True:
355
+ # Listen for messages from the server
356
+ input_message = await ws.recv()
357
+ input_msg = f"server: {input_message}"
358
+ chatbox1.markdown(input_msg)
359
+
360
+ output_message = await askQuestion(input_msg)
361
+ response = f"client: {output_message}"
362
+ chatbox1.markdown(response)
363
+
364
+ await ws.send(response)
365
+ await asyncio.sleep(0.1)
366
+
367
+ async def run_character(characterPort, token, characterID):
368
+
369
+ clientPorts = st.sidebar.container(border=True)
370
+
371
+ uri = f'ws://localhost:{characterPort}'
372
+ client_ports.append(characterPort)
373
+ clientPorts.markdown(client_ports)
374
+ st.session_state.tokenChar = token
375
+ st.session_state.character_ID = characterID
376
+ await client.authenticate_with_token(token)
377
+ username = (await client.fetch_user())['user']['username']
378
+ st.session_state.charName = username
379
+ async with websockets.connect(uri) as ws:
380
+ while True:
381
+ # Listen for messages from the server
382
+ question = await ws.recv()
383
+ input_msg = f"server: {question}"
384
+ chat_Area.markdown(input_msg)
385
+
386
+ chat = await client.create_or_continue_chat(characterID)
387
+ answer = await chat.send_message(question)
388
+ response = f"{answer.src_character_name}: {answer.text}"
389
+ chat_Area.markdown(response)
390
+
391
+ print(f"{answer.src_character_name}: {answer.text}")
392
+ await ws.send(answer.text)
393
+
394
+ async def connector(token):
395
+ await client.authenticate_with_token(token)
396
+ username = (await client.fetch_user())['user']['username']
397
+ print(f'Authenticated as {username}')
398
+ return username
399
+
400
+ async def askCharacter(token, character_id, question):
401
+
402
+ chat_Area = st.container(border=True)
403
+
404
+ await client.authenticate_with_token(token)
405
+ userInput = f"User B: {question}"
406
+ chat_Area.markdown(userInput)
407
+ username = (await client.fetch_user())['user']['username']
408
+ st.session_state.user_ID = username
409
+ st.sidebar.markdown(username)
410
+ chat = await client.create_or_continue_chat(character_id)
411
+ answer = await chat.send_message(question)
412
+ response = f"{answer.src_character_name}: {answer.text}"
413
+ chat_Area.markdown(response)
414
+ print(f"{answer.src_character_name}: {answer.text}")
415
+ return response
416
+
417
+ # Stop the WebSocket server
418
+ async def stop_websockets():
419
+ global server
420
+ if server:
421
+ # Close all connections gracefully
422
+ server.close()
423
+ # Wait for the server to close
424
+ await server.wait_closed()
425
+ print("Stopping WebSocket server...")
426
+ else:
427
+ print("WebSocket server is not running.")
428
+
429
+ st.session_state.gradio_Port = gradioPort
430
+ gradioPorts.markdown(st.session_state.gradio_Port)
431
+ openPage = st.toggle('View Gradio app')
432
+ link = f"http://127.0.0.1:{gradioPort}"
433
+ st.link_button("Open Gradio in new tab", link)
434
+
435
+ if openPage:
436
+ txt = st.text_input("sokolwiekto")
437
+ chatbox1 = st.container(border=True)
438
+ url = f"http://127.0.0.1:{st.session_state.gradio_Port}"
439
+ st.components.v1.iframe(url, height=950, scrolling=True)
440
+
441
+ if startGradio:
442
+
443
+ chatbox = st.container(border=True)
444
+ chatbox.markdown("chuj")
445
+
446
+ with gr.Blocks() as demo:
447
+ chuje = st.container(border=True)
448
+ chuje.markdown("w pite")
449
+ with gr.Tabs(elem_classes="tab-buttons") as tabs:
450
+ chujee = st.container(border=True)
451
+ chujee.markdown("w pitke")
452
+ with gr.TabItem("Websocket Server", elem_id="websocket_server", id=0):
453
+ chujew = st.container(border=True)
454
+ chujew.markdown("w pite 3")
455
+ with gr.Row():
456
+ chatarea = st.container(border=True)
457
+ chatarea.markdown(st.session_state['chat_history'])
458
+ # Use the client_messages list to update the messageTextbox
459
+ client_msg = gr.Textbox(lines=5, max_lines=130, label="Client messages", interactive=False)
460
+ # Use the server_responses list to update the serverMessageTextbox
461
+ server_msg = gr.Textbox(lines=5, max_lines=130, label="Server responses", interactive=False)
462
+ with gr.Row():
463
+ chudee = st.container(border=True)
464
+ user_Input = gr.Textbox(label="User Input")
465
+ with gr.Row():
466
+ ask_Qestion = gr.Button("Ask chat/conversational node")
467
+ chuj = st.container(border=True)
468
+ with gr.Row():
469
+ fireworks_API = gr.Textbox(label="Fireworks API key")
470
+ with gr.Row():
471
+ websocketPort = gr.Slider(minimum=1000, maximum=9999, label="Websocket server port", interactive=True, randomize=False)
472
+ port = gr.Textbox()
473
+ with gr.Row():
474
+ startServer = gr.Button("Start WebSocket Server")
475
+ stopWebsockets = gr.Button("Stop WebSocket Server")
476
+ with gr.Row():
477
+ clientPort = gr.Slider(minimum=1000, maximum=9999, label="Websocket server port", interactive=True, randomize=False)
478
+ startClient = gr.Button("Start WebSocket client")
479
+ stopClient = gr.Button("Stop WebSocket client")
480
+ with gr.Row():
481
+ PortInUse = gr.Textbox()
482
+
483
+ with gr.TabItem("GPT4Free Client", elem_id="gpt4free", id=2):
484
+ with gr.Row():
485
+ # Use the client_messages list to update the messageTextbox
486
+ client_msg1 = gr.Textbox(lines=5, max_lines=130, label="Client messages", interactive=False)
487
+ # Use the server_responses list to update the serverMessageTextbox
488
+ server_msg1 = gr.Textbox(lines=5, max_lines=130, label="Server responses", interactive=False)
489
+ with gr.Row():
490
+ userInput1 = gr.Textbox(label="User Input")
491
+ with gr.Row():
492
+ askG4F = gr.Button("Ask chat/conversational node")
493
+ with gr.Row():
494
+ websocketPort1 = gr.Slider(minimum=1000, maximum=9999, label="Websocket server port", interactive=True, randomize=False)
495
+ port1 = gr.Textbox()
496
+ with gr.Row():
497
+ startServer1 = gr.Button("Start WebSocket Server")
498
+ stopWebsockets1 = gr.Button("Stop WebSocket Server")
499
+ with gr.Row():
500
+ clientPort1 = gr.Slider(minimum=1000, maximum=9999, label="Websocket server port", interactive=True, randomize=False)
501
+ startClient1 = gr.Button("Start WebSocket client")
502
+ stopClient1 = gr.Button("Stop WebSocket client")
503
+ with gr.Row():
504
+ PortInUse1 = gr.Textbox()
505
+
506
+ with gr.TabItem("CharacterAI Client", elem_id="characterai_client", id=1):
507
+ with gr.Row():
508
+ # Use the client_messages list to update the messageTextbox
509
+ clientMsg = gr.Textbox(lines=5, max_lines=130, label="Client messages", interactive=False)
510
+ # Use the gr.Textbox(label="User Input")
511
+ serverMsg = gr.Textbox(lines=5, max_lines=130, label="Server responses", interactive=False)
512
+ with gr.Row():
513
+ userInput2 = gr.Textbox(label="User Input")
514
+ with gr.Row():
515
+ ask_question = gr.Button("Ask Character")
516
+ with gr.Row():
517
+ token = gr.Textbox(label="User Token")
518
+ character_id = gr.Textbox(label="Character ID")
519
+ with gr.Row():
520
+ connect = gr.Button("Connect to Character.ai")
521
+ user = gr.Textbox(label="User ID")
522
+ with gr.Row():
523
+ websocketsPort = gr.Slider(minimum=1000, maximum=9999, label="Websocket server port", interactive=True, randomize=False)
524
+ ports = gr.Textbox()
525
+ with gr.Row():
526
+ start_Server = gr.Button("Start WebSocket Server")
527
+ stop_Websockets = gr.Button("Stop WebSocket Server")
528
+ with gr.Row():
529
+ characterPort = gr.Slider(minimum=1000, maximum=9999, label="Websocket server port", interactive=True, randomize=False)
530
+ Client_Ports = gr.Textbox()
531
+ with gr.Row():
532
+ startCharacter = gr.Button("Start WebSocket client")
533
+ stop_Client = gr.Button("Stop WebSocket client")
534
+
535
+ startServer.click(run_websockets, inputs=[fireworks_API, websocketPort], outputs=port)
536
+ startClient.click(run_client, inputs=[fireworks_API, clientPort], outputs=None)
537
+ stopWebsockets.click(stop_websockets, inputs=None, outputs=port)
538
+ ask_Qestion.click(handleUser, inputs=[fireworks_API, user_Input], outputs=client_msg)
539
+
540
+ askG4F.click(handleUser2, inputs=userInput1, outputs=server_msg1)
541
+ startServer1.click(run_websockets1, inputs=websocketPort1, outputs=port1)
542
+ startClient1.click(run_client1, inputs=clientPort1, outputs=None)
543
+ stop_Websockets.click(stop_websockets, inputs=None, outputs=port1)
544
+
545
+ start_Server.click(run_websockets2, inputs=[token, character_id, websocketsPort], outputs=ports)
546
+ startCharacter.click(run_character, inputs=[characterPort, token, character_id], outputs=None)
547
+ stop_Websockets.click(stop_websockets, inputs=None, outputs=ports)
548
+ connect.click(connector, inputs=token, outputs=user)
549
+ ask_question.click(askCharacter, inputs=[token, character_id, userInput2], outputs=serverMsg)
550
+
551
+ gr_port = int(gradioPort)
552
+ st.session_state.gradio_Port = gr_port
553
+ demo.queue()
554
+ demo.launch(share=True, server_port=gr_port)
555
+ gradioPorts.markdown(st.session_state.gradio_Port)
556
+
557
+ asyncio.run(main())