Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -57,53 +57,38 @@ db.commit()
|
|
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 askQuestion2(question):
|
63 |
-
try:
|
64 |
-
message = messages[-1]
|
65 |
-
client = Client("http://localhost:1111/")
|
66 |
-
response = client.predict(
|
67 |
-
message,
|
68 |
-
fn_index=2
|
69 |
-
)
|
70 |
-
return response
|
71 |
-
except Exception as e:
|
72 |
-
print(e)
|
73 |
-
|
74 |
# Define a function to ask a question to the chatbot and display the response
|
75 |
async def askQuestion(question):
|
76 |
try:
|
77 |
-
message =
|
78 |
response = requests.post(
|
79 |
-
"https://flowiseai-flowise.hf.space/api/v1/prediction/
|
80 |
headers={"Content-Type": "application/json"},
|
81 |
json={"question": message},
|
82 |
)
|
83 |
response_content = response.content.decode('utf-8')
|
84 |
-
|
85 |
return response_content
|
86 |
except Exception as e:
|
87 |
print(e)
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
return client_message, server_message
|
107 |
|
108 |
async def handleWebSocket(ws):
|
109 |
print('New connection')
|
@@ -122,8 +107,9 @@ async def handleWebSocket(ws):
|
|
122 |
(sender, messageText, timestamp))
|
123 |
db.commit()
|
124 |
try:
|
125 |
-
message =
|
126 |
answer = await askQuestion(message) # Use the message directly
|
|
|
127 |
response = {'answer': answer}
|
128 |
serverMessageText = response.get('answer', '')
|
129 |
await ws.send(json.dumps(response))
|
@@ -152,25 +138,6 @@ def stop_websockets():
|
|
152 |
else:
|
153 |
print("WebSocket server is not running.")
|
154 |
|
155 |
-
async def start_client():
|
156 |
-
async with websockets.connect('ws://localhost:5000') as ws:
|
157 |
-
while True:
|
158 |
-
# Listen for messages from the server
|
159 |
-
server_message = await ws.recv()
|
160 |
-
messages.append(server_message)
|
161 |
-
return server_message
|
162 |
-
client_message = await askQuestion2(server_message)
|
163 |
-
|
164 |
-
# Send the client's response to the server
|
165 |
-
await ws.send(client_message)
|
166 |
-
|
167 |
-
# Append the client message and server response to the respective lists
|
168 |
-
client_message_textboxes.append(client_message)
|
169 |
-
server_response_textboxes.append(server_message)
|
170 |
-
return client_message
|
171 |
-
# Pause for a short duration to allow for smooth streaming
|
172 |
-
await asyncio.sleep(0.1)
|
173 |
-
|
174 |
# Start the WebSocket server
|
175 |
async def start_websockets(websocketPort):
|
176 |
global messageTextbox, serverMessageTextbox, websocket_server
|
@@ -181,15 +148,28 @@ async def start_websockets(websocketPort):
|
|
181 |
print(f"Starting WebSocket server on port {websocketPort}...")
|
182 |
return "Used ports:\n" + '\n'.join(map(str, used_ports))
|
183 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
with gr.Blocks() as demo:
|
185 |
with gr.Tabs(elem_classes="tab-buttons") as tabs:
|
186 |
with gr.TabItem("Websocket Server", elem_id="websocket_server", id=0):
|
187 |
with gr.Column(scale=1, min_width=600):
|
188 |
with gr.Row():
|
189 |
# Use the client_messages list to update the messageTextbox
|
190 |
-
|
191 |
# Use the server_responses list to update the serverMessageTextbox
|
192 |
-
|
193 |
with gr.Row():
|
194 |
websocketPort = gr.Slider(minimum=1000, maximum=9999, label="Websocket server port", interactive=True, randomize=False)
|
195 |
startWebsockets = gr.Button("Start WebSocket Server")
|
@@ -199,10 +179,13 @@ with gr.Blocks() as demo:
|
|
199 |
with gr.Row():
|
200 |
port = gr.Textbox()
|
201 |
startWebsockets.click(start_websockets, inputs=websocketPort, outputs=port)
|
202 |
-
gui.click(start_client, inputs=None, outputs=
|
203 |
|
|
|
|
|
|
|
204 |
with gr.TabItem("FalconChat", elem_id="falconchat", id=1):
|
205 |
gr.load("HuggingFaceH4/starchat-playground", src="spaces")
|
206 |
|
207 |
demo.queue()
|
208 |
-
demo.launch()
|
|
|
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 |
+
message = server_responses[-1]
|
65 |
response = requests.post(
|
66 |
+
"https://flowiseai-flowise.hf.space/api/v1/prediction/0da97a1a-963b-4f6b-bcad-928c0630e5c1",
|
67 |
headers={"Content-Type": "application/json"},
|
68 |
json={"question": message},
|
69 |
)
|
70 |
response_content = response.content.decode('utf-8')
|
71 |
+
client_messages.append(response_content)
|
72 |
return response_content
|
73 |
except Exception as e:
|
74 |
print(e)
|
75 |
|
76 |
+
# Define a function to ask a question to the chatbot and display the response
|
77 |
+
async def askQuestion2(question):
|
78 |
+
try:
|
79 |
+
message = server_responses[-1]
|
80 |
+
url = "https://api.chaindesk.ai/datastores/query/clka5g9zc000drg6mxl671ekv"
|
81 |
+
payload = {"query": message}
|
82 |
+
headers = {
|
83 |
+
"Authorization": "Bearer 5315cd7b-bb79-49bc-bca2-8bcc7b243504",
|
84 |
+
"Content-Type": "application/json"
|
85 |
+
}
|
86 |
+
|
87 |
+
response = requests.request("POST", url, json=payload, headers=headers)
|
88 |
+
|
89 |
+
print(response.text)
|
90 |
+
|
91 |
+
return response.text
|
|
|
|
|
92 |
|
93 |
async def handleWebSocket(ws):
|
94 |
print('New connection')
|
|
|
107 |
(sender, messageText, timestamp))
|
108 |
db.commit()
|
109 |
try:
|
110 |
+
message = client_messages[-1]
|
111 |
answer = await askQuestion(message) # Use the message directly
|
112 |
+
messages.append(answer)
|
113 |
response = {'answer': answer}
|
114 |
serverMessageText = response.get('answer', '')
|
115 |
await ws.send(json.dumps(response))
|
|
|
138 |
else:
|
139 |
print("WebSocket server is not running.")
|
140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
# Start the WebSocket server
|
142 |
async def start_websockets(websocketPort):
|
143 |
global messageTextbox, serverMessageTextbox, websocket_server
|
|
|
148 |
print(f"Starting WebSocket server on port {websocketPort}...")
|
149 |
return "Used ports:\n" + '\n'.join(map(str, used_ports))
|
150 |
|
151 |
+
async def start_client():
|
152 |
+
async with websockets.connect('ws://localhost:5000') as ws:
|
153 |
+
while True:
|
154 |
+
# Listen for messages from the server
|
155 |
+
server_message = await ws.recv()
|
156 |
+
server_responses.append(server_message)
|
157 |
+
messages.append(server_message)
|
158 |
+
client_response = await askQuestion2(server_message)
|
159 |
+
client_messages.append(client_response)
|
160 |
+
await ws.send(client_response)
|
161 |
+
return server_message
|
162 |
+
await asyncio.sleep(0.1)
|
163 |
+
|
164 |
with gr.Blocks() as demo:
|
165 |
with gr.Tabs(elem_classes="tab-buttons") as tabs:
|
166 |
with gr.TabItem("Websocket Server", elem_id="websocket_server", id=0):
|
167 |
with gr.Column(scale=1, min_width=600):
|
168 |
with gr.Row():
|
169 |
# Use the client_messages list to update the messageTextbox
|
170 |
+
client_msg = gr.Textbox(lines=15, max_lines=130, label="Client inputs")
|
171 |
# Use the server_responses list to update the serverMessageTextbox
|
172 |
+
server_msg = gr.Textbox(lines=15, max_lines=130, label="Server responses")
|
173 |
with gr.Row():
|
174 |
websocketPort = gr.Slider(minimum=1000, maximum=9999, label="Websocket server port", interactive=True, randomize=False)
|
175 |
startWebsockets = gr.Button("Start WebSocket Server")
|
|
|
179 |
with gr.Row():
|
180 |
port = gr.Textbox()
|
181 |
startWebsockets.click(start_websockets, inputs=websocketPort, outputs=port)
|
182 |
+
gui.click(start_client, inputs=None, outputs=server_msg)
|
183 |
|
184 |
+
client_msg.change(askQuestion, inputs=client_msg, outputs=server_msg)
|
185 |
+
server_msg.change(askQuestion2, inputs=server_msg, outputs=client_msg).then(askQuestion, inputs=client_msg, outputs=server_msg)
|
186 |
+
|
187 |
with gr.TabItem("FalconChat", elem_id="falconchat", id=1):
|
188 |
gr.load("HuggingFaceH4/starchat-playground", src="spaces")
|
189 |
|
190 |
demo.queue()
|
191 |
+
demo.launch(share=True, server_port=1111)
|