WebashalarForML commited on
Commit
6c132b1
·
verified ·
1 Parent(s): 08338b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -15
app.py CHANGED
@@ -14,6 +14,7 @@ import uuid
14
  from langgraph.graph import StateGraph, END
15
  import logging
16
  from typing import Dict, TypedDict, Optional, Any, List
 
17
 
18
  # --- Configure logging ---
19
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
@@ -26,9 +27,18 @@ BLOCKS_FOLDER = "blocks/"
26
  GEN_PROJECT_FOLDER = "generated_projects/"
27
  STATIC_FOLDER = "static/"
28
 
29
- os.makedirs(BLOCKS_FOLDER, exist_ok=True)
30
- os.makedirs(GEN_PROJECT_FOLDER, exist_ok=True)
31
- os.makedirs(STATIC_FOLDER, exist_ok=True)
 
 
 
 
 
 
 
 
 
32
 
33
  # --- LLM / Vision model setup ---
34
  os.environ["GROQ_API_KEY"] = os.getenv("GROQ_API_KEY_2", "default_key_or_placeholder")
@@ -237,23 +247,28 @@ def update_project_with_sprite_positions(project_json: dict, sprite_positions: d
237
  return updated_project
238
 
239
  # Helper function to load the block catalog from a JSON file
240
- def _load_block_catalog(file_path: str) -> Dict:
241
- """Loads the Scratch block catalog from a specified JSON file."""
 
 
 
 
 
242
  try:
243
- with open(file_path, 'r') as f:
244
- catalog = json.load(f)
245
- logger.info(f"Successfully loaded block catalog from {file_path}")
246
  return catalog
 
247
  except FileNotFoundError:
248
- logger.error(f"Error: Block catalog file not found at {file_path}")
249
- # Return an empty dict or raise an error, depending on desired behavior
250
- return {}
251
  except json.JSONDecodeError as e:
252
- logger.error(f"Error decoding JSON from {file_path}: {e}")
253
- return {}
254
  except Exception as e:
255
- logger.error(f"An unexpected error occurred while loading {file_path}: {e}")
256
- return {}
 
 
257
 
258
  # --- Global variable for the block catalog ---
259
  ALL_SCRATCH_BLOCKS_CATALOG = {}
 
14
  from langgraph.graph import StateGraph, END
15
  import logging
16
  from typing import Dict, TypedDict, Optional, Any, List
17
+ from pathlib import Path
18
 
19
  # --- Configure logging ---
20
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
 
27
  GEN_PROJECT_FOLDER = "generated_projects/"
28
  STATIC_FOLDER = "static/"
29
 
30
+ BASE_DIR = Path(__file__).parent
31
+ BLOCKS_DIR = BASE_DIR / "blocks"
32
+ STATIC_DIR = BASE_DIR / "static"
33
+ GEN_PROJECT_DIR = BASE_DIR / "generated_projects"
34
+ ASSET_DIR = STATIC_DIR / "assets"
35
+ BACKDROP_DIR = ASSET_DIR / "backdrops"
36
+ SPRITE_DIR = ASSET_DIR / "sprites"
37
+ SOUND_DIR = ASSET_DIR / "sounds"
38
+
39
+ # Create them if they're missing:
40
+ for d in (BLOCKS_DIR, STATIC_DIR, GEN_PROJECT_DIR, ASSET_DIR, BACKDROP_DIR, SPRITE_DIR, SOUND_DIR):
41
+ d.mkdir(parents=True, exist_ok=True)
42
 
43
  # --- LLM / Vision model setup ---
44
  os.environ["GROQ_API_KEY"] = os.getenv("GROQ_API_KEY_2", "default_key_or_placeholder")
 
247
  return updated_project
248
 
249
  # Helper function to load the block catalog from a JSON file
250
+ def _load_block_catalog_safe(block_type: str) -> Dict:
251
+ """
252
+ Loads the Scratch block catalog named '{block_type}_blocks.json'
253
+ from the <project_root>/blocks/ folder. Returns {} on any error.
254
+ """
255
+ catalog_path = BLOCKS_DIR / f"{block_type}_blocks.json"
256
+
257
  try:
258
+ text = catalog_path.read_text() # will raise FileNotFoundError if missing
259
+ catalog = json.loads(text) # will raise JSONDecodeError if malformed
260
+ logger.info(f"Successfully loaded block catalog from {catalog_path}")
261
  return catalog
262
+
263
  except FileNotFoundError:
264
+ logger.error(f"Error: Block catalog file not found at {catalog_path}")
 
 
265
  except json.JSONDecodeError as e:
266
+ logger.error(f"Error decoding JSON from {catalog_path}: {e}")
 
267
  except Exception as e:
268
+ logger.error(f"Unexpected error loading {catalog_path}: {e}")
269
+
270
+ # In all error cases, return an empty dict so the rest of your app can continue
271
+ return {}
272
 
273
  # --- Global variable for the block catalog ---
274
  ALL_SCRATCH_BLOCKS_CATALOG = {}