akhaliq HF Staff commited on
Commit
4b8181a
·
1 Parent(s): 5c7d8dd

transformers js multi file support

Browse files
Files changed (1) hide show
  1. app.py +107 -7
app.py CHANGED
@@ -5144,6 +5144,15 @@ with gr.Blocks(
5144
  interactive=True,
5145
  label="Generated code"
5146
  )
 
 
 
 
 
 
 
 
 
5147
  with gr.Tab("Preview"):
5148
  sandbox = gr.HTML(label="Live preview")
5149
  # Removed Import Logs tab for cleaner UI
@@ -5261,7 +5270,51 @@ with gr.Blocks(
5261
  language_dropdown.change(update_code_language, inputs=language_dropdown, outputs=code_output)
5262
  language_dropdown.change(update_sdk_based_on_language, inputs=language_dropdown, outputs=sdk_dropdown)
5263
 
5264
- def preview_logic(code, language):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5265
  if language == "html":
5266
  return send_to_sandbox(code)
5267
  if language == "streamlit":
@@ -5273,7 +5326,10 @@ with gr.Blocks(
5273
  return send_streamlit_to_stlite(code)
5274
  return "<div style='padding:1em;color:#888;text-align:center;'>Preview available only for Streamlit apps in Python. Add `import streamlit as st`.</div>"
5275
  if language == "transformers.js":
5276
- files = parse_transformers_js_output(code)
 
 
 
5277
  if files['index.html']:
5278
  return send_transformers_to_sandbox(files)
5279
  return "<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>"
@@ -5281,6 +5337,17 @@ with gr.Blocks(
5281
  return "<div style='padding:1em;color:#888;text-align:center;'>Preview is only available for HTML. Please download your Svelte code and deploy it to see the result.</div>"
5282
  return "<div style='padding:1em;color:#888;text-align:center;'>Preview is only available for HTML.</div>"
5283
 
 
 
 
 
 
 
 
 
 
 
 
5284
  def show_deploy_components(*args):
5285
  return [gr.Textbox(visible=True), gr.Dropdown(visible=True), gr.Button(visible=True)]
5286
 
@@ -5354,6 +5421,11 @@ with gr.Blocks(
5354
  end_generation_ui,
5355
  inputs=None,
5356
  outputs=[sidebar, generating_status]
 
 
 
 
 
5357
  ).then(
5358
  show_deploy_components,
5359
  None,
@@ -5606,6 +5678,10 @@ with gr.Blocks(
5606
  end_generation_ui,
5607
  inputs=None,
5608
  outputs=[sidebar, generating_status]
 
 
 
 
5609
  ).then(
5610
  show_deploy_components,
5611
  None,
@@ -5684,9 +5760,9 @@ with gr.Blocks(
5684
  quick_examples_col,
5685
  ],
5686
  )
5687
- # Update preview when code or language changes
5688
- code_output.change(preview_logic, inputs=[code_output, language_dropdown], outputs=sandbox)
5689
- language_dropdown.change(preview_logic, inputs=[code_output, language_dropdown], outputs=sandbox)
5690
  # Update deploy button text when space name changes
5691
  space_name_input.change(update_deploy_button_text, inputs=[space_name_input], outputs=[deploy_btn])
5692
  clear_btn.click(clear_history, outputs=[history, history_output, file_input, website_url_input])
@@ -5911,8 +5987,14 @@ with gr.Blocks(
5911
  return gr.update(value=f"Error: Could not access space {repo_id} for update.", visible=True)
5912
  except Exception as e:
5913
  return gr.update(value=f"Error: Cannot update space {repo_id}. {str(e)}", visible=True)
5914
- # Parse the transformers.js output to get the three files
5915
- files = parse_transformers_js_output(code)
 
 
 
 
 
 
5916
 
5917
  if not files['index.html'] or not files['index.js'] or not files['style.css']:
5918
  return gr.update(value="Error: Could not parse transformers.js output. Please regenerate the code.", visible=True)
@@ -6205,7 +6287,25 @@ with gr.Blocks(
6205
  os.unlink(temp_path)
6206
 
6207
  # Connect the deploy button to the new function
 
 
 
 
 
 
 
 
 
 
 
 
 
6208
  deploy_btn.click(
 
 
 
 
 
6209
  deploy_to_user_space,
6210
  inputs=[code_output, space_name_input, sdk_dropdown],
6211
  outputs=deploy_status
 
5144
  interactive=True,
5145
  label="Generated code"
5146
  )
5147
+ # Transformers.js multi-file editors (hidden by default)
5148
+ with gr.Group(visible=False) as tjs_group:
5149
+ with gr.Tabs():
5150
+ with gr.Tab("index.html"):
5151
+ tjs_html_code = gr.Code(language="html", lines=20, interactive=True, label="index.html")
5152
+ with gr.Tab("index.js"):
5153
+ tjs_js_code = gr.Code(language="javascript", lines=20, interactive=True, label="index.js")
5154
+ with gr.Tab("style.css"):
5155
+ tjs_css_code = gr.Code(language="css", lines=20, interactive=True, label="style.css")
5156
  with gr.Tab("Preview"):
5157
  sandbox = gr.HTML(label="Live preview")
5158
  # Removed Import Logs tab for cleaner UI
 
5270
  language_dropdown.change(update_code_language, inputs=language_dropdown, outputs=code_output)
5271
  language_dropdown.change(update_sdk_based_on_language, inputs=language_dropdown, outputs=sdk_dropdown)
5272
 
5273
+ # Toggle single vs multi-file editors for transformers.js and populate when switching
5274
+ def toggle_editors(language, code_text):
5275
+ if language == "transformers.js":
5276
+ files = parse_transformers_js_output(code_text or "")
5277
+ return [
5278
+ gr.update(visible=False), # code_output hidden
5279
+ gr.update(visible=True), # tjs_group shown
5280
+ gr.update(value=files.get('index.html', '')),
5281
+ gr.update(value=files.get('index.js', '')),
5282
+ gr.update(value=files.get('style.css', '')),
5283
+ ]
5284
+ else:
5285
+ return [
5286
+ gr.update(visible=True), # code_output shown
5287
+ gr.update(visible=False), # tjs_group hidden
5288
+ gr.update(),
5289
+ gr.update(),
5290
+ gr.update(),
5291
+ ]
5292
+
5293
+ language_dropdown.change(
5294
+ toggle_editors,
5295
+ inputs=[language_dropdown, code_output],
5296
+ outputs=[code_output, tjs_group, tjs_html_code, tjs_js_code, tjs_css_code],
5297
+ )
5298
+
5299
+ def sync_tjs_from_code(code_text, language):
5300
+ if language != "transformers.js":
5301
+ return [gr.update(), gr.update(), gr.update(), gr.update()]
5302
+ files = parse_transformers_js_output(code_text or "")
5303
+ return [
5304
+ gr.update(value=files.get('index.html', '')),
5305
+ gr.update(value=files.get('index.js', '')),
5306
+ gr.update(value=files.get('style.css', '')),
5307
+ gr.update(visible=True),
5308
+ ]
5309
+
5310
+ # Keep multi-file editors in sync when code_output changes and language is transformers.js
5311
+ code_output.change(
5312
+ sync_tjs_from_code,
5313
+ inputs=[code_output, language_dropdown],
5314
+ outputs=[tjs_html_code, tjs_js_code, tjs_css_code, tjs_group],
5315
+ )
5316
+
5317
+ def preview_logic(code, language, html_part=None, js_part=None, css_part=None):
5318
  if language == "html":
5319
  return send_to_sandbox(code)
5320
  if language == "streamlit":
 
5326
  return send_streamlit_to_stlite(code)
5327
  return "<div style='padding:1em;color:#888;text-align:center;'>Preview available only for Streamlit apps in Python. Add `import streamlit as st`.</div>"
5328
  if language == "transformers.js":
5329
+ # Prefer values passed from multi-file editors if present; fallback to parsing single editor content
5330
+ files = {'index.html': html_part or '', 'index.js': js_part or '', 'style.css': css_part or ''}
5331
+ if not (files['index.html'] or files['index.js'] or files['style.css']):
5332
+ files = parse_transformers_js_output(code)
5333
  if files['index.html']:
5334
  return send_transformers_to_sandbox(files)
5335
  return "<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>"
 
5337
  return "<div style='padding:1em;color:#888;text-align:center;'>Preview is only available for HTML. Please download your Svelte code and deploy it to see the result.</div>"
5338
  return "<div style='padding:1em;color:#888;text-align:center;'>Preview is only available for HTML.</div>"
5339
 
5340
+ # Direct preview updates from multi-file editor changes
5341
+ def preview_from_tjs_editors(html_code, js_code, css_code):
5342
+ files = {'index.html': html_code or '', 'index.js': js_code or '', 'style.css': css_code or ''}
5343
+ if files['index.html']:
5344
+ return send_transformers_to_sandbox(files)
5345
+ return gr.update()
5346
+
5347
+ tjs_html_code.change(preview_from_tjs_editors, inputs=[tjs_html_code, tjs_js_code, tjs_css_code], outputs=sandbox)
5348
+ tjs_js_code.change(preview_from_tjs_editors, inputs=[tjs_html_code, tjs_js_code, tjs_css_code], outputs=sandbox)
5349
+ tjs_css_code.change(preview_from_tjs_editors, inputs=[tjs_html_code, tjs_js_code, tjs_css_code], outputs=sandbox)
5350
+
5351
  def show_deploy_components(*args):
5352
  return [gr.Textbox(visible=True), gr.Dropdown(visible=True), gr.Button(visible=True)]
5353
 
 
5421
  end_generation_ui,
5422
  inputs=None,
5423
  outputs=[sidebar, generating_status]
5424
+ ).then(
5425
+ # After generation, toggle editors for transformers.js and populate
5426
+ toggle_editors,
5427
+ inputs=[language_dropdown, code_output],
5428
+ outputs=[code_output, tjs_group, tjs_html_code, tjs_js_code, tjs_css_code]
5429
  ).then(
5430
  show_deploy_components,
5431
  None,
 
5678
  end_generation_ui,
5679
  inputs=None,
5680
  outputs=[sidebar, generating_status]
5681
+ ).then(
5682
+ toggle_editors,
5683
+ inputs=[language_dropdown, code_output],
5684
+ outputs=[code_output, tjs_group, tjs_html_code, tjs_js_code, tjs_css_code]
5685
  ).then(
5686
  show_deploy_components,
5687
  None,
 
5760
  quick_examples_col,
5761
  ],
5762
  )
5763
+ # Update preview when code or language changes (supports multi-file path via optional args)
5764
+ code_output.change(preview_logic, inputs=[code_output, language_dropdown, tjs_html_code, tjs_js_code, tjs_css_code], outputs=sandbox)
5765
+ language_dropdown.change(preview_logic, inputs=[code_output, language_dropdown, tjs_html_code, tjs_js_code, tjs_css_code], outputs=sandbox)
5766
  # Update deploy button text when space name changes
5767
  space_name_input.change(update_deploy_button_text, inputs=[space_name_input], outputs=[deploy_btn])
5768
  clear_btn.click(clear_history, outputs=[history, history_output, file_input, website_url_input])
 
5987
  return gr.update(value=f"Error: Could not access space {repo_id} for update.", visible=True)
5988
  except Exception as e:
5989
  return gr.update(value=f"Error: Cannot update space {repo_id}. {str(e)}", visible=True)
5990
+ # Build files from multi-file editors if available; fallback to parsing single editor value
5991
+ files = {
5992
+ 'index.html': tjs_html_code.value if 'tjs_html_code' in locals() else '',
5993
+ 'index.js': tjs_js_code.value if 'tjs_js_code' in locals() else '',
5994
+ 'style.css': tjs_css_code.value if 'tjs_css_code' in locals() else '',
5995
+ }
5996
+ if not (files['index.html'] and files['index.js'] and files['style.css']):
5997
+ files = parse_transformers_js_output(code)
5998
 
5999
  if not files['index.html'] or not files['index.js'] or not files['style.css']:
6000
  return gr.update(value="Error: Could not parse transformers.js output. Please regenerate the code.", visible=True)
 
6287
  os.unlink(temp_path)
6288
 
6289
  # Connect the deploy button to the new function
6290
+ def gather_code_for_deploy(code_text, language, html_part, js_part, css_part):
6291
+ # When transformers.js is selected, ensure multi-file editors are used; otherwise, return single code
6292
+ if language == "transformers.js":
6293
+ # Join into a combined display string for auditing; actual deploy reads editor values directly
6294
+ files = {
6295
+ 'index.html': html_part or '',
6296
+ 'index.js': js_part or '',
6297
+ 'style.css': css_part or '',
6298
+ }
6299
+ if files['index.html'] and files['index.js'] and files['style.css']:
6300
+ return format_transformers_js_output(files)
6301
+ return code_text
6302
+
6303
  deploy_btn.click(
6304
+ gather_code_for_deploy,
6305
+ inputs=[code_output, language_dropdown, tjs_html_code, tjs_js_code, tjs_css_code],
6306
+ outputs=[code_output],
6307
+ queue=False,
6308
+ ).then(
6309
  deploy_to_user_space,
6310
  inputs=[code_output, space_name_input, sdk_dropdown],
6311
  outputs=deploy_status