gkdivya commited on
Commit
081b690
·
verified ·
1 Parent(s): d84a612

Update admin_patterns.py

Browse files
Files changed (1) hide show
  1. admin_patterns.py +36 -22
admin_patterns.py CHANGED
@@ -31,33 +31,47 @@ def _make_backup_of_patterns():
31
 
32
  def load_pattern_config(debug: bool = False):
33
  """
34
- Load patterns.json. If missing, create an empty skeleton.
 
 
 
35
  """
36
  global pattern_config
37
- if os.path.exists(PATTERN_FILE):
 
 
 
 
 
 
 
 
 
 
 
 
38
  try:
39
- with open(PATTERN_FILE, "r", encoding="utf-8") as f:
40
  cfg = json.load(f)
41
- pattern_config = cfg
 
 
 
 
 
 
 
 
42
  if debug:
43
- print("DEBUG: Successfully loaded patterns.json")
44
- print("DEBUG: global count:", len(cfg.get("global", [])))
45
- print("DEBUG: state keys:", list(cfg.get("states", {}).keys()))
46
- return cfg
47
- except Exception as e:
48
- print("ERROR: Failed to parse patterns.json:", e)
49
- pattern_config = {"global": [], "states": {}}
50
- return pattern_config
51
- else:
52
- # create empty file
53
- try:
54
- with open(PATTERN_FILE, "w", encoding="utf-8") as f:
55
- json.dump(DEFAULT_PATTERN_CONFIG, f, indent=2, ensure_ascii=False)
56
- print("DEBUG: Created empty patterns.json at", PATTERN_FILE)
57
- except Exception as e:
58
- print("ERROR: Could not create patterns.json:", e)
59
- pattern_config = DEFAULT_PATTERN_CONFIG.copy()
60
- return pattern_config
61
 
62
 
63
  def save_pattern_config(cfg: dict):
 
31
 
32
  def load_pattern_config(debug: bool = False):
33
  """
34
+ Simplified config loader for Hugging Face Spaces:
35
+ - Checks common locations in order
36
+ - Returns first valid config found
37
+ - Falls back to default if none found
38
  """
39
  global pattern_config
40
+
41
+ # Search paths in priority order
42
+ search_paths = [
43
+ PATTERN_FILE, # Same dir as module
44
+ "/app/patterns.json", # HF Spaces mount point
45
+ os.path.join(os.getcwd(), "patterns.json") # Current directory
46
+ ]
47
+
48
+ # Try each path
49
+ for path in search_paths:
50
+ if not os.path.exists(path):
51
+ continue
52
+
53
  try:
54
+ with open(path, "r", encoding="utf-8") as f:
55
  cfg = json.load(f)
56
+
57
+ # Validate it has expected structure
58
+ if isinstance(cfg, dict) and ("global" in cfg or "states" in cfg):
59
+ pattern_config = cfg
60
+ if debug:
61
+ print(f"Loaded config from: {path}")
62
+ return cfg
63
+
64
+ except (json.JSONDecodeError, IOError) as e:
65
  if debug:
66
+ print(f"Failed to load {path}: {e}")
67
+ continue
68
+
69
+ # No valid config found - use default
70
+ pattern_config = DEFAULT_PATTERN_CONFIG.copy()
71
+ if debug:
72
+ print("No valid config found, using defaults")
73
+
74
+ return pattern_config
 
 
 
 
 
 
 
 
 
75
 
76
 
77
  def save_pattern_config(cfg: dict):