Sub topic 14
Sharing and Deploying Your Chat App
This chapter assumes you can build a gr.ChatInterface app and run it with python app.py. You should also know what a local port is and how to set environment variables. By the end, you will be able to share the app temporarily, bind it for a server or container, and pick a hosting option.
Three ways to make the app reachable
By default, demo.launch() serves the app at http://127.0.0.1:7860. That address is the loopback interface, so only your own machine can reach it. Each option below widens access a little further.
| Option | How | Who can reach it | Lifetime |
|---|---|---|---|
| Local only | `demo.launch()` | Your machine | While the script runs |
| Temporary share link | `demo.launch(share=True)` | Anyone with the URL | While the script runs, and Gradio limits link lifetime (about a week) |
| Your own server | `server_name="0.0.0.0"` | Anyone who can reach the host and port | As long as you keep the process running |
| Hosted Space | Push to Hugging Face Spaces | Public or private, as configured | Managed by the host |
Temporary share links
Pass share=True to get a public *.gradio.live URL. Gradio creates a tunnel from that URL to your machine.
import gradio as gr
def respond(message, history):
return f"You said: {message}"
demo = gr.ChatInterface(fn=respond, type="messages")
if __name__ == "__main__":
demo.launch(share=True)The terminal prints both the local and the public URL. Your computer still runs the code, so the app stops when you close the script or your laptop sleeps.
Running on a server or in a container
Inside a container, 127.0.0.1 means the container itself, so a published port cannot reach the app. Bind to all interfaces with server_name="0.0.0.0". Set the port explicitly so it matches your container configuration.
import os
import gradio as gr
def respond(message, history):
return f"You said: {message}"
demo = gr.ChatInterface(fn=respond, type="messages")
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860,
auth=(os.environ["CHAT_USER"], os.environ["CHAT_PASSWORD"]),
)Credentials come from environment variables, so they stay out of your source code and version control. Gradio also reads GRADIO_SERVER_NAME and GRADIO_SERVER_PORT, which lets you change the binding without editing code.
A minimal Dockerfile for this app looks like the following. The requirements.txt file would contain gradio.
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY app.py .
EXPOSE 7860
CMD ["python", "app.py"]Run it with docker run -p 7860:7860 -e CHAT_USER=admin -e CHAT_PASSWORD=change-me my-chat. For real deployments, pass secrets through your platform's secret store rather than on the command line.
Hosting on Hugging Face Spaces
Spaces is the simplest permanent host. Create a Space, choose the Gradio SDK, and push app.py and requirements.txt. The Space installs the dependencies and runs the app. Store your OPENAI_API_KEY in the Space's secrets settings, and it appears to your code as an environment variable.
Common errors
- The container port is unreachable. The app is bound to
127.0.0.1. Setserver_name="0.0.0.0". KeyError: 'CHAT_USER'at startup. The environment variables are not set in the process that runs the app.- The share link stops working. The script ended or the link expired. Restart the app for a new URL.
- The port is already in use. Another process holds 7860. Pick a different
server_port, or let Gradio choose by omitting it.
Exercise
Your chat app works at http://127.0.0.1:7860 on your laptop. You put it in a Docker container and publish port 7860, but the browser cannot connect. What change fixes it, and why?
Show answer
Call demo.launch(servername="0.0.0.0", serverport=7860). Inside the container, the default binding of 127.0.0.1 accepts connections only from the container itself, so the published port cannot reach it. Binding to 0.0.0.0 accepts connections on all network interfaces.
Exercise
You use share=True for a demo, and your chat function calls an LLM API with your own key. What risk does this create, and what is one way to reduce it?
Show answer
Anyone with the public URL can send messages, and each one is billed to your API key. Reduce the risk by adding auth=(username, password) to launch, by sharing the link only briefly, or by setting a spending limit on the key.