akhaliq HF Staff commited on
Commit
e747dd7
·
1 Parent(s): a35ce25
Files changed (2) hide show
  1. backend_api.py +30 -3
  2. backend_parsers.py +12 -9
backend_api.py CHANGED
@@ -520,10 +520,37 @@ def cleanup_generated_code(code: str, language: str) -> str:
520
  try:
521
  original_code = code
522
 
523
- # Special handling for transformers.js - preserve === markers
524
  if language == "transformers.js":
525
- # Don't clean transformers.js code - it needs the === markers
526
- return code
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
527
 
528
  # Special handling for ComfyUI JSON
529
  if language == "comfyui":
 
520
  try:
521
  original_code = code
522
 
523
+ # Special handling for transformers.js - extract only the === sections
524
  if language == "transformers.js":
525
+ # Find the first === marker
526
+ first_marker = code.find('===')
527
+ if first_marker == -1:
528
+ # No markers found, return as-is
529
+ return code
530
+
531
+ # Find the last code section end (after the last ===)
532
+ # Look for the last === marker
533
+ last_marker_start = code.rfind('===')
534
+ if last_marker_start == -1:
535
+ return code
536
+
537
+ # Find the end of the last code section
538
+ # This is typically followed by explanatory text starting with --- or multiple newlines
539
+ code_after_last_marker = code[last_marker_start:]
540
+
541
+ # Find where the actual code section ends (look for --- or excessive explanatory text)
542
+ end_markers = ['---', '\n\n\nThis ', '\n\n\n✨', '\n\n\n🎨', '\n\n\n🚀']
543
+ code_end = len(code)
544
+
545
+ for end_marker in end_markers:
546
+ pos = code.find(end_marker, last_marker_start)
547
+ if pos != -1 and pos < code_end:
548
+ code_end = pos
549
+
550
+ # Extract from first === to end of code
551
+ cleaned_code = code[first_marker:code_end].strip()
552
+
553
+ return cleaned_code
554
 
555
  # Special handling for ComfyUI JSON
556
  if language == "comfyui":
backend_parsers.py CHANGED
@@ -18,14 +18,12 @@ def parse_transformers_js_output(code: str) -> Dict[str, str]:
18
  print(f"[Parser] Received code length: {len(code)} characters")
19
  print(f"[Parser] First 200 chars: {code[:200]}")
20
 
21
- # Auto-fix: If code doesn't start with === index.html ===, add it
22
  code_stripped = code.strip()
23
- if not code_stripped.startswith('==='):
24
- print("[Parser] Auto-fixing: Adding missing === index.html === marker")
25
- code = '=== index.html ===\n' + code
26
- code_stripped = code.strip()
27
  else:
28
- print("[Parser] Code starts with === marker, proceeding normally")
29
 
30
  # Check if code starts with HTML instead of markers (common LLM mistake)
31
  if code_stripped.startswith('<!DOCTYPE') or code_stripped.startswith('<html'):
@@ -131,9 +129,14 @@ def parse_transformers_js_output(code: str) -> Dict[str, str]:
131
  if not (files['index.html'] and files['index.js'] and files['style.css']):
132
  # Use regex to extract sections - match === markers with optional whitespace and newlines
133
  # Fixed lookahead to allow any whitespace (not just \n) before next === marker
134
- html_fallback = re.search(r'===\s*index\.html\s*===\s*[\r\n]*([\s\S]+?)(?=\s*===\s*index\.js\s*===|$)', code, re.IGNORECASE)
135
- js_fallback = re.search(r'===\s*index\.js\s*===\s*[\r\n]*([\s\S]+?)(?=\s*===\s*style\.css\s*===|$)', code, re.IGNORECASE)
136
- css_fallback = re.search(r'===\s*style\.css\s*===\s*[\r\n]*([\s\S]+?)$', code, re.IGNORECASE)
 
 
 
 
 
137
 
138
  print(f"[Parser] Fallback extraction - HTML found: {bool(html_fallback)}, JS found: {bool(js_fallback)}, CSS found: {bool(css_fallback)}")
139
 
 
18
  print(f"[Parser] Received code length: {len(code)} characters")
19
  print(f"[Parser] First 200 chars: {code[:200]}")
20
 
21
+ # Check if code has === markers
22
  code_stripped = code.strip()
23
+ if '===' in code_stripped:
24
+ print("[Parser] Code contains === markers, proceeding with parsing")
 
 
25
  else:
26
+ print("[Parser] WARNING: No === markers found in code")
27
 
28
  # Check if code starts with HTML instead of markers (common LLM mistake)
29
  if code_stripped.startswith('<!DOCTYPE') or code_stripped.startswith('<html'):
 
129
  if not (files['index.html'] and files['index.js'] and files['style.css']):
130
  # Use regex to extract sections - match === markers with optional whitespace and newlines
131
  # Fixed lookahead to allow any whitespace (not just \n) before next === marker
132
+ # Also support alternative names: styles.css or style.css, app.js or index.js
133
+ html_fallback = re.search(r'===\s*index\.html\s*===\s*[\r\n]*([\s\S]+?)(?=\s*===|$)', code, re.IGNORECASE)
134
+
135
+ # Try both index.js and app.js
136
+ js_fallback = re.search(r'===\s*(?:index\.js|app\.js)\s*===\s*[\r\n]*([\s\S]+?)(?=\s*===|$)', code, re.IGNORECASE)
137
+
138
+ # Try both style.css and styles.css
139
+ css_fallback = re.search(r'===\s*(?:style\.css|styles\.css)\s*===\s*[\r\n]*([\s\S]+?)$', code, re.IGNORECASE)
140
 
141
  print(f"[Parser] Fallback extraction - HTML found: {bool(html_fallback)}, JS found: {bool(js_fallback)}, CSS found: {bool(css_fallback)}")
142