vincentiusyoshuac commited on
Commit
de7b29d
·
verified ·
1 Parent(s): 41e0e6d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -31
app.py CHANGED
@@ -4,6 +4,7 @@ import torch
4
  import gradio as gr
5
  from huggingface_hub import hf_hub_download, snapshot_download
6
  from pathlib import Path
 
7
 
8
  class CognitiveNetworkDemo:
9
  def __init__(self):
@@ -19,44 +20,36 @@ class CognitiveNetworkDemo:
19
  repo_type="model",
20
  local_dir="./model_repo"
21
  ))
22
-
23
- # Add model directory to Python path
24
- model_dir = self.repo_path
25
- if str(model_dir) not in sys.path:
26
- sys.path.insert(0, str(model_dir))
27
-
28
- # Print directory contents for debugging
29
- print("Repository contents:")
30
- for path in model_dir.rglob("*"):
31
- print(f" {path.relative_to(model_dir)}")
32
 
33
  def load_model(self):
34
  """Load the model if not already loaded"""
35
  if self.model is None:
36
  try:
37
- # Try different import paths
38
- try:
39
- from network import DynamicCognitiveNet
40
- except ImportError:
41
- try:
42
- from cognitive_net.network import DynamicCognitiveNet
43
- except ImportError:
44
- # Search for the network.py file
45
- network_files = list(self.repo_path.rglob("network.py"))
46
- if not network_files:
47
- raise ImportError("Tidak dapat menemukan file network.py")
48
-
49
- # Add the parent directory of the first found network.py to path
50
- network_dir = network_files[0].parent
51
- if str(network_dir) not in sys.path:
52
- sys.path.insert(0, str(network_dir))
53
-
54
- # Try import again
55
- from network import DynamicCognitiveNet
56
 
57
- self.model = DynamicCognitiveNet(input_size=5, output_size=2)
 
58
 
59
- except ImportError as e:
 
 
 
60
  print("Debug - sys.path:", sys.path)
61
  raise ImportError(f"Gagal mengimpor model: {str(e)}")
62
  return self.model
 
4
  import gradio as gr
5
  from huggingface_hub import hf_hub_download, snapshot_download
6
  from pathlib import Path
7
+ import importlib.util
8
 
9
  class CognitiveNetworkDemo:
10
  def __init__(self):
 
20
  repo_type="model",
21
  local_dir="./model_repo"
22
  ))
23
+
24
+ def import_module_from_file(self, module_name, file_path):
25
+ """Import a module from file path"""
26
+ spec = importlib.util.spec_from_file_location(module_name, file_path)
27
+ module = importlib.util.module_from_spec(spec)
28
+ sys.modules[module_name] = module
29
+ spec.loader.exec_module(module)
30
+ return module
 
 
31
 
32
  def load_model(self):
33
  """Load the model if not already loaded"""
34
  if self.model is None:
35
  try:
36
+ # Find and import required modules
37
+ network_path = next(self.repo_path.rglob("network.py"))
38
+ node_path = next(self.repo_path.rglob("node.py"))
39
+ memory_path = next(self.repo_path.rglob("memory.py"))
40
+
41
+ # Import modules using absolute paths
42
+ node_module = self.import_module_from_file("node", node_path)
43
+ memory_module = self.import_module_from_file("memory", memory_path)
44
+ network_module = self.import_module_from_file("network", network_path)
 
 
 
 
 
 
 
 
 
 
45
 
46
+ # Create model instance
47
+ self.model = network_module.DynamicCognitiveNet(input_size=5, output_size=2)
48
 
49
+ except StopIteration:
50
+ raise ImportError("Tidak dapat menemukan file modul yang diperlukan")
51
+ except Exception as e:
52
+ print("Debug - repo path:", self.repo_path)
53
  print("Debug - sys.path:", sys.path)
54
  raise ImportError(f"Gagal mengimpor model: {str(e)}")
55
  return self.model