akhaliq HF Staff commited on
Commit
4b79bb6
·
1 Parent(s): c29eb09
Files changed (2) hide show
  1. backend_deploy.py +38 -21
  2. backend_parsers.py +11 -3
backend_deploy.py CHANGED
@@ -288,29 +288,46 @@ def prettify_comfyui_json_for_html(json_content: str) -> str:
288
  return json_content
289
 
290
 
291
- def parse_transformers_js_output(code: str) -> Dict[str, str]:
292
- """Parse transformers.js output into separate files (index.html, index.js, style.css)
293
-
294
- Uses comprehensive parsing patterns to handle various LLM output formats.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
295
  """
296
- files = {
297
- 'index.html': '',
298
- 'index.js': '',
299
- 'style.css': ''
300
- }
301
-
302
- # Multiple patterns to match the three code blocks with different variations
303
- html_patterns = [
304
- r'```html\s*\n([\s\S]*?)(?:```|\Z)',
305
- r'```htm\s*\n([\s\S]*?)(?:```|\Z)',
306
- r'```\s*(?:index\.html|html)\s*\n([\s\S]*?)(?:```|\Z)'
307
- ]
308
 
309
- js_patterns = [
310
- r'```javascript\s*\n([\s\S]*?)(?:```|\Z)',
311
- r'```js\s*\n([\s\S]*?)(?:```|\Z)',
312
- r'```\s*(?:index\.js|javascript|js)\s*\n([\s\S]*?)(?:```|\Z)'
313
- ]
314
 
315
  css_patterns = [
316
  r'```css\s*\n([\s\S]*?)(?:```|\Z)',
 
288
  return json_content
289
 
290
 
291
+ # Note: parse_transformers_js_output, parse_python_requirements, strip_tool_call_markers,
292
+ # remove_code_block, extract_import_statements, generate_requirements_txt_with_llm,
293
+ # and parse_multi_file_python_output are now imported from backend_parsers.py
294
+
295
+
296
+ def is_streamlit_code(code: str) -> bool:
297
+ """Check if code is Streamlit"""
298
+ return 'import streamlit' in code or 'streamlit.run' in code
299
+
300
+
301
+ def is_gradio_code(code: str) -> bool:
302
+ """Check if code is Gradio"""
303
+ return 'import gradio' in code or 'gr.' in code
304
+
305
+
306
+ def detect_sdk_from_code(code: str, language: str) -> str:
307
+ """Detect the appropriate SDK from code and language"""
308
+ if language == "html":
309
+ return "static"
310
+ elif language == "transformers.js":
311
+ return "static"
312
+ elif language == "comfyui":
313
+ return "static"
314
+ elif language == "react":
315
+ return "docker"
316
+ elif language == "streamlit" or is_streamlit_code(code):
317
+ return "docker"
318
+ elif language == "gradio" or is_gradio_code(code):
319
+ return "gradio"
320
+ else:
321
+ return "gradio" # Default
322
+
323
+
324
+ def add_anycoder_tag_to_readme(api, repo_id: str, app_port: Optional[int] = None) -> None:
325
  """
326
+ Download existing README, add anycoder tag and app_port if needed, and upload back.
327
+ Preserves all existing README content and frontmatter.
 
 
 
 
 
 
 
 
 
 
328
 
329
+ Args:
330
+ api: HuggingFace API client
 
 
 
331
 
332
  css_patterns = [
333
  r'```css\s*\n([\s\S]*?)(?:```|\Z)',
backend_parsers.py CHANGED
@@ -15,12 +15,17 @@ def parse_transformers_js_output(code: str) -> Dict[str, str]:
15
  Uses comprehensive parsing patterns to handle various LLM output formats.
16
  Updated to use transformers.js v3.8.0 CDN.
17
  """
 
 
 
18
  # Auto-fix: If code doesn't start with === index.html ===, add it
19
  code_stripped = code.strip()
20
  if not code_stripped.startswith('==='):
21
  print("[Parser] Auto-fixing: Adding missing === index.html === marker")
22
  code = '=== index.html ===\n' + code
23
  code_stripped = code.strip()
 
 
24
 
25
  # Check if code starts with HTML instead of markers (common LLM mistake)
26
  if code_stripped.startswith('<!DOCTYPE') or code_stripped.startswith('<html'):
@@ -125,9 +130,12 @@ def parse_transformers_js_output(code: str) -> Dict[str, str]:
125
  # Fallback: support === index.html === format if any file is missing
126
  if not (files['index.html'] and files['index.js'] and files['style.css']):
127
  # Use regex to extract sections - match === markers with optional whitespace and newlines
128
- html_fallback = re.search(r'===\s*index\.html\s*===\s*[\r\n]+([\s\S]+?)(?=\n===|$)', code, re.IGNORECASE)
129
- js_fallback = re.search(r'===\s*index\.js\s*===\s*[\r\n]+([\s\S]+?)(?=\n===|$)', code, re.IGNORECASE)
130
- css_fallback = re.search(r'===\s*style\.css\s*===\s*[\r\n]+([\s\S]+?)(?=\n===|$)', code, re.IGNORECASE)
 
 
 
131
 
132
  if html_fallback:
133
  content = html_fallback.group(1).strip()
 
15
  Uses comprehensive parsing patterns to handle various LLM output formats.
16
  Updated to use transformers.js v3.8.0 CDN.
17
  """
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'):
 
130
  # Fallback: support === index.html === format if any file is missing
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
+ # Made [\r\n]+ optional with * instead of + to handle cases where content follows immediately
134
+ html_fallback = re.search(r'===\s*index\.html\s*===\s*[\r\n]*([\s\S]+?)(?=\n===|$)', code, re.IGNORECASE)
135
+ js_fallback = re.search(r'===\s*index\.js\s*===\s*[\r\n]*([\s\S]+?)(?=\n===|$)', code, re.IGNORECASE)
136
+ css_fallback = re.search(r'===\s*style\.css\s*===\s*[\r\n]*([\s\S]+?)(?=\n===|$)', 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
 
140
  if html_fallback:
141
  content = html_fallback.group(1).strip()