Spaces:
Running
Running
Sadjad Alikhani
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -46,6 +46,22 @@ def create_random_image(size=(300, 300)):
|
|
| 46 |
random_image = np.random.rand(*size, 3) * 255
|
| 47 |
return Image.fromarray(random_image.astype('uint8'))
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
# Function to process the uploaded .p file and perform inference using the custom model
|
| 50 |
def process_p_file(uploaded_file, percentage_idx, complexity_idx):
|
| 51 |
capture = PrintCapture()
|
|
@@ -64,15 +80,61 @@ def process_p_file(uploaded_file, percentage_idx, complexity_idx):
|
|
| 64 |
if os.path.exists(model_repo_dir):
|
| 65 |
os.chdir(model_repo_dir)
|
| 66 |
print(f"Changed working directory to {os.getcwd()}")
|
|
|
|
| 67 |
else:
|
| 68 |
print(f"Directory {model_repo_dir} does not exist.")
|
| 69 |
return
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
return
|
| 76 |
|
| 77 |
except Exception as e:
|
| 78 |
return str(e), str(e), capture.get_output()
|
|
@@ -85,7 +147,7 @@ def los_nlos_classification(file, percentage_idx, complexity_idx):
|
|
| 85 |
if file is not None:
|
| 86 |
return process_p_file(file, percentage_idx, complexity_idx)
|
| 87 |
else:
|
| 88 |
-
return
|
| 89 |
|
| 90 |
# Define the Gradio interface
|
| 91 |
with gr.Blocks(css="""
|
|
|
|
| 46 |
random_image = np.random.rand(*size, 3) * 255
|
| 47 |
return Image.fromarray(random_image.astype('uint8'))
|
| 48 |
|
| 49 |
+
# Function to load the pre-trained model from your cloned repository
|
| 50 |
+
def load_custom_model():
|
| 51 |
+
from lwm_model import LWM # Assuming the model is defined in lwm_model.py
|
| 52 |
+
model = LWM() # Modify this according to your model initialization
|
| 53 |
+
model.eval()
|
| 54 |
+
return model
|
| 55 |
+
|
| 56 |
+
import importlib.util
|
| 57 |
+
|
| 58 |
+
# Function to dynamically load a Python module from a given file path
|
| 59 |
+
def load_module_from_path(module_name, file_path):
|
| 60 |
+
spec = importlib.util.spec_from_file_location(module_name, file_path)
|
| 61 |
+
module = importlib.util.module_from_spec(spec)
|
| 62 |
+
spec.loader.exec_module(module)
|
| 63 |
+
return module
|
| 64 |
+
|
| 65 |
# Function to process the uploaded .p file and perform inference using the custom model
|
| 66 |
def process_p_file(uploaded_file, percentage_idx, complexity_idx):
|
| 67 |
capture = PrintCapture()
|
|
|
|
| 80 |
if os.path.exists(model_repo_dir):
|
| 81 |
os.chdir(model_repo_dir)
|
| 82 |
print(f"Changed working directory to {os.getcwd()}")
|
| 83 |
+
print(f"Directory content: {os.listdir(os.getcwd())}") # Debugging: Check repo content
|
| 84 |
else:
|
| 85 |
print(f"Directory {model_repo_dir} does not exist.")
|
| 86 |
return
|
| 87 |
|
| 88 |
+
# Step 3: Dynamically load lwm_model.py, input_preprocess.py, and inference.py
|
| 89 |
+
lwm_model_path = os.path.join(os.getcwd(), 'lwm_model.py')
|
| 90 |
+
input_preprocess_path = os.path.join(os.getcwd(), 'input_preprocess.py')
|
| 91 |
+
inference_path = os.path.join(os.getcwd(), 'inference.py')
|
| 92 |
+
|
| 93 |
+
print(lwm_model_path)
|
| 94 |
+
print(input_preprocess_path)
|
| 95 |
+
print(inference_path)
|
| 96 |
+
|
| 97 |
+
# Load lwm_model
|
| 98 |
+
if os.path.exists(lwm_model_path):
|
| 99 |
+
lwm_model = load_module_from_path("lwm_model", lwm_model_path)
|
| 100 |
+
else:
|
| 101 |
+
return f"Error: lwm_model.py not found at {lwm_model_path}"
|
| 102 |
+
|
| 103 |
+
# Load input_preprocess
|
| 104 |
+
if os.path.exists(input_preprocess_path):
|
| 105 |
+
input_preprocess = load_module_from_path("input_preprocess", input_preprocess_path)
|
| 106 |
+
else:
|
| 107 |
+
return f"Error: input_preprocess.py not found at {input_preprocess_path}"
|
| 108 |
+
|
| 109 |
+
# Load inference
|
| 110 |
+
if os.path.exists(inference_path):
|
| 111 |
+
inference = load_module_from_path("inference", inference_path)
|
| 112 |
+
else:
|
| 113 |
+
return f"Error: inference.py not found at {inference_path}"
|
| 114 |
+
|
| 115 |
+
# Step 4: Load the model from lwm_model module
|
| 116 |
+
device = 'cpu'
|
| 117 |
+
print(f"Loading the LWM model on {device}...")
|
| 118 |
+
model = lwm_model.LWM.from_pretrained(device=device)
|
| 119 |
+
|
| 120 |
+
# Step 5: Tokenize the data using the tokenizer from input_preprocess
|
| 121 |
+
with open(uploaded_file.name, 'rb') as f:
|
| 122 |
+
manual_data = pickle.load(f)
|
| 123 |
+
|
| 124 |
+
preprocessed_chs = input_preprocess.tokenizer(manual_data=manual_data)
|
| 125 |
+
|
| 126 |
+
# Step 6: Perform inference using the functions from inference.py
|
| 127 |
+
output_emb = inference.lwm_inference(preprocessed_chs, 'channel_emb', model)
|
| 128 |
+
output_raw = inference.create_raw_dataset(preprocessed_chs, device)
|
| 129 |
+
|
| 130 |
+
print(f"Output Embeddings Shape: {output_emb.shape}")
|
| 131 |
+
print(f"Output Raw Shape: {output_raw.shape}")
|
| 132 |
+
|
| 133 |
+
# Step 7: Generate random images as a test
|
| 134 |
+
random_raw_image = create_random_image()
|
| 135 |
+
random_embeddings_image = create_random_image()
|
| 136 |
|
| 137 |
+
return random_raw_image, random_embeddings_image, capture.get_output()
|
| 138 |
|
| 139 |
except Exception as e:
|
| 140 |
return str(e), str(e), capture.get_output()
|
|
|
|
| 147 |
if file is not None:
|
| 148 |
return process_p_file(file, percentage_idx, complexity_idx)
|
| 149 |
else:
|
| 150 |
+
return display_predefined_images(percentage_idx, complexity_idx), None
|
| 151 |
|
| 152 |
# Define the Gradio interface
|
| 153 |
with gr.Blocks(css="""
|