hemantn commited on
Commit
009d04a
·
1 Parent(s): 712d350

Fix modeling_ablang2paired.py to work with integrated files instead of ablang2 package

Browse files
Files changed (1) hide show
  1. modeling_ablang2paired.py +30 -25
modeling_ablang2paired.py CHANGED
@@ -3,34 +3,39 @@ import os
3
  from torch import nn
4
  from transformers import PreTrainedModel
5
 
6
- # Import logic to handle the nested directory structure
7
- import importlib.util
8
- import os
9
-
10
- def load_ablang_module():
11
- """Load the AbLang module from the nested directory structure."""
12
- # Try to find the ablang.py file in the nested directory
13
- current_dir = os.path.dirname(os.path.abspath(__file__))
14
- ablang_path = os.path.join(current_dir, "ablang2", "models", "ablang2", "ablang.py")
15
-
16
- if os.path.exists(ablang_path):
17
- spec = importlib.util.spec_from_file_location("ablang", ablang_path)
18
- ablang_module = importlib.util.module_from_spec(spec)
19
- spec.loader.exec_module(ablang_module)
20
- return ablang_module.AbLang
21
- else:
22
- # Fallback to absolute import
23
- from ablang2.models.ablang2.ablang import AbLang
24
- return AbLang
25
-
26
- # Load the AbLang class
27
- AbLang2 = load_ablang_module()
28
-
29
  # Import configuration
30
  try:
31
  from .configuration_ablang2paired import AbLang2PairedConfig
32
  except ImportError:
33
- from ablang2.configuration_ablang2paired import AbLang2PairedConfig
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  class AbLang2PairedHFModel(PreTrainedModel):
36
  config_class = AbLang2PairedConfig
@@ -38,7 +43,7 @@ class AbLang2PairedHFModel(PreTrainedModel):
38
 
39
  def __init__(self, config: AbLang2PairedConfig):
40
  super().__init__(config)
41
- self.model = AbLang2(
42
  vocab_size=config.vocab_size,
43
  hidden_embed_size=config.hidden_embed_size,
44
  n_attn_heads=config.n_attn_heads,
 
3
  from torch import nn
4
  from transformers import PreTrainedModel
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  # Import configuration
7
  try:
8
  from .configuration_ablang2paired import AbLang2PairedConfig
9
  except ImportError:
10
+ from configuration_ablang2paired import AbLang2PairedConfig
11
+
12
+ # Import the AbLang model from the integrated files
13
+ try:
14
+ from ablang2.models.ablang2.ablang import AbLang
15
+ except ImportError:
16
+ # Fallback: try to find the ablang.py file in the current directory structure
17
+ import importlib.util
18
+ import os
19
+
20
+ def load_ablang_module():
21
+ """Load the AbLang module from the nested directory structure."""
22
+ # Try to find the ablang.py file in the nested directory
23
+ current_dir = os.path.dirname(os.path.abspath(__file__))
24
+ ablang_path = os.path.join(current_dir, "ablang2", "models", "ablang2", "ablang.py")
25
+
26
+ if os.path.exists(ablang_path):
27
+ spec = importlib.util.spec_from_file_location("ablang", ablang_path)
28
+ ablang_module = importlib.util.module_from_spec(spec)
29
+ spec.loader.exec_module(ablang_module)
30
+ return ablang_module.AbLang
31
+ else:
32
+ # If not found, raise an error with helpful message
33
+ raise ImportError(
34
+ "Could not find AbLang module. Please ensure the ablang2 directory structure is present "
35
+ "or install the ablang2 package."
36
+ )
37
+
38
+ AbLang = load_ablang_module()
39
 
40
  class AbLang2PairedHFModel(PreTrainedModel):
41
  config_class = AbLang2PairedConfig
 
43
 
44
  def __init__(self, config: AbLang2PairedConfig):
45
  super().__init__(config)
46
+ self.model = AbLang(
47
  vocab_size=config.vocab_size,
48
  hidden_embed_size=config.hidden_embed_size,
49
  n_attn_heads=config.n_attn_heads,