Sadjad Alikhani
commited on
Create lwm_setup.py
Browse files- lwm_setup.py +51 -0
lwm_setup.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import subprocess
|
4 |
+
import importlib.util
|
5 |
+
|
6 |
+
class LWMSetup:
|
7 |
+
def __init__(self, repo_url="https://huggingface.co/sadjadalikhani/LWM", clone_dir="./LWM"):
|
8 |
+
self.repo_url = repo_url
|
9 |
+
self.clone_dir = clone_dir
|
10 |
+
|
11 |
+
def setup(self):
|
12 |
+
# Step 1: Clone the public repository
|
13 |
+
self.clone_repo()
|
14 |
+
|
15 |
+
# Step 2: Add the cloned directory to the Python path so all imports work
|
16 |
+
sys.path.append(self.clone_dir)
|
17 |
+
|
18 |
+
# Step 3: Dynamically import all required modules
|
19 |
+
self.import_modules()
|
20 |
+
|
21 |
+
def clone_repo(self):
|
22 |
+
if not os.path.exists(self.clone_dir):
|
23 |
+
print(f"Cloning repository from {self.repo_url} into {self.clone_dir}")
|
24 |
+
|
25 |
+
# Clone the repository without needing an authentication token
|
26 |
+
result = subprocess.run(["git", "clone", self.repo_url, self.clone_dir], capture_output=True, text=True)
|
27 |
+
|
28 |
+
if result.returncode != 0:
|
29 |
+
print(f"Error cloning the repository: {result.stderr}")
|
30 |
+
sys.exit(1) # Exit if cloning fails
|
31 |
+
else:
|
32 |
+
print(f"Repository cloned successfully into {self.clone_dir}")
|
33 |
+
else:
|
34 |
+
print(f"Repository already cloned into {self.clone_dir}")
|
35 |
+
|
36 |
+
def import_modules(self):
|
37 |
+
def import_module_from_file(module_name, file_path):
|
38 |
+
spec = importlib.util.spec_from_file_location(module_name, file_path)
|
39 |
+
module = importlib.util.module_from_spec(spec)
|
40 |
+
spec.loader.exec_module(module)
|
41 |
+
return module
|
42 |
+
|
43 |
+
try:
|
44 |
+
self.lwm_model = import_module_from_file("lwm_model", os.path.join(self.clone_dir, "lwm_model.py"))
|
45 |
+
self.inference = import_module_from_file("inference", os.path.join(self.clone_dir, "inference.py"))
|
46 |
+
self.load_data = import_module_from_file("load_data", os.path.join(self.clone_dir, "load_data.py"))
|
47 |
+
self.input_preprocess = import_module_from_file("input_preprocess", os.path.join(self.clone_dir, "input_preprocess.py"))
|
48 |
+
print("Modules imported successfully.")
|
49 |
+
except ModuleNotFoundError as e:
|
50 |
+
print(f"Error importing modules: {e}")
|
51 |
+
sys.exit(1)
|