Spaces:
Sleeping
Sleeping
Delete clientCharacter.py
Browse files- clientCharacter.py +0 -59
clientCharacter.py
DELETED
@@ -1,59 +0,0 @@
|
|
1 |
-
import asyncio
|
2 |
-
import websockets
|
3 |
-
import threading
|
4 |
-
import sqlite3
|
5 |
-
import streamlit as st
|
6 |
-
from PyCharacterAI import Client
|
7 |
-
|
8 |
-
# Define the websocket client class
|
9 |
-
class WebSocketClient2:
|
10 |
-
def __init__(self, uri):
|
11 |
-
# Initialize the uri attribute
|
12 |
-
self.uri = uri
|
13 |
-
|
14 |
-
# Define a function that will run the client in a separate thread
|
15 |
-
def run(self):
|
16 |
-
# Create a thread object
|
17 |
-
self.thread = threading.Thread(target=self.run_client)
|
18 |
-
# Start the thread
|
19 |
-
self.thread.start()
|
20 |
-
|
21 |
-
# Define a function that will run the client using asyncio
|
22 |
-
def run_client(self):
|
23 |
-
# Get the asyncio event loop
|
24 |
-
loop = asyncio.new_event_loop()
|
25 |
-
# Set the event loop as the current one
|
26 |
-
asyncio.set_event_loop(loop)
|
27 |
-
# Run the client until it is stopped
|
28 |
-
loop.run_until_complete(self.client())
|
29 |
-
|
30 |
-
# Define a coroutine that will connect to the server and exchange messages
|
31 |
-
async def startClient(self):
|
32 |
-
client = Client()
|
33 |
-
await client.authenticate_with_token(st.session_state.tokenChar)
|
34 |
-
chat = await client.create_or_continue_chat(st.session_state.character_ID)
|
35 |
-
# Connect to the server
|
36 |
-
async with websockets.connect(self.uri) as websocket:
|
37 |
-
# Loop forever
|
38 |
-
while True:
|
39 |
-
# Listen for messages from the server
|
40 |
-
input_message = await websocket.recv()
|
41 |
-
print(f"Server: {input_message}")
|
42 |
-
input_Msg = st.chat_message("assistant")
|
43 |
-
input_Msg.markdown(input_message)
|
44 |
-
try:
|
45 |
-
answer = await chat.send_message(input_message)
|
46 |
-
response = f"{answer.src_character_name}: {answer.text}"
|
47 |
-
print(response)
|
48 |
-
outputMsg1 = st.chat_message("ai")
|
49 |
-
outputMsg1.markdown(response)
|
50 |
-
await websocket.send(response)
|
51 |
-
continue
|
52 |
-
|
53 |
-
except websockets.ConnectionClosed:
|
54 |
-
print("client disconnected")
|
55 |
-
continue
|
56 |
-
|
57 |
-
except Exception as e:
|
58 |
-
print(f"Error: {e}")
|
59 |
-
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|