File size: 10,532 Bytes
c7c5bd2 |
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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 |
#!/bin/bash
# Whisper API Deployment Script for Hugging Face Spaces
# Usage: ./deploy.sh
set -e
echo "π Whisper API Deployment Script"
echo "================================"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
SPACE_NAME="whisper-api-service"
SPACE_SDK="docker"
DEFAULT_HF_USERNAME=""
# Functions
print_step() {
echo -e "\n${BLUE}βΆ $1${NC}"
}
print_success() {
echo -e "${GREEN}β
$1${NC}"
}
print_warning() {
echo -e "${YELLOW}β οΈ $1${NC}"
}
print_error() {
echo -e "${RED}β $1${NC}"
}
# Check if required files exist
check_files() {
print_step "Checking required files..."
required_files=(
"app.py"
"requirements.txt"
"Dockerfile"
"README.md"
)
for file in "${required_files[@]}"; do
if [[ -f "$file" ]]; then
print_success "Found $file"
else
print_error "Missing required file: $file"
exit 1
fi
done
# Check optional files
optional_files=(
"test_api.py"
".env.example"
".gitignore"
)
for file in "${optional_files[@]}"; do
if [[ -f "$file" ]]; then
print_success "Found optional file: $file"
else
print_warning "Optional file not found: $file"
fi
done
}
# Create .gitignore if not exists
create_gitignore() {
if [[ ! -f ".gitignore" ]]; then
print_step "Creating .gitignore file..."
cat > .gitignore << 'EOF'
# Python
__pycache__/
*.pyc
*.pyo
*.pyd
.Python
*.so
.pytest_cache/
*.egg-info/
# Virtual Environment
venv/
env/
.venv/
# IDE
.vscode/
.idea/
*.swp
*.swo
# OS
.DS_Store
Thumbs.db
# Logs
*.log
logs/
# Environment
.env
.env.local
# Cache
.cache/
*.cache
# Temporary files
temp/
tmp/
*.tmp
# Model cache
models/
cache/
# Test files
test_audio.*
test_video.*
EOF
print_success "Created .gitignore"
else
print_success ".gitignore already exists"
fi
}
# Validate Dockerfile
validate_dockerfile() {
print_step "Validating Dockerfile..."
if grep -q "FROM python:" Dockerfile; then
print_success "Base image is Python-based"
else
print_error "Dockerfile should use Python base image"
exit 1
fi
if grep -q "EXPOSE 7860" Dockerfile; then
print_success "Port 7860 is exposed"
else
print_error "Dockerfile should expose port 7860"
exit 1
fi
if grep -q "ffmpeg" Dockerfile; then
print_success "FFmpeg is installed"
else
print_warning "FFmpeg may not be installed"
fi
}
# Test requirements
test_requirements() {
print_step "Testing requirements.txt..."
required_packages=(
"torch"
"transformers"
"flask"
"yt-dlp"
"ffmpeg-python"
)
for package in "${required_packages[@]}"; do
if grep -q "^${package}" requirements.txt; then
print_success "Required package found: $package"
else
print_error "Missing required package: $package"
exit 1
fi
done
}
# Create Space configuration
create_space_config() {
print_step "Creating Space configuration..."
# Create README header for Space
if ! grep -q "title:" README.md; then
print_step "Adding Space metadata to README.md..."
# Backup original README
cp README.md README.md.backup
# Create new README with metadata
cat > README.md << 'EOF'
---
title: Whisper API Service
emoji: π€
colorFrom: blue
colorTo: purple
sdk: docker
app_port: 7860
pinned: false
license: mit
duplicated_from:
models:
- openai/whisper-large-v3
datasets: []
tags:
- audio
- speech-to-text
- transcription
- translation
- whisper
- api
- flask
---
EOF
# Append original README content
cat README.md.backup >> README.md
rm README.md.backup
print_success "Added Space metadata to README.md"
else
print_success "Space metadata already exists in README.md"
fi
}
# Git operations
setup_git() {
print_step "Setting up Git repository..."
if [[ ! -d ".git" ]]; then
git init
print_success "Initialized Git repository"
else
print_success "Git repository already exists"
fi
# Add all files
git add .
# Check if there are changes to commit
if git diff --cached --quiet; then
print_warning "No changes to commit"
else
git commit -m "Initial commit: Whisper API Service
- Complete Flask API for speech-to-text and translation
- Support for 99 languages
- Multiple input types: file upload, YouTube, direct URLs
- Docker deployment ready
- WordPress plugin integration ready
- Extension hooks for future plugins"
print_success "Created initial commit"
fi
}
# Hugging Face deployment
deploy_to_hf() {
print_step "Deploying to Hugging Face Spaces..."
# Check if git-lfs is installed
if ! command -v git-lfs &> /dev/null; then
print_error "Git LFS is required but not installed"
print_step "Install Git LFS:"
echo " Ubuntu/Debian: sudo apt install git-lfs"
echo " macOS: brew install git-lfs"
echo " Windows: Download from https://git-lfs.github.io/"
exit 1
fi
# Initialize git-lfs
git lfs install
# Check if HF CLI is installed
if ! command -v huggingface-cli &> /dev/null; then
print_error "Hugging Face CLI is required but not installed"
print_step "Install HF CLI: pip install huggingface_hub"
exit 1
fi
# Get username
if [[ -z "$DEFAULT_HF_USERNAME" ]]; then
read -p "Enter your Hugging Face username: " HF_USERNAME
else
HF_USERNAME=$DEFAULT_HF_USERNAME
fi
if [[ -z "$HF_USERNAME" ]]; then
print_error "Hugging Face username is required"
exit 1
fi
REPO_URL="https://huggingface.co/spaces/${HF_USERNAME}/${SPACE_NAME}"
print_step "Creating Space: ${HF_USERNAME}/${SPACE_NAME}"
# Create the space (this might fail if it already exists, which is fine)
huggingface-cli repo create "${HF_USERNAME}/${SPACE_NAME}" --type space --space_sdk docker 2>/dev/null || true
# Add remote if not exists
if ! git remote get-url origin &> /dev/null; then
git remote add origin "${REPO_URL}"
print_success "Added remote origin"
else
print_success "Remote origin already exists"
fi
# Push to Hugging Face
print_step "Pushing to Hugging Face Spaces..."
git push origin main -f
print_success "Successfully deployed to Hugging Face Spaces!"
echo -e "\n${GREEN}π Your Space is available at:${NC}"
echo -e "${BLUE}${REPO_URL}${NC}"
print_step "Space will be building... This may take several minutes."
print_step "Monitor the build process in your Space's logs."
}
# Local testing
test_local() {
print_step "Testing local deployment..."
if command -v docker &> /dev/null; then
print_step "Building Docker image..."
docker build -t whisper-api-test .
print_step "Starting container for testing..."
docker run -d -p 7860:7860 --name whisper-api-test whisper-api-test
print_step "Waiting for service to start..."
sleep 10
# Test health endpoint
if curl -f http://localhost:7860/health &> /dev/null; then
print_success "Local deployment test successful!"
print_step "API is running at: http://localhost:7860"
else
print_error "Local deployment test failed"
fi
# Cleanup
docker stop whisper-api-test &> /dev/null || true
docker rm whisper-api-test &> /dev/null || true
else
print_warning "Docker not available for local testing"
print_step "You can test manually with: python app.py"
fi
}
# Create deployment info
create_deployment_info() {
print_step "Creating deployment information..."
cat > DEPLOYMENT.md << EOF
# Deployment Information
## Hugging Face Space
- **URL**: https://huggingface.co/spaces/${HF_USERNAME}/${SPACE_NAME}
- **SDK**: Docker
- **Port**: 7860
- **Model**: openai/whisper-large-v3
## API Endpoints
- **Health Check**: \`GET /health\`
- **Languages**: \`GET /languages\`
- **Transcribe**: \`POST /transcribe\`
- **Batch Transcribe**: \`POST /batch_transcribe\`
- **Initialize**: \`POST /initialize\`
## Testing
\`\`\`bash
# Test the API
python test_api.py
# Or use curl
curl https://your-username-${SPACE_NAME}.hf.space/health
\`\`\`
## WordPress Integration
\`\`\`php
\$api_url = 'https://your-username-${SPACE_NAME}.hf.space';
// Use this URL in your WordPress plugin
\`\`\`
## Next Steps
1. Wait for Space to build (5-10 minutes)
2. Test all endpoints
3. Integrate with WordPress plugin
4. Add custom extensions if needed
## Monitoring
- Check Space logs for errors
- Monitor API performance
- Set up alerts for downtime
Deployed on: $(date)
EOF
print_success "Created DEPLOYMENT.md with deployment information"
}
# Main deployment flow
main() {
print_step "Starting deployment process..."
# Pre-deployment checks
check_files
create_gitignore
validate_dockerfile
test_requirements
# Local testing (optional)
read -p "Do you want to test locally first? (y/n): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
test_local
fi
# Prepare for deployment
create_space_config
setup_git
# Deploy
read -p "Deploy to Hugging Face Spaces? (y/n): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
deploy_to_hf
create_deployment_info
else
print_warning "Deployment cancelled by user"
exit 0
fi
print_success "Deployment process completed!"
echo -e "\n${GREEN}π Next Steps:${NC}"
echo "1. Wait for your Space to build (5-10 minutes)"
echo "2. Test the API endpoints"
echo "3. Integrate with your WordPress plugin"
echo "4. Monitor the logs for any issues"
echo -e "\n${BLUE}π Useful Commands:${NC}"
echo "- Test API: python test_api.py"
echo "- View logs: Check your Space's logs tab"
echo "- Update: git push origin main"
}
# Run main function
main "$@" |