Sadjad Alikhani commited on
Commit
7ee077b
·
verified ·
1 Parent(s): 5ab64c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -21
app.py CHANGED
@@ -73,6 +73,8 @@ def load_custom_model():
73
  model.eval()
74
  return model
75
 
 
 
76
  # Function to process the uploaded .p file and perform inference using the custom model
77
  def process_p_file(uploaded_file, percentage_idx, complexity_idx):
78
  capture = PrintCapture()
@@ -96,40 +98,30 @@ def process_p_file(uploaded_file, percentage_idx, complexity_idx):
96
  print(f"Directory {model_repo_dir} does not exist.")
97
  return
98
 
99
- # Step 3: Add the cloned repo to sys.path for imports
100
- if model_repo_dir not in sys.path:
101
- sys.path.append(model_repo_dir)
102
-
103
- # Step 4: Debugging - Print sys.path to ensure the cloned repo is in the path
104
- print(f"sys.path: {sys.path}")
105
-
106
- # Ensure the 'LWM' directory is in the path
107
- lwm_model_dir = os.path.join(os.getcwd(), 'LWM')
108
- if lwm_model_dir not in sys.path:
109
- sys.path.append(lwm_model_dir)
110
-
111
- # Step 5: Verify if lwm_model.py exists in the directory
112
- lwm_model_path = 'lwm_model.py'
113
  if not os.path.exists(lwm_model_path):
114
  print(f"Error: lwm_model.py not found at {lwm_model_path}")
115
  return f"Error: lwm_model.py not found at {lwm_model_path}"
116
- else:
117
- print(f"lwm_model.py found at {lwm_model_path}")
118
 
119
- # Step 6: Import lwm_model after ensuring path is set
120
- from lwm_model import LWM
 
 
 
 
121
  device = 'cpu'
122
  print(f"Loading the LWM model on {device}...")
123
- model = LWM.from_pretrained(device=device)
124
 
125
- # Step 7: Import tokenizer and load data
126
  from input_preprocess import tokenizer
127
  with open(uploaded_file.name, 'rb') as f:
128
  manual_data = pickle.load(f)
129
 
130
  preprocessed_chs = tokenizer(manual_data=manual_data)
131
 
132
- # Step 8: Perform inference
133
  from inference import lwm_inference, create_raw_dataset
134
  output_emb = lwm_inference(preprocessed_chs, 'channel_emb', model)
135
  output_raw = create_raw_dataset(preprocessed_chs, device)
 
73
  model.eval()
74
  return model
75
 
76
+ import importlib.util
77
+
78
  # Function to process the uploaded .p file and perform inference using the custom model
79
  def process_p_file(uploaded_file, percentage_idx, complexity_idx):
80
  capture = PrintCapture()
 
98
  print(f"Directory {model_repo_dir} does not exist.")
99
  return
100
 
101
+ # Step 3: Dynamically import lwm_model.py using importlib
102
+ lwm_model_path = os.path.join(os.getcwd(), 'lwm_model.py')
 
 
 
 
 
 
 
 
 
 
 
 
103
  if not os.path.exists(lwm_model_path):
104
  print(f"Error: lwm_model.py not found at {lwm_model_path}")
105
  return f"Error: lwm_model.py not found at {lwm_model_path}"
 
 
106
 
107
+ # Use importlib to dynamically load lwm_model.py
108
+ spec = importlib.util.spec_from_file_location("lwm_model", lwm_model_path)
109
+ lwm_model = importlib.util.module_from_spec(spec)
110
+ spec.loader.exec_module(lwm_model)
111
+
112
+ # Step 4: Load the model from LWM module
113
  device = 'cpu'
114
  print(f"Loading the LWM model on {device}...")
115
+ model = lwm_model.LWM.from_pretrained(device=device)
116
 
117
+ # Step 5: Import tokenizer and load data
118
  from input_preprocess import tokenizer
119
  with open(uploaded_file.name, 'rb') as f:
120
  manual_data = pickle.load(f)
121
 
122
  preprocessed_chs = tokenizer(manual_data=manual_data)
123
 
124
+ # Step 6: Perform inference
125
  from inference import lwm_inference, create_raw_dataset
126
  output_emb = lwm_inference(preprocessed_chs, 'channel_emb', model)
127
  output_raw = create_raw_dataset(preprocessed_chs, device)