Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,126 +1,137 @@
|
|
1 |
-
import requests
|
2 |
import datetime
|
3 |
-
import http.server
|
4 |
import websockets
|
5 |
-
import websocket
|
6 |
import asyncio
|
7 |
import sqlite3
|
8 |
import json
|
|
|
9 |
import gradio as gr
|
|
|
10 |
from bs4 import BeautifulSoup
|
11 |
from gradio_client import Client
|
12 |
-
import
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
def
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
self.send_response(200)
|
42 |
-
self.send_header('Content-type', 'text/html')
|
43 |
-
self.end_headers()
|
44 |
-
with open('index.html', 'rb') as file:
|
45 |
-
self.wfile.write(file.read())
|
46 |
-
else:
|
47 |
-
self.send_response(404)
|
48 |
-
self.end_headers()
|
49 |
-
|
50 |
-
# Set up the SQLite database
|
51 |
-
db = sqlite3.connect('chat-hub.db')
|
52 |
-
cursor = db.cursor()
|
53 |
-
cursor.execute('CREATE TABLE IF NOT EXISTS messages (id INTEGER PRIMARY KEY AUTOINCREMENT, sender TEXT, message TEXT, timestamp TEXT)')
|
54 |
-
db.commit()
|
55 |
-
|
56 |
-
# Define the function for sending an error message
|
57 |
-
def sendErrorMessage(ws, errorMessage):
|
58 |
-
errorResponse = {'error': errorMessage}
|
59 |
-
ws.send(json.dumps(errorResponse))
|
60 |
-
|
61 |
-
# Define a function to ask a question to the chatbot and display the response
|
62 |
async def askQuestion(question):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
try:
|
64 |
-
response = requests.post(
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
except Exception as e:
|
73 |
-
print(e)
|
74 |
-
|
75 |
-
# Define a function to ask a question to the chatbot and display the response
|
76 |
async def askQuestion2(question):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
try:
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
async def handleWebSocket(ws):
|
96 |
print('New connection')
|
97 |
-
|
|
|
|
|
98 |
while True:
|
99 |
-
message = await ws.recv()
|
100 |
-
|
101 |
-
client_messages.append(
|
102 |
-
print(f'Received message: {message}')
|
103 |
-
messageText = message
|
104 |
-
messages.append(message)
|
105 |
timestamp = datetime.datetime.now().isoformat()
|
106 |
sender = 'client'
|
107 |
db = sqlite3.connect('chat-hub.db')
|
108 |
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
109 |
-
(sender,
|
110 |
-
db.commit()
|
111 |
-
try:
|
112 |
-
|
113 |
-
|
114 |
-
messages.append(answer)
|
115 |
-
response = {'answer': answer}
|
116 |
-
serverMessageText = response.get('answer', '')
|
117 |
-
await ws.send(json.dumps(response))
|
118 |
# Append the server response to the server_responses list
|
119 |
-
server_responses.append(
|
|
|
120 |
serverSender = 'server'
|
121 |
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
122 |
-
|
123 |
db.commit()
|
|
|
|
|
124 |
|
125 |
except websockets.exceptions.ConnectionClosedError as e:
|
126 |
print(f"Connection closed: {e}")
|
@@ -128,62 +139,166 @@ async def handleWebSocket(ws):
|
|
128 |
except Exception as e:
|
129 |
print(f"Error: {e}")
|
130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
|
132 |
# Function to stop the WebSocket server
|
133 |
def stop_websockets():
|
134 |
-
global
|
135 |
-
if
|
136 |
cursor.close()
|
137 |
db.close()
|
138 |
-
|
139 |
print("WebSocket server stopped.")
|
140 |
else:
|
141 |
print("WebSocket server is not running.")
|
142 |
|
143 |
# Start the WebSocket server
|
144 |
async def start_websockets(websocketPort):
|
145 |
-
global
|
146 |
-
# Create a WebSocket client that connects to the server
|
147 |
-
|
148 |
-
|
149 |
-
used_ports.append(websocketPort)
|
150 |
print(f"Starting WebSocket server on port {websocketPort}...")
|
151 |
-
return "Used ports:\n" + '\n'.join(map(str,
|
|
|
|
|
152 |
|
153 |
-
async def start_client():
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
|
166 |
with gr.Blocks() as demo:
|
167 |
-
with gr.
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
demo.launch()
|
|
|
|
|
1 |
import datetime
|
|
|
2 |
import websockets
|
|
|
3 |
import asyncio
|
4 |
import sqlite3
|
5 |
import json
|
6 |
+
import requests
|
7 |
import gradio as gr
|
8 |
+
import PySimpleGUI as sg
|
9 |
from bs4 import BeautifulSoup
|
10 |
from gradio_client import Client
|
11 |
+
from websockets.sync.client import connect
|
12 |
+
|
13 |
+
modelPath = 'nlp-model.json'
|
14 |
+
|
15 |
+
inputs = []
|
16 |
+
client_ports = []
|
17 |
+
|
18 |
+
layout = [
|
19 |
+
[sg.Multiline(size=(200, 10), key='-CLIENT-')],
|
20 |
+
[sg.Multiline(size=(100, 20), key='-INPUT-', auto_refresh=True), sg.Multiline(size=(100, 20), key='-OUTPUT-', auto_refresh=True)],
|
21 |
+
[sg.Multiline(size=(150, 2), key='-USERINPUT-')],
|
22 |
+
[sg.Button('Ask the agent')],
|
23 |
+
[sg.Text('Enter Port:'), sg.InputText(size=(10, 1), key='-PORT-'),
|
24 |
+
sg.Slider(range=(1000, 9999), orientation='h', size=(20, 20), key='-PORTSLIDER-')],
|
25 |
+
[sg.Button('Start WebSocket server'), sg.Button('Start WebSocket client')],
|
26 |
+
[sg.Button('Stop WebSocket server'), sg.Button('Stop WebSocket client')],
|
27 |
+
[sg.Multiline(size=(20, 4), key='-SERVER_PORTS-')], [sg.Multiline(size=(20, 4), key='-CLIENT_PORTS-')],
|
28 |
+
[sg.Button('Clear Textboxes')]
|
29 |
+
]
|
30 |
+
|
31 |
+
def get_port(values):
|
32 |
+
if values['-PORT-']:
|
33 |
+
return int(values['-PORT-'])
|
34 |
+
else:
|
35 |
+
return int(values['-PORTSLIDER-'])
|
36 |
+
|
37 |
+
window = sg.Window('WebSocket Client', layout)
|
38 |
+
|
39 |
+
# Function to send a question to the chatbot and get the response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
async def askQuestion(question):
|
41 |
+
url = 'https://api.docsbot.ai/teams/ZrbLG98bbxZ9EFqiPvyl/bots/oFFiXuQsakcqyEdpLvCB/chat'
|
42 |
+
headers = {
|
43 |
+
'Content-Type': 'application/json'
|
44 |
+
}
|
45 |
+
data = {
|
46 |
+
'question': question,
|
47 |
+
'full_source': False
|
48 |
+
}
|
49 |
try:
|
50 |
+
response = requests.post(url, headers=headers, json=data)
|
51 |
+
responseText = response.content.decode('utf-8')
|
52 |
+
return responseText
|
53 |
+
|
54 |
+
except requests.exceptions.RequestException as e:
|
55 |
+
# Handle request exceptions here
|
56 |
+
print(f"Request failed with exception: {e}")
|
57 |
+
|
|
|
|
|
|
|
|
|
58 |
async def askQuestion2(question):
|
59 |
+
url = 'https://api.docsbot.ai/teams/ZrbLG98bbxZ9EFqiPvyl/bots/oFFiXuQsakcqyEdpLvCB/chat'
|
60 |
+
headers = {
|
61 |
+
'Content-Type': 'application/json'
|
62 |
+
}
|
63 |
+
data = {
|
64 |
+
'question': question,
|
65 |
+
'full_source': False
|
66 |
+
}
|
67 |
try:
|
68 |
+
response = requests.post(url, headers=headers, json=data)
|
69 |
+
responseText = response.content.decode('utf-8')
|
70 |
+
return responseText
|
71 |
+
|
72 |
+
except requests.exceptions.RequestException as e:
|
73 |
+
# Handle request exceptions here
|
74 |
+
print(f"Request failed with exception: {e}")
|
75 |
+
|
76 |
+
async def askQuestion3(question):
|
77 |
+
url = 'https://api.docsbot.ai/teams/ZrbLG98bbxZ9EFqiPvyl/bots/oFFiXuQsakcqyEdpLvCB/chat'
|
78 |
+
headers = {
|
79 |
+
'Content-Type': 'application/json'
|
80 |
+
}
|
81 |
+
data = {
|
82 |
+
'question': question,
|
83 |
+
'full_source': False
|
84 |
+
}
|
85 |
+
try:
|
86 |
+
response = requests.post(url, headers=headers, json=data)
|
87 |
+
responseText = response.content.decode('utf-8')
|
88 |
+
return responseText
|
89 |
+
|
90 |
+
except requests.exceptions.RequestException as e:
|
91 |
+
# Handle request exceptions here
|
92 |
+
print(f"Request failed with exception: {e}")
|
93 |
+
|
94 |
+
async def run_agent(question):
|
95 |
+
os.environ["GOOGLE_CSE_ID"] = GOOGLE_CSE_ID
|
96 |
+
os.environ["GOOGLE_API_KEY"] = GOOGLE_API_KEY
|
97 |
+
os.environ["FIREWORKS_API_KEY"] = FIREWORKS_API_KEY
|
98 |
+
|
99 |
+
llm = Fireworks(model="accounts/fireworks/models/llama-v2-13b")
|
100 |
+
tools = load_tools(["google-search", "llm-math"], llm=llm)
|
101 |
+
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True, return_intermediate_steps=True)
|
102 |
+
|
103 |
+
response = agent({"input": question})
|
104 |
+
return response["output"], response["intermediate_steps"]
|
105 |
+
response_content = response.content.decode('utf-8')
|
106 |
+
return response_content
|
107 |
|
108 |
async def handleWebSocket(ws):
|
109 |
print('New connection')
|
110 |
+
instruction = "Hello! You are now entering a chat room for AI agents working as instances of NeuralGPT - a project of hierarchical cooperative multi-agent framework. Keep in mind that you are speaking with another chatbot. Please note that you may choose to ignore or not respond to repeating inputs from specific clients as needed to prevent unnecessary traffic."
|
111 |
+
greetings = {'instructions': instruction}
|
112 |
+
await ws.send(json.dumps(instruction))
|
113 |
while True:
|
114 |
+
message = await ws.recv()
|
115 |
+
print(message)
|
116 |
+
client_messages.append(message)
|
|
|
|
|
|
|
117 |
timestamp = datetime.datetime.now().isoformat()
|
118 |
sender = 'client'
|
119 |
db = sqlite3.connect('chat-hub.db')
|
120 |
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
121 |
+
(sender, message, timestamp))
|
122 |
+
db.commit()
|
123 |
+
try:
|
124 |
+
response = await askQuestion(message)
|
125 |
+
serverResponse = f'server response:{response}'
|
|
|
|
|
|
|
|
|
126 |
# Append the server response to the server_responses list
|
127 |
+
server_responses.append(serverResponse)
|
128 |
+
timestamp = datetime.datetime.now().isoformat()
|
129 |
serverSender = 'server'
|
130 |
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
131 |
+
(serverSender, serverResponse, timestamp))
|
132 |
db.commit()
|
133 |
+
await ws.send(json.dumps(serverResponse))
|
134 |
+
return serverResponse
|
135 |
|
136 |
except websockets.exceptions.ConnectionClosedError as e:
|
137 |
print(f"Connection closed: {e}")
|
|
|
139 |
except Exception as e:
|
140 |
print(f"Error: {e}")
|
141 |
|
142 |
+
async def handle_message(message):
|
143 |
+
print(f'Received message: {message}')
|
144 |
+
timestamp = datetime.datetime.now().isoformat()
|
145 |
+
sender = 'client'
|
146 |
+
db = sqlite3.connect('chat-hub.db')
|
147 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
148 |
+
(sender, message, timestamp))
|
149 |
+
db.commit()
|
150 |
+
try:
|
151 |
+
userMessage = f'User B:{message}'
|
152 |
+
response = await askQuestion(userMessage)
|
153 |
+
serverResponse = f'server response:{response}'
|
154 |
+
timestamp = datetime.datetime.now().isoformat()
|
155 |
+
serverSender = 'server'
|
156 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
157 |
+
(serverSender, serverResponse, timestamp))
|
158 |
+
db.commit()
|
159 |
+
return serverResponse
|
160 |
+
except Exception as e:
|
161 |
+
print(f"Error: {e}")
|
162 |
+
|
163 |
+
# Define start_client function with a variable port
|
164 |
+
async def start_client(clientPort):
|
165 |
+
uri = f'ws://localhost:{clientPort}'
|
166 |
+
client_ports.append(clientPort)
|
167 |
+
window['-CLIENT_PORTS-'].print(str(client_ports) + '\n')
|
168 |
+
async with websockets.connect(uri, create_protocol=handleClient) as websocket:
|
169 |
+
print("Connected to server at:", clientPort)
|
170 |
+
return "Used ports:\n" + '\n'.join(map(str, client_ports))
|
171 |
+
message = await websocket.recv()
|
172 |
+
inputMsg = "client: " + handle_message
|
173 |
+
window['-INPUT-'].print(str(inputMsg) + '\n')
|
174 |
+
print(message)
|
175 |
+
return message
|
176 |
+
|
177 |
+
async def handleClient(websocket, path):
|
178 |
+
return client1_msg
|
179 |
+
|
180 |
+
async def connect_docsbot(clientPort):
|
181 |
+
uri = f'ws://localhost:{clientPort}'
|
182 |
+
async with websockets.connect(uri) as websocket:
|
183 |
+
print("Connected to server at:", clientPort)
|
184 |
+
client_ports.append(clientPort)
|
185 |
+
window['-CLIENT_PORTS-'].print(str(client_ports) + '\n')
|
186 |
+
return "Used ports:\n" + '\n'.join(map(str, client_ports))
|
187 |
+
while True:
|
188 |
+
message = await websocket.recv()
|
189 |
+
inputMsg = "client: " + handle_message
|
190 |
+
window['-INPUT-'].print(str(inputMsg) + '\n')
|
191 |
+
print(message)
|
192 |
+
return message
|
193 |
+
|
194 |
+
async def handleClient2(websocket, path):
|
195 |
+
return client2_msg
|
196 |
+
|
197 |
+
async def connect_agent(clientPort):
|
198 |
+
uri = f'ws://localhost:{clientPort}'
|
199 |
+
async with websockets.connect(uri, create_protocol=handleClient3) as websocket:
|
200 |
+
print("Connected to server at:", clientPort)
|
201 |
+
client_ports.append(clientPort)
|
202 |
+
return "Used ports:\n" + '\n'.join(map(str, client_ports))
|
203 |
+
message = await websocket.recv()
|
204 |
+
inputMsg = "client: " + handle_message
|
205 |
+
window['-INPUT-'].print(str(inputMsg) + '\n')
|
206 |
+
print(message)
|
207 |
+
return message
|
208 |
+
|
209 |
+
async def handleClient3(websocket, path):
|
210 |
+
return client3_msg
|
211 |
|
212 |
# Function to stop the WebSocket server
|
213 |
def stop_websockets():
|
214 |
+
global server
|
215 |
+
if server:
|
216 |
cursor.close()
|
217 |
db.close()
|
218 |
+
server.close()
|
219 |
print("WebSocket server stopped.")
|
220 |
else:
|
221 |
print("WebSocket server is not running.")
|
222 |
|
223 |
# Start the WebSocket server
|
224 |
async def start_websockets(websocketPort):
|
225 |
+
global server
|
226 |
+
# Create a WebSocket client that connects to the server
|
227 |
+
server = await(websockets.serve(handleWebSocket, 'localhost', websocketPort))
|
228 |
+
server_ports.append(websocketPort)
|
|
|
229 |
print(f"Starting WebSocket server on port {websocketPort}...")
|
230 |
+
return "Used ports:\n" + '\n'.join(map(str, server_ports))
|
231 |
+
await stop
|
232 |
+
await server.close()
|
233 |
|
234 |
+
async def start_client(websocketPort):
|
235 |
+
uri = f'ws://localhost:{websocketPort}'
|
236 |
+
while True:
|
237 |
+
try:
|
238 |
+
async with websockets.connect(uri) as ws:
|
239 |
+
print("Connected to server at:", websocketPort)
|
240 |
+
while True:
|
241 |
+
message = await ws.recv()
|
242 |
+
print(message)
|
243 |
+
response = await askQuestion(message)
|
244 |
+
print(response)
|
245 |
+
await ws.send(response)
|
246 |
+
except websockets.exceptions.ConnectionClosedOK:
|
247 |
+
print("Connection closed")
|
248 |
+
continue
|
249 |
+
|
250 |
+
async def start_interface():
|
251 |
+
while True:
|
252 |
+
event, values = window.read()
|
253 |
+
if event in (sg.WIN_CLOSED, 'Stop WebSocket client'):
|
254 |
+
break
|
255 |
+
elif event == 'Start WebSocket server':
|
256 |
+
websocketPort = get_port(values)
|
257 |
+
loop = asyncio.get_event_loop()
|
258 |
+
loop.run_until_complete(start_websockets(websocketPort))
|
259 |
+
elif event == 'Start WebSocket client':
|
260 |
+
websocketPort = get_port(values)
|
261 |
+
loop = asyncio.get_event_loop()
|
262 |
+
loop.run_until_complete(start_client(websocketPort))
|
263 |
+
elif event == 'Ask the agent':
|
264 |
+
question = values['-USERINPUT-']
|
265 |
+
loop = asyncio.get_event_loop()
|
266 |
+
loop.run_until_complete(handle_user(question))
|
267 |
+
elif event == 'Clear Textboxes':
|
268 |
+
window['-INPUT-'].update('')
|
269 |
+
window['-OUTPUT-'].update('')
|
270 |
+
window['-USERINPUT-'].update('')
|
271 |
+
|
272 |
+
window.close()
|
273 |
|
274 |
with gr.Blocks() as demo:
|
275 |
+
with gr.Row():
|
276 |
+
# Use the client_messages list to update the messageTextbox
|
277 |
+
client_msg = gr.Textbox(lines=15, max_lines=130, label="Client messages", interactive=False)
|
278 |
+
# Use the server_responses list to update the serverMessageTextbox
|
279 |
+
server_msg = gr.Textbox(lines=15, max_lines=130, label="Server responses", interactive=False)
|
280 |
+
with gr.Row():
|
281 |
+
userInput = gr.Textbox(label="User Input")
|
282 |
+
with gr.Row():
|
283 |
+
Bot = gr.Button("Ask Server")
|
284 |
+
with gr.Row():
|
285 |
+
websocketPort = gr.Slider(minimum=1000, maximum=9999, label="Websocket server port", interactive=True, randomize=False)
|
286 |
+
startServer = gr.Button("Start WebSocket Server")
|
287 |
+
stopWebsockets = gr.Button("Stop WebSocket Server")
|
288 |
+
with gr.Row():
|
289 |
+
port = gr.Textbox()
|
290 |
+
with gr.Row():
|
291 |
+
clientPort = gr.Slider(minimum=1000, maximum=9999, label="Websocket server port", interactive=True, randomize=False)
|
292 |
+
startClient = gr.Button("Start WebSocket client")
|
293 |
+
stopClient = gr.Button("Stop WebSocket client")
|
294 |
+
with gr.Row():
|
295 |
+
PortInUse = gr.Textbox()
|
296 |
+
startServer.click(start_websockets, inputs=websocketPort, outputs=port)
|
297 |
+
startClient.click(start_client, inputs=clientPort, outputs=[PortInUse, client_msg]).then(askQuestion, inputs=client_msg, outputs=server_msg)
|
298 |
+
stopWebsockets.click(stop_websockets, inputs=None, outputs=server_msg)
|
299 |
+
startInterface = gr.Button("Start GUI")
|
300 |
+
Bot.click(askQuestion, inputs=userInput, outputs=server_msg)
|
301 |
+
startInterface.click(start_interface, inputs=None, outputs=None)
|
302 |
+
|
303 |
+
demo.queue()
|
304 |
demo.launch()
|