Update app.py
Browse files
app.py
CHANGED
|
@@ -489,6 +489,56 @@ def clean_base64_for_model(raw_b64):
|
|
| 489 |
# 6. Return with the correct data URI prefix
|
| 490 |
return f"data:image/png;base64,{clean_b64}"
|
| 491 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 492 |
|
| 493 |
# Node 1: Logic updating if any issue here
|
| 494 |
def pseudo_generator_node(state: GameState):
|
|
@@ -1636,9 +1686,10 @@ def overall_block_builder_node_2(state: dict):
|
|
| 1636 |
for plan_entry in sprite_actions_data.get("plans", []):
|
| 1637 |
logic_sequence = str(plan_entry["logic"])
|
| 1638 |
opcode_counts = plan_entry.get("opcode_counts", {})
|
| 1639 |
-
|
|
|
|
| 1640 |
try:
|
| 1641 |
-
generated_blocks = block_builder(opcode_counts,
|
| 1642 |
|
| 1643 |
# Ensure generated_blocks is a dictionary
|
| 1644 |
if not isinstance(generated_blocks, dict):
|
|
|
|
| 489 |
# 6. Return with the correct data URI prefix
|
| 490 |
return f"data:image/png;base64,{clean_b64}"
|
| 491 |
|
| 492 |
+
def format_scratch_pseudo_code(code_string):
|
| 493 |
+
"""
|
| 494 |
+
Parses and formats Scratch pseudo-code with correct indentation,
|
| 495 |
+
specifically handling if/else/end structures correctly.
|
| 496 |
+
|
| 497 |
+
Args:
|
| 498 |
+
code_string (str): A string containing Scratch pseudo-code with
|
| 499 |
+
potentially inconsistent indentation.
|
| 500 |
+
|
| 501 |
+
Returns:
|
| 502 |
+
str: The correctly formatted and indented pseudo-code string.
|
| 503 |
+
"""
|
| 504 |
+
lines = code_string.strip().split('\n')
|
| 505 |
+
formatted_lines = []
|
| 506 |
+
indent_level = 0
|
| 507 |
+
|
| 508 |
+
# Keywords that increase indentation for the NEXT line
|
| 509 |
+
indent_keywords = ['when', 'forever', 'if', 'repeat', 'else']
|
| 510 |
+
|
| 511 |
+
# Keywords that decrease indentation for the CURRENT line
|
| 512 |
+
unindent_keywords = ['end', 'else']
|
| 513 |
+
|
| 514 |
+
for line in lines:
|
| 515 |
+
stripped_line = line.strip()
|
| 516 |
+
if not stripped_line:
|
| 517 |
+
continue
|
| 518 |
+
|
| 519 |
+
# Check for keywords that should un-indent the current line
|
| 520 |
+
if any(keyword in stripped_line for keyword in unindent_keywords):
|
| 521 |
+
# Special case for 'else': it should align with its 'if'
|
| 522 |
+
if 'else' in stripped_line:
|
| 523 |
+
# Decrease indentation for 'else' and its following lines
|
| 524 |
+
indentation = ' ' * (indent_level -1)
|
| 525 |
+
formatted_lines.append(indentation + stripped_line)
|
| 526 |
+
continue
|
| 527 |
+
|
| 528 |
+
# For 'end', decrease the level before formatting
|
| 529 |
+
indent_level = max(0, indent_level - 1)
|
| 530 |
+
|
| 531 |
+
indentation = ' ' * indent_level
|
| 532 |
+
formatted_lines.append(indentation + stripped_line)
|
| 533 |
+
|
| 534 |
+
# Check for keywords that should indent the next line
|
| 535 |
+
if any(keyword in stripped_line for keyword in indent_keywords):
|
| 536 |
+
# 'else' both un-indents and indents, so the level remains the same for the next block
|
| 537 |
+
if 'else' not in stripped_line:
|
| 538 |
+
indent_level += 1
|
| 539 |
+
|
| 540 |
+
return '\n'.join(formatted_lines)
|
| 541 |
+
|
| 542 |
|
| 543 |
# Node 1: Logic updating if any issue here
|
| 544 |
def pseudo_generator_node(state: GameState):
|
|
|
|
| 1686 |
for plan_entry in sprite_actions_data.get("plans", []):
|
| 1687 |
logic_sequence = str(plan_entry["logic"])
|
| 1688 |
opcode_counts = plan_entry.get("opcode_counts", {})
|
| 1689 |
+
refined_indent_logic = format_scratch_pseudo_code(logic_sequence)
|
| 1690 |
+
print(f"\n--------------------------- refined indent logic: {refined_indent_logic}-------------------------------\n")
|
| 1691 |
try:
|
| 1692 |
+
generated_blocks = block_builder(opcode_counts, refined_indent_logic)
|
| 1693 |
|
| 1694 |
# Ensure generated_blocks is a dictionary
|
| 1695 |
if not isinstance(generated_blocks, dict):
|