update
Browse filesFiles changed (2)
- backend_deploy.py+38-21
- backend_parsers.py+11-3
287 unmodified lines288289290291292293294295296297298299300301302303304305306307308309310311312313314315316287 unmodified linesreturn json_contentdef parse_transformers_js_output(code: str) -> Dict[str, str]:"""Parse transformers.js output into separate files (index.html, index.js, style.css)Uses comprehensive parsing patterns to handle various LLM output formats."""files = {'index.html': '','index.js': '','style.css': ''}# Multiple patterns to match the three code blocks with different variationshtml_patterns = [r'```html\s*\n([\s\S]*?)(?:```|\Z)',r'```htm\s*\n([\s\S]*?)(?:```|\Z)',r'```\s*(?:index\.html|html)\s*\n([\s\S]*?)(?:```|\Z)']js_patterns = [r'```javascript\s*\n([\s\S]*?)(?:```|\Z)',r'```js\s*\n([\s\S]*?)(?:```|\Z)',r'```\s*(?:index\.js|javascript|js)\s*\n([\s\S]*?)(?:```|\Z)']css_patterns = [r'```css\s*\n([\s\S]*?)(?:```|\Z)',287 unmodified lines288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333287 unmodified linesreturn json_content# Note: parse_transformers_js_output, parse_python_requirements, strip_tool_call_markers,# remove_code_block, extract_import_statements, generate_requirements_txt_with_llm,# and parse_multi_file_python_output are now imported from backend_parsers.pydef is_streamlit_code(code: str) -> bool:"""Check if code is Streamlit"""return 'import streamlit' in code or 'streamlit.run' in codedef is_gradio_code(code: str) -> bool:"""Check if code is Gradio"""return 'import gradio' in code or 'gr.' in codedef detect_sdk_from_code(code: str, language: str) -> str:"""Detect the appropriate SDK from code and language"""if language == "html":return "static"elif language == "transformers.js":return "static"elif language == "comfyui":return "static"elif language == "react":return "docker"elif language == "streamlit" or is_streamlit_code(code):return "docker"elif language == "gradio" or is_gradio_code(code):return "gradio"else:return "gradio" # Defaultdef add_anycoder_tag_to_readme(api, repo_id: str, app_port: Optional[int] = None) -> None:"""Download existing README, add anycoder tag and app_port if needed, and upload back.Preserves all existing README content and frontmatter.Args:api: HuggingFace API clientcss_patterns = [r'```css\s*\n([\s\S]*?)(?:```|\Z)',
14 unmodified lines15161718192021222324252698 unmodified lines12512612712812913013113213314 unmodified linesUses comprehensive parsing patterns to handle various LLM output formats.Updated to use transformers.js v3.8.0 CDN."""# Auto-fix: If code doesn't start with === index.html ===, add itcode_stripped = code.strip()if not code_stripped.startswith('==='):print("[Parser] Auto-fixing: Adding missing === index.html === marker")code = '=== index.html ===\n' + codecode_stripped = code.strip()# Check if code starts with HTML instead of markers (common LLM mistake)if code_stripped.startswith('<!DOCTYPE') or code_stripped.startswith('<html'):98 unmodified lines# Fallback: support === index.html === format if any file is missingif not (files['index.html'] and files['index.js'] and files['style.css']):# Use regex to extract sections - match === markers with optional whitespace and newlineshtml_fallback = re.search(r'===\s*index\.html\s*===\s*[\r\n]+([\s\S]+?)(?=\n===|$)', code, re.IGNORECASE)js_fallback = re.search(r'===\s*index\.js\s*===\s*[\r\n]+([\s\S]+?)(?=\n===|$)', code, re.IGNORECASE)css_fallback = re.search(r'===\s*style\.css\s*===\s*[\r\n]+([\s\S]+?)(?=\n===|$)', code, re.IGNORECASE)if html_fallback:content = html_fallback.group(1).strip()14 unmodified lines151617181920212223242526272829303198 unmodified lines13013113213313413513613713813914014114 unmodified linesUses comprehensive parsing patterns to handle various LLM output formats.Updated to use transformers.js v3.8.0 CDN."""print(f"[Parser] Received code length: {len(code)} characters")print(f"[Parser] First 200 chars: {code[:200]}")# Auto-fix: If code doesn't start with === index.html ===, add itcode_stripped = code.strip()if not code_stripped.startswith('==='):print("[Parser] Auto-fixing: Adding missing === index.html === marker")code = '=== index.html ===\n' + codecode_stripped = code.strip()else:print("[Parser] Code starts with === marker, proceeding normally")# Check if code starts with HTML instead of markers (common LLM mistake)if code_stripped.startswith('<!DOCTYPE') or code_stripped.startswith('<html'):98 unmodified lines# Fallback: support === index.html === format if any file is missingif not (files['index.html'] and files['index.js'] and files['style.css']):# Use regex to extract sections - match === markers with optional whitespace and newlines# Made [\r\n]+ optional with * instead of + to handle cases where content follows immediatelyhtml_fallback = re.search(r'===\s*index\.html\s*===\s*[\r\n]*([\s\S]+?)(?=\n===|$)', code, re.IGNORECASE)js_fallback = re.search(r'===\s*index\.js\s*===\s*[\r\n]*([\s\S]+?)(?=\n===|$)', code, re.IGNORECASE)css_fallback = re.search(r'===\s*style\.css\s*===\s*[\r\n]*([\s\S]+?)(?=\n===|$)', code, re.IGNORECASE)print(f"[Parser] Fallback extraction - HTML found: {bool(html_fallback)}, JS found: {bool(js_fallback)}, CSS found: {bool(css_fallback)}")if html_fallback:content = html_fallback.group(1).strip()
backend_deploy.py+38-21
backend_parsers.py+11-3