Spaces:
Sleeping
Sleeping
Commit
·
7f7357a
1
Parent(s):
f693974
fastrtfc
Browse files
ui.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
3 |
-
from fastapi.responses import JSONResponse
|
4 |
import asyncio
|
5 |
import json
|
6 |
import logging
|
@@ -11,8 +11,13 @@ import httpx
|
|
11 |
import websockets
|
12 |
from fastrtc import RTCComponent
|
13 |
|
|
|
|
|
|
|
|
|
14 |
class Config:
|
15 |
def __init__(self):
|
|
|
16 |
self.hf_space_url = os.getenv("HF_SPACE_URL", "androidguy-speaker-diarization.hf.space")
|
17 |
self.render_url = os.getenv("RENDER_URL", "render-signal-audio.onrender.com")
|
18 |
self.default_threshold = float(os.getenv("DEFAULT_THRESHOLD", "0.7"))
|
@@ -20,7 +25,6 @@ class Config:
|
|
20 |
self.max_speakers_limit = int(os.getenv("MAX_SPEAKERS_LIMIT", "8"))
|
21 |
|
22 |
config = Config()
|
23 |
-
logger = logging.getLogger(__name__)
|
24 |
|
25 |
class ConnectionManager:
|
26 |
"""Manage WebSocket connections"""
|
@@ -494,9 +498,54 @@ async def process_audio_chunk(audio_data: bytes) -> dict:
|
|
494 |
fastapi_app = create_fastapi_app()
|
495 |
gradio_app = create_gradio_app()
|
496 |
|
497 |
-
#
|
|
|
|
|
|
|
|
|
|
|
498 |
fastapi_app.mount("/ui", gradio_app.app)
|
499 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
500 |
if __name__ == "__main__":
|
501 |
import uvicorn
|
502 |
-
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
3 |
+
from fastapi.responses import JSONResponse, RedirectResponse
|
4 |
import asyncio
|
5 |
import json
|
6 |
import logging
|
|
|
11 |
import websockets
|
12 |
from fastrtc import RTCComponent
|
13 |
|
14 |
+
# Configure logging
|
15 |
+
logging.basicConfig(level=logging.INFO)
|
16 |
+
logger = logging.getLogger(__name__)
|
17 |
+
|
18 |
class Config:
|
19 |
def __init__(self):
|
20 |
+
# URLs should not include http/https prefix as we add it contextually
|
21 |
self.hf_space_url = os.getenv("HF_SPACE_URL", "androidguy-speaker-diarization.hf.space")
|
22 |
self.render_url = os.getenv("RENDER_URL", "render-signal-audio.onrender.com")
|
23 |
self.default_threshold = float(os.getenv("DEFAULT_THRESHOLD", "0.7"))
|
|
|
25 |
self.max_speakers_limit = int(os.getenv("MAX_SPEAKERS_LIMIT", "8"))
|
26 |
|
27 |
config = Config()
|
|
|
28 |
|
29 |
class ConnectionManager:
|
30 |
"""Manage WebSocket connections"""
|
|
|
498 |
fastapi_app = create_fastapi_app()
|
499 |
gradio_app = create_gradio_app()
|
500 |
|
501 |
+
# Root redirect
|
502 |
+
@fastapi_app.get("/")
|
503 |
+
async def root():
|
504 |
+
return RedirectResponse(url="/ui")
|
505 |
+
|
506 |
+
# Mount Gradio app to FastAPI at /ui
|
507 |
fastapi_app.mount("/ui", gradio_app.app)
|
508 |
|
509 |
+
# Add diagnostic endpoints to check connections
|
510 |
+
@fastapi_app.get("/check-backend")
|
511 |
+
async def check_backend():
|
512 |
+
"""Check connection to the Render backend"""
|
513 |
+
try:
|
514 |
+
# Check if we can connect to the WebSocket endpoint on Render
|
515 |
+
websocket_url = f"wss://{config.render_url}/stream"
|
516 |
+
logger.info(f"Checking connection to Render backend at {websocket_url}")
|
517 |
+
|
518 |
+
# Don't actually connect, just return status
|
519 |
+
return {
|
520 |
+
"status": "configured",
|
521 |
+
"render_backend_url": websocket_url,
|
522 |
+
"hf_space_url": f"wss://{config.hf_space_url}/ws_inference",
|
523 |
+
"rtc_component_config": {
|
524 |
+
"url": f"wss://{config.render_url}/stream",
|
525 |
+
"modality": "audio",
|
526 |
+
"mode": "send-receive"
|
527 |
+
}
|
528 |
+
}
|
529 |
+
except Exception as e:
|
530 |
+
logger.error(f"Error checking backend: {e}")
|
531 |
+
return {
|
532 |
+
"status": "error",
|
533 |
+
"error": str(e)
|
534 |
+
}
|
535 |
+
|
536 |
+
# Log configuration on startup
|
537 |
+
@fastapi_app.on_event("startup")
|
538 |
+
async def log_configuration():
|
539 |
+
logger.info(f"Starting UI with configuration:")
|
540 |
+
logger.info(f"- HF Space URL: {config.hf_space_url}")
|
541 |
+
logger.info(f"- Render URL: {config.render_url}")
|
542 |
+
logger.info(f"- WebRTC URL: wss://{config.render_url}/stream")
|
543 |
+
logger.info(f"- WebSocket URL: wss://{config.hf_space_url}/ws_inference")
|
544 |
+
logger.info("Note: Audio will be streamed through the Render backend using WebRTC")
|
545 |
+
|
546 |
if __name__ == "__main__":
|
547 |
import uvicorn
|
548 |
+
# Use the correct port for Hugging Face Spaces (7860)
|
549 |
+
port = int(os.environ.get("PORT", 7860))
|
550 |
+
logger.info(f"Starting server on port {port}")
|
551 |
+
uvicorn.run(fastapi_app, host="0.0.0.0", port=port)
|