transformers js multi file support
Browse files5143 unmodified lines514451455146514751485149111 unmodified lines52615262526352645265526652675 unmodified lines52735274527552765277527852791 unmodified line52815282528352845285528667 unmodified lines535453555356535753585359246 unmodified lines56065607560856095610561172 unmodified lines568456855686568756885689569056915692218 unmodified lines59115912591359145915591659175918286 unmodified lines62056206620762086209621062115143 unmodified linesinteractive=True,label="Generated code")with gr.Tab("Preview"):sandbox = gr.HTML(label="Live preview")# Removed Import Logs tab for cleaner UI111 unmodified lineslanguage_dropdown.change(update_code_language, inputs=language_dropdown, outputs=code_output)language_dropdown.change(update_sdk_based_on_language, inputs=language_dropdown, outputs=sdk_dropdown)def preview_logic(code, language):if language == "html":return send_to_sandbox(code)if language == "streamlit":5 unmodified linesreturn send_streamlit_to_stlite(code)return "<div style='padding:1em;color:#888;text-align:center;'>Preview available only for Streamlit apps in Python. Add `import streamlit as st`.</div>"if language == "transformers.js":files = parse_transformers_js_output(code)if files['index.html']:return send_transformers_to_sandbox(files)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>"1 unmodified linereturn "<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>"return "<div style='padding:1em;color:#888;text-align:center;'>Preview is only available for HTML.</div>"def show_deploy_components(*args):return [gr.Textbox(visible=True), gr.Dropdown(visible=True), gr.Button(visible=True)]67 unmodified linesend_generation_ui,inputs=None,outputs=[sidebar, generating_status]).then(show_deploy_components,None,246 unmodified linesend_generation_ui,inputs=None,outputs=[sidebar, generating_status]).then(show_deploy_components,None,72 unmodified linesquick_examples_col,],)# Update preview when code or language changescode_output.change(preview_logic, inputs=[code_output, language_dropdown], outputs=sandbox)language_dropdown.change(preview_logic, inputs=[code_output, language_dropdown], outputs=sandbox)# Update deploy button text when space name changesspace_name_input.change(update_deploy_button_text, inputs=[space_name_input], outputs=[deploy_btn])clear_btn.click(clear_history, outputs=[history, history_output, file_input, website_url_input])218 unmodified linesreturn gr.update(value=f"Error: Could not access space {repo_id} for update.", visible=True)except Exception as e:return gr.update(value=f"Error: Cannot update space {repo_id}. {str(e)}", visible=True)# Parse the transformers.js output to get the three filesfiles = parse_transformers_js_output(code)if not files['index.html'] or not files['index.js'] or not files['style.css']:return gr.update(value="Error: Could not parse transformers.js output. Please regenerate the code.", visible=True)286 unmodified linesos.unlink(temp_path)# Connect the deploy button to the new functiondeploy_btn.click(deploy_to_user_space,inputs=[code_output, space_name_input, sdk_dropdown],outputs=deploy_status5143 unmodified lines514451455146514751485149515051515152515351545155515651575158111 unmodified lines5270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205 unmodified lines53265327532853295330533153325333533453351 unmodified line5337533853395340534153425343534453455346534753485349535053515352535367 unmodified lines54215422542354245425542654275428542954305431246 unmodified lines567856795680568156825683568456855686568772 unmodified lines576057615762576357645765576657675768218 unmodified lines59875988598959905991599259935994599559965997599859996000286 unmodified lines62876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063115143 unmodified linesinteractive=True,label="Generated code")# Transformers.js multi-file editors (hidden by default)with gr.Group(visible=False) as tjs_group:with gr.Tabs():with gr.Tab("index.html"):tjs_html_code = gr.Code(language="html", lines=20, interactive=True, label="index.html")with gr.Tab("index.js"):tjs_js_code = gr.Code(language="javascript", lines=20, interactive=True, label="index.js")with gr.Tab("style.css"):tjs_css_code = gr.Code(language="css", lines=20, interactive=True, label="style.css")with gr.Tab("Preview"):sandbox = gr.HTML(label="Live preview")# Removed Import Logs tab for cleaner UI111 unmodified lineslanguage_dropdown.change(update_code_language, inputs=language_dropdown, outputs=code_output)language_dropdown.change(update_sdk_based_on_language, inputs=language_dropdown, outputs=sdk_dropdown)# Toggle single vs multi-file editors for transformers.js and populate when switchingdef toggle_editors(language, code_text):if language == "transformers.js":files = parse_transformers_js_output(code_text or "")return [gr.update(visible=False), # code_output hiddengr.update(visible=True), # tjs_group showngr.update(value=files.get('index.html', '')),gr.update(value=files.get('index.js', '')),gr.update(value=files.get('style.css', '')),]else:return [gr.update(visible=True), # code_output showngr.update(visible=False), # tjs_group hiddengr.update(),gr.update(),gr.update(),]language_dropdown.change(toggle_editors,inputs=[language_dropdown, code_output],outputs=[code_output, tjs_group, tjs_html_code, tjs_js_code, tjs_css_code],)def sync_tjs_from_code(code_text, language):if language != "transformers.js":return [gr.update(), gr.update(), gr.update(), gr.update()]files = parse_transformers_js_output(code_text or "")return [gr.update(value=files.get('index.html', '')),gr.update(value=files.get('index.js', '')),gr.update(value=files.get('style.css', '')),gr.update(visible=True),]# Keep multi-file editors in sync when code_output changes and language is transformers.jscode_output.change(sync_tjs_from_code,inputs=[code_output, language_dropdown],outputs=[tjs_html_code, tjs_js_code, tjs_css_code, tjs_group],)def preview_logic(code, language, html_part=None, js_part=None, css_part=None):if language == "html":return send_to_sandbox(code)if language == "streamlit":5 unmodified linesreturn send_streamlit_to_stlite(code)return "<div style='padding:1em;color:#888;text-align:center;'>Preview available only for Streamlit apps in Python. Add `import streamlit as st`.</div>"if language == "transformers.js":# Prefer values passed from multi-file editors if present; fallback to parsing single editor contentfiles = {'index.html': html_part or '', 'index.js': js_part or '', 'style.css': css_part or ''}if not (files['index.html'] or files['index.js'] or files['style.css']):files = parse_transformers_js_output(code)if files['index.html']:return send_transformers_to_sandbox(files)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>"1 unmodified linereturn "<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>"return "<div style='padding:1em;color:#888;text-align:center;'>Preview is only available for HTML.</div>"# Direct preview updates from multi-file editor changesdef preview_from_tjs_editors(html_code, js_code, css_code):files = {'index.html': html_code or '', 'index.js': js_code or '', 'style.css': css_code or ''}if files['index.html']:return send_transformers_to_sandbox(files)return gr.update()tjs_html_code.change(preview_from_tjs_editors, inputs=[tjs_html_code, tjs_js_code, tjs_css_code], outputs=sandbox)tjs_js_code.change(preview_from_tjs_editors, inputs=[tjs_html_code, tjs_js_code, tjs_css_code], outputs=sandbox)tjs_css_code.change(preview_from_tjs_editors, inputs=[tjs_html_code, tjs_js_code, tjs_css_code], outputs=sandbox)def show_deploy_components(*args):return [gr.Textbox(visible=True), gr.Dropdown(visible=True), gr.Button(visible=True)]67 unmodified linesend_generation_ui,inputs=None,outputs=[sidebar, generating_status]).then(# After generation, toggle editors for transformers.js and populatetoggle_editors,inputs=[language_dropdown, code_output],outputs=[code_output, tjs_group, tjs_html_code, tjs_js_code, tjs_css_code]).then(show_deploy_components,None,246 unmodified linesend_generation_ui,inputs=None,outputs=[sidebar, generating_status]).then(toggle_editors,inputs=[language_dropdown, code_output],outputs=[code_output, tjs_group, tjs_html_code, tjs_js_code, tjs_css_code]).then(show_deploy_components,None,72 unmodified linesquick_examples_col,],)# Update preview when code or language changes (supports multi-file path via optional args)code_output.change(preview_logic, inputs=[code_output, language_dropdown, tjs_html_code, tjs_js_code, tjs_css_code], outputs=sandbox)language_dropdown.change(preview_logic, inputs=[code_output, language_dropdown, tjs_html_code, tjs_js_code, tjs_css_code], outputs=sandbox)# Update deploy button text when space name changesspace_name_input.change(update_deploy_button_text, inputs=[space_name_input], outputs=[deploy_btn])clear_btn.click(clear_history, outputs=[history, history_output, file_input, website_url_input])218 unmodified linesreturn gr.update(value=f"Error: Could not access space {repo_id} for update.", visible=True)except Exception as e:return gr.update(value=f"Error: Cannot update space {repo_id}. {str(e)}", visible=True)# Build files from multi-file editors if available; fallback to parsing single editor valuefiles = {'index.html': tjs_html_code.value if 'tjs_html_code' in locals() else '','index.js': tjs_js_code.value if 'tjs_js_code' in locals() else '','style.css': tjs_css_code.value if 'tjs_css_code' in locals() else '',}if not (files['index.html'] and files['index.js'] and files['style.css']):files = parse_transformers_js_output(code)if not files['index.html'] or not files['index.js'] or not files['style.css']:return gr.update(value="Error: Could not parse transformers.js output. Please regenerate the code.", visible=True)286 unmodified linesos.unlink(temp_path)# Connect the deploy button to the new functiondef gather_code_for_deploy(code_text, language, html_part, js_part, css_part):# When transformers.js is selected, ensure multi-file editors are used; otherwise, return single codeif language == "transformers.js":# Join into a combined display string for auditing; actual deploy reads editor values directlyfiles = {'index.html': html_part or '','index.js': js_part or '','style.css': css_part or '',}if files['index.html'] and files['index.js'] and files['style.css']:return format_transformers_js_output(files)return code_textdeploy_btn.click(gather_code_for_deploy,inputs=[code_output, language_dropdown, tjs_html_code, tjs_js_code, tjs_css_code],outputs=[code_output],queue=False,).then(deploy_to_user_space,inputs=[code_output, space_name_input, sdk_dropdown],outputs=deploy_status
app.py+107-7