hemantn commited on
Commit
056b066
Β·
1 Parent(s): 9c57bc9

Simplify adapter to use direct imports without package structure

Browse files
Files changed (1) hide show
  1. adapter.py +27 -53
adapter.py CHANGED
@@ -68,65 +68,39 @@ def ensure_utility_files_available():
68
  # Ensure utility files are available before importing
69
  ensure_utility_files_available()
70
 
71
- # Create the ablang2.pretrained_utils package structure
72
- if not os.path.exists('ablang2'):
73
- os.makedirs('ablang2', exist_ok=True)
74
- if not os.path.exists('ablang2/pretrained_utils'):
75
- os.makedirs('ablang2/pretrained_utils', exist_ok=True)
76
-
77
- # Create __init__.py files
78
- with open('ablang2/__init__.py', 'w') as f:
79
- f.write('# Mock ablang2 package\n')
80
-
81
- with open('ablang2/pretrained_utils/__init__.py', 'w') as f:
82
- f.write('# Mock pretrained_utils package\n')
83
-
84
- # Copy utility files to the package structure
85
  for file in UTILITY_FILES:
86
  src = os.path.join(current_dir, file)
87
- dst = os.path.join(current_dir, 'ablang2', 'pretrained_utils', file)
88
- if os.path.exists(src) and not os.path.exists(dst):
89
- # Ensure the destination directory exists
90
- os.makedirs(os.path.dirname(dst), exist_ok=True)
91
- shutil.copy2(src, dst)
92
-
93
- # Also copy ablang_encodings.py as encodings.py (original name for compatibility)
94
- # The file should be in the current directory after the first copy operation
95
- encodings_src = os.path.join(current_dir, 'ablang_encodings.py')
96
- print(f"πŸ” Checking if ablang_encodings.py exists: {os.path.exists(encodings_src)}")
97
- if os.path.exists(encodings_src):
98
- # Ensure the destination directory exists
99
- os.makedirs('ablang2/pretrained_utils', exist_ok=True)
100
- dst = 'ablang2/pretrained_utils/encodings.py'
101
- print(f"πŸ“‹ Copying {encodings_src} to {dst}")
102
- try:
103
- shutil.copy2(encodings_src, dst)
104
- print("βœ… Copied ablang_encodings.py as encodings.py for compatibility")
105
- # Verify the copy worked
106
- if os.path.exists(dst):
107
- print(f"βœ… Verified {dst} exists after copy")
108
- else:
109
- print(f"❌ {dst} does not exist after copy!")
110
- except Exception as e:
111
- print(f"❌ Error copying file: {e}")
112
- else:
113
- print("❌ ablang_encodings.py not found!")
114
-
115
- # Debug: Check what files are actually in the pretrained_utils directory
116
- pretrained_utils_dir = os.path.join(current_dir, 'ablang2', 'pretrained_utils')
117
- if os.path.exists(pretrained_utils_dir):
118
- print(f"πŸ“ Files in {pretrained_utils_dir}:")
119
- for f in os.listdir(pretrained_utils_dir):
120
  print(f" {f}")
121
 
122
- # Now import using the original structure
123
- from ablang2.pretrained_utils.restoration import AbRestore
124
- from ablang2.pretrained_utils.encodings import AbEncoding
125
- from ablang2.pretrained_utils.alignment import AbAlignment
126
- from ablang2.pretrained_utils.scores import AbScores
127
  import torch
128
  import numpy as np
129
- from ablang2.pretrained_utils.extra_utils import res_to_seq, res_to_list
130
 
131
  class HuggingFaceTokenizerAdapter:
132
  def __init__(self, tokenizer, device):
 
68
  # Ensure utility files are available before importing
69
  ensure_utility_files_available()
70
 
71
+ # Copy utility files to the current directory if they don't exist
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  for file in UTILITY_FILES:
73
  src = os.path.join(current_dir, file)
74
+ if not os.path.exists(src):
75
+ # Try to find the file in possible source locations
76
+ possible_paths = [
77
+ os.path.join(current_dir, '..'), # Parent directory
78
+ os.path.join(current_dir, '..', '..'), # Grandparent directory
79
+ os.path.join(os.path.expanduser('~'), 'ablang2'), # Home directory
80
+ '/data/hn533621/ablang2', # Known repository location
81
+ ]
82
+
83
+ for path in possible_paths:
84
+ potential_src = os.path.join(path, file)
85
+ if os.path.exists(potential_src):
86
+ shutil.copy2(potential_src, src)
87
+ print(f"βœ… Copied {file} to current directory")
88
+ break
89
+
90
+ # Debug: Check what files are in the current directory
91
+ print(f"πŸ“ Files in current directory ({current_dir}):")
92
+ for f in os.listdir(current_dir):
93
+ if f.endswith('.py'):
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  print(f" {f}")
95
 
96
+ # Import utility modules directly (no package structure needed)
97
+ from restoration import AbRestore
98
+ from ablang_encodings import AbEncoding
99
+ from alignment import AbAlignment
100
+ from scores import AbScores
101
  import torch
102
  import numpy as np
103
+ from extra_utils import res_to_seq, res_to_list
104
 
105
  class HuggingFaceTokenizerAdapter:
106
  def __init__(self, tokenizer, device):