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
+ #!/bin/bash
2
+
3
+ # Whisper API Deployment Script for Hugging Face Spaces
4
+ # Usage: ./deploy.sh
5
+
6
+ set -e
7
+
8
+ echo "🚀 Whisper API Deployment Script"
9
+ echo "================================"
10
+
11
+ # Colors for output
12
+ RED='\033[0;31m'
13
+ GREEN='\033[0;32m'
14
+ YELLOW='\033[1;33m'
15
+ BLUE='\033[0;34m'
16
+ NC='\033[0m' # No Color
17
+
18
+ # Configuration
19
+ SPACE_NAME="whisper-api-service"
20
+ SPACE_SDK="docker"
21
+ DEFAULT_HF_USERNAME=""
22
+
23
+ # Functions
24
+ print_step() {
25
+ echo -e "\n${BLUE}▶ $1${NC}"
26
+ }
27
+
28
+ print_success() {
29
+ echo -e "${GREEN}✅ $1${NC}"
30
+ }
31
+
32
+ print_warning() {
33
+ echo -e "${YELLOW}⚠️ $1${NC}"
34
+ }
35
+
36
+ print_error() {
37
+ echo -e "${RED}❌ $1${NC}"
38
+ }
39
+
40
+ # Check if required files exist
41
+ check_files() {
42
+ print_step "Checking required files..."
43
+
44
+ required_files=(
45
+ "app.py"
46
+ "requirements.txt"
47
+ "Dockerfile"
48
+ "README.md"
49
+ )
50
+
51
+ for file in "${required_files[@]}"; do
52
+ if [[ -f "$file" ]]; then
53
+ print_success "Found $file"
54
+ else
55
+ print_error "Missing required file: $file"
56
+ exit 1
57
+ fi
58
+ done
59
+
60
+ # Check optional files
61
+ optional_files=(
62
+ "test_api.py"
63
+ ".env.example"
64
+ ".gitignore"
65
+ )
66
+
67
+ for file in "${optional_files[@]}"; do
68
+ if [[ -f "$file" ]]; then
69
+ print_success "Found optional file: $file"
70
+ else
71
+ print_warning "Optional file not found: $file"
72
+ fi
73
+ done
74
+ }
75
+
76
+ # Create .gitignore if not exists
77
+ create_gitignore() {
78
+ if [[ ! -f ".gitignore" ]]; then
79
+ print_step "Creating .gitignore file..."
80
+ cat > .gitignore << 'EOF'
81
+ # Python
82
+ __pycache__/
83
+ *.pyc
84
+ *.pyo
85
+ *.pyd
86
+ .Python
87
+ *.so
88
+ .pytest_cache/
89
+ *.egg-info/
90
+
91
+ # Virtual Environment
92
+ venv/
93
+ env/
94
+ .venv/
95
+
96
+ # IDE
97
+ .vscode/
98
+ .idea/
99
+ *.swp
100
+ *.swo
101
+
102
+ # OS
103
+ .DS_Store
104
+ Thumbs.db
105
+
106
+ # Logs
107
+ *.log
108
+ logs/
109
+
110
+ # Environment
111
+ .env
112
+ .env.local
113
+
114
+ # Cache
115
+ .cache/
116
+ *.cache
117
+
118
+ # Temporary files
119
+ temp/
120
+ tmp/
121
+ *.tmp
122
+
123
+ # Model cache
124
+ models/
125
+ cache/
126
+
127
+ # Test files
128
+ test_audio.*
129
+ test_video.*
130
+ EOF
131
+ print_success "Created .gitignore"
132
+ else
133
+ print_success ".gitignore already exists"
134
+ fi
135
+ }
136
+
137
+ # Validate Dockerfile
138
+ validate_dockerfile() {
139
+ print_step "Validating Dockerfile..."
140
+
141
+ if grep -q "FROM python:" Dockerfile; then
142
+ print_success "Base image is Python-based"
143
+ else
144
+ print_error "Dockerfile should use Python base image"
145
+ exit 1
146
+ fi
147
+
148
+ if grep -q "EXPOSE 7860" Dockerfile; then
149
+ print_success "Port 7860 is exposed"
150
+ else
151
+ print_error "Dockerfile should expose port 7860"
152
+ exit 1
153
+ fi
154
+
155
+ if grep -q "ffmpeg" Dockerfile; then
156
+ print_success "FFmpeg is installed"
157
+ else
158
+ print_warning "FFmpeg may not be installed"
159
+ fi
160
+ }
161
+
162
+ # Test requirements
163
+ test_requirements() {
164
+ print_step "Testing requirements.txt..."
165
+
166
+ required_packages=(
167
+ "torch"
168
+ "transformers"
169
+ "flask"
170
+ "yt-dlp"
171
+ "ffmpeg-python"
172
+ )
173
+
174
+ for package in "${required_packages[@]}"; do
175
+ if grep -q "^${package}" requirements.txt; then
176
+ print_success "Required package found: $package"
177
+ else
178
+ print_error "Missing required package: $package"
179
+ exit 1
180
+ fi
181
+ done
182
+ }
183
+
184
+ # Create Space configuration
185
+ create_space_config() {
186
+ print_step "Creating Space configuration..."
187
+
188
+ # Create README header for Space
189
+ if ! grep -q "title:" README.md; then
190
+ print_step "Adding Space metadata to README.md..."
191
+
192
+ # Backup original README
193
+ cp README.md README.md.backup
194
+
195
+ # Create new README with metadata
196
+ cat > README.md << 'EOF'
197
+ ---
198
+ title: Whisper API Service
199
+ emoji: 🎤
200
+ colorFrom: blue
201
+ colorTo: purple
202
+ sdk: docker
203
+ app_port: 7860
204
+ pinned: false
205
+ license: mit
206
+ duplicated_from:
207
+ models:
208
+ - openai/whisper-large-v3
209
+ datasets: []
210
+ tags:
211
+ - audio
212
+ - speech-to-text
213
+ - transcription
214
+ - translation
215
+ - whisper
216
+ - api
217
+ - flask
218
+ ---
219
+
220
+ EOF
221
+
222
+ # Append original README content
223
+ cat README.md.backup >> README.md
224
+ rm README.md.backup
225
+
226
+ print_success "Added Space metadata to README.md"
227
+ else
228
+ print_success "Space metadata already exists in README.md"
229
+ fi
230
+ }
231
+
232
+ # Git operations
233
+ setup_git() {
234
+ print_step "Setting up Git repository..."
235
+
236
+ if [[ ! -d ".git" ]]; then
237
+ git init
238
+ print_success "Initialized Git repository"
239
+ else
240
+ print_success "Git repository already exists"
241
+ fi
242
+
243
+ # Add all files
244
+ git add .
245
+
246
+ # Check if there are changes to commit
247
+ if git diff --cached --quiet; then
248
+ print_warning "No changes to commit"
249
+ else
250
+ git commit -m "Initial commit: Whisper API Service
251
+
252
+ - Complete Flask API for speech-to-text and translation
253
+ - Support for 99 languages
254
+ - Multiple input types: file upload, YouTube, direct URLs
255
+ - Docker deployment ready
256
+ - WordPress plugin integration ready
257
+ - Extension hooks for future plugins"
258
+ print_success "Created initial commit"
259
+ fi
260
+ }
261
+
262
+ # Hugging Face deployment
263
+ deploy_to_hf() {
264
+ print_step "Deploying to Hugging Face Spaces..."
265
+
266
+ # Check if git-lfs is installed
267
+ if ! command -v git-lfs &> /dev/null; then
268
+ print_error "Git LFS is required but not installed"
269
+ print_step "Install Git LFS:"
270
+ echo " Ubuntu/Debian: sudo apt install git-lfs"
271
+ echo " macOS: brew install git-lfs"
272
+ echo " Windows: Download from https://git-lfs.github.io/"
273
+ exit 1
274
+ fi
275
+
276
+ # Initialize git-lfs
277
+ git lfs install
278
+
279
+ # Check if HF CLI is installed
280
+ if ! command -v huggingface-cli &> /dev/null; then
281
+ print_error "Hugging Face CLI is required but not installed"
282
+ print_step "Install HF CLI: pip install huggingface_hub"
283
+ exit 1
284
+ fi
285
+
286
+ # Get username
287
+ if [[ -z "$DEFAULT_HF_USERNAME" ]]; then
288
+ read -p "Enter your Hugging Face username: " HF_USERNAME
289
+ else
290
+ HF_USERNAME=$DEFAULT_HF_USERNAME
291
+ fi
292
+
293
+ if [[ -z "$HF_USERNAME" ]]; then
294
+ print_error "Hugging Face username is required"
295
+ exit 1
296
+ fi
297
+
298
+ REPO_URL="https://huggingface.co/spaces/${HF_USERNAME}/${SPACE_NAME}"
299
+
300
+ print_step "Creating Space: ${HF_USERNAME}/${SPACE_NAME}"
301
+
302
+ # Create the space (this might fail if it already exists, which is fine)
303
+ huggingface-cli repo create "${HF_USERNAME}/${SPACE_NAME}" --type space --space_sdk docker 2>/dev/null || true
304
+
305
+ # Add remote if not exists
306
+ if ! git remote get-url origin &> /dev/null; then
307
+ git remote add origin "${REPO_URL}"
308
+ print_success "Added remote origin"
309
+ else
310
+ print_success "Remote origin already exists"
311
+ fi
312
+
313
+ # Push to Hugging Face
314
+ print_step "Pushing to Hugging Face Spaces..."
315
+ git push origin main -f
316
+
317
+ print_success "Successfully deployed to Hugging Face Spaces!"
318
+ echo -e "\n${GREEN}🎉 Your Space is available at:${NC}"
319
+ echo -e "${BLUE}${REPO_URL}${NC}"
320
+
321
+ print_step "Space will be building... This may take several minutes."
322
+ print_step "Monitor the build process in your Space's logs."
323
+ }
324
+
325
+ # Local testing
326
+ test_local() {
327
+ print_step "Testing local deployment..."
328
+
329
+ if command -v docker &> /dev/null; then
330
+ print_step "Building Docker image..."
331
+ docker build -t whisper-api-test .
332
+
333
+ print_step "Starting container for testing..."
334
+ docker run -d -p 7860:7860 --name whisper-api-test whisper-api-test
335
+
336
+ print_step "Waiting for service to start..."
337
+ sleep 10
338
+
339
+ # Test health endpoint
340
+ if curl -f http://localhost:7860/health &> /dev/null; then
341
+ print_success "Local deployment test successful!"
342
+ print_step "API is running at: http://localhost:7860"
343
+ else
344
+ print_error "Local deployment test failed"
345
+ fi
346
+
347
+ # Cleanup
348
+ docker stop whisper-api-test &> /dev/null || true
349
+ docker rm whisper-api-test &> /dev/null || true
350
+
351
+ else
352
+ print_warning "Docker not available for local testing"
353
+ print_step "You can test manually with: python app.py"
354
+ fi
355
+ }
356
+
357
+ # Create deployment info
358
+ create_deployment_info() {
359
+ print_step "Creating deployment information..."
360
+
361
+ cat > DEPLOYMENT.md << EOF
362
+ # Deployment Information
363
+
364
+ ## Hugging Face Space
365
+ - **URL**: https://huggingface.co/spaces/${HF_USERNAME}/${SPACE_NAME}
366
+ - **SDK**: Docker
367
+ - **Port**: 7860
368
+ - **Model**: openai/whisper-large-v3
369
+
370
+ ## API Endpoints
371
+ - **Health Check**: \`GET /health\`
372
+ - **Languages**: \`GET /languages\`
373
+ - **Transcribe**: \`POST /transcribe\`
374
+ - **Batch Transcribe**: \`POST /batch_transcribe\`
375
+ - **Initialize**: \`POST /initialize\`
376
+
377
+ ## Testing
378
+ \`\`\`bash
379
+ # Test the API
380
+ python test_api.py
381
+
382
+ # Or use curl
383
+ curl https://your-username-${SPACE_NAME}.hf.space/health
384
+ \`\`\`
385
+
386
+ ## WordPress Integration
387
+ \`\`\`php
388
+ \$api_url = 'https://your-username-${SPACE_NAME}.hf.space';
389
+ // Use this URL in your WordPress plugin
390
+ \`\`\`
391
+
392
+ ## Next Steps
393
+ 1. Wait for Space to build (5-10 minutes)
394
+ 2. Test all endpoints
395
+ 3. Integrate with WordPress plugin
396
+ 4. Add custom extensions if needed
397
+
398
+ ## Monitoring
399
+ - Check Space logs for errors
400
+ - Monitor API performance
401
+ - Set up alerts for downtime
402
+
403
+ Deployed on: $(date)
404
+ EOF
405
+
406
+ print_success "Created DEPLOYMENT.md with deployment information"
407
+ }
408
+
409
+ # Main deployment flow
410
+ main() {
411
+ print_step "Starting deployment process..."
412
+
413
+ # Pre-deployment checks
414
+ check_files
415
+ create_gitignore
416
+ validate_dockerfile
417
+ test_requirements
418
+
419
+ # Local testing (optional)
420
+ read -p "Do you want to test locally first? (y/n): " -n 1 -r
421
+ echo
422
+ if [[ $REPLY =~ ^[Yy]$ ]]; then
423
+ test_local
424
+ fi
425
+
426
+ # Prepare for deployment
427
+ create_space_config
428
+ setup_git
429
+
430
+ # Deploy
431
+ read -p "Deploy to Hugging Face Spaces? (y/n): " -n 1 -r
432
+ echo
433
+ if [[ $REPLY =~ ^[Yy]$ ]]; then
434
+ deploy_to_hf
435
+ create_deployment_info
436
+ else
437
+ print_warning "Deployment cancelled by user"
438
+ exit 0
439
+ fi
440
+
441
+ print_success "Deployment process completed!"
442
+
443
+ echo -e "\n${GREEN}🎉 Next Steps:${NC}"
444
+ echo "1. Wait for your Space to build (5-10 minutes)"
445
+ echo "2. Test the API endpoints"
446
+ echo "3. Integrate with your WordPress plugin"
447
+ echo "4. Monitor the logs for any issues"
448
+
449
+ echo -e "\n${BLUE}📖 Useful Commands:${NC}"
450
+ echo "- Test API: python test_api.py"
451
+ echo "- View logs: Check your Space's logs tab"
452
+ echo "- Update: git push origin main"
453
+ }
454
+
455
+ # Run main function
456
+ main "$@"