|
|
#!/bin/bash |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
set -e |
|
|
|
|
|
echo "๐ Whisper API Deployment Script" |
|
|
echo "================================" |
|
|
|
|
|
|
|
|
RED='\033[0;31m' |
|
|
GREEN='\033[0;32m' |
|
|
YELLOW='\033[1;33m' |
|
|
BLUE='\033[0;34m' |
|
|
NC='\033[0m' |
|
|
|
|
|
|
|
|
SPACE_NAME="whisper-api-service" |
|
|
SPACE_SDK="docker" |
|
|
DEFAULT_HF_USERNAME="" |
|
|
|
|
|
|
|
|
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_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 |
|
|
|
|
|
|
|
|
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 [[ ! -f ".gitignore" ]]; then |
|
|
print_step "Creating .gitignore file..." |
|
|
cat > .gitignore << 'EOF' |
|
|
|
|
|
__pycache__/ |
|
|
*.pyc |
|
|
*.pyo |
|
|
*.pyd |
|
|
.Python |
|
|
*.so |
|
|
.pytest_cache/ |
|
|
*.egg-info/ |
|
|
|
|
|
|
|
|
venv/ |
|
|
env/ |
|
|
.venv/ |
|
|
|
|
|
|
|
|
.vscode/ |
|
|
.idea/ |
|
|
*.swp |
|
|
*.swo |
|
|
|
|
|
|
|
|
.DS_Store |
|
|
Thumbs.db |
|
|
|
|
|
|
|
|
*.log |
|
|
logs/ |
|
|
|
|
|
|
|
|
.env |
|
|
.env.local |
|
|
|
|
|
|
|
|
.cache/ |
|
|
*.cache |
|
|
|
|
|
|
|
|
temp/ |
|
|
tmp/ |
|
|
*.tmp |
|
|
|
|
|
|
|
|
models/ |
|
|
cache/ |
|
|
|
|
|
|
|
|
test_audio.* |
|
|
test_video.* |
|
|
EOF |
|
|
print_success "Created .gitignore" |
|
|
else |
|
|
print_success ".gitignore already exists" |
|
|
fi |
|
|
} |
|
|
|
|
|
|
|
|
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() { |
|
|
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_config() { |
|
|
print_step "Creating Space configuration..." |
|
|
|
|
|
|
|
|
if ! grep -q "title:" README.md; then |
|
|
print_step "Adding Space metadata to README.md..." |
|
|
|
|
|
|
|
|
cp README.md README.md.backup |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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 |
|
|
} |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
git add . |
|
|
|
|
|
|
|
|
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 |
|
|
} |
|
|
|
|
|
|
|
|
deploy_to_hf() { |
|
|
print_step "Deploying to Hugging Face Spaces..." |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
git lfs install |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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}" |
|
|
|
|
|
|
|
|
huggingface-cli repo create "${HF_USERNAME}/${SPACE_NAME}" --type space --space_sdk docker 2>/dev/null || true |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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." |
|
|
} |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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() { |
|
|
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() { |
|
|
print_step "Starting deployment process..." |
|
|
|
|
|
|
|
|
check_files |
|
|
create_gitignore |
|
|
validate_dockerfile |
|
|
test_requirements |
|
|
|
|
|
|
|
|
read -p "Do you want to test locally first? (y/n): " -n 1 -r |
|
|
echo |
|
|
if [[ $REPLY =~ ^[Yy]$ ]]; then |
|
|
test_local |
|
|
fi |
|
|
|
|
|
|
|
|
create_space_config |
|
|
setup_git |
|
|
|
|
|
|
|
|
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" |
|
|
} |
|
|
|
|
|
|
|
|
main "$@" |