--- # slug: /tir title: Gradio sidebar_position: 9 --- # Gradio ## Using Gradio in TIR Instances Gradio allows you to build a simple web interface (UI) for your machine learning model or Python function so others can interact with it easily. In **TIR**, you can attach Gradio to your instance, launch the interface, and get a shareable URL — no frontend development required. :::warning Important You must select **Gradio** as an **Add-on** in your instance for the interface to be exposed properly. ::: ## Install Gradio Run the following in a TIR instance cell: ```bash !pip3 install gradio ``` ## Create and Launch a Gradio App ```python import os import gradio as gr def greet(name): return f"Hello {name}!" demo = gr.Interface(fn=greet, inputs="text", outputs="text") # For TIR Instances: demo.launch( server_name="0.0.0.0", root_path=os.getenv("GRADIO_ROOT_PATH") ) # Get the public URL assigned by TIR print("Access the Gradio UI here:", os.getenv("GRADIO_URL")) ``` ### Why these parameters? - `server_name="0.0.0.0"` → binds to all interfaces inside the instance container. - `root_path=os.getenv("GRADIO_ROOT_PATH")` → ensures routing works with the URL generated by TIR. - `os.getenv("GRADIO_URL")` → retrieves the final URL for your interface. ## Accessing the UI - The Gradio app will **not** render inside the instance cell. - Open the URL printed from `GRADIO_URL` in your browser. - You can also find this link in the instance’s **Add-ons / Overview** section. ## Notes & Best Practices - Always select **Gradio Add-on** when creating the Instance. - Avoid using `share=True` for production use: - This creates a temporary `*.gradio.live` link that expires in one week. - Use the **`GRADIO_URL` provided by TIR** for stable access. - For production or enterprise setups, put the Gradio app behind **authentication** or a **reverse proxy**. ## Example Output When you run the code above, you’ll see a URL like: ``` Access the Gradio UI here: https://.gradio.e2enetworks.net/ ``` Opening this link shows your Gradio interface (**inputs, outputs, buttons**) in the browser. ---