Arcypojeb commited on
Commit
4bfaa35
·
1 Parent(s): b144c13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -24
app.py CHANGED
@@ -7,6 +7,7 @@ import asyncio
7
  import sqlite3
8
  import json
9
  import gradio as gr
 
10
  from gradio_client import Client
11
  import time
12
 
@@ -22,6 +23,17 @@ stop = asyncio.Future()
22
  messageTextbox = None
23
  serverMessageTextbox = None
24
 
 
 
 
 
 
 
 
 
 
 
 
25
  # Set up the HTTP server
26
  class SimpleHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
27
  def do_GET(self):
@@ -46,21 +58,34 @@ def sendErrorMessage(ws, errorMessage):
46
  errorResponse = {'error': errorMessage}
47
  ws.send(json.dumps(errorResponse))
48
 
49
- # Function to send a question to the chatbot and get the response
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  async def askQuestion(question):
51
  try:
 
52
  response = requests.post(
53
- "https://flowiseai-flowise.hf.space/api/v1/prediction/522afa32-484c-471e-9ba5-4d6d2edfb89b",
54
  headers={"Content-Type": "application/json"},
55
  json={"question": message},
56
  )
57
  response_content = response.content.decode('utf-8')
58
-
59
  return response_content
60
  except Exception as e:
61
  print(e)
62
 
63
-
64
  async def listen_for_messages():
65
  while True:
66
  if len(client_messages) > 0:
@@ -88,7 +113,6 @@ async def handleWebSocket(ws):
88
  message_copy = message
89
  client_messages.append(message_copy)
90
  print(f'Received message: {message}')
91
- parsedMessage = json.loads(message)
92
  messageText = message
93
  messages.append(message)
94
  timestamp = datetime.datetime.now().isoformat()
@@ -128,6 +152,25 @@ def stop_websockets():
128
  else:
129
  print("WebSocket server is not running.")
130
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
  # Start the WebSocket server
132
  async def start_websockets(websocketPort):
133
  global messageTextbox, serverMessageTextbox, websocket_server
@@ -139,24 +182,27 @@ async def start_websockets(websocketPort):
139
  return "Used ports:\n" + '\n'.join(map(str, used_ports))
140
 
141
  with gr.Blocks() as demo:
142
-
143
- with gr.Column(scale=1, min_width=600):
144
- with gr.Row():
145
- # Use the client_messages list to update the messageTextbox
146
- client_message = gr.Textbox(lines=15, max_lines=130, label="Client inputs")
147
- # Use the server_responses list to update the serverMessageTextbox
148
- server_message = gr.Textbox(lines=15, max_lines=130, label="Server responses")
149
- with gr.Row():
150
- websocketPort = gr.Slider(minimum=1000, maximum=9999, label="Websocket server port", interactive=True, randomize=False)
151
- startWebsockets = gr.Button("Start WebSocket Server")
152
- stopWebsockets = gr.Button("Stop WebSocket Server")
153
- with gr.Row():
154
- gui = gr.Button("connect interface")
155
- with gr.Row():
156
- port = gr.Textbox()
157
- startWebsockets.click(start_websockets, inputs=websocketPort, outputs=port)
158
- gui.click(listen_for_messages, inputs=None, outputs={client_message, server_message})
159
- stopWebsockets.click(stop_websockets)
 
 
 
160
 
161
  demo.queue()
162
- demo.launch(share=True)
 
7
  import sqlite3
8
  import json
9
  import gradio as gr
10
+ from bs4 import BeautifulSoup
11
  from gradio_client import Client
12
  import time
13
 
 
23
  messageTextbox = None
24
  serverMessageTextbox = None
25
 
26
+ def slow_echo(message, history):
27
+ for i in range(len(message)):
28
+ time.sleep(0.3)
29
+ yield "You typed: " + message[: i+1]
30
+
31
+ # Define a function to read the HTML file
32
+ def read_html_file(file_name):
33
+ with open(file_name, 'r') as file:
34
+ html_content = file.read()
35
+ return html_content
36
+
37
  # Set up the HTTP server
38
  class SimpleHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
39
  def do_GET(self):
 
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 = messages[-1]
78
  response = requests.post(
79
+ "https://flowiseai-flowise.hf.space/api/v1/prediction/8f9d1727-7f27-4445-a5c6-9efd72dd0320",
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
  async def listen_for_messages():
90
  while True:
91
  if len(client_messages) > 0:
 
113
  message_copy = message
114
  client_messages.append(message_copy)
115
  print(f'Received message: {message}')
 
116
  messageText = message
117
  messages.append(message)
118
  timestamp = datetime.datetime.now().isoformat()
 
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
 
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
+ client_message = gr.Textbox(lines=15, max_lines=130, label="Client inputs")
191
+ # Use the server_responses list to update the serverMessageTextbox
192
+ server_message = gr.Textbox(lines=15, max_lines=130, label="Server responses")
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")
196
+ stopWebsockets = gr.Button("Stop WebSocket Server")
197
+ with gr.Row():
198
+ gui = gr.Button("connect interface")
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=[client_message, server_message])
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(share=True, server_port=1111)