HF Spaces Best Practices Advanced

Build production-quality ML demos that load fast, handle concurrent users, and run reliably. This lesson covers performance optimization, secrets management, GPU best practices, and tips for creating engaging Spaces.

Performance Optimization

  • Use model caching: Decorate model loading functions with @st.cache_resource (Streamlit) or load models at module level (Gradio) to avoid reloading on every request
  • Use quantized models: Load 4-bit or 8-bit quantized models to reduce memory usage and speed up inference
  • Use the Inference API: For large models, use the HF Inference API instead of loading models locally - no GPU needed
  • Lazy loading: Only load models when needed, not at startup time
  • Cache examples: Pre-compute example outputs to give users instant results
  • Optimize images: Resize input images before processing to reduce inference time

Secrets Management

Method How to Set How to Access
Space Settings Settings > Repository secrets os.environ["KEY"]
Streamlit Secrets Settings > Repository secrets st.secrets["KEY"]
.env file Add to .gitignore (local only) python-dotenv
Never commit secrets: Do not hardcode API keys, tokens, or credentials in your code. Always use environment variables or the Spaces secrets management. Add .env to your .gitignore.

GPU Management

  • Start with CPU: Develop and test on free CPU tier before upgrading to GPU
  • Use sleep settings: Configure auto-sleep timeouts to reduce GPU costs when not in use
  • Monitor VRAM: Check GPU memory usage and optimize batch sizes accordingly
  • Use ZeroGPU: Hugging Face offers ZeroGPU for free GPU access on popular Spaces - apply through the Spaces settings
  • Offload to CPU: Keep model weights on CPU and only move to GPU during inference

Building Engaging Demos

  • Add a clear title and description explaining what the demo does
  • Provide examples so users can try the demo immediately without preparing inputs
  • Show loading states - use progress bars or spinners during inference
  • Handle errors gracefully with user-friendly error messages
  • Add a README with screenshots, usage instructions, and links to the model card
  • Pin your best Spaces to your profile to showcase your work

Common Issues & Solutions

Issue Solution
Space crashes on startup Check build logs, ensure requirements are correct, verify port 7860
Out of memory Use smaller models, quantization, or upgrade hardware tier
Slow model loading Cache models, use Inference API, or preload at startup
Space sleeps too quickly Upgrade to a paid tier for longer keep-alive or use Space API for health checks
Git LFS issues Use Git LFS for files over 10 MB, or load models from the Hub at runtime
Key Takeaway: The best Spaces load quickly, have clear examples, and handle errors gracefully. Focus on the user experience: provide pre-computed examples, show progress during inference, and give helpful feedback when things go wrong.

Course Complete!

Congratulations! You have mastered Hugging Face Spaces. You can now build and deploy ML demos with Gradio, Streamlit, or Docker on the world's largest ML community platform.

← Back to Course Overview

Ready to Go Deeper?

Live instructor-led courses from our partners. Affiliate disclosure.