"""Shared helpers for the bundled templates. Every template runs on both sets in this bundle: * Python 3.9 + gradio 4.44.1 * Python 3.12 + gradio 6.28.0 Where the two gradio majors differ, GRADIO_MAJOR gates the difference. """ import os import gradio as gr GRADIO_VERSION = gr.__version__ GRADIO_MAJOR = int(GRADIO_VERSION.split(".")[0]) def no_flagging(): """Kwargs that disable gr.Interface flagging, on either gradio major. Left enabled, gradio writes flagged samples into a "flagged/" directory in the working directory. The argument was renamed between majors (allow_flagging -> flagging_mode) and passing the wrong one raises TypeError, so return only the name this gradio accepts. """ key = "allow_flagging" if GRADIO_MAJOR < 5 else "flagging_mode" return {key: "never"} def launch(demo, port=None, **kwargs): """Launch with offline-safe defaults. share=False is the important one: share=True makes gradio download a tunnel binary from its CDN, which cannot work on an isolated host. Only arguments that both gradio majors accept are passed here; launch() rejects unknown keywords, and gradio 6 dropped several gradio 4 options (show_api among them). """ opts = dict( server_name=os.environ.get("GRADIO_SERVER_NAME", "127.0.0.1"), server_port=port or int(os.environ.get("GRADIO_SERVER_PORT", "7860")), share=False, ) opts.update(kwargs) print(f"[template] gradio {GRADIO_VERSION} on " f"http://{opts['server_name']}:{opts['server_port']}") demo.launch(**opts)