Sub topic 15
Adding authentication and basic security
This chapter assumes you have a working gr.ChatInterface app, know how launch() works, and can set environment variables in your shell. It covers how to bind the app to a network, require a login, and keep secrets out of your code.
Why a chat app needs protection
A chat app that calls an LLM spends your money on every message. Anyone who can reach the page can send requests and use your API quota. Once you share a link or deploy to a server, you need to decide who gets in.
By default, launch() binds to 127.0.0.1, so only your own machine can connect. Each step below makes the app more reachable, so each one needs more protection.
| Launch option | Who can reach the app | Protection needed |
|---|---|---|
| `demo.launch()` | Only your machine | Usually none |
| `server_name="0.0.0.0"` | Anyone on your network or the internet, if the port is open | Login, firewall, TLS |
| `share=True` | Anyone with the temporary public link | Login |
Requiring a username and password
Pass auth to launch(). Gradio then shows a login page before the chat UI. Read the credentials from environment variables so they never appear in your source code or version control.
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"]),
)Set the variables before you run the script:
export CHAT_USER=alice
export CHAT_PASSWORD='use-a-long-random-password'
python app.pyos.environ["CHAT_USER"] raises a KeyError if the variable is missing. For a security setting, failing at startup is better than silently launching without a login.
auth also accepts a list of (username, password) tuples for several users. It also accepts a function that takes (username, password) and returns True or False, so you can check against your own user store.
Calling a protected app from code
A password-protected app rejects anonymous API calls. Give gradio_client the same credentials.
import os
from gradio_client import Client
client = Client(
"http://127.0.0.1:7860/",
auth=(os.environ["CHAT_USER"], os.environ["CHAT_PASSWORD"]),
)
print(client.predict("Hello!", api_name="/chat"))Basic security habits
- Keep API keys in the environment. The OpenAI client reads
OPENAI_API_KEYfor you. Never paste a key into the script or print it in logs. - Use HTTPS in production. Gradio's login sends the password with the request. Without TLS, anyone on the network path can read it. Put the app behind a reverse proxy such as nginx or Caddy that terminates TLS.
- Treat
authas basic protection. It has no password reset, rate limiting, or single sign-on. For many users or sensitive data, put an identity provider in front of the app. - Limit the cost of abuse. Calling
demo.queue(max_size=20)caps how many requests can wait at once. Set a spending limit with your LLM provider too. - Do not share widely by accident. A
share=Truelink goes through Gradio's relay servers and is public. Combine it withauthor skip it.
Common mistakes
- Binding to
0.0.0.0on a cloud machine with noauthand an open port. - Hard-coding a password such as
("admin", "admin")and leaving it in place. - Forgetting that
gradio_clientalso needs credentials onceauthis on.
Exercise
You deploy a chat app in a container with server_name="0.0.0.0" and auth set from environment variables. A colleague says the login is enough protection for a public internet deployment. What is missing, and why?
Show answer
TLS. Gradio's login sends the credentials with each request, so over plain HTTP they can be read on the network. Run the app behind a reverse proxy that serves HTTPS. Also set a spending limit with the LLM provider, and consider queue limits, because a leaked password still lets someone run up costs.