Tonic commited on
Commit
f754e6a
Β·
unverified Β·
1 Parent(s): 4838ba1

fix chord mapping file error add sys and shutils

Browse files
Files changed (1) hide show
  1. main.py +77 -36
main.py CHANGED
@@ -379,24 +379,25 @@ def patch_jasco_cache():
379
  patch_jasco_cache()
380
 
381
  def ensure_chord_mapping():
382
- """Ensure chord mapping file exists and is properly located"""
383
- # Create mapping in the cache directory
384
- cache_mapping_path = os.path.join(CACHE_DIR, "chord_to_index_mapping.pkl")
385
-
386
- # Create mapping in the models directory
387
- models_dir = os.path.join(os.path.dirname(__file__), "models")
388
- os.makedirs(models_dir, exist_ok=True)
389
- models_mapping_path = os.path.join(models_dir, "chord_to_index_mapping.pkl")
390
-
391
- # Create the mapping if it doesn't exist in either location
392
  chord_mapping = create_default_chord_mapping()
393
 
394
- # Save to both locations
395
- for mapping_path in [cache_mapping_path, models_mapping_path]:
396
- with open(mapping_path, "wb") as f:
 
 
 
 
 
 
 
 
 
397
  pickle.dump(chord_mapping, f)
 
398
 
399
- return models_mapping_path
400
 
401
  # Create necessary directories
402
  os.makedirs("models", exist_ok=True)
@@ -408,45 +409,79 @@ def load_model(version='facebook/jasco-chords-drums-melody-400M'):
408
  if MODEL is None or MODEL.name != version:
409
  MODEL = None
410
  try:
411
- # Set up custom cache paths
412
  cache_path = os.path.join(CACHE_DIR, version.replace('/', '_'))
413
  os.makedirs(cache_path, exist_ok=True)
414
 
415
- # Set environment variables for caching
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
416
  os.environ['AUDIOCRAFT_CACHE_DIR'] = cache_path
417
  os.environ['TRANSFORMERS_CACHE'] = os.path.join(cache_path, 'transformers')
418
 
419
- # Initialize chord mapping
420
- mapping_file = initialize_chord_mapping()
421
- os.environ['AUDIOCRAFT_CHORD_MAPPING'] = mapping_file
 
 
422
 
423
- # Load the model with only supported parameters
424
- MODEL = JASCO.get_pretrained(
425
- version,
426
- device='cuda'
427
- )
428
  MODEL.name = version
429
 
430
- # Configure model paths after loading
431
- if hasattr(MODEL, '_cache_dir'):
432
- MODEL._cache_dir = cache_path
 
 
 
433
 
434
- # Load the chord mapping
435
- with open(mapping_file, 'rb') as f:
436
- MODEL.chord_to_index = pickle.load(f)
437
-
438
  except Exception as e:
 
439
  raise gr.Error(f"Error loading model: {str(e)}")
440
 
441
  if MODEL is None:
442
  raise gr.Error("Failed to load model")
443
  return MODEL
444
 
445
- # Initialize chord mapping at startup
446
- mapping_file = ensure_chord_mapping()
447
- os.environ['AUDIOCRAFT_CHORD_MAPPING'] = mapping_file
448
- print(f"Initial chord mapping file location: {mapping_file}")
449
- print(f"Chord mapping exists: {os.path.exists(mapping_file)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
450
 
451
  class ModelLoadingError(Exception):
452
  pass
@@ -635,6 +670,12 @@ with gr.Blocks() as demo:
635
 
636
  if __name__ == "__main__":
637
  try:
 
 
 
 
 
 
638
  demo.queue().launch(ssr_mode=False)
639
  except Exception as e:
640
  print(f"Error launching demo: {str(e)}")
 
379
  patch_jasco_cache()
380
 
381
  def ensure_chord_mapping():
382
+ """Ensure chord mapping file exists in all necessary locations"""
 
 
 
 
 
 
 
 
 
383
  chord_mapping = create_default_chord_mapping()
384
 
385
+ # Define all necessary paths
386
+ paths = [
387
+ os.path.join(CACHE_DIR, "chord_to_index_mapping.pkl"),
388
+ os.path.join(os.path.dirname(__file__), "models", "chord_to_index_mapping.pkl"),
389
+ "/tmp/chord_to_index_mapping.pkl", # Additional fallback location
390
+ os.path.join(CACHE_DIR, "models", "chord_to_index_mapping.pkl")
391
+ ]
392
+
393
+ # Create directories and save mapping to all locations
394
+ for path in paths:
395
+ os.makedirs(os.path.dirname(path), exist_ok=True)
396
+ with open(path, "wb") as f:
397
  pickle.dump(chord_mapping, f)
398
+ print(f"Created chord mapping at: {path}")
399
 
400
+ return paths[0] # Return the primary mapping path
401
 
402
  # Create necessary directories
403
  os.makedirs("models", exist_ok=True)
 
409
  if MODEL is None or MODEL.name != version:
410
  MODEL = None
411
  try:
412
+ # Set up cache paths
413
  cache_path = os.path.join(CACHE_DIR, version.replace('/', '_'))
414
  os.makedirs(cache_path, exist_ok=True)
415
 
416
+ # Ensure chord mapping exists and set environment
417
+ mapping_file = ensure_chord_mapping()
418
+ os.environ['AUDIOCRAFT_CHORD_MAPPING'] = mapping_file
419
+
420
+ # Create symbolic links to ensure the mapping is found
421
+ target_paths = [
422
+ os.path.join(cache_path, "chord_to_index_mapping.pkl"),
423
+ os.path.join(os.path.dirname(__file__), "chord_to_index_mapping.pkl")
424
+ ]
425
+
426
+ for target in target_paths:
427
+ try:
428
+ if not os.path.exists(target):
429
+ os.symlink(mapping_file, target)
430
+ except Exception as e:
431
+ print(f"Warning: Could not create symlink at {target}: {e}")
432
+
433
+ # Set environment variables
434
  os.environ['AUDIOCRAFT_CACHE_DIR'] = cache_path
435
  os.environ['TRANSFORMERS_CACHE'] = os.path.join(cache_path, 'transformers')
436
 
437
+ # Initialize JASCO with explicit paths
438
+ kwargs = {
439
+ 'device': 'cuda',
440
+ 'chords_mapping_path': mapping_file
441
+ }
442
 
443
+ # Load the model
444
+ MODEL = JASCO.get_pretrained(version, **kwargs)
 
 
 
445
  MODEL.name = version
446
 
447
+ # Verify chord mapping is loaded
448
+ if not hasattr(MODEL, 'chord_to_index'):
449
+ with open(mapping_file, 'rb') as f:
450
+ MODEL.chord_to_index = pickle.load(f)
451
+
452
+ print("Model loaded successfully")
453
 
 
 
 
 
454
  except Exception as e:
455
+ print(f"Detailed error loading model: {str(e)}")
456
  raise gr.Error(f"Error loading model: {str(e)}")
457
 
458
  if MODEL is None:
459
  raise gr.Error("Failed to load model")
460
  return MODEL
461
 
462
+ # Initialize environment and mappings at startup
463
+ def initialize_environment():
464
+ """Initialize all necessary environment variables and files"""
465
+ # Create cache directory structure
466
+ for subdir in ['models', 'cache', 'huggingface', 'drum_cache', 'transformers']:
467
+ os.makedirs(os.path.join(CACHE_DIR, subdir), exist_ok=True)
468
+
469
+ # Initialize chord mapping
470
+ mapping_file = ensure_chord_mapping()
471
+ os.environ['AUDIOCRAFT_CHORD_MAPPING'] = mapping_file
472
+
473
+ # Set up environment variables
474
+ os.environ['AUDIOCRAFT_CACHE_DIR'] = CACHE_DIR
475
+ os.environ['TORCH_HOME'] = CACHE_DIR
476
+ os.environ['HF_HOME'] = os.path.join(CACHE_DIR, 'huggingface')
477
+ os.environ['TRANSFORMERS_CACHE'] = os.path.join(CACHE_DIR, 'transformers')
478
+ os.environ['XDG_CACHE_HOME'] = CACHE_DIR
479
+
480
+ print("Environment initialized successfully")
481
+ return mapping_file
482
+
483
+ # Call initialization at startup
484
+ mapping_file = initialize_environment()
485
 
486
  class ModelLoadingError(Exception):
487
  pass
 
670
 
671
  if __name__ == "__main__":
672
  try:
673
+ # Initialize environment
674
+ mapping_file = initialize_environment()
675
+ print(f"Using chord mapping file: {mapping_file}")
676
+ print(f"Chord mapping exists: {os.path.exists(mapping_file)}")
677
+
678
+ # Launch the demo
679
  demo.queue().launch(ssr_mode=False)
680
  except Exception as e:
681
  print(f"Error launching demo: {str(e)}")