akhaliq HF Staff commited on
Commit
698eb9f
·
1 Parent(s): dc37bd7

transformers js streaming code

Browse files
Files changed (1) hide show
  1. app.py +25 -16
app.py CHANGED
@@ -1089,20 +1089,20 @@ def parse_transformers_js_output(text):
1089
 
1090
  # Multiple patterns to match the three code blocks with different variations
1091
  html_patterns = [
1092
- r'```html\s*\n([\s\S]+?)\n```',
1093
- r'```htm\s*\n([\s\S]+?)\n```',
1094
- r'```\s*(?:index\.html|html)\s*\n([\s\S]+?)\n```'
1095
  ]
1096
 
1097
  js_patterns = [
1098
- r'```javascript\s*\n([\s\S]+?)\n```',
1099
- r'```js\s*\n([\s\S]+?)\n```',
1100
- r'```\s*(?:index\.js|javascript)\s*\n([\s\S]+?)\n```'
1101
  ]
1102
 
1103
  css_patterns = [
1104
- r'```css\s*\n([\s\S]+?)\n```',
1105
- r'```\s*(?:style\.css|css)\s*\n([\s\S]+?)\n```'
1106
  ]
1107
 
1108
  # Extract HTML content
@@ -3920,13 +3920,18 @@ This will help me create a better design for you."""
3920
  # Handle transformers.js output differently
3921
  if language == "transformers.js":
3922
  files = parse_transformers_js_output(content)
3923
- if files['index.html'] and files['index.js'] and files['style.css']:
3924
- # Model returned complete transformers.js output
3925
- formatted_output = format_transformers_js_output(files)
 
 
 
 
 
3926
  yield {
3927
- code_output: gr.update(value=formatted_output, language="html"),
3928
  history_output: history_to_chatbot_messages(_history),
3929
- sandbox: send_transformers_to_sandbox(files) if files['index.html'] else "<div style='padding:1em;color:#888;text-align:center;'>Preview is only available for HTML. Please download your code using the download button above.</div>",
3930
  }
3931
  elif has_existing_content:
3932
  # Model is returning search/replace changes for transformers.js - apply them
@@ -5562,9 +5567,11 @@ with gr.Blocks(
5562
  def toggle_editors(language, code_text):
5563
  if language == "transformers.js":
5564
  files = parse_transformers_js_output(code_text or "")
 
 
5565
  return [
5566
- gr.update(visible=False), # code_output hidden
5567
- gr.update(visible=True), # tjs_group shown
5568
  gr.update(value=files.get('index.html', '')),
5569
  gr.update(value=files.get('index.js', '')),
5570
  gr.update(value=files.get('style.css', '')),
@@ -5588,11 +5595,13 @@ with gr.Blocks(
5588
  if language != "transformers.js":
5589
  return [gr.update(), gr.update(), gr.update(), gr.update()]
5590
  files = parse_transformers_js_output(code_text or "")
 
 
5591
  return [
5592
  gr.update(value=files.get('index.html', '')),
5593
  gr.update(value=files.get('index.js', '')),
5594
  gr.update(value=files.get('style.css', '')),
5595
- gr.update(visible=True),
5596
  ]
5597
 
5598
  # Keep multi-file editors in sync when code_output changes and language is transformers.js
 
1089
 
1090
  # Multiple patterns to match the three code blocks with different variations
1091
  html_patterns = [
1092
+ r'```html\s*\n([\s\S]*?)(?:```|\Z)',
1093
+ r'```htm\s*\n([\s\S]*?)(?:```|\Z)',
1094
+ r'```\s*(?:index\.html|html)\s*\n([\s\S]*?)(?:```|\Z)'
1095
  ]
1096
 
1097
  js_patterns = [
1098
+ r'```javascript\s*\n([\s\S]*?)(?:```|\Z)',
1099
+ r'```js\s*\n([\s\S]*?)(?:```|\Z)',
1100
+ r'```\s*(?:index\.js|javascript|js)\s*\n([\s\S]*?)(?:```|\Z)'
1101
  ]
1102
 
1103
  css_patterns = [
1104
+ r'```css\s*\n([\s\S]*?)(?:```|\Z)',
1105
+ r'```\s*(?:style\.css|css)\s*\n([\s\S]*?)(?:```|\Z)'
1106
  ]
1107
 
1108
  # Extract HTML content
 
3920
  # Handle transformers.js output differently
3921
  if language == "transformers.js":
3922
  files = parse_transformers_js_output(content)
3923
+
3924
+ # Stream ALL code by merging current parts into a single HTML (inline CSS & JS)
3925
+ has_any_part = any([files.get('index.html'), files.get('index.js'), files.get('style.css')])
3926
+ if has_any_part:
3927
+ merged_html = build_transformers_inline_html(files)
3928
+ preview_val = None
3929
+ if files['index.html'] and files['index.js'] and files['style.css']:
3930
+ preview_val = send_transformers_to_sandbox(files)
3931
  yield {
3932
+ code_output: gr.update(value=merged_html, language="html"),
3933
  history_output: history_to_chatbot_messages(_history),
3934
+ sandbox: preview_val or "<div style='padding:1em;color:#888;text-align:center;'>Generating transformers.js app...</div>",
3935
  }
3936
  elif has_existing_content:
3937
  # Model is returning search/replace changes for transformers.js - apply them
 
5567
  def toggle_editors(language, code_text):
5568
  if language == "transformers.js":
5569
  files = parse_transformers_js_output(code_text or "")
5570
+ # Hide multi-file editors until all files exist; show single code until then
5571
+ editors_visible = True if (files.get('index.html') and files.get('index.js') and files.get('style.css')) else False
5572
  return [
5573
+ gr.update(visible=not editors_visible), # code_output shown if editors hidden
5574
+ gr.update(visible=editors_visible), # tjs_group shown only when complete
5575
  gr.update(value=files.get('index.html', '')),
5576
  gr.update(value=files.get('index.js', '')),
5577
  gr.update(value=files.get('style.css', '')),
 
5595
  if language != "transformers.js":
5596
  return [gr.update(), gr.update(), gr.update(), gr.update()]
5597
  files = parse_transformers_js_output(code_text or "")
5598
+ # Only reveal the multi-file editors when all three files are present
5599
+ editors_visible = True if (files.get('index.html') and files.get('index.js') and files.get('style.css')) else None
5600
  return [
5601
  gr.update(value=files.get('index.html', '')),
5602
  gr.update(value=files.get('index.js', '')),
5603
  gr.update(value=files.get('style.css', '')),
5604
+ gr.update(visible=editors_visible) if editors_visible is not None else gr.update(),
5605
  ]
5606
 
5607
  # Keep multi-file editors in sync when code_output changes and language is transformers.js