Spaces:
Running
Running
Sadjad Alikhani
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
from PIL import Image
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
|
| 6 |
# Paths to the predefined images folder
|
| 7 |
RAW_PATH = os.path.join("images", "raw")
|
|
@@ -30,14 +31,7 @@ def display_predefined_images(percentage_idx, complexity_idx):
|
|
| 30 |
return raw_image, embeddings_image
|
| 31 |
|
| 32 |
import torch
|
| 33 |
-
import numpy as np
|
| 34 |
-
import importlib.util
|
| 35 |
-
|
| 36 |
-
import torch
|
| 37 |
-
import numpy as np
|
| 38 |
-
import importlib.util
|
| 39 |
import subprocess
|
| 40 |
-
import os
|
| 41 |
|
| 42 |
# Function to load the pre-trained model from your cloned repository
|
| 43 |
def load_custom_model():
|
|
@@ -47,8 +41,8 @@ def load_custom_model():
|
|
| 47 |
model.eval() # Set the model to evaluation mode
|
| 48 |
return model
|
| 49 |
|
| 50 |
-
# Function to process the uploaded .
|
| 51 |
-
def
|
| 52 |
try:
|
| 53 |
# Clone the repository if not already done (for model and tokenizer)
|
| 54 |
model_repo_url = "https://huggingface.co/sadjadalikhani/LWM"
|
|
@@ -67,7 +61,6 @@ def process_python_file(uploaded_file, percentage_idx, complexity_idx):
|
|
| 67 |
|
| 68 |
# Step 1: Load the custom model
|
| 69 |
from lwm_model import LWM
|
| 70 |
-
import torch
|
| 71 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 72 |
print(f"Loading the LWM model on {device}...")
|
| 73 |
model = LWM.from_pretrained(device=device)
|
|
@@ -75,17 +68,7 @@ def process_python_file(uploaded_file, percentage_idx, complexity_idx):
|
|
| 75 |
# Step 2: Import the tokenizer
|
| 76 |
from input_preprocess import tokenizer
|
| 77 |
|
| 78 |
-
|
| 79 |
-
## Import the Python file dynamically
|
| 80 |
-
#spec = importlib.util.spec_from_file_location("uploaded_module", uploaded_file.name)
|
| 81 |
-
#uploaded_module = importlib.util.module_from_spec(spec)
|
| 82 |
-
#spec.loader.exec_module(uploaded_module)
|
| 83 |
-
|
| 84 |
-
## Assuming the uploaded file defines a variable called 'dataset'
|
| 85 |
-
#manual_data = uploaded_module.dataset # This should be defined in the uploaded file
|
| 86 |
-
|
| 87 |
-
import pickle
|
| 88 |
-
# Load the .p file containing the wireless channel matrix
|
| 89 |
with open(uploaded_file.name, 'rb') as f:
|
| 90 |
manual_data = pickle.load(f)
|
| 91 |
|
|
@@ -93,28 +76,11 @@ def process_python_file(uploaded_file, percentage_idx, complexity_idx):
|
|
| 93 |
preprocessed_chs = tokenizer(manual_data=manual_data)
|
| 94 |
|
| 95 |
# Step 5: Perform inference on the channel matrix using the model
|
| 96 |
-
#with torch.no_grad():
|
| 97 |
-
# input_tensor = torch.tensor(preprocessed_data).unsqueeze(0) # Add batch dimension
|
| 98 |
-
# output = model(input_tensor) # Perform inference
|
| 99 |
from inference import lwm_inference, create_raw_dataset
|
| 100 |
output_emb = lwm_inference(preprocessed_chs, 'channel_emb', model)
|
| 101 |
output_raw = create_raw_dataset(preprocessed_chs, device)
|
| 102 |
print(output_emb.shape)
|
| 103 |
print(output_raw.shape)
|
| 104 |
-
# Step 6: Generate new images based on the inference results
|
| 105 |
-
#generated_raw_img = np.random.rand(300, 300, 3) * 255 # Placeholder: Replace with actual inference result
|
| 106 |
-
#generated_embeddings_img = np.random.rand(300, 300, 3) * 255 # Placeholder: Replace with actual inference result
|
| 107 |
-
|
| 108 |
-
# Save the generated images
|
| 109 |
-
#generated_raw_image_path = os.path.join(GENERATED_PATH, f"generated_raw_{percentage_idx}_{complexity_idx}.png")
|
| 110 |
-
#generated_embeddings_image_path = os.path.join(GENERATED_PATH, f"generated_embeddings_{percentage_idx}_{complexity_idx}.png")
|
| 111 |
-
|
| 112 |
-
#Image.fromarray(generated_raw_img.astype(np.uint8)).save(generated_raw_image_path)
|
| 113 |
-
#Image.fromarray(generated_embeddings_img.astype(np.uint8)).save(generated_embeddings_image_path)
|
| 114 |
-
|
| 115 |
-
# Load the generated images
|
| 116 |
-
#raw_image = Image.open(generated_raw_image_path)
|
| 117 |
-
#embeddings_image = Image.open(generated_embeddings_image_path)
|
| 118 |
|
| 119 |
return output_emb, output_raw
|
| 120 |
|
|
@@ -124,8 +90,8 @@ def process_python_file(uploaded_file, percentage_idx, complexity_idx):
|
|
| 124 |
# Function to handle logic based on whether a file is uploaded or not
|
| 125 |
def los_nlos_classification(file, percentage_idx, complexity_idx):
|
| 126 |
if file is not None:
|
| 127 |
-
# Process the uploaded file and generate new images
|
| 128 |
-
return
|
| 129 |
else:
|
| 130 |
# Display predefined images if no file is uploaded
|
| 131 |
return display_predefined_images(percentage_idx, complexity_idx)
|
|
@@ -150,8 +116,8 @@ with gr.Blocks(css="""
|
|
| 150 |
"""
|
| 151 |
## Contact
|
| 152 |
<div style="display: flex; align-items: center;">
|
| 153 |
-
<a target="_blank" href="
|
| 154 |
-
<a target="_blank" href="
|
| 155 |
</div>
|
| 156 |
"""
|
| 157 |
)
|
|
@@ -181,8 +147,8 @@ with gr.Blocks(css="""
|
|
| 181 |
with gr.Tab("LoS/NLoS Classification Task"):
|
| 182 |
gr.Markdown("### LoS/NLoS Classification Task")
|
| 183 |
|
| 184 |
-
# File uploader for uploading .
|
| 185 |
-
file_input = gr.File(label="Upload .
|
| 186 |
|
| 187 |
# Sliders for percentage and complexity
|
| 188 |
with gr.Row():
|
|
|
|
| 2 |
import os
|
| 3 |
from PIL import Image
|
| 4 |
import numpy as np
|
| 5 |
+
import pickle
|
| 6 |
|
| 7 |
# Paths to the predefined images folder
|
| 8 |
RAW_PATH = os.path.join("images", "raw")
|
|
|
|
| 31 |
return raw_image, embeddings_image
|
| 32 |
|
| 33 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
import subprocess
|
|
|
|
| 35 |
|
| 36 |
# Function to load the pre-trained model from your cloned repository
|
| 37 |
def load_custom_model():
|
|
|
|
| 41 |
model.eval() # Set the model to evaluation mode
|
| 42 |
return model
|
| 43 |
|
| 44 |
+
# Function to process the uploaded .p file and perform inference using the custom model
|
| 45 |
+
def process_p_file(uploaded_file, percentage_idx, complexity_idx):
|
| 46 |
try:
|
| 47 |
# Clone the repository if not already done (for model and tokenizer)
|
| 48 |
model_repo_url = "https://huggingface.co/sadjadalikhani/LWM"
|
|
|
|
| 61 |
|
| 62 |
# Step 1: Load the custom model
|
| 63 |
from lwm_model import LWM
|
|
|
|
| 64 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 65 |
print(f"Loading the LWM model on {device}...")
|
| 66 |
model = LWM.from_pretrained(device=device)
|
|
|
|
| 68 |
# Step 2: Import the tokenizer
|
| 69 |
from input_preprocess import tokenizer
|
| 70 |
|
| 71 |
+
# Step 3: Load the uploaded .p file that contains the wireless channel matrix
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
with open(uploaded_file.name, 'rb') as f:
|
| 73 |
manual_data = pickle.load(f)
|
| 74 |
|
|
|
|
| 76 |
preprocessed_chs = tokenizer(manual_data=manual_data)
|
| 77 |
|
| 78 |
# Step 5: Perform inference on the channel matrix using the model
|
|
|
|
|
|
|
|
|
|
| 79 |
from inference import lwm_inference, create_raw_dataset
|
| 80 |
output_emb = lwm_inference(preprocessed_chs, 'channel_emb', model)
|
| 81 |
output_raw = create_raw_dataset(preprocessed_chs, device)
|
| 82 |
print(output_emb.shape)
|
| 83 |
print(output_raw.shape)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
return output_emb, output_raw
|
| 86 |
|
|
|
|
| 90 |
# Function to handle logic based on whether a file is uploaded or not
|
| 91 |
def los_nlos_classification(file, percentage_idx, complexity_idx):
|
| 92 |
if file is not None:
|
| 93 |
+
# Process the uploaded .p file and generate new images
|
| 94 |
+
return process_p_file(file, percentage_idx, complexity_idx)
|
| 95 |
else:
|
| 96 |
# Display predefined images if no file is uploaded
|
| 97 |
return display_predefined_images(percentage_idx, complexity_idx)
|
|
|
|
| 116 |
"""
|
| 117 |
## Contact
|
| 118 |
<div style="display: flex; align-items: center;">
|
| 119 |
+
<a target="_blank" href="https://www.wi-lab.net"><img src="https://www.wi-lab.net/wp-content/uploads/2021/08/WI-name.png" alt="Wireless Model" style="height: 30px;"></a>
|
| 120 |
+
<a target="_blank" href="mailto:alikhani@asu.edu"><img src="https://img.shields.io/badge/email-alikhani@asu.edu-blue.svg?logo=gmail " alt="Email"></a>
|
| 121 |
</div>
|
| 122 |
"""
|
| 123 |
)
|
|
|
|
| 147 |
with gr.Tab("LoS/NLoS Classification Task"):
|
| 148 |
gr.Markdown("### LoS/NLoS Classification Task")
|
| 149 |
|
| 150 |
+
# File uploader for uploading .p file
|
| 151 |
+
file_input = gr.File(label="Upload .p File", file_types=[".p"])
|
| 152 |
|
| 153 |
# Sliders for percentage and complexity
|
| 154 |
with gr.Row():
|