Spaces:
Sleeping
Sleeping
File size: 4,969 Bytes
c024705 818ef98 c024705 818ef98 c024705 |
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 151 152 |
#!/usr/bin/env python3
"""
AIMHSA Simple Launcher
Runs the simplified app.pybcp.py backend and frontend server
"""
import os
import sys
import time
import threading
import webbrowser
import argparse
from http.server import ThreadingHTTPServer, SimpleHTTPRequestHandler
from urllib.parse import urlparse
class ChatBotHandler(SimpleHTTPRequestHandler):
def log_message(self, format, *args):
# Suppress logging for Chrome DevTools and other browser noise
if (len(args) > 0 and
('.well-known' in str(args) or
'favicon.ico' in str(args) or
'apple-touch-icon' in str(args) or
'robots.txt' in str(args) or
'sitemap.xml' in str(args))):
return
super().log_message(format, *args)
def do_GET(self):
parsed_path = urlparse(self.path)
path = parsed_path.path
# Handle browser requests silently
if (path.startswith('/.well-known/') or
path.startswith('/favicon.ico') or
path.startswith('/apple-touch-icon') or
path.startswith('/robots.txt') or
path.startswith('/sitemap.xml')):
self.send_response(404)
self.end_headers()
return
# Handle routing
if path == '/':
self.serve_file('index.html')
elif path == '/landing' or path == '/landing.html':
self.serve_file('landing.html')
elif path == '/login':
self.serve_file('login.html')
elif path == '/register':
self.serve_file('register.html')
elif path.startswith('/') and '.' in path:
super().do_GET()
else:
self.serve_file('index.html')
def serve_file(self, filename):
try:
if os.path.exists(filename):
self.path = '/' + filename
super().do_GET()
else:
self.send_error(404, f"File not found: {filename}")
except Exception as e:
self.send_error(500, f"Server error: {str(e)}")
def run_simple_backend(api_port=7860):
"""Run the simplified Flask backend"""
print(f"π Starting Simple AIMHSA Backend on port {api_port}...")
try:
# Import the simplified app
import app as flask_app
# Run the Flask app
flask_app.app.run(host="0.0.0.0", port=api_port, debug=False, use_reloader=False)
except ImportError as e:
print(f"β Error importing Flask app: {e}")
print("Make sure app.pybcp.py exists and is renamed to app.py")
except Exception as e:
print(f"β Error starting backend: {e}")
def run_frontend(frontend_port=8000):
"""Run the frontend HTTP server"""
print(f"π Starting Frontend on port {frontend_port}...")
base_dir = os.path.dirname(os.path.abspath(__file__))
chatbot_dir = os.path.join(base_dir, "chatbot")
if not os.path.isdir(chatbot_dir):
print(f"β ERROR: chatbot/ directory not found at {chatbot_dir}")
return
os.chdir(chatbot_dir)
addr = ("", frontend_port)
httpd = ThreadingHTTPServer(addr, ChatBotHandler)
url = f"http://localhost:{frontend_port}/"
print(f"β
Frontend running at {url}")
print("Available routes:")
print(f" - {url} (main chat)")
print(f" - {url}login (login page)")
print(f" - {url}register (register page)")
try:
webbrowser.open(url)
except Exception:
pass
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("π Shutting down...")
httpd.server_close()
def main():
parser = argparse.ArgumentParser(description="Simple AIMHSA Launcher")
parser.add_argument("--api-port", "-a", type=int, default=7860, help="Backend port (default: 7860)")
parser.add_argument("--frontend-port", "-f", type=int, default=8000, help="Frontend port (default: 8000)")
parser.add_argument("--backend-only", "-b", action="store_true", help="Run only backend")
parser.add_argument("--frontend-only", "-w", action="store_true", help="Run only frontend")
args = parser.parse_args()
print("="*50)
print("π§ AIMHSA Simple Launcher")
print("="*50)
print(f"Backend: http://localhost:{args.api_port}")
print(f"Frontend: http://localhost:{args.frontend_port}")
print("="*50)
if args.backend_only:
run_simple_backend(args.api_port)
elif args.frontend_only:
run_frontend(args.frontend_port)
else:
print("π Starting both services...")
# Start backend in thread
backend_thread = threading.Thread(
target=run_simple_backend,
args=(args.api_port,),
daemon=True
)
backend_thread.start()
time.sleep(2)
run_frontend(args.frontend_port)
if __name__ == "__main__":
main()
|