Intermediate

Deploying Gradio Apps

From instant sharing with a public URL to permanent deployment on Hugging Face Spaces — learn every way to get your Gradio app in front of users.

Instant Sharing with share=True

The fastest way to share your Gradio app is with a single parameter:

Python
demo.launch(share=True)

# Output:
# Running on local URL: http://127.0.0.1:7860
# Running on public URL: https://abc123.gradio.live
# This share link expires in 72 hours.
How it works: Gradio creates a tunnel from their servers to your local machine. No port forwarding or DNS setup required. Links expire after 72 hours.

Hugging Face Spaces

For permanent, free hosting, deploy to Hugging Face Spaces:

  1. Create a Space

    Go to huggingface.co/new-space, select "Gradio" as the SDK, and choose a name.

  2. Add Your Files

    Push your app.py and requirements.txt to the Space repository.

  3. Automatic Deployment

    Hugging Face builds and deploys your app automatically on every git push.

Terminal
# Clone your Space
git clone https://huggingface.co/spaces/YOUR_USERNAME/my-app
cd my-app

# Add your files
cp ../app.py .
cp ../requirements.txt .

# Push to deploy
git add . && git commit -m "Initial deploy" && git push

API Access

Every Gradio app automatically exposes a REST API:

Python - Using the API Client
from gradio_client import Client

# Connect to a running Gradio app
client = Client("https://your-space.hf.space")

# Call the API
result = client.predict(
    "Hello, world!",    # input text
    api_name="/predict"
)
print(result)
cURL
curl -X POST https://your-space.hf.space/api/predict \
  -H "Content-Type: application/json" \
  -d '{"data": ["Hello, world!"]}'

Authentication

Python
# Simple username/password
demo.launch(auth=("admin", "password"))

# Multiple users
demo.launch(auth=[("user1", "pass1"), ("user2", "pass2")])

# Custom auth function
def auth_fn(username, password):
    return username == "admin" and password == os.environ["APP_PASSWORD"]

demo.launch(auth=auth_fn)

Docker Deployment

Dockerfile
FROM python:3.11-slim

WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

EXPOSE 7860
CMD ["python", "app.py"]
Python - app.py for Docker
# Bind to 0.0.0.0 for Docker
demo.launch(server_name="0.0.0.0", server_port=7860)
Security note: Never hardcode passwords or API keys. Use environment variables or secret management for production deployments.

What's Next?

In our final lesson, we will cover best practices for building production-quality Gradio applications.