Spaces:
Sleeping
Sleeping
File size: 8,600 Bytes
4046a11 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | # Token Usage Examples
This document provides practical examples for using BirdNET Uploader with various token authentication methods.
## Quick Start (Interactive Use with Keyring)
```bash
# Step 1: Store your token securely
birdnet-uploader login
# You'll be prompted for your Hugging Face token
# It's stored securely in your OS credential manager (Credential Manager on Windows, Keychain on macOS, Secret Service on Linux)
# Step 2: Upload without needing to provide token again
birdnet-uploader upload \
--repo-id user/my-dataset \
--segments C:\my\audio\segments \
--csv detections.csv
```
## Docker with Environment Variable
Store token in a `.env` file (keep it secret, don't commit to git):
**.env file:**
```
HF_TOKEN=hf_your_actual_token_here
```
**Run container:**
```bash
docker run --rm \
--env-file .env \
-v C:\my\segments:C:\data\segments \
-v C:\my\sessions:C:\data\sessions \
birdnet-uploader:latest \
upload --repo-id user/dataset --segments C:\data\segments
```
## Docker Compose (Recommended for Production)
**docker-compose.yml:**
```yaml
version: '3.8'
services:
uploader:
build:
context: .
dockerfile: Dockerfile.windows
environment:
- HF_TOKEN=${HF_TOKEN}
volumes:
- ./segments:C:\data\segments
- ./sessions:C:\data\sessions
command: >
upload
--repo-id ${REPO_ID}
--segments C:\data\segments
--workers 4
```
**Run:**
```bash
export HF_TOKEN="hf_your_actual_token"
export REPO_ID="user/dataset"
docker-compose up
```
## GitHub Actions CI/CD
**`.github/workflows/upload.yml`:**
```yaml
name: Upload to Hugging Face
on:
push:
branches: [main]
paths: ['audio/**']
jobs:
upload:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install uploader
run: pip install -e .
- name: Upload audio segments
env:
HF_TOKEN: ${{ secrets.HF_TOKEN }}
run: |
birdnet-uploader upload `
--repo-id ${{ vars.REPO_ID }} `
--segments .\audio `
--csv detections.csv `
--workers 4
```
**Set up in GitHub:**
1. Go to **Settings > Secrets and variables > Actions**
2. Click **New repository secret**
3. Name: `HF_TOKEN`, Value: `hf_your_token`
4. Click **New repository variable**
5. Name: `REPO_ID`, Value: `username/dataset-name`
## PowerShell Script with Token Verification
```powershell
# verify_and_upload.ps1
param(
[string]$SegmentsPath = ".\segments",
[string]$CsvFile = ".\detections.csv",
[string]$RepoId = "user/dataset"
)
# Verify token is set
if (-not $env:HF_TOKEN) {
Write-Host "ERROR: HF_TOKEN environment variable not set" -ForegroundColor Red
exit 1
}
# Verify token format
if (-not $env:HF_TOKEN.StartsWith("hf_")) {
Write-Host "WARNING: Token doesn't start with 'hf_', verify it's correct" -ForegroundColor Yellow
}
# Verify paths exist
if (-not (Test-Path $SegmentsPath)) {
Write-Host "ERROR: Segments directory not found: $SegmentsPath" -ForegroundColor Red
exit 1
}
# Run upload
Write-Host "Starting upload to $RepoId..." -ForegroundColor Green
birdnet-uploader upload `
--repo-id $RepoId `
--segments $SegmentsPath `
--csv $CsvFile `
--workers 4
if ($LASTEXITCODE -eq 0) {
Write-Host "Upload completed successfully!" -ForegroundColor Green
} else {
Write-Host "Upload failed with exit code $LASTEXITCODE" -ForegroundColor Red
exit $LASTEXITCODE
}
```
**Run:**
```powershell
$env:HF_TOKEN = "hf_your_token"
.\verify_and_upload.ps1 -SegmentsPath "C:\audio\segments" -RepoId "user/dataset"
```
## Kubernetes Deployment
**secret.yaml:**
```yaml
apiVersion: v1
kind: Secret
metadata:
name: hf-token
type: Opaque
stringData:
token: hf_your_actual_token_here
```
**deployment.yaml:**
```yaml
apiVersion: batch/v1
kind: Job
metadata:
name: birdnet-upload-job
spec:
template:
spec:
containers:
- name: uploader
image: birdnet-uploader:latest
env:
- name: HF_TOKEN
valueFrom:
secretKeyRef:
name: hf-token
key: token
volumeMounts:
- name: segments
mountPath: /data/segments
readOnly: true
- name: sessions
mountPath: /data/sessions
args:
- upload
- --repo-id
- user/dataset
- --segments
- /data/segments
- --workers
- "4"
restartPolicy: Never
volumes:
- name: segments
hostPath:
path: /mnt/audio-segments
- name: sessions
emptyDir: {}
```
**Deploy:**
```bash
kubectl create secret generic hf-token --from-literal=token=hf_your_actual_token
kubectl apply -f deployment.yaml
kubectl logs -f job/birdnet-upload-job
```
## Windows Service (Advanced)
**install-service.ps1:**
```powershell
param(
[string]$SegmentsPath = "C:\audio\segments",
[string]$RepositoryId = "user/dataset"
)
$ServiceName = "BirdNETUploader"
$ExePath = "C:\Program Files\BirdNET-Uploader\birdnet-uploader.exe"
$LogPath = "C:\logs\birdnet-uploader"
# Create log directory
New-Item -ItemType Directory -Path $LogPath -Force
# Register scheduled task
$TaskAction = New-ScheduledTaskAction -Execute $ExePath -Argument "upload --repo-id $RepositoryId --segments $SegmentsPath --workers 4"
$TaskTrigger = New-ScheduledTaskTrigger -Daily -At 2AM
$TaskSettings = New-ScheduledTaskSettingsSet -RunOnlyIfNetworkAvailable
Register-ScheduledTask -TaskName $ServiceName `
-Action $TaskAction `
-Trigger $TaskTrigger `
-Settings $TaskSettings `
-RunLevel Highest
# Store token securely in Credential Manager
$Token = Read-Host "Enter Hugging Face token" -AsSecureString
$Credential = New-Object System.Management.Automation.PSCredential("hf_token", $Token)
# The birdnet-uploader uses keyring, which respects Windows Credential Manager
```
**Run task manually:**
```powershell
Start-ScheduledTask -TaskName "BirdNETUploader"
```
## CI/CD Matrix Testing
**`.github/workflows/test-upload.yml`:**
```yaml
name: Test Upload (Multiple OS)
on: [push, pull_request]
jobs:
test-upload:
strategy:
matrix:
os: [windows-latest, ubuntu-latest, macos-latest]
python-version: ['3.10', '3.11', '3.12']
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: pip install -e .
- name: Run token security tests
run: pytest tests/unit/test_token_security.py -v
- name: Test token from environment variable
env:
HF_TOKEN: ${{ secrets.TEST_HF_TOKEN }}
run: birdnet-uploader login --token $env:HF_TOKEN || true
```
## Troubleshooting Token Issues
### Issue: "No stored token found"
**Solution 1 - Use environment variable:**
```bash
set HF_TOKEN=hf_your_token
birdnet-uploader upload --repo-id user/dataset --segments ./segments
```
**Solution 2 - Store token:**
```bash
birdnet-uploader login
# Enter token when prompted
```
**Solution 3 - Pass via CLI:**
```bash
birdnet-uploader upload --repo-id user/dataset --segments ./segments --token hf_your_token
```
### Issue: Token expires
Generate a new token at https://huggingface.co/settings/tokens and:
```bash
# Update stored token
birdnet-uploader login
# Enter new token
# Or use environment variable
set HF_TOKEN=hf_new_token
birdnet-uploader upload ...
```
### Issue: Permission denied on dataset
Verify token has correct permissions:
```bash
# Test token
curl -H "Authorization: Bearer hf_your_token" https://huggingface.co/api/user
# Check token permissions at https://huggingface.co/settings/tokens
# Ensure it has: repo.content.write
# Ensure it's limited to your target dataset if possible
```
## Security Checklist
- [ ] Never commit `HF_TOKEN` values to version control
- [ ] Use GitHub Secrets for CI/CD, not plain environment variables
- [ ] Use Docker secrets or volume mounts for orchestrated environments
- [ ] Rotate tokens every 3-6 months
- [ ] Use read-only tokens where possible
- [ ] Limit token scope to specific datasets
- [ ] Use different tokens for different services/projects
- [ ] Monitor token usage in Hugging Face settings
- [ ] Immediately regenerate leaked tokens
|