popboat1 commited on
Commit
31222e8
·
1 Parent(s): 0bc36ed

Fix WebSocket CORS and TensorFlow event loop freeze

Browse files
frontend/3d-visualizer/src/app/page.js CHANGED
@@ -299,9 +299,6 @@ export default function NetworkVisualizer() {
299
  <div className="p-6 flex-1 overflow-y-auto relative custom-scrollbar">
300
  {activeTab.type === 'layer' && activeLayerPanelData && (
301
  <div className="animate-fadeIn">
302
- <h2 className="text-xl md:text-2xl font-bold font-mono text-[#00ffcc] mb-4 border-b border-white/10 pb-2">
303
- Layer {activeLayerPanelData.layer_index}
304
- </h2>
305
  <div className="mb-6">
306
  <p className="text-[10px] md:text-xs text-gray-500 uppercase tracking-wider mb-1">Tensor Shape</p>
307
  <p className="font-mono text-xs md:text-sm bg-black/50 px-3 py-2 rounded-lg border border-white/10 tracking-widest inline-block text-gray-200">
 
299
  <div className="p-6 flex-1 overflow-y-auto relative custom-scrollbar">
300
  {activeTab.type === 'layer' && activeLayerPanelData && (
301
  <div className="animate-fadeIn">
 
 
 
302
  <div className="mb-6">
303
  <p className="text-[10px] md:text-xs text-gray-500 uppercase tracking-wider mb-1">Tensor Shape</p>
304
  <p className="font-mono text-xs md:text-sm bg-black/50 px-3 py-2 rounded-lg border border-white/10 tracking-widest inline-block text-gray-200">
src/api/api.py CHANGED
@@ -22,7 +22,7 @@ app = FastAPI()
22
  app.add_middleware(
23
  CORSMiddleware,
24
  allow_origins=["*"],
25
- allow_credentials=True,
26
  allow_methods=["*"],
27
  allow_headers=["*"],
28
  )
@@ -56,6 +56,7 @@ feature_extractor = tf.keras.Model(inputs=model.inputs, outputs=[layer.output fo
56
  CIFAR10_CLASSES = ['Airplane', 'Automobile', 'Bird', 'Cat', 'Deer',
57
  'Dog', 'Frog', 'Horse', 'Ship', 'Truck']
58
 
 
59
  def generate_feature_grid(feature_map, max_features=64):
60
  if len(feature_map.shape) == 4:
61
  feature_map = feature_map[0]
@@ -85,7 +86,7 @@ def generate_feature_grid(feature_map, max_features=64):
85
  colored_grid = cv2.applyColorMap(grid_image, cv2.COLORMAP_VIRIDIS)
86
 
87
  b_channel, g_channel, r_channel = cv2.split(colored_grid)
88
- alpha_channel = grid_image # Use the raw grayscale intensity as the alpha map
89
 
90
  transparent_grid = cv2.merge((b_channel, g_channel, r_channel, alpha_channel))
91
 
@@ -102,19 +103,19 @@ async def predict_image(file: UploadFile = File(...)):
102
  img_normalized = img_resized.astype(np.float32) / 255.0
103
  img_batch = np.expand_dims(img_normalized, axis=0)
104
 
105
- activations = feature_extractor.predict(img_batch)
106
-
107
- predictions = model.predict(img_batch)
108
- class_idx = np.argmax(predictions[0])
109
 
110
  layer_data = []
111
  for i, activation in enumerate(activations):
112
- b64_image = generate_feature_grid(activation)
113
 
114
  layer_data.append({
115
  "layer_index": i + 1,
116
  "shape": activation.shape[1:],
117
- "texture_b64": f"data:image/jpeg;base64,{b64_image}"
118
  })
119
 
120
  return {
@@ -142,13 +143,13 @@ async def predict_video_stream(websocket: WebSocket):
142
  img_normalized = img_resized.astype(np.float32) / 255.0
143
  img_batch = np.expand_dims(img_normalized, axis=0)
144
 
145
- activations = feature_extractor.predict(img_batch, verbose=0)
146
- predictions = model.predict(img_batch, verbose=0)
147
- class_idx = np.argmax(predictions[0])
148
 
149
  layer_data = []
150
  for i, activation in enumerate(activations):
151
- b64_image = generate_feature_grid(activation)
152
  layer_data.append({
153
  "layer_index": i + 1,
154
  "shape": activation.shape[1:],
 
22
  app.add_middleware(
23
  CORSMiddleware,
24
  allow_origins=["*"],
25
+ allow_credentials=False,
26
  allow_methods=["*"],
27
  allow_headers=["*"],
28
  )
 
56
  CIFAR10_CLASSES = ['Airplane', 'Automobile', 'Bird', 'Cat', 'Deer',
57
  'Dog', 'Frog', 'Horse', 'Ship', 'Truck']
58
 
59
+ # YOUR ORIGINAL PNG LOGIC RESTORED
60
  def generate_feature_grid(feature_map, max_features=64):
61
  if len(feature_map.shape) == 4:
62
  feature_map = feature_map[0]
 
86
  colored_grid = cv2.applyColorMap(grid_image, cv2.COLORMAP_VIRIDIS)
87
 
88
  b_channel, g_channel, r_channel = cv2.split(colored_grid)
89
+ alpha_channel = grid_image
90
 
91
  transparent_grid = cv2.merge((b_channel, g_channel, r_channel, alpha_channel))
92
 
 
103
  img_normalized = img_resized.astype(np.float32) / 255.0
104
  img_batch = np.expand_dims(img_normalized, axis=0)
105
 
106
+ # DIRECT CALLS FOR IMAGE AS WELL
107
+ activations = feature_extractor(img_batch, training=False)
108
+ predictions = model(img_batch, training=False)
109
+ class_idx = np.argmax(predictions[0].numpy())
110
 
111
  layer_data = []
112
  for i, activation in enumerate(activations):
113
+ b64_image = generate_feature_grid(activation.numpy())
114
 
115
  layer_data.append({
116
  "layer_index": i + 1,
117
  "shape": activation.shape[1:],
118
+ "texture_b64": f"data:image/png;base64,{b64_image}"
119
  })
120
 
121
  return {
 
143
  img_normalized = img_resized.astype(np.float32) / 255.0
144
  img_batch = np.expand_dims(img_normalized, axis=0)
145
 
146
+ activations = feature_extractor(img_batch, training=False)
147
+ predictions = model(img_batch, training=False)
148
+ class_idx = np.argmax(predictions[0].numpy())
149
 
150
  layer_data = []
151
  for i, activation in enumerate(activations):
152
+ b64_image = generate_feature_grid(activation.numpy())
153
  layer_data.append({
154
  "layer_index": i + 1,
155
  "shape": activation.shape[1:],