dennisvdang commited on
Commit
88140b5
·
1 Parent(s): 66bef05

Fix module import issues with robust path handling

Browse files
Files changed (3) hide show
  1. app.py +26 -0
  2. src/chorus_detection/__init__.py +10 -0
  3. src/streamlit_app.py +48 -7
app.py CHANGED
@@ -10,6 +10,14 @@ without circular imports.
10
  import os
11
  import sys
12
  import logging
 
 
 
 
 
 
 
 
13
 
14
  # Configure logging
15
  logging.basicConfig(
@@ -27,6 +35,24 @@ if os.environ.get("SPACE_ID"):
27
  logger.info(f"Directory contents: {os.listdir()}")
28
  if os.path.exists('src'):
29
  logger.info(f"src directory contents: {os.listdir('src')}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  def main():
32
  """Main entry point for the Streamlit app."""
 
10
  import os
11
  import sys
12
  import logging
13
+ import subprocess
14
+
15
+ # Add src directory to path
16
+ current_dir = os.path.dirname(os.path.abspath(__file__))
17
+ src_dir = os.path.join(current_dir, "src")
18
+ if src_dir not in sys.path:
19
+ sys.path.insert(0, src_dir)
20
+ sys.path.insert(0, current_dir)
21
 
22
  # Configure logging
23
  logging.basicConfig(
 
35
  logger.info(f"Directory contents: {os.listdir()}")
36
  if os.path.exists('src'):
37
  logger.info(f"src directory contents: {os.listdir('src')}")
38
+
39
+ # Install package in development mode
40
+ try:
41
+ logger.info("Installing package in development mode...")
42
+ subprocess.check_call([sys.executable, "-m", "pip", "install", "-e", "."])
43
+ logger.info("Package installation successful")
44
+ except Exception as e:
45
+ logger.error(f"Error installing package: {e}")
46
+
47
+ # Verify python path
48
+ logger.info(f"Python path after update: {sys.path}")
49
+
50
+ # Try importing chorus_detection to verify
51
+ try:
52
+ import chorus_detection
53
+ logger.info(f"Successfully imported chorus_detection from {chorus_detection.__file__}")
54
+ except ImportError as e:
55
+ logger.error(f"Failed to import chorus_detection: {e}")
56
 
57
  def main():
58
  """Main entry point for the Streamlit app."""
src/chorus_detection/__init__.py CHANGED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ """Chorus Detection package for identifying choruses in music.
2
+
3
+ This package contains modules for:
4
+ - Audio processing and feature extraction
5
+ - Machine learning models for chorus detection
6
+ - Visualization tools for audio analysis
7
+ - Utility functions
8
+ """
9
+
10
+ __version__ = "0.1.0"
src/streamlit_app.py CHANGED
@@ -17,6 +17,14 @@ logger = logging.getLogger("streamlit-app")
17
  # Configure TensorFlow logging before importing TensorFlow
18
  os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Suppress TensorFlow logs
19
 
 
 
 
 
 
 
 
 
20
  # Import model downloader to ensure model is available
21
  try:
22
  if os.path.exists(os.path.join(os.getcwd(), "download_model.py")):
@@ -54,22 +62,55 @@ from pydub import AudioSegment
54
  warnings.filterwarnings("ignore") # Suppress all warnings
55
  tf.get_logger().setLevel('ERROR') # Suppress TensorFlow ERROR logs
56
 
 
 
 
 
 
57
  try:
 
 
 
 
 
58
  from chorus_detection.audio.data_processing import process_audio
59
  from chorus_detection.audio.processor import extract_audio
60
  from chorus_detection.models.crnn import load_CRNN_model, make_predictions
61
  from chorus_detection.utils.cli import is_youtube_url
62
  from chorus_detection.utils.logging import logger
 
 
63
  except ImportError as e:
64
  logger.error(f"Error importing chorus_detection modules: {e}")
65
  logger.info("Trying alternative imports...")
66
- # Adjust import paths as needed
67
- sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
68
- from chorus_detection.audio.data_processing import process_audio
69
- from chorus_detection.audio.processor import extract_audio
70
- from chorus_detection.models.crnn import load_CRNN_model, make_predictions
71
- from chorus_detection.utils.cli import is_youtube_url
72
- from chorus_detection.utils.logging import logger
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
  # Define the MODEL_PATH directly
75
  MODEL_PATH = os.path.join(os.getcwd(), "models", "CRNN", "best_model_V3.h5")
 
17
  # Configure TensorFlow logging before importing TensorFlow
18
  os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Suppress TensorFlow logs
19
 
20
+ # Ensure proper import paths
21
+ current_dir = os.path.dirname(os.path.abspath(__file__))
22
+ root_dir = os.path.dirname(current_dir)
23
+ if current_dir not in sys.path:
24
+ sys.path.insert(0, current_dir)
25
+ if root_dir not in sys.path:
26
+ sys.path.insert(0, root_dir)
27
+
28
  # Import model downloader to ensure model is available
29
  try:
30
  if os.path.exists(os.path.join(os.getcwd(), "download_model.py")):
 
62
  warnings.filterwarnings("ignore") # Suppress all warnings
63
  tf.get_logger().setLevel('ERROR') # Suppress TensorFlow ERROR logs
64
 
65
+ # Debug import paths
66
+ logger.info(f"Python path: {sys.path}")
67
+ logger.info(f"Current working directory: {os.getcwd()}")
68
+
69
+ # First try direct import with src in path
70
  try:
71
+ # Add src directory to Python path if not already there
72
+ src_path = os.path.dirname(current_dir)
73
+ if src_path not in sys.path:
74
+ sys.path.insert(0, src_path)
75
+
76
  from chorus_detection.audio.data_processing import process_audio
77
  from chorus_detection.audio.processor import extract_audio
78
  from chorus_detection.models.crnn import load_CRNN_model, make_predictions
79
  from chorus_detection.utils.cli import is_youtube_url
80
  from chorus_detection.utils.logging import logger
81
+
82
+ logger.info("Successfully imported chorus_detection modules")
83
  except ImportError as e:
84
  logger.error(f"Error importing chorus_detection modules: {e}")
85
  logger.info("Trying alternative imports...")
86
+
87
+ # Try with manual path adjustment
88
+ try:
89
+ # Adjust import paths - try different directories
90
+ possible_paths = [
91
+ os.path.join(os.getcwd(), "src"),
92
+ os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
93
+ os.path.dirname(os.getcwd())
94
+ ]
95
+
96
+ for path in possible_paths:
97
+ if path not in sys.path and os.path.exists(path):
98
+ sys.path.insert(0, path)
99
+ logger.info(f"Added path to sys.path: {path}")
100
+
101
+ # Try importing directly from chorus_detection module path
102
+ sys.path.insert(0, os.path.join(os.getcwd(), "src", "chorus_detection"))
103
+
104
+ from chorus_detection.audio.data_processing import process_audio
105
+ from chorus_detection.audio.processor import extract_audio
106
+ from chorus_detection.models.crnn import load_CRNN_model, make_predictions
107
+ from chorus_detection.utils.cli import is_youtube_url
108
+ from chorus_detection.utils.logging import logger
109
+
110
+ logger.info("Successfully imported chorus_detection modules after path adjustment")
111
+ except ImportError as e2:
112
+ logger.error(f"Alternative imports also failed: {e2}")
113
+ raise
114
 
115
  # Define the MODEL_PATH directly
116
  MODEL_PATH = os.path.join(os.getcwd(), "models", "CRNN", "best_model_V3.h5")