Fix transformers.js parser: Support both === and markdown formats
Browse filesIssue: LLM was generating code with markdown blocks (html, javascript, css)
but parser only looked for === filename === format
Fixes:
1. Updated TRANSFORMERS_JS_SYSTEM_PROMPT to specify === format explicitly
- Added clear examples showing === index.html ===, === index.js ===, === style.css ===
- Explicitly stated NOT to use markdown code blocks
2. Enhanced parse_transformers_js_output() with fallback logic:
- Primary: Parse === filename === sections
- Fallback: Parse markdown code blocks (html, javascript, css)
- Last resort: Try single HTML file extraction
- Removes placeholder comments from extracted content
This ensures backward compatibility with already-generated code while
guiding future generations to use the correct format.
Files changed (2)
- backend_deploy.py+51-5
- backend_prompts.py+11-7
39 unmodified lines404142434445467 unmodified lines54555657585960267 unmodified lines32832933033133233333433533639 unmodified lines"""Parse transformers.js output into separate files"""files = {}# Pattern to match file sectionspattern = r'===\s*(\S+\.(?:html|js|css))\s*===\s*(.*?)(?====|$)'matches = re.finditer(pattern, code, re.DOTALL | re.IGNORECASE)7 unmodified linesfiles[filename] = content# If no files were parsed, try to extract as single HTML fileif not files:html_content = parse_html_code(code)if html_content:267 unmodified linesfiles = parse_transformers_js_output(code)print(f"[Deploy] Parsed transformers.js files: {list(files.keys())}")# Validate all three files are present (match original deploy.py check)if not files['index.html'] or not files['index.js'] or not files['style.css']:return False, "Error: Could not parse transformers.js output. Please regenerate the code.", None# Write transformers.js filesfor filename, content in files.items():39 unmodified lines404142434445467 unmodified lines54555657585960616263646566676869707172737475767778798081828384858687267 unmodified lines35535635735835936036136236336436536636736836937037137237337437537637737837938038138239 unmodified lines"""Parse transformers.js output into separate files"""files = {}# First try: Pattern to match === filename === sectionspattern = r'===\s*(\S+\.(?:html|js|css))\s*===\s*(.*?)(?====|$)'matches = re.finditer(pattern, code, re.DOTALL | re.IGNORECASE)7 unmodified linesfiles[filename] = content# Fallback: Try to extract from markdown code blocks if === format not foundif not files:print("[Deploy] === format not found, trying markdown code blocks fallback")# Try to find ```html, ```javascript, ```css blockshtml_match = re.search(r'```html\s*(.*?)```', code, re.DOTALL | re.IGNORECASE)js_match = re.search(r'```javascript\s*(.*?)```', code, re.DOTALL | re.IGNORECASE)css_match = re.search(r'```css\s*(.*?)```', code, re.DOTALL | re.IGNORECASE)if html_match:content = html_match.group(1).strip()# Remove comment lines like "<!-- index.html content here -->"content = re.sub(r'<!--\s*index\.html.*?-->\s*', '', content, flags=re.IGNORECASE)files['index.html'] = contentif js_match:content = js_match.group(1).strip()# Remove comment lines like "// index.js content here"content = re.sub(r'//\s*index\.js.*?\n', '', content, flags=re.IGNORECASE)files['index.js'] = contentif css_match:content = css_match.group(1).strip()# Remove comment lines like "/* style.css content here */"content = re.sub(r'/\*\s*style\.css.*?\*/', '', content, flags=re.IGNORECASE)files['style.css'] = content# Last resort: try to extract as single HTML fileif not files:html_content = parse_html_code(code)if html_content:267 unmodified linesfiles = parse_transformers_js_output(code)print(f"[Deploy] Parsed transformers.js files: {list(files.keys())}")# Validate all three files are presentmissing_files = []if not files.get('index.html'):missing_files.append('index.html')if not files.get('index.js'):missing_files.append('index.js')if not files.get('style.css'):missing_files.append('style.css')if missing_files:error_msg = f"Missing required files: {', '.join(missing_files)}. "error_msg += f"Found only: {', '.join(files.keys()) if files else 'no files'}. "error_msg += "Transformers.js apps require all three files with === filename === markers. Please regenerate the code."print(f"[Deploy] {error_msg}")return False, error_msg, None# Validate files have contentempty_files = [name for name, content in files.items() if not content or not content.strip()]if empty_files:error_msg = f"Empty files detected: {', '.join(empty_files)}. Please regenerate the code with actual content."print(f"[Deploy] {error_msg}")return False, error_msg, None# Write transformers.js filesfor filename, content in files.items():
25 unmodified lines2627282930313233343536373839404142434425 unmodified lines- A template README.md is automatically provided and will be overridden by the deployment system- Generating a README.md will break the deployment processIMPORTANT: You MUST output ALL THREE files in the following format:```html<!-- index.html content here -->``````javascript// index.js content here``````css/* style.css content here */```Requirements:1. Create a modern, responsive web application using transformers.js25 unmodified lines262728293031323334353637383940414243444546474825 unmodified lines- A template README.md is automatically provided and will be overridden by the deployment system- Generating a README.md will break the deployment process**🚨 CRITICAL: Required Output Format**You MUST output ALL THREE files using this EXACT format with === markers:=== index.html ===<!DOCTYPE html><html lang="en"><!-- index.html content here --></html>=== index.js ===// index.js content here=== style.css ===/* style.css content here */DO NOT use markdown code blocks (```html, ```javascript, ```css).ONLY use the === filename === format shown above.Requirements:1. Create a modern, responsive web application using transformers.js
backend_deploy.py+51-5
backend_prompts.py+11-7