Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -309,13 +309,23 @@ def format_python_code(code):
|
|
| 309 |
('->', '.'), # Convert arrow operator to dot
|
| 310 |
('::', '.'), # Convert scope resolution to dot
|
| 311 |
|
| 312 |
-
# Braces and brackets
|
| 313 |
-
('{', '
|
| 314 |
('}', ''), # Remove closing braces
|
| 315 |
|
| 316 |
-
# Semicolons (
|
| 317 |
('; ', '\n'), # Convert semicolons to newlines
|
| 318 |
(';', '\n'), # Convert semicolons to newlines
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 319 |
]
|
| 320 |
|
| 321 |
for old, new in corrections:
|
|
@@ -362,6 +372,14 @@ def format_python_code(code):
|
|
| 362 |
# Add colons where missing for control structures
|
| 363 |
formatted_code = re.sub(r'\b(if|for|while|def|class|try|except|with)\s+([^:]+)(?!\s*:)', r'\1 \2:', formatted_code)
|
| 364 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 365 |
return formatted_code
|
| 366 |
|
| 367 |
except Exception as e:
|
|
@@ -383,7 +401,7 @@ def generate_code_from_pseudo(pseudo_code, max_length, temperature, top_k, top_p
|
|
| 383 |
start_time = time.time()
|
| 384 |
|
| 385 |
# Format input with Python-specific instructions
|
| 386 |
-
prompt = f"<PSEUDO> {pseudo_code.strip()} <SEP> Generate Python code with
|
| 387 |
|
| 388 |
# Tokenize with error handling
|
| 389 |
device = next(loaded_model.parameters()).device
|
|
@@ -608,6 +626,11 @@ def generate_code_from_pseudo(pseudo_code, max_length, temperature, top_k, top_p
|
|
| 608 |
# Format as proper Python code
|
| 609 |
code = format_python_code(code)
|
| 610 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 611 |
# Ensure we have some content
|
| 612 |
if not code or code.isspace():
|
| 613 |
code = f"# Generated sequence {i+1} was empty after cleaning"
|
|
|
|
| 309 |
('->', '.'), # Convert arrow operator to dot
|
| 310 |
('::', '.'), # Convert scope resolution to dot
|
| 311 |
|
| 312 |
+
# Braces and brackets - convert to Python indentation
|
| 313 |
+
('{', ':\n'), # Convert opening braces to colons with newline
|
| 314 |
('}', ''), # Remove closing braces
|
| 315 |
|
| 316 |
+
# Semicolons (convert to newlines)
|
| 317 |
('; ', '\n'), # Convert semicolons to newlines
|
| 318 |
(';', '\n'), # Convert semicolons to newlines
|
| 319 |
+
|
| 320 |
+
# Ternary operators (C++ style to Python)
|
| 321 |
+
(' ? ', ' if '), # Convert ternary ? to if
|
| 322 |
+
(' : ', ' else '), # Convert ternary : to else
|
| 323 |
+
|
| 324 |
+
# Comparison operators
|
| 325 |
+
('< SEP', '<'), # Fix common typo
|
| 326 |
+
('< SEp', '<'), # Fix common typo
|
| 327 |
+
('<SEP', '<'), # Fix common typo
|
| 328 |
+
('<SEp', '<'), # Fix common typo
|
| 329 |
]
|
| 330 |
|
| 331 |
for old, new in corrections:
|
|
|
|
| 372 |
# Add colons where missing for control structures
|
| 373 |
formatted_code = re.sub(r'\b(if|for|while|def|class|try|except|with)\s+([^:]+)(?!\s*:)', r'\1 \2:', formatted_code)
|
| 374 |
|
| 375 |
+
# Fix ternary operators that became malformed
|
| 376 |
+
# Convert "return a if condition else b" to proper Python ternary
|
| 377 |
+
formatted_code = re.sub(r'return\s+(.+?)\s+if\s+(.+?)\s+else\s+(.+)', r'return \1 if \2 else \3', formatted_code)
|
| 378 |
+
|
| 379 |
+
# Ensure functions have proper structure
|
| 380 |
+
# If a line starts with "def" and doesn't end with ":", add it
|
| 381 |
+
formatted_code = re.sub(r'(def\s+\w+\([^)]*)\s*$', r'\1:', formatted_code, flags=re.MULTILINE)
|
| 382 |
+
|
| 383 |
return formatted_code
|
| 384 |
|
| 385 |
except Exception as e:
|
|
|
|
| 401 |
start_time = time.time()
|
| 402 |
|
| 403 |
# Format input with Python-specific instructions
|
| 404 |
+
prompt = f"<PSEUDO> {pseudo_code.strip()} <SEP> Generate proper Python code with correct syntax, indentation, and separate lines. Use def for functions, if/else for conditionals, for/while for loops, and proper Python syntax only. No C++ or other languages. <CODE>"
|
| 405 |
|
| 406 |
# Tokenize with error handling
|
| 407 |
device = next(loaded_model.parameters()).device
|
|
|
|
| 626 |
# Format as proper Python code
|
| 627 |
code = format_python_code(code)
|
| 628 |
|
| 629 |
+
# For debugging: include raw generated code
|
| 630 |
+
raw_code = generated.strip()
|
| 631 |
+
if not code.startswith('#'):
|
| 632 |
+
code = f"# Raw generated code:\n# {raw_code}\n\n# Formatted Python code:\n{code}"
|
| 633 |
+
|
| 634 |
# Ensure we have some content
|
| 635 |
if not code or code.isspace():
|
| 636 |
code = f"# Generated sequence {i+1} was empty after cleaning"
|