File size: 5,257 Bytes
a406cc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/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()