Avinyaa commited on
Commit
1b567fa
Β·
1 Parent(s): 503fe40
Files changed (6) hide show
  1. Dockerfile +1 -10
  2. app.py +12 -9
  3. app_config.py +8 -23
  4. startup.py +11 -32
  5. test.py +3 -10
  6. test_kokoro_install.py +4 -11
Dockerfile CHANGED
@@ -2,13 +2,8 @@ FROM python:3.11
2
 
3
  WORKDIR /app
4
 
5
- # Set environment variables to fix Numba caching issues and configure HF cache
6
- ENV NUMBA_CACHE_DIR=/tmp/numba_cache
7
  ENV NUMBA_DISABLE_JIT=1
8
- ENV HF_HOME=/tmp/hf_cache
9
- ENV TRANSFORMERS_CACHE=/tmp/hf_cache
10
- ENV HF_HUB_CACHE=/tmp/hf_cache
11
- ENV TORCH_HOME=/tmp/torch_cache
12
 
13
  # Install git, git-lfs, and espeak-ng for Kokoro TTS
14
  RUN apt-get update && apt-get install -y git git-lfs espeak-ng && rm -rf /var/lib/apt/lists/*
@@ -16,10 +11,6 @@ RUN apt-get update && apt-get install -y git git-lfs espeak-ng && rm -rf /var/li
16
  # Initialize git lfs
17
  RUN git lfs install
18
 
19
- # Create cache directories with proper permissions
20
- RUN mkdir -p /tmp/hf_cache /tmp/torch_cache /tmp/numba_cache && \
21
- chmod -R 777 /tmp/hf_cache /tmp/torch_cache /tmp/numba_cache
22
-
23
  COPY requirements.txt .
24
 
25
  RUN pip install uv
 
2
 
3
  WORKDIR /app
4
 
5
+ # Set basic environment variables
 
6
  ENV NUMBA_DISABLE_JIT=1
 
 
 
 
7
 
8
  # Install git, git-lfs, and espeak-ng for Kokoro TTS
9
  RUN apt-get update && apt-get install -y git git-lfs espeak-ng && rm -rf /var/lib/apt/lists/*
 
11
  # Initialize git lfs
12
  RUN git lfs install
13
 
 
 
 
 
14
  COPY requirements.txt .
15
 
16
  RUN pip install uv
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # Import configuration first to setup cache directories
2
  import app_config
3
 
4
  from fastapi import FastAPI, HTTPException, Form
@@ -33,10 +33,9 @@ class KokoroTTSService:
33
  logger.info("Running on Hugging Face Spaces")
34
 
35
  try:
36
- # Initialize Kokoro pipeline with default language
37
- # Explicitly specify the repo_id to avoid warnings
38
  logger.info("Initializing Kokoro TTS pipeline...")
39
- self.pipeline = KPipeline(lang_code='a', repo_id='hexgrad/Kokoro-82M')
40
  logger.info("Kokoro TTS pipeline loaded successfully")
41
  except Exception as e:
42
  logger.error(f"Failed to load Kokoro TTS pipeline: {e}")
@@ -52,9 +51,9 @@ class KokoroTTSService:
52
  # Update pipeline language if different
53
  if self.pipeline.lang_code != lang_code:
54
  logger.info(f"Switching language from {self.pipeline.lang_code} to {lang_code}")
55
- self.pipeline = KPipeline(lang_code=lang_code, repo_id='hexgrad/Kokoro-82M')
56
 
57
- # Generate speech using Kokoro
58
  generator = self.pipeline(text, voice=voice)
59
 
60
  # Get the first (and typically only) audio output
@@ -71,10 +70,14 @@ class KokoroTTSService:
71
 
72
  def get_available_voices(self):
73
  """Return list of available voices"""
74
- # Common Kokoro voices - you may want to expand this list
75
  return [
76
- "af_heart", "af_sky", "af_bella", "af_sarah", "af_nicole",
77
- "am_adam", "am_michael", "am_edward", "am_lewis"
 
 
 
 
78
  ]
79
 
80
  # Initialize Kokoro TTS service
 
1
+ # Import configuration first to setup environment
2
  import app_config
3
 
4
  from fastapi import FastAPI, HTTPException, Form
 
33
  logger.info("Running on Hugging Face Spaces")
34
 
35
  try:
36
+ # Initialize Kokoro pipeline following the working example pattern
 
37
  logger.info("Initializing Kokoro TTS pipeline...")
38
+ self.pipeline = KPipeline(lang_code='a')
39
  logger.info("Kokoro TTS pipeline loaded successfully")
40
  except Exception as e:
41
  logger.error(f"Failed to load Kokoro TTS pipeline: {e}")
 
51
  # Update pipeline language if different
52
  if self.pipeline.lang_code != lang_code:
53
  logger.info(f"Switching language from {self.pipeline.lang_code} to {lang_code}")
54
+ self.pipeline = KPipeline(lang_code=lang_code)
55
 
56
+ # Generate speech using Kokoro (following the working example pattern)
57
  generator = self.pipeline(text, voice=voice)
58
 
59
  # Get the first (and typically only) audio output
 
70
 
71
  def get_available_voices(self):
72
  """Return list of available voices"""
73
+ # Extended list based on the working example
74
  return [
75
+ "af_heart", "af_bella", "af_nicole", "af_aoede", "af_kore",
76
+ "af_sarah", "af_nova", "af_sky", "af_alloy", "af_jessica", "af_river",
77
+ "am_michael", "am_fenrir", "am_puck", "am_echo", "am_eric",
78
+ "am_liam", "am_onyx", "am_santa", "am_adam",
79
+ "bf_emma", "bf_isabella", "bf_alice", "bf_lily",
80
+ "bm_george", "bm_fable", "bm_lewis", "bm_daniel"
81
  ]
82
 
83
  # Initialize Kokoro TTS service
app_config.py CHANGED
@@ -11,35 +11,20 @@ logging.basicConfig(level=logging.INFO)
11
  logger = logging.getLogger(__name__)
12
 
13
  def setup_hf_cache():
14
- """Setup cache directories for Hugging Face Spaces"""
15
- cache_dirs = {
16
- 'HF_HOME': '/tmp/hf_cache',
17
- 'TRANSFORMERS_CACHE': '/tmp/hf_cache',
18
- 'HF_HUB_CACHE': '/tmp/hf_cache',
19
- 'TORCH_HOME': '/tmp/torch_cache',
20
- 'NUMBA_CACHE_DIR': '/tmp/numba_cache'
21
  }
22
 
23
  # Set environment variables
24
- for key, value in cache_dirs.items():
25
  os.environ[key] = value
26
  logger.info(f"Set {key} to {value}")
27
 
28
- # Create directories
29
- for cache_dir in set(cache_dirs.values()):
30
- try:
31
- os.makedirs(cache_dir, exist_ok=True)
32
- # Ensure write permissions
33
- os.chmod(cache_dir, 0o777)
34
- logger.info(f"Created cache directory: {cache_dir}")
35
- except Exception as e:
36
- logger.warning(f"Could not create/modify {cache_dir}: {e}")
37
-
38
- # Additional HF settings
39
- os.environ['NUMBA_DISABLE_JIT'] = '1'
40
- os.environ['HF_HUB_DISABLE_TELEMETRY'] = '1'
41
-
42
- logger.info("Cache directories setup completed")
43
 
44
  def get_temp_dir():
45
  """Get a writable temporary directory"""
 
11
  logger = logging.getLogger(__name__)
12
 
13
  def setup_hf_cache():
14
+ """Setup cache environment variables for Hugging Face Spaces"""
15
+ # Don't try to create custom directories, just set environment variables
16
+ # HF Spaces will handle the actual cache locations
17
+ cache_settings = {
18
+ 'NUMBA_DISABLE_JIT': '1',
19
+ 'HF_HUB_DISABLE_TELEMETRY': '1'
 
20
  }
21
 
22
  # Set environment variables
23
+ for key, value in cache_settings.items():
24
  os.environ[key] = value
25
  logger.info(f"Set {key} to {value}")
26
 
27
+ logger.info("Cache environment setup completed")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  def get_temp_dir():
30
  """Get a writable temporary directory"""
startup.py CHANGED
@@ -33,35 +33,15 @@ def check_environment():
33
  except Exception as e:
34
  logger.warning(f"Could not check disk space: {e}")
35
 
36
- # Check write permissions
37
- test_dirs = ['/tmp', '/app', '.']
38
- for test_dir in test_dirs:
39
- try:
40
- test_file = os.path.join(test_dir, 'test_write.tmp')
41
- with open(test_file, 'w') as f:
42
- f.write('test')
43
- os.remove(test_file)
44
- logger.info(f"βœ… Write permission OK: {test_dir}")
45
- except Exception as e:
46
- logger.warning(f"❌ Write permission failed: {test_dir} - {e}")
47
-
48
- def setup_cache_dirs():
49
- """Setup cache directories with proper permissions"""
50
- logger.info("=== Setting up cache directories ===")
51
-
52
- cache_dirs = [
53
- '/tmp/hf_cache',
54
- '/tmp/torch_cache',
55
- '/tmp/numba_cache'
56
- ]
57
-
58
- for cache_dir in cache_dirs:
59
- try:
60
- os.makedirs(cache_dir, exist_ok=True)
61
- os.chmod(cache_dir, 0o777)
62
- logger.info(f"βœ… Created cache directory: {cache_dir}")
63
- except Exception as e:
64
- logger.error(f"❌ Failed to create {cache_dir}: {e}")
65
 
66
  def check_dependencies():
67
  """Check if required packages are installed"""
@@ -88,11 +68,11 @@ def test_kokoro():
88
 
89
  try:
90
  # Import after setting up environment
91
- import app_config # This will setup cache dirs
92
  from kokoro import KPipeline
93
 
94
  logger.info("Initializing Kokoro pipeline...")
95
- pipeline = KPipeline(lang_code='a', repo_id='hexgrad/Kokoro-82M')
96
  logger.info("βœ… Kokoro pipeline initialized successfully")
97
 
98
  # Test generation
@@ -116,7 +96,6 @@ def main():
116
  logger.info("πŸš€ Starting Kokoro TTS API setup...")
117
 
118
  check_environment()
119
- setup_cache_dirs()
120
  check_dependencies()
121
 
122
  if test_kokoro():
 
33
  except Exception as e:
34
  logger.warning(f"Could not check disk space: {e}")
35
 
36
+ # Check write permissions for /tmp only (the main one we need)
37
+ try:
38
+ test_file = os.path.join('/tmp', 'test_write.tmp')
39
+ with open(test_file, 'w') as f:
40
+ f.write('test')
41
+ os.remove(test_file)
42
+ logger.info(f"βœ… Write permission OK: /tmp")
43
+ except Exception as e:
44
+ logger.error(f"❌ Write permission failed: /tmp - {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  def check_dependencies():
47
  """Check if required packages are installed"""
 
68
 
69
  try:
70
  # Import after setting up environment
71
+ import app_config # This will setup environment
72
  from kokoro import KPipeline
73
 
74
  logger.info("Initializing Kokoro pipeline...")
75
+ pipeline = KPipeline(lang_code='a')
76
  logger.info("βœ… Kokoro pipeline initialized successfully")
77
 
78
  # Test generation
 
96
  logger.info("πŸš€ Starting Kokoro TTS API setup...")
97
 
98
  check_environment()
 
99
  check_dependencies()
100
 
101
  if test_kokoro():
test.py CHANGED
@@ -1,21 +1,14 @@
1
  import os
2
 
3
- # Configure cache directories for Hugging Face Spaces
4
- os.environ['HF_HOME'] = '/tmp/hf_cache'
5
- os.environ['TRANSFORMERS_CACHE'] = '/tmp/hf_cache'
6
- os.environ['HF_HUB_CACHE'] = '/tmp/hf_cache'
7
- os.environ['TORCH_HOME'] = '/tmp/torch_cache'
8
-
9
- # Create cache directories
10
- os.makedirs('/tmp/hf_cache', exist_ok=True)
11
- os.makedirs('/tmp/torch_cache', exist_ok=True)
12
 
13
  from kokoro import KPipeline
14
  import soundfile as sf
15
  import torch
16
 
17
  # Initialize Kokoro pipeline
18
- pipeline = KPipeline(lang_code='a', repo_id='hexgrad/Kokoro-82M')
19
 
20
  # Text to convert to speech
21
  text = '''
 
1
  import os
2
 
3
+ # Set basic environment variables
4
+ os.environ['NUMBA_DISABLE_JIT'] = '1'
 
 
 
 
 
 
 
5
 
6
  from kokoro import KPipeline
7
  import soundfile as sf
8
  import torch
9
 
10
  # Initialize Kokoro pipeline
11
+ pipeline = KPipeline(lang_code='a')
12
 
13
  # Text to convert to speech
14
  text = '''
test_kokoro_install.py CHANGED
@@ -5,15 +5,8 @@ Simple test script to verify Kokoro TTS installation and functionality.
5
 
6
  import os
7
 
8
- # Configure cache directories for Hugging Face Spaces
9
- os.environ['HF_HOME'] = '/tmp/hf_cache'
10
- os.environ['TRANSFORMERS_CACHE'] = '/tmp/hf_cache'
11
- os.environ['HF_HUB_CACHE'] = '/tmp/hf_cache'
12
- os.environ['TORCH_HOME'] = '/tmp/torch_cache'
13
-
14
- # Create cache directories
15
- os.makedirs('/tmp/hf_cache', exist_ok=True)
16
- os.makedirs('/tmp/torch_cache', exist_ok=True)
17
 
18
  def test_kokoro_import():
19
  """Test if Kokoro can be imported"""
@@ -31,7 +24,7 @@ def test_kokoro_pipeline():
31
  """Test if Kokoro pipeline can be initialized"""
32
  try:
33
  from kokoro import KPipeline
34
- pipeline = KPipeline(lang_code='a', repo_id='hexgrad/Kokoro-82M')
35
  print("βœ… Kokoro pipeline initialized successfully!")
36
  return True
37
  except Exception as e:
@@ -44,7 +37,7 @@ def test_kokoro_generation():
44
  from kokoro import KPipeline
45
  import soundfile as sf
46
 
47
- pipeline = KPipeline(lang_code='a', repo_id='hexgrad/Kokoro-82M')
48
  text = "Hello, this is a test of Kokoro TTS."
49
 
50
  generator = pipeline(text, voice='af_heart')
 
5
 
6
  import os
7
 
8
+ # Set basic environment variables
9
+ os.environ['NUMBA_DISABLE_JIT'] = '1'
 
 
 
 
 
 
 
10
 
11
  def test_kokoro_import():
12
  """Test if Kokoro can be imported"""
 
24
  """Test if Kokoro pipeline can be initialized"""
25
  try:
26
  from kokoro import KPipeline
27
+ pipeline = KPipeline(lang_code='a')
28
  print("βœ… Kokoro pipeline initialized successfully!")
29
  return True
30
  except Exception as e:
 
37
  from kokoro import KPipeline
38
  import soundfile as sf
39
 
40
+ pipeline = KPipeline(lang_code='a')
41
  text = "Hello, this is a test of Kokoro TTS."
42
 
43
  generator = pipeline(text, voice='af_heart')