--- title: "Use Gradio on an instance" description: "Run a Gradio app on a TIR Instance with the Gradio add-on and open it in your browser at the web address TIR gives the add-on." content_type: "how-to" product: "tir-instance" platform: "TIR" url: "/tir/tir-instance/how-to/use-gradio-on-an-instance/" topic: ["guides"] --- # Use Gradio on an instance Run a Gradio app in your instance and open it in your browser. Gradio builds a simple web page around a machine learning model or a Python function, so you can try the model in a browser and leave the frontend code to Gradio. You reach the app through the **Gradio** add-on, which gives the app on port 7860 a web address of its own. The app reaches your browser only when both parts are in place: the add-on is turned on, and the app is running on port 7860 in the instance. Add-ons themselves are free. ## Before you begin - A **Running** instance whose image includes JupyterLab, such as a TIR pre-built image. Add-ons work only on images with JupyterLab. See [Create an instance](/tir/tir-instance/how-to/create-an-instance/). - A role in the project that lets you update instances, if you turn the add-on on for an existing instance. ## Turn on the Gradio add-on You can choose the add-on when you create the instance: under **Advanced Settings** > **Add-Ons**, pick **Gradio**. See [Create an instance](/tir/tir-instance/how-to/create-an-instance/). To turn it on for an instance you already have: 1. Open the instance and go to **Overview** > **Add-ons**. 2. Select **Configure Add-ons**, pick **Gradio**, and select **Save changes**. The instance keeps running while you add or remove an add-on. The add-on then shows **Active** with port 7860, an **Open** link, and the commands that install and start Gradio in a notebook cell. Until its address is ready, it shows **Starting...** in place of **Open**. ## Install Gradio Open JupyterLab and run this in a notebook cell: ```bash !pip3 install gradio ``` A package you install this way is kept only until the instance restarts, because everything outside the workspace at `/home/jovyan` returns to the image's contents. Install it again after a restart, or install it from a start script so it is there after every start. See [Run start scripts on an instance](/tir/tir-instance/how-to/run-start-scripts-on-an-instance/). ## Run the example app The example app greets a name with as many exclamation marks as the slider is set to. Run it in a notebook cell: ```python import os import gradio as gr def greet(name, intensity): return "Hello, " + name + "!" * int(intensity) demo = gr.Interface( fn=greet, inputs=["text", "slider"], outputs=["text"], ) gr.close_all() # stops an app left running by an earlier run of this cell demo.launch( server_name="0.0.0.0", server_port=7860, root_path=os.getenv("GRADIO_ROOT_PATH"), ) print("Access the Gradio UI here:", os.getenv("GRADIO_URL")) ``` Two settings in the launch make the app reachable through the add-on: - The server name `0.0.0.0` makes the app listen on all addresses instead of `localhost` alone, so the add-on can reach it. - Port 7860 is the add-on's port, and the launch keeps the app on it. With the port left unset, Gradio moves to the next free port when 7860 is taken, such as when you run the cell a second time. The add-on reaches only 7860. The close-all line before the launch stops an app left over from an earlier run, so the port is free again. The root path setting is optional. It tells Gradio the prefix of the add-on address, which the add-on removes before each request reaches Gradio. The app opens through the add-on either way, and the example includes it. The last line prints the add-on's address. For an instance in Delhi, the output looks like this: ```text Access the Gradio UI here: https://notebooks.e2enetworks.com//notebook/// ``` TIR fills in the rest of the path with the add-on, your project, and the instance's internal name. An instance in Chennai has an address on the Chennai notebooks domain instead. To put your own model behind the page, replace `greet` with your function, and set the inputs and outputs to match what it takes and returns. Keep the launch settings as they are. ## Open the app Open the printed address in a new browser tab. The same address opens from **Open** under **Overview** > **Add-ons**. You need to be signed in to TIR to open it. If you are signed out, it redirects you to the sign-in page. Work with the app in the browser tab rather than in the notebook cell. The page shows a name box and an intensity slider on the left, with **Clear** and **Submit** under them, and an output box with **Flag** on the right. Type a name, move the slider, and select **Submit**. With the name `user` and the slider at 12, the output is `Hello, user!!!!!!!!!!!!`. ![The example Gradio app. On the left, the name box holds "user" and the intensity slider is set to 12, with Clear and Submit buttons below. On the right, the output box shows "Hello, user" followed by 12 exclamation marks, with a Flag button below.](/images/tir/tir-instance/gradio-example-app.png) The app runs until you stop it or the instance restarts. A restart ends every program in the instance, so run the cell again after a restart. If you installed Gradio from a notebook cell, install it again first. ## Share links and sign-in Open the app at the add-on address rather than through Gradio's share setting. With `share=True`, Gradio creates a temporary public link on its own site, and the link expires after one week. For an app that other people use, put it behind a sign-in, such as Gradio's own username and password option, or behind a reverse proxy that asks for one. The **TensorBoard** add-on works the same way for watching a training run. See [Use TensorBoard on an instance](/tir/tir-instance/how-to/use-tensorboard-on-an-instance/). To run an app on a port of your choice instead, see [Open a port on an instance](/tir/tir-instance/how-to/open-a-port-on-an-instance/). If a restart clears Gradio, see [Troubleshooting TIR Instances](/tir/tir-instance/troubleshooting/tir-instance/#my-files-or-installed-packages-are-gone-after-a-restart). If the add-on address shows an error because nothing is listening on its port, see [Troubleshooting TIR Instances](/tir/tir-instance/troubleshooting/tir-instance/#upstream-connect-error-or-disconnectreset-before-headers).