nexusbert commited on
Commit
933b4a7
·
1 Parent(s): 9db7ef0
Files changed (2) hide show
  1. Dockerfile +2 -5
  2. main.py +24 -11
Dockerfile CHANGED
@@ -55,11 +55,8 @@ RUN mkdir -p /models/huggingface && \
55
  mkdir -p /code/models && \
56
  chmod -R 777 /models/huggingface
57
 
58
- # Pre-download model at build time (YarnGPT2 model)
59
- RUN python -c "from huggingface_hub import snapshot_download; snapshot_download(repo_id='saheedniyi/YarnGPT2')"
60
-
61
- # Preload tokenizer (avoid runtime delays)
62
- RUN python -c "from transformers import AutoTokenizer; AutoTokenizer.from_pretrained('saheedniyi/YarnGPT2', use_fast=True)"
63
 
64
  # Download wavtokenizer configuration file (to both root and models directory)
65
  RUN wget -O /code/wavtokenizer_mediumdata_frame75_3s_nq1_code4096_dim512_kmeans200_attn.yaml \
 
55
  mkdir -p /code/models && \
56
  chmod -R 777 /models/huggingface
57
 
58
+ # Note: Models will be downloaded lazily at runtime to save build storage
59
+ # Pre-downloading is disabled to avoid storage limit issues
 
 
 
60
 
61
  # Download wavtokenizer configuration file (to both root and models directory)
62
  RUN wget -O /code/wavtokenizer_mediumdata_frame75_3s_nq1_code4096_dim512_kmeans200_attn.yaml \
main.py CHANGED
@@ -172,15 +172,10 @@ def load_model():
172
 
173
  @app.on_event("startup")
174
  async def startup_event():
175
- """Initialize model and tokenizer on startup."""
176
- try:
177
- logger.info("Initializing YarnGPT2 TTS model...")
178
- load_model()
179
- load_audio_tokenizer()
180
- logger.info("Model initialization complete")
181
- except Exception as e:
182
- logger.error(f"Failed to initialize model: {e}")
183
- logger.warning("Server will start but TTS functionality will be unavailable")
184
 
185
  @app.get("/")
186
  async def root():
@@ -224,10 +219,19 @@ async def text_to_speech(request: TTSRequest):
224
  Returns:
225
  - Audio file in WAV format
226
  """
 
 
 
 
 
 
 
 
 
227
  if model is None or audio_tokenizer is None:
228
  raise HTTPException(
229
  status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
230
- detail="Model not loaded. Please wait or restart the server."
231
  )
232
 
233
  try:
@@ -280,10 +284,19 @@ async def text_to_speech_stream(request: TTSRequest):
280
 
281
  Same parameters as /tts endpoint.
282
  """
 
 
 
 
 
 
 
 
 
283
  if model is None or audio_tokenizer is None:
284
  raise HTTPException(
285
  status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
286
- detail="Model not loaded. Please wait or restart the server."
287
  )
288
 
289
  try:
 
172
 
173
  @app.on_event("startup")
174
  async def startup_event():
175
+ """Initialize model and tokenizer lazily."""
176
+ logger.info("Server started. Models will be loaded on first request.")
177
+ # Don't load models at startup to save storage and startup time
178
+ # Models will be loaded lazily when first API call is made
 
 
 
 
 
179
 
180
  @app.get("/")
181
  async def root():
 
219
  Returns:
220
  - Audio file in WAV format
221
  """
222
+ # Lazy load models on first request
223
+ global model, audio_tokenizer
224
+ if model is None:
225
+ logger.info("Loading YarnGPT2 model (lazy loading)...")
226
+ load_model()
227
+ if audio_tokenizer is None:
228
+ logger.info("Loading audio tokenizer (lazy loading)...")
229
+ load_audio_tokenizer()
230
+
231
  if model is None or audio_tokenizer is None:
232
  raise HTTPException(
233
  status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
234
+ detail="Model loading failed. Please check logs."
235
  )
236
 
237
  try:
 
284
 
285
  Same parameters as /tts endpoint.
286
  """
287
+ # Lazy load models on first request
288
+ global model, audio_tokenizer
289
+ if model is None:
290
+ logger.info("Loading YarnGPT2 model (lazy loading)...")
291
+ load_model()
292
+ if audio_tokenizer is None:
293
+ logger.info("Loading audio tokenizer (lazy loading)...")
294
+ load_audio_tokenizer()
295
+
296
  if model is None or audio_tokenizer is None:
297
  raise HTTPException(
298
  status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
299
+ detail="Model loading failed. Please check logs."
300
  )
301
 
302
  try: