@woai
Prepare for Hugging Face Spaces deployment
d619c43
#!/usr/bin/env python3
"""
Unified launcher for YouTube MCP application.
Provides options to run API server, Gradio UI, or both.
"""
import argparse
import asyncio
import uvicorn
import threading
import time
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
def start_api_server(host="127.0.0.1", port=8080):
"""Start FastAPI server."""
from api_server import app
print(f"Starting API server on http://{host}:{port}")
uvicorn.run(app, host=host, port=port)
def start_gradio_ui(host="127.0.0.1", port=8081):
"""Start Gradio UI."""
import gradio_app
print(f"Starting Gradio UI on http://{host}:{port}")
gradio_app.demo.launch(server_name=host, server_port=port, share=False)
def start_both(api_host="127.0.0.1", api_port=8080, ui_host="127.0.0.1", ui_port=8081):
"""Start both API server and Gradio UI."""
print(f"Starting API server on http://{api_host}:{api_port}")
print(f"Starting Gradio UI on http://{ui_host}:{ui_port}")
# Start API server in a separate thread
api_thread = threading.Thread(
target=start_api_server,
args=(api_host, api_port),
daemon=True
)
api_thread.start()
# Wait a moment for API server to start
time.sleep(2)
# Start Gradio UI in main thread
start_gradio_ui(ui_host, ui_port)
def main():
parser = argparse.ArgumentParser(description="YouTube MCP Application Launcher")
parser.add_argument(
"--mode",
choices=["api", "ui", "both"],
default="both",
help="Launch mode: 'api' for FastAPI server only, 'ui' for Gradio UI only, 'both' for both services"
)
parser.add_argument(
"--host",
default="127.0.0.1",
help="Host address (default: 127.0.0.1)"
)
parser.add_argument(
"--port",
type=int,
default=8080,
help="Port number (default: 8080 for API, 8081 for UI in 'both' mode)"
)
args = parser.parse_args()
try:
if args.mode == "api":
start_api_server(args.host, args.port)
elif args.mode == "ui":
start_gradio_ui(args.host, args.port)
elif args.mode == "both":
start_both(args.host, args.port, args.host, args.port + 1)
except KeyboardInterrupt:
print("\nKeyboard interruption in main thread... closing server.")
except Exception as e:
print(f"Error starting application: {e}")
if __name__ == "__main__":
main()