MoraxCheng commited on
Commit
5f24d6a
·
1 Parent(s): 457d8df

Implement mock spaces module for Zero GPU fallback and improve error handling for GPU decorator application

Browse files
Files changed (1) hide show
  1. app.py +28 -22
app.py CHANGED
@@ -21,25 +21,36 @@ import gc
21
  # Check if Zero GPU should be disabled via environment variable
22
  DISABLE_ZERO_GPU = os.environ.get('DISABLE_ZERO_GPU', 'false').lower() == 'true'
23
 
 
 
 
 
 
 
 
 
 
24
  # Try to import spaces for Zero GPU support
25
  if DISABLE_ZERO_GPU:
26
  SPACES_AVAILABLE = False
 
27
  print("Zero GPU disabled via environment variable")
28
  else:
29
  try:
30
- import spaces
 
 
 
31
  SPACES_AVAILABLE = True
32
- # Check if we're actually running on Hugging Face Spaces with Zero GPU
33
- if hasattr(spaces, 'config') and hasattr(spaces.config, 'Config'):
34
- print("Zero GPU support detected and available")
35
- else:
36
- print("Spaces module imported but Zero GPU may not be fully configured")
37
  except ImportError:
38
  SPACES_AVAILABLE = False
 
39
  print("Warning: spaces module not available. Running without Zero GPU support.")
40
  except Exception as e:
41
  SPACES_AVAILABLE = False
42
- print(f"Warning: Error importing spaces module: {e}. Running without Zero GPU support.")
 
43
 
44
  # Add current directory to path
45
  sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
@@ -354,22 +365,16 @@ def score_and_create_matrix_all_singles_impl(sequence,mutation_range_start=None,
354
  if torch.cuda.is_available():
355
  torch.cuda.empty_cache()
356
 
357
- # Apply Zero GPU decorator if available
358
- if SPACES_AVAILABLE:
 
 
 
359
  try:
360
- # Try with explicit duration
361
- score_and_create_matrix_all_singles = spaces.GPU(duration=300)(score_and_create_matrix_all_singles_impl)
362
- except Exception as e:
363
- print(f"Warning: Failed to apply Zero GPU decorator with duration: {e}")
364
- try:
365
- # Try without duration parameter
366
- score_and_create_matrix_all_singles = spaces.GPU()(score_and_create_matrix_all_singles_impl)
367
- except Exception as e2:
368
- print(f"Warning: Failed to apply Zero GPU decorator: {e2}")
369
- print("Falling back to CPU mode")
370
- score_and_create_matrix_all_singles = score_and_create_matrix_all_singles_impl
371
- else:
372
- score_and_create_matrix_all_singles = score_and_create_matrix_all_singles_impl
373
 
374
  def extract_sequence(protein_id, taxon, sequence):
375
  return sequence
@@ -484,6 +489,7 @@ if __name__ == "__main__":
484
  )
485
 
486
  # Launch with appropriate settings for HF Spaces
 
487
  tranception_design.launch(
488
  max_threads=2, # Limit concurrent threads
489
  show_error=True,
 
21
  # Check if Zero GPU should be disabled via environment variable
22
  DISABLE_ZERO_GPU = os.environ.get('DISABLE_ZERO_GPU', 'false').lower() == 'true'
23
 
24
+ # Create a mock spaces module for fallback
25
+ class MockSpaces:
26
+ """Mock spaces module for when Zero GPU is not available"""
27
+ def GPU(self, *args, **kwargs):
28
+ """Mock GPU decorator that just returns the function as-is"""
29
+ def decorator(func):
30
+ return func
31
+ return decorator
32
+
33
  # Try to import spaces for Zero GPU support
34
  if DISABLE_ZERO_GPU:
35
  SPACES_AVAILABLE = False
36
+ spaces = MockSpaces()
37
  print("Zero GPU disabled via environment variable")
38
  else:
39
  try:
40
+ import spaces as real_spaces
41
+ # Test if spaces is working properly
42
+ test_decorator = real_spaces.GPU()
43
+ spaces = real_spaces
44
  SPACES_AVAILABLE = True
45
+ print("Zero GPU support detected and available")
 
 
 
 
46
  except ImportError:
47
  SPACES_AVAILABLE = False
48
+ spaces = MockSpaces()
49
  print("Warning: spaces module not available. Running without Zero GPU support.")
50
  except Exception as e:
51
  SPACES_AVAILABLE = False
52
+ spaces = MockSpaces()
53
+ print(f"Warning: Error with spaces module: {e}. Running without Zero GPU support.")
54
 
55
  # Add current directory to path
56
  sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
 
365
  if torch.cuda.is_available():
366
  torch.cuda.empty_cache()
367
 
368
+ # Apply Zero GPU decorator - will use real decorator if available, mock otherwise
369
+ try:
370
+ score_and_create_matrix_all_singles = spaces.GPU(duration=300)(score_and_create_matrix_all_singles_impl)
371
+ except Exception as e:
372
+ print(f"Warning: Failed to apply GPU decorator with duration: {e}")
373
  try:
374
+ score_and_create_matrix_all_singles = spaces.GPU()(score_and_create_matrix_all_singles_impl)
375
+ except Exception as e2:
376
+ print(f"Warning: Failed to apply GPU decorator: {e2}")
377
+ score_and_create_matrix_all_singles = score_and_create_matrix_all_singles_impl
 
 
 
 
 
 
 
 
 
378
 
379
  def extract_sequence(protein_id, taxon, sequence):
380
  return sequence
 
489
  )
490
 
491
  # Launch with appropriate settings for HF Spaces
492
+ # The spaces module is already handled above (either real or mock)
493
  tranception_design.launch(
494
  max_threads=2, # Limit concurrent threads
495
  show_error=True,