BitTransformerLM / launch_dual_dashboard.py
WCNegentropy's picture
πŸš€ OS Launch: Clean documentation and refined licensing
a406cc1 verified
raw
history blame
5.26 kB
#!/usr/bin/env python3
"""
BitTransformerLM Dual Dashboard Launcher
========================================
Launch both Flask and Gradio dashboards simultaneously for maximum flexibility.
- Flask Dashboard: http://localhost:5000 (Docker/production compatible)
- Gradio Dashboard: http://localhost:7860 (HuggingFace Spaces compatible)
- MCP Server: http://localhost:8000 (if enabled)
"""
import os
import sys
import time
import threading
import subprocess
from pathlib import Path
# Add current directory to path
sys.path.insert(0, str(Path(__file__).parent))
def launch_flask_dashboard():
"""Launch the Flask dashboard in a separate thread."""
print("🌐 Starting Flask Dashboard...")
try:
os.environ["FLASK_ENV"] = "development"
# Import and run Flask app
from bit_transformer.dashboard_app import app
app.run(host="127.0.0.1", port=5000, debug=False, use_reloader=False)
except Exception as e:
print(f"❌ Flask dashboard failed: {e}")
def launch_gradio_dashboard():
"""Launch the Gradio dashboard in a separate thread."""
print("🎨 Starting Gradio Dashboard...")
try:
from gradio_dashboard import run_gradio_server
run_gradio_server(host="127.0.0.1", port=7860, share=False)
except Exception as e:
print(f"❌ Gradio dashboard failed: {e}")
def launch_mcp_server():
"""Launch the MCP server if requested."""
print("πŸ”— Starting MCP Server...")
try:
from mcp_server import app as mcp_app
mcp_app.run(host="127.0.0.1", port=8000, debug=False, use_reloader=False)
except Exception as e:
print(f"❌ MCP server failed: {e}")
def main():
"""Main launcher function."""
print("πŸš€ BitTransformerLM Dual Dashboard Launcher")
print("=" * 50)
# Check requirements
try:
import flask
import gradio
print(f"βœ… Flask {flask.__version__} and Gradio {gradio.__version__} are available")
except ImportError as e:
print(f"❌ Missing dependencies: {e}")
print("Please run: pip install -r requirements-gradio.txt")
return
# Configuration
enable_flask = os.getenv("ENABLE_FLASK", "true").lower() == "true"
enable_gradio = os.getenv("ENABLE_GRADIO", "true").lower() == "true"
enable_mcp = os.getenv("ENABLE_MCP", "false").lower() == "true"
print(f"πŸ”§ Configuration:")
print(f" Flask Dashboard: {'Enabled' if enable_flask else 'Disabled'}")
print(f" Gradio Dashboard: {'Enabled' if enable_gradio else 'Disabled'}")
print(f" MCP Server: {'Enabled' if enable_mcp else 'Disabled'}")
print()
# Launch threads
threads = []
if enable_flask:
flask_thread = threading.Thread(target=launch_flask_dashboard, daemon=True)
flask_thread.start()
threads.append(("Flask", flask_thread))
time.sleep(2) # Stagger startup
if enable_mcp:
# Set MCP server address for other components
os.environ["MCP_SERVER_ADDR"] = "http://localhost:8000"
mcp_thread = threading.Thread(target=launch_mcp_server, daemon=True)
mcp_thread.start()
threads.append(("MCP", mcp_thread))
time.sleep(2) # Stagger startup
if enable_gradio:
gradio_thread = threading.Thread(target=launch_gradio_dashboard, daemon=True)
gradio_thread.start()
threads.append(("Gradio", gradio_thread))
# Wait for startup
time.sleep(3)
# Print access URLs
print("🌍 Access URLs:")
if enable_flask:
print(" Flask Dashboard: http://localhost:5000")
if enable_gradio:
print(" Gradio Dashboard: http://localhost:7860")
if enable_mcp:
print(" MCP Server: http://localhost:8000")
print()
print("πŸ’‘ Usage Tips:")
print(" β€’ Flask dashboard: Full Docker/production compatibility")
print(" β€’ Gradio dashboard: HuggingFace Spaces ready interface")
print(" β€’ MCP server: Programmatic API access")
print(" β€’ Both dashboards share the same model state")
print(" β€’ Press Ctrl+C to stop all services")
print()
try:
# Keep main thread alive
while True:
time.sleep(1)
except KeyboardInterrupt:
print("\nπŸ›‘ Shutting down all services...")
# Threads will be cleaned up automatically as they are daemon threads
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="BitTransformerLM Dual Dashboard")
parser.add_argument("--flask-only", action="store_true", help="Launch only Flask dashboard")
parser.add_argument("--gradio-only", action="store_true", help="Launch only Gradio dashboard")
parser.add_argument("--enable-mcp", action="store_true", help="Enable MCP server")
args = parser.parse_args()
# Override environment based on args
if args.flask_only:
os.environ["ENABLE_FLASK"] = "true"
os.environ["ENABLE_GRADIO"] = "false"
elif args.gradio_only:
os.environ["ENABLE_FLASK"] = "false"
os.environ["ENABLE_GRADIO"] = "true"
if args.enable_mcp:
os.environ["ENABLE_MCP"] = "true"
main()