Alikestocode commited on
Commit
cdac920
·
1 Parent(s): fc0ab14

Fix: Pre-create GPU wrappers at module load time for startup detection

Browse files

- Create GPU-decorated functions at module load time (not dynamically)
- This ensures spaces.GPU decorators are detected during startup
- Pre-create wrappers for all durations from 60-1800 seconds (every 60s)
- Fix closure variable capture issue
- Slider value now properly controls GPU duration allocation

Files changed (1) hide show
  1. app.py +34 -15
app.py CHANGED
@@ -387,16 +387,38 @@ def _generate_router_plan_streaming_internal(
387
  yield "", {}, error_msg, ""
388
 
389
 
390
- def _create_gpu_wrapper(duration: int):
391
- """Create a GPU-decorated wrapper function with specific duration."""
 
 
 
 
 
 
392
  @spaces.GPU(duration=duration)
393
- def wrapper(*args, **kwargs):
394
- yield from _generate_router_plan_streaming_internal(*args, **kwargs)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
395
  return wrapper
396
 
397
-
398
- # Cache for GPU wrappers to avoid recreating them
399
- _gpu_wrapper_cache: Dict[int, Any] = {}
400
 
401
 
402
  def generate_router_plan_streaming(
@@ -415,17 +437,14 @@ def generate_router_plan_streaming(
415
  """
416
  Generate router plan with streaming output.
417
 
418
- Uses user-specified gpu_duration to create a dynamically decorated function.
419
  """
420
- # Round to nearest 60 seconds for caching efficiency
421
  rounded_duration = ((gpu_duration + 30) // 60) * 60
422
- rounded_duration = max(60, min(1800, rounded_duration)) # Clamp between 60 and 1800
423
-
424
- # Get or create wrapper with this duration
425
- if rounded_duration not in _gpu_wrapper_cache:
426
- _gpu_wrapper_cache[rounded_duration] = _create_gpu_wrapper(rounded_duration)
427
 
428
- wrapper = _gpu_wrapper_cache[rounded_duration]
 
429
  yield from wrapper(
430
  user_task, context, acceptance, extra_guidance,
431
  difficulty, tags, model_choice, max_new_tokens,
 
387
  yield "", {}, error_msg, ""
388
 
389
 
390
+ # Pre-create GPU wrappers for common durations at module load time
391
+ # This ensures spaces.GPU decorators are detected during startup
392
+ _GPU_WRAPPERS: Dict[int, Any] = {}
393
+
394
+ # Create wrappers for durations: 60, 120, 180, 240, 300, 360, 420, 480, 540, 600,
395
+ # 720, 840, 960, 1080, 1200, 1320, 1440, 1560, 1680, 1800 (every 60s from 60 to 1800)
396
+ def _make_gpu_wrapper(duration: int):
397
+ """Factory function to create GPU-decorated wrapper with closure over duration."""
398
  @spaces.GPU(duration=duration)
399
+ def wrapper(
400
+ user_task: str,
401
+ context: str,
402
+ acceptance: str,
403
+ extra_guidance: str,
404
+ difficulty: str,
405
+ tags: str,
406
+ model_choice: str,
407
+ max_new_tokens: int,
408
+ temperature: float,
409
+ top_p: float,
410
+ gpu_duration: int,
411
+ ):
412
+ yield from _generate_router_plan_streaming_internal(
413
+ user_task, context, acceptance, extra_guidance,
414
+ difficulty, tags, model_choice, max_new_tokens,
415
+ temperature, top_p, duration
416
+ )
417
  return wrapper
418
 
419
+ # Pre-create all wrappers at module load time
420
+ for duration in range(60, 1801, 60):
421
+ _GPU_WRAPPERS[duration] = _make_gpu_wrapper(duration)
422
 
423
 
424
  def generate_router_plan_streaming(
 
437
  """
438
  Generate router plan with streaming output.
439
 
440
+ Uses user-specified gpu_duration to select the appropriate GPU wrapper.
441
  """
442
+ # Round to nearest 60 seconds and clamp between 60 and 1800
443
  rounded_duration = ((gpu_duration + 30) // 60) * 60
444
+ rounded_duration = max(60, min(1800, rounded_duration))
 
 
 
 
445
 
446
+ # Get the pre-created wrapper with this duration
447
+ wrapper = _GPU_WRAPPERS[rounded_duration]
448
  yield from wrapper(
449
  user_task, context, acceptance, extra_guidance,
450
  difficulty, tags, model_choice, max_new_tokens,