Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Startup script for Kokoro TTS API on Hugging Face Spaces | |
| """ | |
| import os | |
| import sys | |
| import logging | |
| import subprocess | |
| # Configure logging | |
| logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') | |
| logger = logging.getLogger(__name__) | |
| def check_environment(): | |
| """Check the environment and permissions""" | |
| logger.info("=== Environment Check ===") | |
| # Check if running on HF Spaces | |
| space_id = os.environ.get('SPACE_ID') | |
| if space_id: | |
| logger.info(f"Running on Hugging Face Spaces: {space_id}") | |
| else: | |
| logger.info("Not running on Hugging Face Spaces") | |
| # Check Python version | |
| logger.info(f"Python version: {sys.version}") | |
| # Check available disk space | |
| try: | |
| result = subprocess.run(['df', '-h', '/tmp'], capture_output=True, text=True) | |
| logger.info(f"Disk space in /tmp:\n{result.stdout}") | |
| except Exception as e: | |
| logger.warning(f"Could not check disk space: {e}") | |
| # Check write permissions for /tmp only (the main one we need) | |
| try: | |
| test_file = os.path.join('/tmp', 'test_write.tmp') | |
| with open(test_file, 'w') as f: | |
| f.write('test') | |
| os.remove(test_file) | |
| logger.info(f"β Write permission OK: /tmp") | |
| except Exception as e: | |
| logger.error(f"β Write permission failed: /tmp - {e}") | |
| def check_dependencies(): | |
| """Check if required packages are installed""" | |
| logger.info("=== Checking dependencies ===") | |
| required_packages = [ | |
| 'kokoro', | |
| 'soundfile', | |
| 'torch', | |
| 'fastapi', | |
| 'uvicorn' | |
| ] | |
| for package in required_packages: | |
| try: | |
| __import__(package) | |
| logger.info(f"β {package} is available") | |
| except ImportError: | |
| logger.error(f"β {package} is not available") | |
| def test_kokoro(): | |
| """Test Kokoro TTS functionality""" | |
| logger.info("=== Testing Kokoro TTS ===") | |
| try: | |
| # Import after setting up environment | |
| import app_config # This will setup environment | |
| from kokoro import KPipeline | |
| logger.info("Initializing Kokoro pipeline...") | |
| pipeline = KPipeline(lang_code='a') | |
| logger.info("β Kokoro pipeline initialized successfully") | |
| # Test generation | |
| logger.info("Testing speech generation...") | |
| text = "Hello, this is a test." | |
| generator = pipeline(text, voice='af_heart') | |
| for i, (gs, ps, audio) in enumerate(generator): | |
| logger.info(f"β Generated audio segment {i}: gs={gs}, ps={ps}, audio shape: {audio.shape}") | |
| break | |
| logger.info("β Kokoro TTS test completed successfully") | |
| return True | |
| except Exception as e: | |
| logger.error(f"β Kokoro TTS test failed: {e}") | |
| return False | |
| def main(): | |
| """Main startup function""" | |
| logger.info("π Starting Kokoro TTS API setup...") | |
| check_environment() | |
| check_dependencies() | |
| if test_kokoro(): | |
| logger.info("π All checks passed! Starting the API...") | |
| # Import and start the app | |
| import uvicorn | |
| uvicorn.run("app:app", host="0.0.0.0", port=7860, log_level="info") | |
| else: | |
| logger.error("β Setup failed. Please check the logs above.") | |
| sys.exit(1) | |
| if __name__ == "__main__": | |
| main() |