prthm11 commited on
Commit
c243f10
·
verified ·
1 Parent(s): c94f3ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -26
app.py CHANGED
@@ -217,59 +217,122 @@ agent_json_resolver = create_react_agent(
217
  )
218
 
219
  # Helper function to load the block catalog from a JSON file
220
- def _load_block_catalog(file_path: str) -> Dict:
221
- """Loads the Scratch block catalog from a specified JSON file."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
222
  try:
223
- with open(file_path, 'r') as f:
224
- catalog = json.load(f)
225
- logger.info(f"Successfully loaded block catalog from {file_path}")
226
  return catalog
 
227
  except FileNotFoundError:
228
- logger.error(f"Error: Block catalog file not found at {file_path}")
229
- # Return an empty dict or raise an error, depending on desired behavior
230
- return {}
231
  except json.JSONDecodeError as e:
232
- logger.error(f"Error decoding JSON from {file_path}: {e}")
233
- return {}
234
  except Exception as e:
235
- logger.error(f"An unexpected error occurred while loading {file_path}: {e}")
236
- return {}
237
 
238
  # --- Global variable for the block catalog ---
239
  # --- Global variable for the block catalog ---
240
  ALL_SCRATCH_BLOCKS_CATALOG = {}
241
- BLOCK_CATALOG_PATH = r"blocks\blocks.json" # Define the path to your JSON file
242
- HAT_BLOCKS_PATH = r"blocks\hat_blocks.json" # Path to the hat blocks JSON file
243
- STACK_BLOCKS_PATH = r"blocks\stack_blocks.json" # Path to the stack blocks JSON file
244
- REPORTER_BLOCKS_PATH = r"blocks\reporter_blocks.json" # Path to the reporter blocks JSON file
245
- BOOLEAN_BLOCKS_PATH = r"blocks\boolean_blocks.json" # Path to the boolean blocks JSON file
246
- C_BLOCKS_PATH = r"blocks\c_blocks.json" # Path to the C blocks JSON file
247
- CAP_BLOCKS_PATH = r"blocks\cap_blocks.json" # Path to the cap blocks JSON file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
248
 
249
  # Load the block catalogs from their respective JSON files
250
  hat_block_data = _load_block_catalog(HAT_BLOCKS_PATH)
251
- hat_description = hat_block_data["description"]
252
- hat_opcodes_functionalities = "\n".join([f" - Opcode: {block['op_code']}, functionality: {block['functionality']} example: standalone use: {block['example_standalone']}" for block in hat_block_data["blocks"]])
 
 
 
 
 
253
  print("Hat blocks loaded successfully.", hat_description)
 
254
  boolean_block_data = _load_block_catalog(BOOLEAN_BLOCKS_PATH)
255
  boolean_description = boolean_block_data["description"]
256
- boolean_opcodes_functionalities = "\n".join([f" - Opcode: {block['op_code']}, functionality: {block['functionality']} example: standalone use: {block['example_standalone']}" for block in boolean_block_data["blocks"]])
 
 
 
 
257
 
258
  c_block_data = _load_block_catalog(C_BLOCKS_PATH)
259
  c_description = c_block_data["description"]
260
- c_opcodes_functionalities = "\n".join([f" - Opcode: {block['op_code']}, functionality: {block['functionality']} example: standalone use: {block['example_standalone']}" for block in c_block_data["blocks"]])
 
 
 
 
261
 
262
  cap_block_data = _load_block_catalog(CAP_BLOCKS_PATH)
263
  cap_description = cap_block_data["description"]
264
- cap_opcodes_functionalities = "\n".join([f" - Opcode: {block['op_code']}, functionality: {block['functionality']} example: standalone use: {block['example_standalone']}" for block in cap_block_data["blocks"]])
 
 
 
 
265
 
266
  reporter_block_data = _load_block_catalog(REPORTER_BLOCKS_PATH)
267
  reporter_description = reporter_block_data["description"]
268
- reporter_opcodes_functionalities = "\n".join([f" - Opcode: {block['op_code']}, functionality: {block['functionality']} example: standalone use: {block['example_standalone']}" for block in reporter_block_data["blocks"]])
 
 
 
 
269
 
270
  stack_block_data = _load_block_catalog(STACK_BLOCKS_PATH)
271
  stack_description = stack_block_data["description"]
272
- stack_opcodes_functionalities = "\n".join([f" - Opcode: {block['op_code']}, functionality: {block['functionality']} example: standalone use: {block['example_standalone']}" for block in stack_block_data["blocks"]])
 
 
 
 
273
 
274
  # This makes ALL_SCRATCH_BLOCKS_CATALOG available globally
275
  ALL_SCRATCH_BLOCKS_CATALOG = _load_block_catalog(BLOCK_CATALOG_PATH)
 
217
  )
218
 
219
  # Helper function to load the block catalog from a JSON file
220
+ # def _load_block_catalog(file_path: str) -> Dict:
221
+ # """Loads the Scratch block catalog from a specified JSON file."""
222
+ # try:
223
+ # with open(file_path, 'r') as f:
224
+ # catalog = json.load(f)
225
+ # logger.info(f"Successfully loaded block catalog from {file_path}")
226
+ # return catalog
227
+ # except FileNotFoundError:
228
+ # logger.error(f"Error: Block catalog file not found at {file_path}")
229
+ # # Return an empty dict or raise an error, depending on desired behavior
230
+ # return {}
231
+ # except json.JSONDecodeError as e:
232
+ # logger.error(f"Error decoding JSON from {file_path}: {e}")
233
+ # return {}
234
+ # except Exception as e:
235
+ # logger.error(f"An unexpected error occurred while loading {file_path}: {e}")
236
+ # return {}
237
+
238
+ # Helper function to load the block catalog from a JSON file
239
+ def _load_block_catalog(block_type: str) -> Dict:
240
+ """
241
+ Loads the Scratch block catalog named '{block_type}_blocks.json'
242
+ from the <project_root>/blocks/ folder. Returns {} on any error.
243
+ """
244
+ catalog_path = BLOCKS_DIR / f"{block_type}.json"
245
+
246
  try:
247
+ text = catalog_path.read_text() # will raise FileNotFoundError if missing
248
+ catalog = json.loads(text) # will raise JSONDecodeError if malformed
249
+ logger.info(f"Successfully loaded block catalog from {catalog_path}")
250
  return catalog
251
+
252
  except FileNotFoundError:
253
+ logger.error(f"Error: Block catalog file not found at {catalog_path}")
 
 
254
  except json.JSONDecodeError as e:
255
+ logger.error(f"Error decoding JSON from {catalog_path}: {e}")
 
256
  except Exception as e:
257
+ logger.error(f"Unexpected error loading {catalog_path}: {e}")
 
258
 
259
  # --- Global variable for the block catalog ---
260
  # --- Global variable for the block catalog ---
261
  ALL_SCRATCH_BLOCKS_CATALOG = {}
262
+ # BLOCK_CATALOG_PATH = r"blocks\blocks.json" # Define the path to your JSON file
263
+ # HAT_BLOCKS_PATH = r"blocks\hat_blocks.json" # Path to the hat blocks JSON file
264
+ # STACK_BLOCKS_PATH = r"blocks\stack_blocks.json" # Path to the stack blocks JSON file
265
+ # REPORTER_BLOCKS_PATH = r"blocks\reporter_blocks.json" # Path to the reporter blocks JSON file
266
+ # BOOLEAN_BLOCKS_PATH = r"blocks\boolean_blocks.json" # Path to the boolean blocks JSON file
267
+ # C_BLOCKS_PATH = r"blocks\c_blocks.json" # Path to the C blocks JSON file
268
+ # CAP_BLOCKS_PATH = r"blocks\cap_blocks.json" # Path to the cap blocks JSON file
269
+
270
+ # BLOCK_CATALOG_PATH = r"blocks/blocks.json"
271
+ # HAT_BLOCKS_PATH = r"blocks/hat_blocks.json"
272
+ # STACK_BLOCKS_PATH = r"blocks/stack_blocks.json"
273
+ # REPORTER_BLOCKS_PATH = r"blocks/reporter_blocks.json"
274
+ # BOOLEAN_BLOCKS_PATH = r"blocks/boolean_blocks.json"
275
+ # C_BLOCKS_PATH = r"blocks/c_blocks.json"
276
+ # CAP_BLOCKS_PATH = r"blocks/cap_blocks.json"
277
+
278
+ BLOCK_CATALOG_PATH = "blocks" # Define the path to your JSON file
279
+ HAT_BLOCKS_PATH = "hat_blocks" # Path to the hat blocks JSON file
280
+ STACK_BLOCKS_PATH = "stack_blocks" # Path to the stack blocks JSON file
281
+ REPORTER_BLOCKS_PATH = "reporter_blocks" # Path to the reporter blocks JSON file
282
+ BOOLEAN_BLOCKS_PATH = "boolean_blocks" # Path to the boolean blocks JSON file
283
+ C_BLOCKS_PATH = "c_blocks" # Path to the C blocks JSON file
284
+ CAP_BLOCKS_PATH = "cap_blocks" # Path to the cap blocks JSON file
285
 
286
  # Load the block catalogs from their respective JSON files
287
  hat_block_data = _load_block_catalog(HAT_BLOCKS_PATH)
288
+ # hat_description = hat_block_data["description"]
289
+ hat_description = hat_block_data.get("description", "No description available")
290
+ # hat_opcodes_functionalities = "\n".join([f" - Opcode: {block['op_code']}, functionality: {block['functionality']} example: standalone use: {block['example_standalone']}" for block in hat_block_data["blocks"]])
291
+ hat_opcodes_functionalities = "\n".join([
292
+ f" - Opcode: {block.get('op_code', 'N/A')}, functionality: {block.get('functionality', 'N/A')}, example: standalone use {block.get('example_standalone', 'N/A')}"
293
+ for block in hat_block_data.get("blocks", [])
294
+ ]) if isinstance(hat_block_data.get("blocks"), list) else " No blocks information available."
295
  print("Hat blocks loaded successfully.", hat_description)
296
+
297
  boolean_block_data = _load_block_catalog(BOOLEAN_BLOCKS_PATH)
298
  boolean_description = boolean_block_data["description"]
299
+ # boolean_opcodes_functionalities = "\n".join([f" - Opcode: {block['op_code']}, functionality: {block['functionality']} example: standalone use: {block['example_standalone']}" for block in boolean_block_data["blocks"]])
300
+ boolean_opcodes_functionalities = "\n".join([
301
+ f" - Opcode: {block.get('op_code', 'N/A')}, functionality: {block.get('functionality', 'N/A')}, example: standalone use {block.get('example_standalone', 'N/A')}"
302
+ for block in boolean_block_data.get("blocks", [])
303
+ ]) if isinstance(boolean_block_data.get("blocks"), list) else " No blocks information available."
304
 
305
  c_block_data = _load_block_catalog(C_BLOCKS_PATH)
306
  c_description = c_block_data["description"]
307
+ # c_opcodes_functionalities = "\n".join([f" - Opcode: {block['op_code']}, functionality: {block['functionality']} example: standalone use: {block['example_standalone']}" for block in c_block_data["blocks"]])
308
+ c_opcodes_functionalities = "\n".join([
309
+ f" - Opcode: {block.get('op_code', 'N/A')}, functionality: {block.get('functionality', 'N/A')}, example: standalone use {block.get('example_standalone', 'N/A')}"
310
+ for block in c_block_data.get("blocks", [])
311
+ ]) if isinstance(c_block_data.get("blocks"), list) else " No blocks information available."
312
 
313
  cap_block_data = _load_block_catalog(CAP_BLOCKS_PATH)
314
  cap_description = cap_block_data["description"]
315
+ # cap_opcodes_functionalities = "\n".join([f" - Opcode: {block['op_code']}, functionality: {block['functionality']} example: standalone use: {block['example_standalone']}" for block in cap_block_data["blocks"]])
316
+ cap_opcodes_functionalities = "\n".join([
317
+ f" - Opcode: {block.get('op_code', 'N/A')}, functionality: {block.get('functionality', 'N/A')}, example: standalone use {block.get('example_standalone', 'N/A')}"
318
+ for block in cap_block_data.get("blocks", [])
319
+ ]) if isinstance(cap_block_data.get("blocks"), list) else " No blocks information available."
320
 
321
  reporter_block_data = _load_block_catalog(REPORTER_BLOCKS_PATH)
322
  reporter_description = reporter_block_data["description"]
323
+ # reporter_opcodes_functionalities = "\n".join([f" - Opcode: {block['op_code']}, functionality: {block['functionality']} example: standalone use: {block['example_standalone']}" for block in reporter_block_data["blocks"]])
324
+ reporter_opcodes_functionalities = "\n".join([
325
+ f" - Opcode: {block.get('op_code', 'N/A')}, functionality: {block.get('functionality', 'N/A')}, example: standalone use {block.get('example_standalone', 'N/A')}"
326
+ for block in reporter_block_data.get("blocks", [])
327
+ ]) if isinstance(reporter_block_data.get("blocks"), list) else " No blocks information available."
328
 
329
  stack_block_data = _load_block_catalog(STACK_BLOCKS_PATH)
330
  stack_description = stack_block_data["description"]
331
+ # stack_opcodes_functionalities = "\n".join([f" - Opcode: {block['op_code']}, functionality: {block['functionality']} example: standalone use: {block['example_standalone']}" for block in stack_block_data["blocks"]])
332
+ stack_opcodes_functionalities = "\n".join([
333
+ f" - Opcode: {block.get('op_code', 'N/A')}, functionality: {block.get('functionality', 'N/A')}, example: standalone use {block.get('example_standalone', 'N/A')}"
334
+ for block in stack_block_data.get("blocks", [])
335
+ ]) if isinstance(stack_block_data.get("blocks"), list) else " No blocks information available."
336
 
337
  # This makes ALL_SCRATCH_BLOCKS_CATALOG available globally
338
  ALL_SCRATCH_BLOCKS_CATALOG = _load_block_catalog(BLOCK_CATALOG_PATH)