Spaces:
Running
Running
Ramesh-vani
commited on
Commit
·
d2db5a7
1
Parent(s):
4aa8910
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import asyncio
|
2 |
+
import websockets
|
3 |
+
|
4 |
+
async def handle_user_client(websocket, path):
|
5 |
+
external_uri = 'wss://ramesh-vani-wspython.hf.space'
|
6 |
+
|
7 |
+
async with websockets.connect(external_uri) as external_ws:
|
8 |
+
async def forward_user_to_external():
|
9 |
+
async for user_message in websocket:
|
10 |
+
await external_ws.send(user_message)
|
11 |
+
|
12 |
+
async def forward_external_to_user():
|
13 |
+
async for external_message in external_ws:
|
14 |
+
print(f"Received from external server: {external_message}")
|
15 |
+
await websocket.send(external_message)
|
16 |
+
|
17 |
+
# Start two tasks to handle message forwarding in both directions
|
18 |
+
await asyncio.gather(
|
19 |
+
forward_user_to_external(),
|
20 |
+
forward_external_to_user(),
|
21 |
+
)
|
22 |
+
|
23 |
+
# Set up WebSocket server for user
|
24 |
+
user_server = websockets.serve(handle_user_client, "0.0.0.0", 7860)
|
25 |
+
|
26 |
+
# Start the server and run the event loop
|
27 |
+
asyncio.get_event_loop().run_until_complete(user_server)
|
28 |
+
asyncio.get_event_loop().run_forever()
|