By Ryze AI Team
Hugging Face Spaces hosts Gradio and Streamlit apps for free, with optional GPU. This tutorial deploys a simple chat interface.
On huggingface.co, click New Space. Choose a name, select Gradio as the SDK, and set visibility. The Space is a git repository.
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()
List dependencies, one per line: gradio, transformers, torch. Pin versions for reproducibility.
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.
Add API keys in the Space settings under Secrets, never in code. Access them via os.environ.
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.