feat: convert python backend to a pure WebSocket API server
This commit is contained in:
@@ -20,9 +20,6 @@ import logging
|
||||
import traceback
|
||||
import argparse
|
||||
import threading
|
||||
import webbrowser
|
||||
from http.server import SimpleHTTPRequestHandler
|
||||
import socketserver
|
||||
import numpy as np
|
||||
import torch
|
||||
import onnxruntime as ort
|
||||
@@ -616,27 +613,7 @@ async def start_websocket_server(host, port):
|
||||
async with websockets.serve(websocket_handler, host, port):
|
||||
await asyncio.Future()
|
||||
|
||||
# --- HTTP STATIC FILE SERVER FOR FRONTEND ---
|
||||
def start_http_server(port, directory="frontend"):
|
||||
class MyHandler(SimpleHTTPRequestHandler):
|
||||
def __init__(self, *args, **kwargs):
|
||||
# Force serve from directory relative to the project root
|
||||
base_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
full_dir = os.path.join(base_dir, directory)
|
||||
super().__init__(*args, directory=full_dir, **kwargs)
|
||||
|
||||
def log_message(self, format, *args):
|
||||
# Suppress standard logging to prevent console pollution
|
||||
pass
|
||||
|
||||
try:
|
||||
# Create a TCPServer that allows address reuse
|
||||
socketserver.TCPServer.allow_reuse_address = True
|
||||
with socketserver.TCPServer(("", port), MyHandler) as httpd:
|
||||
logger.info(f"Serving HTTP frontend on http://localhost:{port}")
|
||||
httpd.serve_forever()
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to start HTTP server: {e}")
|
||||
|
||||
# --- LOCAL AUDIO DEVICE STREAM MODE ---
|
||||
def run_local_device_mode(model_name, f0_up_key, f0_method, device, input_device, output_device, chunk_size):
|
||||
@@ -709,7 +686,6 @@ if __name__ == "__main__":
|
||||
parser.add_argument("--mode", type=str, default="websocket", choices=["websocket", "device"], help="Server running mode")
|
||||
parser.add_argument("--host", type=str, default="127.0.0.1", help="WebSocket host")
|
||||
parser.add_argument("--port", type=int, default=8765, help="WebSocket port")
|
||||
parser.add_argument("--http_port", type=int, default=8000, help="HTTP static server port for Web UI")
|
||||
parser.add_argument("--model", type=str, default="", help="RVC Model folder name inside weights/")
|
||||
parser.add_argument("--transpose", type=int, default=0, help="Pitch shift in semitones (transpose)")
|
||||
parser.add_argument("--f0_method", type=str, default="pm", choices=["pm", "harvest", "dio", "rmvpe"], help="Pitch extraction method")
|
||||
@@ -731,27 +707,7 @@ if __name__ == "__main__":
|
||||
sys.exit(1)
|
||||
|
||||
if args.mode == "websocket":
|
||||
# 1. Start HTTP Server in a background thread to serve the frontend!
|
||||
http_thread = threading.Thread(
|
||||
target=start_http_server,
|
||||
args=(args.http_port, "frontend"),
|
||||
daemon=True
|
||||
)
|
||||
http_thread.start()
|
||||
|
||||
# 2. Automatically open the Web UI in the default browser!
|
||||
web_ui_url = f"http://127.0.0.1:{args.http_port}"
|
||||
logger.info(f"Automatically launching Web UI at {web_ui_url} in browser...")
|
||||
|
||||
# We give it a tiny delay to ensure the HTTP server socket is open
|
||||
def open_browser():
|
||||
time.sleep(0.5)
|
||||
webbrowser.open(web_ui_url)
|
||||
|
||||
browser_thread = threading.Thread(target=open_browser, daemon=True)
|
||||
browser_thread.start()
|
||||
|
||||
# 3. Start the WebSocket server on the main event loop
|
||||
# Start the WebSocket server on the main event loop
|
||||
try:
|
||||
asyncio.run(start_websocket_server(args.host, args.port))
|
||||
except KeyboardInterrupt:
|
||||
|
||||
Reference in New Issue
Block a user