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

fix chord mapping file error add sys and shutils

Browse files
Files changed (1) hide show
  1. main.py +49 -3
main.py CHANGED
@@ -17,7 +17,8 @@ import os
17
  import tempfile
18
  from huggingface_hub import login
19
  from pathlib import Path
20
-
 
21
 
22
  title = """# 🙋🏻‍♂️Welcome to 🌟Tonic's 🎼Jasco🎶AudioCraft Demo"""
23
  description = """Facebook presents JASCO, a temporally controlled text-to-music generation model utilizing both symbolic and audio-based conditions. JASCO can generate high-quality music samples conditioned on global text descriptions along with fine-grained local controls. JASCO is based on the Flow Matching modeling paradigm together with a novel conditioning method, allowing for music generation controlled both locally (e.g., chords) and globally (text description). [run this demo locally](https://huggingface.co/spaces/Tonic/audiocraft?docker=true) or [embed this space](https://huggingface.co/spaces/Tonic/audiocraft?embed=true) or [duplicate this space](https://huggingface.co/spaces/Tonic/audiocraft?duplicate=true) to run it privately . you can also use this demo via API by clicking the link at the bottom of the page."""
@@ -190,6 +191,8 @@ Model: facebook/jasco-chords-drums-1B
190
  - Melody-enabled models may be slower
191
  """
192
 
 
 
193
  MODEL = None
194
  INTERRUPTING = False
195
 
@@ -213,12 +216,29 @@ for subdir in ['models', 'cache', 'huggingface', 'drum_cache', 'transformers']:
213
  os.makedirs(os.path.join(CACHE_DIR, subdir), exist_ok=True)
214
 
215
  def cleanup_cache():
216
- """Clean up temporary cache files"""
217
  try:
218
- import shutil
 
 
 
 
 
 
 
219
  if os.path.exists(CACHE_DIR):
220
  shutil.rmtree(CACHE_DIR)
 
 
221
  os.makedirs(CACHE_DIR, exist_ok=True)
 
 
 
 
 
 
 
 
222
  except Exception as e:
223
  print(f"Error cleaning cache: {e}")
224
 
@@ -358,6 +378,26 @@ def patch_jasco_cache():
358
  # Apply the patch
359
  patch_jasco_cache()
360
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
361
  # Create necessary directories
362
  os.makedirs("models", exist_ok=True)
363
 
@@ -402,6 +442,12 @@ def load_model(version='facebook/jasco-chords-drums-melody-400M'):
402
  raise gr.Error("Failed to load model")
403
  return MODEL
404
 
 
 
 
 
 
 
405
  class ModelLoadingError(Exception):
406
  pass
407
 
 
17
  import tempfile
18
  from huggingface_hub import login
19
  from pathlib import Path
20
+ import sys
21
+ import shutil
22
 
23
  title = """# 🙋🏻‍♂️Welcome to 🌟Tonic's 🎼Jasco🎶AudioCraft Demo"""
24
  description = """Facebook presents JASCO, a temporally controlled text-to-music generation model utilizing both symbolic and audio-based conditions. JASCO can generate high-quality music samples conditioned on global text descriptions along with fine-grained local controls. JASCO is based on the Flow Matching modeling paradigm together with a novel conditioning method, allowing for music generation controlled both locally (e.g., chords) and globally (text description). [run this demo locally](https://huggingface.co/spaces/Tonic/audiocraft?docker=true) or [embed this space](https://huggingface.co/spaces/Tonic/audiocraft?embed=true) or [duplicate this space](https://huggingface.co/spaces/Tonic/audiocraft?duplicate=true) to run it privately . you can also use this demo via API by clicking the link at the bottom of the page."""
 
191
  - Melody-enabled models may be slower
192
  """
193
 
194
+ sys.path.append(os.path.dirname(os.path.abspath(__file__)))
195
+
196
  MODEL = None
197
  INTERRUPTING = False
198
 
 
216
  os.makedirs(os.path.join(CACHE_DIR, subdir), exist_ok=True)
217
 
218
  def cleanup_cache():
219
+ """Clean up temporary cache files while preserving chord mapping"""
220
  try:
221
+ # Save the chord mapping if it exists
222
+ mapping_file = os.path.join(CACHE_DIR, "chord_to_index_mapping.pkl")
223
+ temp_mapping = None
224
+ if os.path.exists(mapping_file):
225
+ with open(mapping_file, 'rb') as f:
226
+ temp_mapping = pickle.load(f)
227
+
228
+ # Clean the cache directory
229
  if os.path.exists(CACHE_DIR):
230
  shutil.rmtree(CACHE_DIR)
231
+
232
+ # Recreate necessary directories
233
  os.makedirs(CACHE_DIR, exist_ok=True)
234
+ for subdir in ['models', 'cache', 'huggingface', 'drum_cache', 'transformers']:
235
+ os.makedirs(os.path.join(CACHE_DIR, subdir), exist_ok=True)
236
+
237
+ # Restore the chord mapping
238
+ if temp_mapping is not None:
239
+ with open(mapping_file, 'wb') as f:
240
+ pickle.dump(temp_mapping, f)
241
+
242
  except Exception as e:
243
  print(f"Error cleaning cache: {e}")
244
 
 
378
  # Apply the patch
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)
403
 
 
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
453