Spaces:
Sleeping
Deploying to Hugging Face Spaces
This guide will help you deploy your FastAPI application to Hugging Face Spaces.
Prerequisites
- A Hugging Face account (sign up at https://huggingface.co/)
- Git installed on your system
- Your Jira API credentials
Deployment Steps
Step 1: Create a New Space
- Go to https://huggingface.co/new-space
- Fill in the details:
- Space name: Choose a name (e.g.,
enterprise-intelligence-api) - License: MIT (or your preferred license)
- Select SDK: Choose Docker
- Space hardware: Select the free tier or upgrade if needed
- Space name: Choose a name (e.g.,
- Click "Create Space"
Step 2: Push Your Code to Hugging Face
Initialize Git (if not already done):
git init git add . git commit -m "Initial commit for HF deployment"Add Hugging Face Space as remote:
git remote add hf https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAMEReplace
YOUR_USERNAMEandYOUR_SPACE_NAMEwith your actual values.Push to Hugging Face:
git push hf mainIf you're on a different branch, use:
git push hf master:main
Step 3: Configure Environment Variables (Secrets)
Your API needs environment variables (like Jira credentials). To add them:
Go to your Space settings:
https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME/settingsScroll down to Repository secrets
Add the following secrets one by one:
JIRA_URL: Your Jira instance URL (e.g.,https://yourcompany.atlassian.net)JIRA_EMAIL: Your Jira emailJIRA_API_TOKEN: Your Jira API tokenDEBUG: Set tofalseENVIRONMENT: Set toproduction- Any other secrets from your
.env.examplefile
Click "Add secret" for each one
Step 4: Wait for Build
- Hugging Face will automatically build your Docker container
- Check the Logs tab to monitor the build process
- Once complete, your API will be available at:
https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space
Step 5: Test Your Deployment
- Visit your Space URL
- Add
/docsto access the FastAPI Swagger documentation:https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/docs - Test the endpoints to ensure everything works
Important Notes
Free Tier Limitations
- The free tier may have limited resources
- The app might go to sleep after inactivity
- Consider upgrading to a paid tier for production workloads
Environment Variables via Dockerfile (Alternative)
If you don't want to use secrets (not recommended for sensitive data), you can set default values in the Dockerfile:
ENV JIRA_URL=https://example.atlassian.net
ENV DEBUG=false
Updating Your Deployment
To update your deployed app:
git add .
git commit -m "Update: description of changes"
git push hf main
Hugging Face will automatically rebuild and redeploy.
Troubleshooting
Build Fails
- Check the Logs tab in your Space
- Ensure all dependencies in
requirements.txtare compatible - Make sure the Dockerfile syntax is correct
App Crashes on Startup
- Check if all required environment variables are set in Secrets
- Review the application logs in the Space's Logs tab
- Ensure the port is 7860 (required by HF Spaces)
Can't Access the API
- Verify the Space is "Running" (not "Sleeping" or "Building")
- Check the URL is correct:
https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space - Free tier spaces may take a moment to wake up
Quick Reference Commands
# Clone your existing Space
git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
# Add changes
git add .
git commit -m "Your message"
git push
# Force push (use carefully)
git push --force
Resources
Support
If you encounter issues:
- Check Hugging Face Spaces documentation
- Review the build logs in your Space
- Visit Hugging Face forums or Discord for community support