Simplify adapter to use direct imports without package structure
Browse files- 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 |
-
#
|
| 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 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
#
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 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 |
-
#
|
| 123 |
-
from
|
| 124 |
-
from
|
| 125 |
-
from
|
| 126 |
-
from
|
| 127 |
import torch
|
| 128 |
import numpy as np
|
| 129 |
-
from
|
| 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):
|