File size: 17,090 Bytes
21ae628
14a5a90
 
 
59196d8
 
 
14a5a90
 
 
59196d8
14a5a90
59196d8
 
14a5a90
59196d8
 
 
14a5a90
 
 
21ae628
14a5a90
 
 
59196d8
21ae628
14a5a90
59196d8
 
 
 
 
 
 
 
 
 
 
 
 
 
21ae628
 
 
14a5a90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59196d8
 
14a5a90
 
21ae628
59196d8
14a5a90
 
 
 
 
 
 
 
 
 
 
 
59196d8
 
 
 
 
 
 
 
 
 
 
 
 
 
14a5a90
 
59196d8
 
 
 
 
 
14a5a90
 
 
21ae628
 
 
 
 
14a5a90
 
 
 
 
 
59196d8
14a5a90
 
 
59196d8
14a5a90
 
 
21ae628
59196d8
 
14a5a90
59196d8
 
 
14a5a90
59196d8
 
 
 
 
 
 
14a5a90
59196d8
 
 
14a5a90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59196d8
14a5a90
 
 
 
 
 
 
 
 
59196d8
14a5a90
59196d8
 
21ae628
 
59196d8
14a5a90
59196d8
 
 
21ae628
 
59196d8
21ae628
14a5a90
59196d8
 
 
 
 
 
14a5a90
59196d8
 
 
 
 
 
 
 
 
 
 
14a5a90
59196d8
 
 
 
 
 
 
14a5a90
59196d8
 
21ae628
 
59196d8
 
14a5a90
59196d8
14a5a90
59196d8
14a5a90
59196d8
14a5a90
59196d8
 
 
 
 
 
 
14a5a90
59196d8
 
 
 
 
 
14a5a90
59196d8
 
 
 
21ae628
59196d8
 
 
14a5a90
59196d8
 
 
 
 
14a5a90
59196d8
14a5a90
 
 
59196d8
14a5a90
59196d8
14a5a90
59196d8
 
 
14a5a90
59196d8
 
 
14a5a90
59196d8
 
 
 
 
 
 
 
 
14a5a90
59196d8
 
 
 
21ae628
59196d8
 
 
 
 
 
14a5a90
59196d8
14a5a90
59196d8
14a5a90
 
 
 
 
 
21ae628
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14a5a90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59196d8
 
14a5a90
59196d8
14a5a90
 
 
 
 
 
 
 
 
21ae628
14a5a90
 
 
 
 
59196d8
 
14a5a90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59196d8
 
7f8c563
14a5a90
 
 
 
59196d8
14a5a90
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
import os
import g4f
import json
import websockets
import datetime
import asyncio
import sqlite3
import requests
import http.server
import socketserver
import fireworks.client
import streamlit as st
import streamlit.components.v1 as components
from ServG4F import WebSocketServer1
from ServG4F2 import WebSocketServer3
from ServFire import WebSocketServer
from ServChar import WebSocketServer2
from clientG4F import WebSocketClient1
from forefront import ForefrontClient
from clientG4F2 import WebSocketClient3
from ServFlowise import WebSocketServer5
from ServAgentGPT import WebSocketServer7
from clientFlowise import WebSocketClient5
from ServForefront import WebSocketServer4
from ServChaindesk import WebSocketServer6
from PyCharacterAI import Client
from clientChaindesk import WebSocketClient6
from clientForefront import WebSocketClient4
from clientFireworks import WebSocketClient
from clientCharacter import WebSocketClient2
from websockets.sync.client import connect

client = Client()

servers = {}
clients = {}
inputs = []
outputs = []
used_ports = []
server_ports = []
client_ports = []

FIREWORKS_API_KEY = os.getenv("FIREWORKS_API_KEY")
fireworks_api_key = os.getenv("FIREWORKS_API_KEY")

# Stop the WebSocket server
async def stop_websockets():    
    global server
    if server:
        # Close all connections gracefully
        await server.close()
        # Wait for the server to close
        await server.wait_closed()
        print("Stopping WebSocket server...")
    else:
        print("WebSocket server is not running.")

# Stop the WebSocket client
async def stop_client():
    global ws
    # Close the connection with the server
    await ws.close()
    print("Stopping WebSocket client...")

async def main():
       
    st.set_page_config(layout="wide")
    st.title("serverovnia")        

    if "server_ports" not in st.session_state:
        st.session_state['server_ports'] = ""
    if "client_ports" not in st.session_state:
        st.session_state['client_ports'] = ""
    if "user_ID" not in st.session_state:
        st.session_state.user_ID = ""
    if "gradio_Port" not in st.session_state:
        st.session_state.gradio_Port = "" 
    if "server" not in st.session_state:
        st.session_state.server = False
    if "client" not in st.session_state:
        st.session_state.client = False
    if "server_ports" not in st.session_state:
        st.session_state['server_ports'] = ""
    if "client_ports" not in st.session_state:
        st.session_state['client_ports'] = ""
    if "gradio_Port" not in st.session_state:
        st.session_state.gradio_Port = "" 
    if "servers" not in st.session_state:
        st.session_state.servers = None
    if "server" not in st.session_state:
        st.session_state.server = False    
    if "client" not in st.session_state:
        st.session_state.client = False    
    if "api_key" not in st.session_state:
        st.session_state.api_key = ""
    if "forefront_api" not in st.session_state:
        st.session_state.forefront_api = ""    
    if "tokenChar" not in st.session_state:
        st.session_state.tokenChar = ""           
    if "charName" not in st.session_state:
        st.session_state.charName = ""
    if "character_ID" not in st.session_state:
        st.session_state.character_ID = "" 
    if "flow" not in st.session_state:
        st.session_state.flow = ""        
    if "agentID" not in st.session_state:
        st.session_state.agentID = "" 
    if "googleAPI" not in st.session_state:
        st.session_state.googleAPI = ""        
    if "cseID" not in st.session_state:
        st.session_state.cseID = ""                        
        
    if "http_server" not in st.session_state:
        
        PORT = 8001
        Handler = http.server.SimpleHTTPRequestHandler
        st.session_state.http_server = True

        with socketserver.TCPServer(("", PORT), Handler) as httpd:
            print("serving at port", PORT)
            httpd.serve_forever()    


    userInput = st.chat_input("Ask agent")    

    selectServ = st.selectbox("Select source", ("Fireworks", "Bing", "GPT-3,5", "character.ai", "Forefront", "AgentsGPT", "ChainDesk", "Flowise", "DocsBot"))

    c1, c2 = st.columns(2)
        
    with c1:
        websocketPort = st.number_input("Websocket server port", min_value=1000, max_value=9999, value=1000)   
        startServer = st.button("Start server")
        stopServer = st.button("Stop server")
        st.text("Server ports")
        serverPorts1 = st.container(border=True)
        serverPorts1.markdown(st.session_state['server_ports'])
    
    with c2:
        clientPort = st.number_input("Websocket client port", min_value=1000, max_value=9999, value=1000)
        runClient = st.button("Start client")
        stopClient = st.button("Stop client")        
        st.text("Client ports")
        clientPorts1 = st.container(border=True)
        clientPorts1.markdown(st.session_state['client_ports'])

    with st.sidebar:
        # Wyświetlanie danych, które mogą być modyfikowane na różnych stronach
        serverPorts = st.container(border=True)
        serverPorts.markdown(st.session_state['server_ports'])
        st.text("Client ports")
        clientPorts = st.container(border=True)
        clientPorts.markdown(st.session_state['client_ports'])
        st.text("Character.ai ID")
        user_id = st.container(border=True)
        user_id.markdown(st.session_state.user_ID)    
        status = st.status(label="runs", state="complete", expanded=False)

        if st.session_state.server == True:
            st.markdown("server running...")
        
        if st.session_state.client == True:    
            st.markdown("client running")    
   
    if stopServer:
        stop_websockets

    if stopClient:
        stop_client  

    if selectServ == "Fireworks":
        fireworksAPI = st.text_input("Fireworks API") 

        if startServer:
            fireworks.client.api_key = fireworksAPI
            st.session_state.api_key = fireworks.client.api_key
            server_ports.append(websocketPort)
            st.session_state.server = True
            st.session_state['server_ports'] = server_ports
            serverPorts1.markdown(st.session_state['server_ports'])
            try:
                server = WebSocketServer("localhost")
                servers.append(server)
                print(f"Starting WebSocket server on port {websocketPort}...")
                await server.start_server(websocketPort)
                status.update(label="runs", state="running", expanded=True)
                await asyncio.Future()

            except Exception as e:
                print(f"Error: {e}")

        if runClient:
            st.session_state.client = True
            fireworks.client.api_key = fireworksAPI
            st.session_state.api_key = fireworks.client.api_key
            client_ports.append(clientPort)
            st.session_state['client_ports'] = client_ports
            clientPorts1.markdown(st.session_state['client_ports'])
            try:
                uri = f'ws://localhost:{clientPort}'
                client = WebSocketClient(uri)    
                print(f"Connecting client on port {clientPort}...")
                await client.startClient()
                st.session_state.client = client
                status.update(label="runs", state="running", expanded=True)
                await asyncio.Future()

            except Exception as e:
                print(f"Error: {e}")                

        if userInput:        
            print(f"User B: {userInput}")
            st.session_state.api_key = fireworksAPI
            user_input = st.chat_message("human")
            user_input.markdown(userInput)
            fireworks1 = WebSocketServer()
            response1 = await fireworks1.chatCompletion(userInput)
            print(response1)
            outputMsg = st.chat_message("ai") 
            outputMsg.markdown(response1)

    if selectServ == "Bing":

        if startServer:            
            server_ports.append(websocketPort)
            st.session_state.server = True
            st.session_state['server_ports'] = server_ports
            serverPorts1.markdown(st.session_state['server_ports'])
            try:      
                server1 = WebSocketServer1("localhost", websocketPort)    
                print(f"Starting WebSocket server on port {websocketPort}...")
                await server1.start_server()
                st.session_state.server = server1
                status.update(label="runs", state="running", expanded=True)
                await asyncio.Future()

            except Exception as e:
                print(f"Error: {e}")

        if runClient:
            st.session_state.client = True
            client_ports.append(clientPort)
            st.session_state['client_ports'] = client_ports
            clientPorts1.markdown(st.session_state['client_ports'])
            try:
                client1 = WebSocketClient1(clientPort)    
                print(f"Connecting client on port {clientPort}...")
                await client1.startClient()
                st.session_state.client = client1
                status.update(label="runs", state="running", expanded=True)
                await asyncio.Future()

            except Exception as e:
                print(f"Error: {e}")                

        if userInput:
            user_input1 = st.chat_message("human")
            user_input1.markdown(userInput)
            bing = WebSocketServer1("localhost", websocketPort)
            response = await bing.askQuestion(userInput)
            outputMsg1 = st.chat_message("ai") 
            outputMsg1.markdown(response)

    if selectServ == "GPT-3,5":
        
        if startServer:
            server_ports.append(websocketPort)
            st.session_state.server = True
            st.session_state['server_ports'] = server_ports
            serverPorts1.markdown(st.session_state['server_ports'])
            try:      
                server2 = WebSocketServer3("localhost", websocketPort)    
                print(f"Starting WebSocket server on port {websocketPort}...")
                await server2.start_server()
                st.session_state.server = server2
                await asyncio.Future()

            except Exception as e:
                print(f"Error: {e}")

        if runClient:
            st.session_state.client = True
            client_ports.append(clientPort)
            st.session_state['client_ports'] = client_ports
            clientPorts1.markdown(st.session_state['client_ports'])
            try:
                client2 = WebSocketClient3(clientPort)    
                print(f"Connecting client on port {clientPort}...")
                await client2.startClient()
                st.session_state.client = client2
                await asyncio.Future()

            except Exception as e:
                print(f"Error: {e}")                

        if userInput:
            user_input2 = st.chat_message("human")
            user_input2.markdown(userInput)
            GPT = WebSocketServer3("localhost", websocketPort)
            response = await GPT.askQuestion(userInput)
            outputMsg2 = st.chat_message("ai") 
            outputMsg2.markdown(response)

    if selectServ == "character.ai":

        characterToken = st.text_input("Character AI user token") 
        characterID = st.text_input("Your characters ID") 

        if startServer:
            st.session_state.tokenChar = characterToken
            st.session_state.character_ID = characterID
            server_ports.append(websocketPort)
            st.session_state.server = True
            st.session_state['server_ports'] = server_ports
            serverPorts1.markdown(st.session_state['server_ports'])
            try:
                server = WebSocketServer2(characterToken, characterID)    
                print(f"Starting WebSocket server on port {websocketPort}...")
                await server.start_server(websocketPort)
                status.update(label="runs", state="running", expanded=True)
                await asyncio.Future()

            except Exception as e:
                print(f"Error: {e}")

        if runClient:
            st.session_state.tokenChar = characterToken
            st.session_state.character_ID = characterID
            client_ports.append(clientPort)
            st.session_state['client_ports'] = client_ports
            clientPorts1.markdown(st.session_state['client_ports'])
            try:
                uri = f'ws://localhost:{clientPort}'
                client = WebSocketClient2(clientPort)    
                print(f"Connecting client on port {clientPort}...")
                await client.startClient()
                st.session_state.client = client
                status.update(label="runs", state="running", expanded=True)
                await asyncio.Future()

            except Exception as e:
                print(f"Error: {e}")                

        if userInput:
            print(f"User B: {userInput}")
            character = WebSocketServer2(characterToken, characterID)
            response1 = await character.askCharacter(userInput)
            print(response1)
            return response1

    if selectServ == "ChainDesk":

        agentID = st.text_input("Agent ID")

        if userInput:
            st.session_state.agentID = agentID
            user_input6 = st.chat_message("human")
            user_input6.markdown(userInput)
            chaindesk = WebSocketServer6("localhost")
            response6 = await chaindesk.askChaindesk(userInput)
            outputMsg = st.chat_message("ai")
            outputMsg.markdown(response6)

        if startServer:
            st.session_state.agentID = agentID
            server_ports.append(websocketPort)
            st.session_state.server = True
            st.session_state['server_ports'] = server_ports
            serverPorts1.markdown(st.session_state['server_ports'])
            try:      
                server6 = WebSocketServer6("localhost")    
                print(f"Starting WebSocket server on port {websocketPort}...")
                await server6.start_server(websocketPort)
                st.session_state.server = server6
                await asyncio.Future()

            except Exception as e:
                print(f"Error: {e}")

        if runClient:
            st.session_state.agentID = agentID
            st.session_state.client = True
            client_ports.append(clientPort)
            st.session_state['client_ports'] = client_ports
            clientPorts1.markdown(st.session_state['client_ports'])
            try:
                client6 = WebSocketClient6(clientPort)    
                print(f"Connecting client on port {clientPort}...")
                await client6.startClient()
                st.session_state.client = client6
                await asyncio.Future()

            except Exception as e:
                print(f"Error: {e}")  

    if selectServ == "Flowise":
        
        flow = st.text_input("flow ID")

        if userInput:
            st.session_state.flow = flow
            user_input6 = st.chat_message("human")
            user_input6.markdown(userInput)
            flowise = WebSocketServer5("localhost")
            response6 = await flowise.askQuestion(userInput)
            outputMsg = st.chat_message("ai")
            outputMsg.markdown(response6)

        if startServer:
            st.session_state.flow = flow
            server_ports.append(websocketPort)
            st.session_state.server = True
            st.session_state['server_ports'] = server_ports
            serverPorts1.markdown(st.session_state['server_ports'])
            try:      
                server5 = WebSocketServer5("localhost")    
                print(f"Starting WebSocket server on port {websocketPort}...")
                await server5.start_server()
                st.session_state.server = server5
                await asyncio.Future()

            except Exception as e:
                print(f"Error: {e}")

        if runClient:
            st.session_state.flow = flow
            st.session_state.client = True
            client_ports.append(clientPort)
            st.session_state['client_ports'] = client_ports
            clientPorts1.markdown(st.session_state['client_ports'])
            try:
                client5 = WebSocketClient5(clientPort)    
                print(f"Connecting client on port {clientPort}...")
                await client5.startClient(clientPort)
                st.session_state.client = client5
                await asyncio.Future()

            except Exception as e:
                print(f"Error: {e}")                          

    if selectServ == "DocsBot":

        botID = st.text("Docsbot ID")

        url = f"http://localhost:8001/Docsbotport.html"
        st.components.v1.iframe(url, height=950, scrolling=True)

asyncio.run(main())