#!/usr/bin/env python3 """ Deployment script for Hugging Face Spaces Prepares files and provides deployment instructions """ import os import shutil import logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) def prepare_hf_deployment(): """Prepare files for Hugging Face Spaces deployment""" logger.info("šŸš€ Preparing Video Transcription Service for Hugging Face Spaces") logger.info("=" * 60) # Create deployment directory deploy_dir = "hf_spaces_deploy" if os.path.exists(deploy_dir): shutil.rmtree(deploy_dir) os.makedirs(deploy_dir) # Files to copy/create for HF Spaces files_to_copy = [ "app.py", # Main Gradio app "config.py", # Configuration "models.py", # Data models "storage.py", # Storage management "transcription_service.py", # Core transcription logic "logging_config.py", # Logging configuration "restart_handler.py" # Restart prevention ] # Copy core files for file in files_to_copy: if os.path.exists(file): shutil.copy2(file, deploy_dir) logger.info(f"āœ… Copied {file}") else: logger.warning(f"āš ļø File not found: {file}") # Copy and rename HF-specific files if os.path.exists("requirements_hf.txt"): shutil.copy2("requirements_hf.txt", os.path.join(deploy_dir, "requirements.txt")) logger.info("āœ… Copied requirements_hf.txt -> requirements.txt") if os.path.exists("README_HF.md"): shutil.copy2("README_HF.md", os.path.join(deploy_dir, "README.md")) logger.info("āœ… Copied README_HF.md -> README.md") if os.path.exists("config_hf.py"): # Replace config.py with HF-optimized version shutil.copy2("config_hf.py", os.path.join(deploy_dir, "config.py")) logger.info("āœ… Using HF-optimized config.py") # Create .gitignore for HF Spaces gitignore_content = """ __pycache__/ *.py[cod] *$py.class *.so .Python *.log .env .venv env/ venv/ .DS_Store *.tmp *.temp flagged/ """ with open(os.path.join(deploy_dir, ".gitignore"), "w") as f: f.write(gitignore_content.strip()) logger.info("āœ… Created .gitignore") logger.info("\nšŸŽ‰ Deployment files prepared successfully!") logger.info(f"šŸ“ Files are ready in: {deploy_dir}/") return deploy_dir def print_deployment_instructions(deploy_dir): """Print step-by-step deployment instructions""" instructions = f""" šŸš€ HUGGING FACE SPACES DEPLOYMENT INSTRUCTIONS {'=' * 50} 1. šŸ“ PREPARE YOUR HUGGING FACE ACCOUNT - Go to https://huggingface.co - Sign up/login to your account - Go to "Spaces" tab 2. šŸ†• CREATE NEW SPACE - Click "Create new Space" - Choose a name: e.g., "video-transcription" - Select "Gradio" as SDK - Choose "Public" or "Private" - Click "Create Space" 3. šŸ“¤ UPLOAD FILES Option A - Web Interface: - Upload all files from {deploy_dir}/ to your Space - Make sure app.py is in the root directory Option B - Git (Recommended): ```bash cd {deploy_dir} git init git add . git commit -m "Initial commit" git remote add origin https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME git push -u origin main ``` 4. āš™ļø CONFIGURE SPACE SETTINGS - Go to your Space settings - Set "Hardware" to "CPU basic" (free) or "CPU upgrade" (better performance) - Enable "Public" if you want API access from external applications 5. šŸš€ DEPLOY - Your Space will automatically build and deploy - Wait for the build to complete (5-10 minutes) - Check logs for any errors 6. āœ… TEST YOUR DEPLOYMENT Web Interface: - Visit: https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space - Upload a test video file - Verify transcription works API Access: ```bash # Test health endpoint curl "https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/api/health" # Test transcription curl -X POST "https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/api/transcribe" \\ -F "file=@test_video.mp4" \\ -F "language=en" ``` 7. šŸ“Š MONITOR PERFORMANCE - Check Space logs for any issues - Monitor memory usage - Test with different video formats šŸŽÆ IMPORTANT NOTES: - First model load takes 2-3 minutes (downloads Whisper model) - Subsequent requests are much faster - API endpoints work exactly like your local FastAPI - Both web interface and API are available simultaneously šŸ”§ TROUBLESHOOTING: - If build fails: Check requirements.txt and logs - If model loading fails: Try WHISPER_MODEL=tiny in Space settings - If memory issues: Upgrade to CPU upgrade hardware šŸ“ž NEED HELP? - Check Space logs in the "Logs" tab - Visit Hugging Face Spaces documentation - Test locally first: python app.py šŸŽ‰ Your Video Transcription Service will be live at: https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space """ print(instructions) def main(): """Main deployment preparation function""" try: deploy_dir = prepare_hf_deployment() print_deployment_instructions(deploy_dir) logger.info("\nāœ… Ready for Hugging Face Spaces deployment!") logger.info(f"šŸ“ Next step: Upload files from {deploy_dir}/ to your HF Space") except Exception as e: logger.error(f"āŒ Deployment preparation failed: {e}") return False return True if __name__ == "__main__": main()