Spaces:
Sleeping
Sleeping
""" | |
Configuration for Kokoro TTS API, especially for Hugging Face Spaces deployment. | |
""" | |
import os | |
import tempfile | |
import logging | |
# Configure logging | |
logging.basicConfig(level=logging.INFO) | |
logger = logging.getLogger(__name__) | |
def setup_hf_cache(): | |
"""Setup cache directories for Hugging Face Spaces""" | |
cache_dirs = { | |
'HF_HOME': '/tmp/hf_cache', | |
'TRANSFORMERS_CACHE': '/tmp/hf_cache', | |
'HF_HUB_CACHE': '/tmp/hf_cache', | |
'TORCH_HOME': '/tmp/torch_cache', | |
'NUMBA_CACHE_DIR': '/tmp/numba_cache' | |
} | |
# Set environment variables | |
for key, value in cache_dirs.items(): | |
os.environ[key] = value | |
logger.info(f"Set {key} to {value}") | |
# Create directories | |
for cache_dir in set(cache_dirs.values()): | |
try: | |
os.makedirs(cache_dir, exist_ok=True) | |
# Ensure write permissions | |
os.chmod(cache_dir, 0o777) | |
logger.info(f"Created cache directory: {cache_dir}") | |
except Exception as e: | |
logger.warning(f"Could not create/modify {cache_dir}: {e}") | |
# Additional HF settings | |
os.environ['NUMBA_DISABLE_JIT'] = '1' | |
os.environ['HF_HUB_DISABLE_TELEMETRY'] = '1' | |
logger.info("Cache directories setup completed") | |
def get_temp_dir(): | |
"""Get a writable temporary directory""" | |
return tempfile.gettempdir() | |
def is_hf_spaces(): | |
"""Check if running on Hugging Face Spaces""" | |
return os.environ.get('SPACE_ID') is not None | |
# Initialize cache setup | |
setup_hf_cache() |