import threading import uvicorn import g4f.gui.server.app as gui_module from g4f.mcp.server import main as mcp_app from fastapi import FastAPI from starlette.middleware.wsgi import WSGIMiddleware # 1. Setup the MCP Wrapper mcp_wrapper = FastAPI() mcp_wrapper.mount("/mcp", mcp_app(http=True)) def run_mcp(): # Run MCP internally on a different port uvicorn.run(mcp_wrapper, host="127.0.0.1", port=8765) # 2. Setup the GUI gui_flask_app = getattr(gui_module, 'app', None) if gui_flask_app is None: gui_flask_app = gui_module.create_app() # 3. Execution Logic if __name__ == "__main__": # Start MCP in a background thread so it doesn't block threading.Thread(target=run_mcp, daemon=True).start() print("🚀 G4F GUI starting on 7860 (Hugging Face heartbeat)...") # Run the Flask app directly. This is what HF expects to see. # This prevents the 'Starting...' hang. gui_flask_app.run(host="0.0.0.0", port=7860)