#!/usr/bin/env python """ Startup script for Qwen3-TTS API and Gradio demo. Runs both the FastAPI backend and Gradio frontend. """ import sys import subprocess import time import webbrowser from pathlib import Path import argparse def main(): parser = argparse.ArgumentParser( description="Start Qwen3-TTS API and optional Gradio demo" ) parser.add_argument( "--no-demo", action="store_true", help="Run only the API server without Gradio demo" ) parser.add_argument( "--host", default="0.0.0.0", help="API host (default: 0.0.0.0)" ) parser.add_argument( "--port", type=int, default=8000, help="API port (default: 8000)" ) parser.add_argument( "--demo-port", type=int, default=7860, help="Gradio demo port (default: 7860)" ) parser.add_argument( "--no-browser", action="store_true", help="Don't open browser automatically" ) args = parser.parse_args() print(""" ╔════════════════════════════════════════════════════════════════╗ ║ Qwen3-TTS OpenAI-like API Server ║ ║ Starting API and Gradio Demo... ║ ╚════════════════════════════════════════════════════════════════╝ """) # Start API server print(f"\n📡 Starting API server on http://{args.host}:{args.port}") print(" Docs: http://localhost:{}/docs".format(args.port)) api_cmd = [ sys.executable, "-m", "uvicorn", "api.main:app", "--host", args.host, "--port", str(args.port), "--log-level", "info", ] api_process = subprocess.Popen(api_cmd) # Give API time to start time.sleep(5) if not args.no_demo: # Start Gradio demo in separate process print(f"\n🎨 Starting Gradio demo on http://localhost:{args.demo_port}") demo_cmd = [ sys.executable, "-m", "api.gradio_demo", ] # Note: Gradio will bind to 0.0.0.0:7860 by default # Modify api/gradio_demo.py if you need to change this demo_process = subprocess.Popen(demo_cmd) # Open browser if requested if not args.no_browser: time.sleep(3) try: webbrowser.open(f"http://localhost:{args.demo_port}") except Exception as e: print(f"Could not open browser: {e}") print(""" ╔════════════════════════════════════════════════════════════════╗ ║ ✅ Services started successfully! ║ ║ ║ ║ API Server: http://localhost:{} ║ ║ API Docs: http://localhost:{}/docs ║ ║ Gradio Demo: http://localhost:{} ║ ║ ║ ║ Press Ctrl+C to stop all services ║ ╚════════════════════════════════════════════════════════════════╝ """.format(args.port, args.port, args.demo_port)) try: if not args.no_demo: # Wait for both processes api_process.wait() demo_process.wait() else: # Wait for API process api_process.wait() except KeyboardInterrupt: print("\n\n🛑 Shutting down...") api_process.terminate() if not args.no_demo: demo_process.terminate() # Wait for graceful shutdown try: api_process.wait(timeout=5) except subprocess.TimeoutExpired: api_process.kill() if not args.no_demo: try: demo_process.wait(timeout=5) except subprocess.TimeoutExpired: demo_process.kill() print("👋 Services stopped.") sys.exit(0) if __name__ == "__main__": main()