prthm11 commited on
Commit
b54eb80
·
verified ·
1 Parent(s): 10cf457

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -3
app.py CHANGED
@@ -435,21 +435,35 @@ def extract_json_from_llm_response(raw_response: str) -> dict:
435
  logger.error("Sanitized JSON still invalid:\n%s", json_string)
436
  raise
437
 
438
- def clean_base64_for_model(raw_b64: str) -> str:
439
  if not raw_b64:
440
  return ""
441
- # Remove prefix if exists
 
 
 
 
 
 
 
 
 
 
 
 
442
  clean_b64 = re.sub(r"^data:image\/[a-zA-Z]+;base64,", "", raw_b64)
443
- # Remove spaces/newlines
444
  clean_b64 = clean_b64.replace("\n", "").replace("\r", "").strip()
 
445
  # Validate
446
  try:
447
  base64.b64decode(clean_b64)
448
  except Exception as e:
449
  logger.error(f"Invalid Base64 passed to model: {e}")
450
  raise
 
451
  return f"data:image/png;base64,{clean_b64}"
452
 
 
453
  # Node 1: Logic updating if any issue here
454
  def pseudo_generator_node(state: GameState):
455
  logger.info("--- Running plan_logic_aligner_node ---")
 
435
  logger.error("Sanitized JSON still invalid:\n%s", json_string)
436
  raise
437
 
438
+ def clean_base64_for_model(raw_b64):
439
  if not raw_b64:
440
  return ""
441
+
442
+ # If it's a list, take the first image (or join if multiple are expected)
443
+ if isinstance(raw_b64, list):
444
+ if len(raw_b64) == 0:
445
+ return ""
446
+ raw_b64 = raw_b64[0] # take first element
447
+ # Or: raw_b64 = raw_b64[-1] # take last element if that’s your logic
448
+
449
+ # Ensure string
450
+ if not isinstance(raw_b64, str):
451
+ raise TypeError(f"Expected base64 string, got {type(raw_b64)}")
452
+
453
+ # Remove prefix
454
  clean_b64 = re.sub(r"^data:image\/[a-zA-Z]+;base64,", "", raw_b64)
 
455
  clean_b64 = clean_b64.replace("\n", "").replace("\r", "").strip()
456
+
457
  # Validate
458
  try:
459
  base64.b64decode(clean_b64)
460
  except Exception as e:
461
  logger.error(f"Invalid Base64 passed to model: {e}")
462
  raise
463
+
464
  return f"data:image/png;base64,{clean_b64}"
465
 
466
+
467
  # Node 1: Logic updating if any issue here
468
  def pseudo_generator_node(state: GameState):
469
  logger.info("--- Running plan_logic_aligner_node ---")