Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
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 |
-
#
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
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 |
-
|
|
|
58 |
|
59 |
-
except
|
|
|
|
|
|
|
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
|