Spring / springboot-app 2 /HUGGINGFACE_DEPLOYMENT.md
AI-RESEARCHER-2024's picture
Upload 9 files
26f6e80 verified

Hugging Face Deployment Guide

Quick Start Deployment Steps

1. Create a Hugging Face Account

2. Create a New Space

  1. Click on your profile picture β†’ "New Space"
  2. Give your Space a name (e.g., "springboot-demo")
  3. Select Docker as the SDK (important!)
  4. Choose the hardware (free CPU Basic is fine for this demo)
  5. Set visibility (public or private)
  6. Click "Create Space"

3. Upload Your Files

You have two options:

Option A: Using Git

# Clone your Space repository
git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME

# Copy all the Spring Boot files to the cloned repository
cp -r springboot-app/* YOUR_SPACE_NAME/

# Navigate to the repository
cd YOUR_SPACE_NAME

# Add, commit, and push
git add .
git commit -m "Initial Spring Boot application"
git push

Option B: Using Web Interface

  1. Go to your Space's "Files" tab
  2. Click "Add file" β†’ "Upload files"
  3. Upload the following structure:
    Dockerfile
    pom.xml
    src/
    β”œβ”€β”€ main/
    β”‚   β”œβ”€β”€ java/com/example/demo/
    β”‚   β”‚   β”œβ”€β”€ DemoApplication.java
    β”‚   β”‚   └── controller/GreetingController.java
    β”‚   └── resources/
    β”‚       └── application.properties
    └── test/java/com/example/demo/
        └── DemoApplicationTests.java
    

4. Wait for Build and Deployment

  • Hugging Face will automatically detect the Dockerfile and start building
  • You can monitor the build logs in the "Logs" tab
  • The build typically takes 3-5 minutes

5. Access Your Application

Once deployed, your application will be available at:

https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/api/

Testing Your Endpoints

Once deployed, you can test your API endpoints:

# Test the home endpoint
curl https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/api/

# Test the greeting endpoint
curl https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/api/greeting?name=HuggingFace

# Test the health endpoint
curl https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/api/health

# Test the echo endpoint
curl -X POST https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/api/echo \
  -H "Content-Type: application/json" \
  -d '{"message":"Hello from Hugging Face!"}'

Troubleshooting

Common Issues and Solutions

  1. Port Configuration Error

    • Ensure server.port=7860 in application.properties
    • Hugging Face requires port 7860
  2. Build Fails

    • Check the Dockerfile syntax
    • Ensure all file paths are correct
    • Verify Java version compatibility
  3. Application Doesn't Start

    • Check logs in the "Logs" tab
    • Verify the JAR file name in Dockerfile matches the build output
    • Ensure proper permissions (running as non-root user)
  4. Slow Response Times

    • Free tier hardware has limited resources
    • Consider upgrading to better hardware if needed

Environment Variables (Optional)

You can add environment variables in the Space settings:

  1. Go to Settings β†’ "Repository secrets"
  2. Add variables like:
    • SPRING_PROFILES_ACTIVE=production
    • JAVA_OPTS=-Xmx512m

Updating Your Application

To update your deployed application:

  1. Make changes to your code locally
  2. Push updates to the Space repository
  3. Hugging Face will automatically rebuild and redeploy

Additional Resources

Support

If you encounter issues:

  1. Check the Space logs first
  2. Visit Hugging Face forums
  3. Check Spring Boot community resources