Spaces:
Sleeping
A newer version of the Gradio SDK is available:
6.2.0
π Deploying to Hugging Face Spaces
Complete guide to deploying your D&D Campaign Manager to Hugging Face Spaces.
π Pre-Deployment Checklist
Before deploying, ensure you have:
- Hugging Face account (free at https://huggingface.co)
- API keys for at least one AI provider:
- Anthropic (recommended): https://console.anthropic.com
- OpenAI: https://platform.openai.com
- Google AI: https://makersuite.google.com
π― Quick Deploy (Recommended)
Option 1: Upload Files to New Space
Create a New Space
- Go to https://huggingface.co/new-space
- Space name:
dnd-campaign-manager(or your choice) - SDK: Gradio
- Visibility: Public (or Private)
- Click Create Space
Upload Files
Upload these files from your project:
Required Files:
app_v2.py # Main application README_HF.md # Space description (rename to README.md) requirements_hf.txt # Dependencies (rename to requirements.txt) .env.example # Environment variable template src/ # Entire src directory mcp_server/ # Entire mcp_server directory data/ # Create empty folder for databaseFile Operations:
- Rename
README_HF.mdβREADME.md - Rename
requirements_hf.txtβrequirements.txt
- Rename
Set Environment Variables (Secrets)
In your Space settings β Variables and secrets:
Add at least ONE of these:
ANTHROPIC_API_KEY = sk-ant-... OPENAI_API_KEY = sk-... GOOGLE_API_KEY = AIza...Optional variables:
HUGGINGFACE_API_KEY = hf_... PRIMARY_MODEL_PROVIDER = anthropic PRIMARY_MODEL = claude-3-5-sonnet-20241022Wait for Build
- HF Spaces will automatically install dependencies
- Build takes ~3-5 minutes
- Your app will be live at:
https://huggingface.co/spaces/YOUR_USERNAME/dnd-campaign-manager
π§ Option 2: Git Push (Advanced)
Step 1: Clone Your Space
# Install git-lfs if you haven't
git lfs install
# Clone your space
git clone https://huggingface.co/spaces/YOUR_USERNAME/dnd-campaign-manager
cd dnd-campaign-manager
Step 2: Copy Files
# From your dungeon-smasher-pro directory:
cp app_v2.py path/to/dnd-campaign-manager/
cp README_HF.md path/to/dnd-campaign-manager/README.md
cp requirements_hf.txt path/to/dnd-campaign-manager/requirements.txt
cp .env.example path/to/dnd-campaign-manager/
# Copy directories
cp -r src path/to/dnd-campaign-manager/
cp -r mcp_server path/to/dnd-campaign-manager/
mkdir -p path/to/dnd-campaign-manager/data
Step 3: Commit and Push
cd path/to/dnd-campaign-manager
git add .
git commit -m "Initial deployment of D&D Campaign Manager"
git push
Step 4: Configure Secrets
Go to your Space settings and add API keys as secrets (see Option 1, step 3).
π Final Directory Structure on HF Space
dnd-campaign-manager/ # Your HF Space root
βββ README.md # Renamed from README_HF.md
βββ requirements.txt # Renamed from requirements_hf.txt
βββ app_v2.py # Main app (HF will run this)
βββ .env.example # Environment variable template
β
βββ src/ # Application source
β βββ agents/
β β βββ campaign_agent.py
β β βββ character_agent.py
β βββ models/
β β βββ campaign.py
β β βββ character.py
β β βββ session_notes.py
β βββ ui/
β β βββ app.py
β β βββ components/
β β βββ tabs/
β βββ utils/
β βββ ai_client.py
β βββ file_parsers.py
β
βββ mcp_server/ # MCP tools
β βββ dnd_mcp_server.py
β βββ mcp_config.json
β βββ README.md
β
βββ data/ # Database (auto-created)
βββ dnd_campaign_manager.db
βοΈ Configuration Tips
Memory Management
HF Spaces free tier has 16GB RAM. If you encounter memory issues:
In your Space settings:
- Hardware: Start with CPU Basic (free)
- Upgrade to CPU Upgrade or T4 small if needed
Optimize database:
# Already implemented in campaign_agent.py # Uses SQLite with limited result sets
Persistent Storage
Important: HF Spaces free tier has ephemeral storage. Your database will reset on Space restart.
Solutions:
Use HF Datasets for persistence (recommended):
# Add to requirements.txt: # datasets>=2.0.0 # Save to HF Dataset periodically from datasets import Dataset # Implementation in src/utils/persistence.pyUpgrade to Persistent Storage:
- Go to Space Settings β Storage
- Enable persistent storage ($5/month)
Accept ephemeral storage:
- Good for demos/testing
- Users can export their data
API Rate Limits
To avoid hitting rate limits:
- Set rate limit warnings (already implemented in AI client)
- Add caching for repeated requests
- Consider adding Redis for session management
π§ͺ Testing Your Deployment
Once deployed, test these features:
Character Creation
- Create a character
- Generate backstory
- Export character sheet
Campaign Synthesis
- Create 3-4 characters
- Synthesize campaign
- Verify campaign loads
Session Generation
- Auto-generate a session
- Upload session notes
- Generate next session
MCP Tools (if using MCP server)
- Character validation
- Encounter CR calculation
π Troubleshooting
Build Fails
Error: Could not find a version that satisfies the requirement...
Fix: Check requirements.txt version constraints:
# Too strict (may fail):
gradio==5.0.0
# Better (flexible):
gradio>=5.0.0
App Doesn't Start
Check logs:
- Go to your Space
- Click Logs tab
- Look for errors
Common issues:
- Missing API key β Add in Secrets
- Import error β Check all files uploaded
- Database path β Ensure
data/directory exists
Slow Performance
Solutions:
- Upgrade hardware tier
- Enable caching
- Use faster model (e.g., Gemini Flash)
- Reduce session history lookback
Database Resets
This is normal on free tier.
Options:
- Enable persistent storage ($5/month)
- Use HF Datasets for backup
- Allow users to export/import data
π¨ Customizing Your Space
Update README Header
Edit README.md frontmatter:
---
title: My D&D Campaign Manager
emoji: βοΈ
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app_v2.py
pinned: true
license: mit
tags:
- dnd
- ttrpg
- ai
- campaign-manager
- mcp
---
Add Custom Domain (Pro)
HF Spaces Pro allows custom domains:
- Settings β Custom Domain
- Point your DNS to HF
- Enable HTTPS
Enable Authentication
Restrict access to your Space:
# In app_v2.py
from src.ui import launch_ui
if __name__ == "__main__":
launch_ui(auth=("admin", "your_password"))
Or use HF's built-in auth:
- Settings β Access Control
- Set to "Private" or "Token-gated"
π Monitoring
View Analytics
HF Spaces provides:
- App traffic (visits, unique users)
- Resource usage (CPU, memory)
- Build history
Access: Space Settings β Analytics
Add Custom Analytics
# Add to app.py
import gradio as gr
def track_event(event_name):
# Log to your analytics service
pass
# Add to Gradio components
gr.Button("Create Character").click(
fn=lambda: track_event("character_created"),
...
)
π Going to Production
For serious production use:
- Persistent Storage: Enable in Space settings
- Upgrade Hardware: T4 Small GPU or better
- Add Monitoring: Sentry, LogRocket, etc.
- Rate Limiting: Implement per-user limits
- Backups: Daily exports to HF Dataset
- Custom Domain: For branding
- Load Testing: Test with multiple users
π° Cost Estimates
Free Tier:
- Hosting: Free
- Storage: Ephemeral (resets on restart)
- Hardware: CPU Basic
- Good for: Demos, testing, low traffic
Paid Tiers:
- Persistent Storage: $5/month
- CPU Upgrade: $0/hour (included)
- T4 Small (GPU): $0.60/hour
- A10G Small: $1.50/hour
API Costs (separate):
- Anthropic Claude: ~$3-15 per 1M tokens
- OpenAI GPT-4: ~$10-30 per 1M tokens
- Google Gemini: Free tier available
π Checklist: Ready to Deploy?
- All files ready (app_v2.py, src/, mcp_server/)
- requirements_hf.txt renamed to requirements.txt
- README_HF.md renamed to README.md
- .env.example included (for local dev reference)
- API keys ready to add as Secrets
- HF Space created
- Files uploaded or pushed
- Secrets configured
- Build succeeded
- App tested
π You're Live!
Once deployed, share your Space:
π² Check out my D&D Campaign Manager!
https://huggingface.co/spaces/YOUR_USERNAME/dnd-campaign-manager
Features:
β¨ AI-powered character creation
π― Autonomous campaign synthesis
π Auto-generated session plans
βοΈ MCP-powered encounter balancing
Need help?
- HF Spaces Docs: https://huggingface.co/docs/hub/spaces
- Gradio Docs: https://gradio.app/docs/
- Open an issue on GitHub
Happy deploying! π