Spaces:
Sleeping
Sleeping
File size: 24,982 Bytes
59196d8 |
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 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 |
import streamlit as st
import datetime
import asyncio
import sqlite3
import g4f
import websockets
import streamlit as st
import fireworks.client
import gradio as gr
import streamlit.components.v1 as components
from PyCharacterAI import Client
from ServG4F import WebSocketServer1
from ServFire import WebSocketServer
from ServChar import WebSocketServer2
from clientG4F import WebSocketClient1
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 = []
chat_history = []
ports_g4f = []
ports_fire = []
serv_char = []
serv_g4f = []
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."
db = sqlite3.connect('chat-hub.db')
cursor = db.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS messages (id INTEGER PRIMARY KEY AUTOINCREMENT, sender TEXT, message TEXT, timestamp TEXT)')
db.commit()
st.set_page_config(layout="wide")
async def askQuestion(question):
try:
db = sqlite3.connect('chat-hub.db')
cursor = db.cursor()
cursor.execute("SELECT * FROM messages ORDER BY timestamp DESC LIMIT 30")
messages = cursor.fetchall()
messages.reverse()
past_user_inputs = []
generated_responses = []
for message in messages:
if message[1] == 'client':
past_user_inputs.append(message[2])
else:
generated_responses.append(message[2])
response = await g4f.ChatCompletion.create_async(
model=g4f.models.gpt_4,
provider=g4f.Provider.Bing,
messages=[
{"role": "system", "content": system_instruction},
*[{"role": "user", "content": message} for message in past_user_inputs],
*[{"role": "assistant", "content": message} for message in generated_responses],
{"role": "user", "content": question}
])
print(response)
return response
except Exception as e:
print(e)
async def chatCompletion(fireworksAPI, question):
fireworks.client.api_key = fireworksAPI
st.session_state.api_key = fireworksAPI
try:
# Connect to the database and get the last 30 messages
db = sqlite3.connect('chat-hub.db')
cursor = db.cursor()
cursor.execute("SELECT * FROM messages ORDER BY timestamp DESC LIMIT 10")
messages = cursor.fetchall()
messages.reverse()
# Extract user inputs and generated responses from the messages
past_user_inputs = []
generated_responses = []
for message in messages:
if message[1] == 'client':
past_user_inputs.append(message[2])
else:
generated_responses.append(message[2])
# Prepare data to send to the chatgpt-api.shn.hk
response = fireworks.client.ChatCompletion.create(
model="accounts/fireworks/models/llama-v2-7b-chat",
messages=[
{"role": "system", "content": system_instruction},
*[{"role": "user", "content": input} for input in past_user_inputs],
*[{"role": "assistant", "content": response} for response in generated_responses],
{"role": "user", "content": question}
],
stream=False,
n=1,
max_tokens=2500,
temperature=0.5,
top_p=0.7,
)
answer = response.choices[0].message.content
print(answer)
return str(answer)
except Exception as error:
print("Error while fetching or processing the response:", error)
return "Error: Unable to generate a response."
async def main():
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 "chat_history" not in st.session_state:
st.session_state['chat_history'] = ""
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 "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 "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 = ""
gradioPorts = st.container(border=True)
gradioPort = st.number_input("Gradio server port", min_value=1000, max_value=9999, value=1000)
startGradio = st.button("Start Gradio")
st.sidebar.text("Server ports:")
serverPorts = st.sidebar.container(border=True)
serverPorts.markdown(st.session_state['server_ports'])
st.sidebar.text("Client ports")
clientPorts = st.sidebar.container(border=True)
clientPorts.markdown(st.session_state['client_ports'])
st.sidebar.text("Character.ai ID")
user_id = st.sidebar.container(border=True)
user_id.markdown(st.session_state.user_ID)
chat_Area = st.container(border=True)
async def askCharacter(token, character_ID, question):
chatbox2 = st.container(border=True)
await client.authenticate_with_token(token)
username = (await client.fetch_user())['user']['username']
chat = await client.create_or_continue_chat(character_ID)
input = f"User B: {question}"
chatbox2.chat_message("human").write(input)
st.session_state.tokenChar = token
st.session_state.charName = username
st.session_state.character_ID = character_ID
st.session_state.user_ID = username
timestamp = datetime.datetime.now().isoformat()
sender = 'client'
db = sqlite3.connect('chat-hub.db')
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
(sender, question, timestamp))
db.commit()
try:
answer = await chat.send_message(input)
response = f"{answer.src_character_name}: {answer.text}"
print(response)
chatbox2.chat_message("ai").write(response)
timestamp = datetime.datetime.now().isoformat()
serverSender = 'server'
db = sqlite3.connect('chat-hub.db')
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
(serverSender, response, timestamp))
db.commit()
return response
except Exception as e:
print(f"Error: {e}")
with st.sidebar:
user_id = st.sidebar.container(border=True)
user_id.markdown(st.session_state.user_ID)
async def handleUser(fireworksAPI, userInput):
question = f"User B: {userInput}"
print(question)
chat_Area.markdown(question)
timestamp = datetime.datetime.now().isoformat()
sender = 'client'
db = sqlite3.connect('chat-hub.db')
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
(sender, question, timestamp))
db.commit()
try:
response2 = await chatCompletion(fireworksAPI, question)
response = f"Llama2: {response2}"
chat_Area.markdown(response)
serverSender = 'server'
timestamp = datetime.datetime.now().isoformat()
db = sqlite3.connect('chat-hub.db')
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
(serverSender, response, timestamp))
db.commit()
return response
except Exception as e:
print(f"Error: {e}")
async def handleUser2(userInput):
question = f"User B: {userInput}"
print(question)
user_input = st.chat_message("human")
chat_history.append(question)
user_input.markdown(question)
timestamp = datetime.datetime.now().isoformat()
sender = 'client'
db = sqlite3.connect('chat-hub.db')
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
(sender, question, timestamp))
db.commit()
try:
response3 = await askQuestion(question)
response = f"GPT4Free: {response3}"
chat_Area.markdown(response)
serverSender = 'server'
timestamp = datetime.datetime.now().isoformat()
db = sqlite3.connect('chat-hub.db')
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
(serverSender, response3, timestamp))
db.commit()
return response3
except Exception as e:
print(f"Error: {e}")
# Start the WebSocket server
async def run_websockets(fireworks_API, websocketPort):
global server
fireworks.client.api_key = fireworks_API
st.session_state.api_key = fireworks_API
server_ports.append(websocketPort)
try:
server = WebSocketServer("localhost", websocketPort)
print(f"Starting WebSocket server on port {websocketPort}...")
await server.start_server()
serverPorts.markdown(server_ports)
return "Used ports:\n" + '\n'.join(map(str, server_ports))
except Exception as e:
print(f"Error: {e}")
# Start the WebSocket server1
async def run_websockets1(websocketPort):
global server
serv_g4f.append(websocketPort)
try:
server = WebSocketServer1("localhost", websocketPort)
print(f"Starting WebSocket server on port {websocketPort}...")
await server.start_server()
return "Used ports:\n" + '\n'.join(map(str, server_ports))
except Exception as e:
print(f"Error: {e}")
with st.sidebar:
portsG4F = st.container(border=True)
portsG4F.markdown(serv_g4f)
async def run_websockets2(websocketPort, token, characterID):
global server
client = Client()
serv_char.append(websocketPort)
await client.authenticate_with_token(token)
username = (await client.fetch_user())['user']['username']
user_id.markdown(username)
st.session_state.user_ID = username
st.session_state.character_ID = characterID
st.session_state.tokenChar = token
try:
server2 = WebSocketServer2("localhost", websocketPort)
print(f"Starting WebSocket server on port {websocketPort}...")
await server.start_server()
return "Used ports:\n" + '\n'.join(map(str, server_ports))
except Exception as e:
print(f"Error: {e}")
with st.sidebar:
char_ports = st.sidebar.container(border=True)
char_ports.markdown(serv_char)
async def run_client(fireworksAPI, clientPort):
chat_Area = st.container(border=True)
with st.sidebar:
fire_ports = st.container(border=True)
fire_ports.markdown(ports_fire)
uri = f'ws://localhost:{clientPort}'
ports_fire.append(clientPort)
async with websockets.connect(uri) as ws:
while True:
# Listen for messages from the server
input_message = await ws.recv()
question = f"server: {input_message}"
chat_Area.markdown(question)
output_message = await chatCompletion(fireworksAPI, input_message)
response = f"client: {output_message}"
chat_Area.markdown(response)
await ws.send(output_message)
await asyncio.sleep(0.1)
async def run_client1(clientPort):
g4f_ports = st.sidebar.container(border=True)
g4f_ports.markdown("GPT4Free clients: ")
chatbox1 = st.container(border=True)
uri = f'ws://localhost:{clientPort}'
ports_g4f.append(clientPort)
g4f_ports.markdown(ports_g4f)
async with websockets.connect(uri) as ws:
while True:
# Listen for messages from the server
input_message = await ws.recv()
input_msg = f"server: {input_message}"
chatbox1.markdown(input_msg)
output_message = await askQuestion(input_msg)
response = f"client: {output_message}"
chatbox1.markdown(response)
await ws.send(response)
await asyncio.sleep(0.1)
async def run_character(characterPort, token, characterID):
clientPorts = st.sidebar.container(border=True)
uri = f'ws://localhost:{characterPort}'
client_ports.append(characterPort)
clientPorts.markdown(client_ports)
st.session_state.tokenChar = token
st.session_state.character_ID = characterID
await client.authenticate_with_token(token)
username = (await client.fetch_user())['user']['username']
st.session_state.charName = username
async with websockets.connect(uri) as ws:
while True:
# Listen for messages from the server
question = await ws.recv()
input_msg = f"server: {question}"
chat_Area.markdown(input_msg)
chat = await client.create_or_continue_chat(characterID)
answer = await chat.send_message(question)
response = f"{answer.src_character_name}: {answer.text}"
chat_Area.markdown(response)
print(f"{answer.src_character_name}: {answer.text}")
await ws.send(answer.text)
async def connector(token):
await client.authenticate_with_token(token)
username = (await client.fetch_user())['user']['username']
print(f'Authenticated as {username}')
return username
async def askCharacter(token, character_id, question):
chat_Area = st.container(border=True)
await client.authenticate_with_token(token)
userInput = f"User B: {question}"
chat_Area.markdown(userInput)
username = (await client.fetch_user())['user']['username']
st.session_state.user_ID = username
st.sidebar.markdown(username)
chat = await client.create_or_continue_chat(character_id)
answer = await chat.send_message(question)
response = f"{answer.src_character_name}: {answer.text}"
chat_Area.markdown(response)
print(f"{answer.src_character_name}: {answer.text}")
return response
# Stop the WebSocket server
async def stop_websockets():
global server
if server:
# Close all connections gracefully
server.close()
# Wait for the server to close
await server.wait_closed()
print("Stopping WebSocket server...")
else:
print("WebSocket server is not running.")
st.session_state.gradio_Port = gradioPort
gradioPorts.markdown(st.session_state.gradio_Port)
openPage = st.toggle('View Gradio app')
link = f"http://127.0.0.1:{gradioPort}"
st.link_button("Open Gradio in new tab", link)
if openPage:
txt = st.text_input("sokolwiekto")
chatbox1 = st.container(border=True)
url = f"http://127.0.0.1:{st.session_state.gradio_Port}"
st.components.v1.iframe(url, height=950, scrolling=True)
if startGradio:
chatbox = st.container(border=True)
chatbox.markdown("chuj")
with gr.Blocks() as demo:
chuje = st.container(border=True)
chuje.markdown("w pite")
with gr.Tabs(elem_classes="tab-buttons") as tabs:
chujee = st.container(border=True)
chujee.markdown("w pitke")
with gr.TabItem("Websocket Server", elem_id="websocket_server", id=0):
chujew = st.container(border=True)
chujew.markdown("w pite 3")
with gr.Row():
chatarea = st.container(border=True)
chatarea.markdown(st.session_state['chat_history'])
# Use the client_messages list to update the messageTextbox
client_msg = gr.Textbox(lines=5, max_lines=130, label="Client messages", interactive=False)
# Use the server_responses list to update the serverMessageTextbox
server_msg = gr.Textbox(lines=5, max_lines=130, label="Server responses", interactive=False)
with gr.Row():
chudee = st.container(border=True)
user_Input = gr.Textbox(label="User Input")
with gr.Row():
ask_Qestion = gr.Button("Ask chat/conversational node")
chuj = st.container(border=True)
with gr.Row():
fireworks_API = gr.Textbox(label="Fireworks API key")
with gr.Row():
websocketPort = gr.Slider(minimum=1000, maximum=9999, label="Websocket server port", interactive=True, randomize=False)
port = gr.Textbox()
with gr.Row():
startServer = gr.Button("Start WebSocket Server")
stopWebsockets = gr.Button("Stop WebSocket Server")
with gr.Row():
clientPort = gr.Slider(minimum=1000, maximum=9999, label="Websocket server port", interactive=True, randomize=False)
startClient = gr.Button("Start WebSocket client")
stopClient = gr.Button("Stop WebSocket client")
with gr.Row():
PortInUse = gr.Textbox()
with gr.TabItem("GPT4Free Client", elem_id="gpt4free", id=2):
with gr.Row():
# Use the client_messages list to update the messageTextbox
client_msg1 = gr.Textbox(lines=5, max_lines=130, label="Client messages", interactive=False)
# Use the server_responses list to update the serverMessageTextbox
server_msg1 = gr.Textbox(lines=5, max_lines=130, label="Server responses", interactive=False)
with gr.Row():
userInput1 = gr.Textbox(label="User Input")
with gr.Row():
askG4F = gr.Button("Ask chat/conversational node")
with gr.Row():
websocketPort1 = gr.Slider(minimum=1000, maximum=9999, label="Websocket server port", interactive=True, randomize=False)
port1 = gr.Textbox()
with gr.Row():
startServer1 = gr.Button("Start WebSocket Server")
stopWebsockets1 = gr.Button("Stop WebSocket Server")
with gr.Row():
clientPort1 = gr.Slider(minimum=1000, maximum=9999, label="Websocket server port", interactive=True, randomize=False)
startClient1 = gr.Button("Start WebSocket client")
stopClient1 = gr.Button("Stop WebSocket client")
with gr.Row():
PortInUse1 = gr.Textbox()
with gr.TabItem("CharacterAI Client", elem_id="characterai_client", id=1):
with gr.Row():
# Use the client_messages list to update the messageTextbox
clientMsg = gr.Textbox(lines=5, max_lines=130, label="Client messages", interactive=False)
# Use the gr.Textbox(label="User Input")
serverMsg = gr.Textbox(lines=5, max_lines=130, label="Server responses", interactive=False)
with gr.Row():
userInput2 = gr.Textbox(label="User Input")
with gr.Row():
ask_question = gr.Button("Ask Character")
with gr.Row():
token = gr.Textbox(label="User Token")
character_id = gr.Textbox(label="Character ID")
with gr.Row():
connect = gr.Button("Connect to Character.ai")
user = gr.Textbox(label="User ID")
with gr.Row():
websocketsPort = gr.Slider(minimum=1000, maximum=9999, label="Websocket server port", interactive=True, randomize=False)
ports = gr.Textbox()
with gr.Row():
start_Server = gr.Button("Start WebSocket Server")
stop_Websockets = gr.Button("Stop WebSocket Server")
with gr.Row():
characterPort = gr.Slider(minimum=1000, maximum=9999, label="Websocket server port", interactive=True, randomize=False)
Client_Ports = gr.Textbox()
with gr.Row():
startCharacter = gr.Button("Start WebSocket client")
stop_Client = gr.Button("Stop WebSocket client")
startServer.click(run_websockets, inputs=[fireworks_API, websocketPort], outputs=port)
startClient.click(run_client, inputs=[fireworks_API, clientPort], outputs=None)
stopWebsockets.click(stop_websockets, inputs=None, outputs=port)
ask_Qestion.click(handleUser, inputs=[fireworks_API, user_Input], outputs=client_msg)
askG4F.click(handleUser2, inputs=userInput1, outputs=server_msg1)
startServer1.click(run_websockets1, inputs=websocketPort1, outputs=port1)
startClient1.click(run_client1, inputs=clientPort1, outputs=None)
stop_Websockets.click(stop_websockets, inputs=None, outputs=port1)
start_Server.click(run_websockets2, inputs=[token, character_id, websocketsPort], outputs=ports)
startCharacter.click(run_character, inputs=[characterPort, token, character_id], outputs=None)
stop_Websockets.click(stop_websockets, inputs=None, outputs=ports)
connect.click(connector, inputs=token, outputs=user)
ask_question.click(askCharacter, inputs=[token, character_id, userInput2], outputs=serverMsg)
gr_port = int(gradioPort)
st.session_state.gradio_Port = gr_port
demo.queue()
demo.launch(share=True, server_port=gr_port)
gradioPorts.markdown(st.session_state.gradio_Port)
asyncio.run(main()) |