Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
| import os | |
| import sys | |
| from dotenv import load_dotenv | |
| # Ensure environment variables are loaded | |
| load_dotenv() | |
| # Insert the backend module into python path so internal absolute imports work | |
| root_dir = os.path.dirname(os.path.abspath(__file__)) | |
| sys.path.insert(0, os.path.join(root_dir, 'backend')) | |
| sys.path.insert(0, root_dir) | |
| # Import the application factory from our partitioned architecture | |
| from backend import create_app | |
| # Initialize the Flask application | |
| app = create_app() | |
| # --------------------------------------------------------------------------- | |
| # Serve the built React frontend (HF Spaces / single-container mode) | |
| # In development the Vite dev-server proxies /api/ to Flask instead. | |
| # --------------------------------------------------------------------------- | |
| from flask import send_from_directory, abort | |
| FRONTEND_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'frontend', 'dist') | |
| def serve_root_uploads(filename): | |
| """Serve uploaded logos and static media files directly from uploads directory.""" | |
| uploads_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'uploads') | |
| file_path = os.path.join(uploads_dir, filename) | |
| if os.path.exists(file_path): | |
| return send_from_directory(uploads_dir, filename) | |
| # Check inside uploads/logos subfolder if requested without 'logos/' prefix | |
| logos_dir = os.path.join(uploads_dir, 'logos') | |
| if os.path.exists(os.path.join(logos_dir, filename)): | |
| return send_from_directory(logos_dir, filename) | |
| abort(404) | |
| def serve_react(path): | |
| """Serve React SPA. Static assets go to dist/, everything else → index.html.""" | |
| # Never intercept API or socket.io requests | |
| if path.startswith('api/') or path.startswith('socket.io'): | |
| abort(404) | |
| if path and os.path.exists(os.path.join(FRONTEND_DIR, path)): | |
| res = send_from_directory(FRONTEND_DIR, path) | |
| else: | |
| res = send_from_directory(FRONTEND_DIR, 'index.html') | |
| res.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate' | |
| res.headers['Pragma'] = 'no-cache' | |
| res.headers['Expires'] = '0' | |
| return res | |
| if __name__ == '__main__': | |
| from backend.extensions import socketio | |
| port = int(os.getenv('PORT', 7860)) | |
| socketio.run(app, host='0.0.0.0', port=port, debug=True, allow_unsafe_werkzeug=True) | |