Spaces:
Running
Running
Commit
·
d3215d5
1
Parent(s):
e0cfbe7
UI
Browse files- test_websocket.py +81 -0
- ui.py +1 -1
test_websocket.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import websockets
|
| 3 |
+
import json
|
| 4 |
+
import numpy as np
|
| 5 |
+
import time
|
| 6 |
+
|
| 7 |
+
# Configuration
|
| 8 |
+
HF_SPACE_URL = "https://androidguy-speaker-diarization.hf.space"
|
| 9 |
+
RENDER_SIGNAL_URL = "wss://render-signal-audio.onrender.com/stream"
|
| 10 |
+
WS_INFERENCE_ENDPOINT = f"wss://{HF_SPACE_URL.replace('https://', '')}/ws_inference"
|
| 11 |
+
WS_RELAY_ENDPOINT = "wss://render-signal-audio.onrender.com/ws_relay"
|
| 12 |
+
|
| 13 |
+
async def test_websocket_connections():
|
| 14 |
+
print(f"Testing WebSocket connections...")
|
| 15 |
+
|
| 16 |
+
# Test HF Space /ws_inference connection
|
| 17 |
+
print(f"Connecting to {WS_INFERENCE_ENDPOINT}...")
|
| 18 |
+
try:
|
| 19 |
+
async with websockets.connect(WS_INFERENCE_ENDPOINT, ping_interval=30) as ws:
|
| 20 |
+
print("Connected to HF Space inference WebSocket!")
|
| 21 |
+
|
| 22 |
+
# Send test data (simulated audio)
|
| 23 |
+
test_audio = np.zeros(1600, dtype=np.float32).tobytes()
|
| 24 |
+
await ws.send(test_audio)
|
| 25 |
+
print(f"Sent test audio data: {len(test_audio)} bytes")
|
| 26 |
+
|
| 27 |
+
# Wait for response
|
| 28 |
+
for _ in range(5): # Try to get 5 responses
|
| 29 |
+
try:
|
| 30 |
+
response = await asyncio.wait_for(ws.recv(), timeout=3.0)
|
| 31 |
+
if isinstance(response, str):
|
| 32 |
+
try:
|
| 33 |
+
json_data = json.loads(response)
|
| 34 |
+
print(f"Received JSON response: {json.dumps(json_data, indent=2)}")
|
| 35 |
+
except json.JSONDecodeError:
|
| 36 |
+
print(f"Received non-JSON response: {response[:500]}...")
|
| 37 |
+
else:
|
| 38 |
+
print(f"Received binary data: {len(response)} bytes")
|
| 39 |
+
except asyncio.TimeoutError:
|
| 40 |
+
print("No response received within timeout")
|
| 41 |
+
break
|
| 42 |
+
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print(f"Error connecting to HF Space WebSocket: {e}")
|
| 45 |
+
|
| 46 |
+
# Test Render Signal WebSocket relay connection
|
| 47 |
+
print(f"\nConnecting to {WS_RELAY_ENDPOINT}...")
|
| 48 |
+
try:
|
| 49 |
+
async with websockets.connect(WS_RELAY_ENDPOINT) as ws:
|
| 50 |
+
print("Connected to Render Signal WebSocket relay!")
|
| 51 |
+
|
| 52 |
+
# Send test configuration message
|
| 53 |
+
test_config = {
|
| 54 |
+
"type": "config",
|
| 55 |
+
"client_id": f"test_client_{time.time()}",
|
| 56 |
+
"test": True
|
| 57 |
+
}
|
| 58 |
+
await ws.send(json.dumps(test_config))
|
| 59 |
+
print(f"Sent test config: {test_config}")
|
| 60 |
+
|
| 61 |
+
# Wait for responses
|
| 62 |
+
for _ in range(5): # Try to get 5 responses
|
| 63 |
+
try:
|
| 64 |
+
response = await asyncio.wait_for(ws.recv(), timeout=3.0)
|
| 65 |
+
if isinstance(response, str):
|
| 66 |
+
try:
|
| 67 |
+
json_data = json.loads(response)
|
| 68 |
+
print(f"Received JSON response: {json.dumps(json_data, indent=2)}")
|
| 69 |
+
except json.JSONDecodeError:
|
| 70 |
+
print(f"Received non-JSON response: {response[:500]}...")
|
| 71 |
+
else:
|
| 72 |
+
print(f"Received binary data: {len(response)} bytes")
|
| 73 |
+
except asyncio.TimeoutError:
|
| 74 |
+
print("No response received within timeout")
|
| 75 |
+
break
|
| 76 |
+
except Exception as e:
|
| 77 |
+
print(f"Error connecting to Render Signal WebSocket: {e}")
|
| 78 |
+
|
| 79 |
+
# Run tests for direct WebSocket communication
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
asyncio.run(test_websocket_connections())
|
ui.py
CHANGED
|
@@ -491,7 +491,7 @@ demo = build_ui()
|
|
| 491 |
|
| 492 |
def mount_ui(app: FastAPI):
|
| 493 |
"""Mount Gradio app to FastAPI"""
|
| 494 |
-
app.mount("/", demo.app)
|
| 495 |
|
| 496 |
# For standalone testing
|
| 497 |
if __name__ == "__main__":
|
|
|
|
| 491 |
|
| 492 |
def mount_ui(app: FastAPI):
|
| 493 |
"""Mount Gradio app to FastAPI"""
|
| 494 |
+
app.mount("/ui", demo.app)
|
| 495 |
|
| 496 |
# For standalone testing
|
| 497 |
if __name__ == "__main__":
|