Deploy to Hugging Face Spaces

By Ryze AI Team

Hugging Face Spaces hosts Gradio and Streamlit apps for free, with optional GPU. This tutorial deploys a simple chat interface.

1. Create a Space

On huggingface.co, click New Space. Choose a name, select Gradio as the SDK, and set visibility. The Space is a git repository.

2. Write app.py

import gradio as gr
from transformers import pipeline

gen = pipeline("text-generation", model="gpt2")
def chat(message, history):
    out = gen(message, max_new_tokens=64)
    return out[0]["generated_text"]
gr.ChatInterface(chat).launch()

3. requirements.txt

List dependencies, one per line: gradio, transformers, torch. Pin versions for reproducibility.

4. Push and deploy

Clone the Space repo, add files, commit, and push. The Space builds automatically and shows build logs. On success, the app is live at its URL.

5. Secrets

Add API keys in the Space settings under Secrets, never in code. Access them via os.environ.

6. Tips

Use the free CPU tier for small models. Upgrade to GPU for larger ones. Keep dependencies minimal to speed up builds. Add a README.md to document the app. And test locally with gradio app.py before pushing.

Docs: HF Spaces.