add preview for transformers js
Browse files804 unmodified lines8058068078088098101407 unmodified lines2218221922202221222222232224237 unmodified lines246224632464246524662467246824692470247124722473247424752476247771 unmodified lines25492550255125522553255425551 unmodified line25572558255925602561256225632564256525661083 unmodified lines3650365136523653365436553656804 unmodified linesoutput.append(files['style.css'])return '\n'.join(output)def parse_svelte_output(text):"""Parse Svelte output to extract individual files"""files = {1407 unmodified linesyield {code_output: formatted_output,history: _history,sandbox: send_to_sandbox(files['index.html']),history_output: history_to_chatbot_messages(_history),}else:237 unmodified linesyield {code_output: gr.update(value=formatted_output, language="html"),history_output: history_to_chatbot_messages(_history),sandbox: send_to_sandbox(files['index.html']) 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>",}elif has_existing_content:# Model is returning search/replace changes for transformers.js - apply themlast_content = _history[-1][1] if _history and len(_history[-1]) > 1 else ""modified_content = apply_transformers_js_search_replace_changes(last_content, content)yield {code_output: gr.update(value=modified_content, language="html"),history_output: history_to_chatbot_messages(_history),sandbox: send_to_sandbox(parse_transformers_js_output(modified_content)['index.html']) if parse_transformers_js_output(modified_content)['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>",}else:# Still streaming, show partial content71 unmodified linesyield {code_output: formatted_output,history: _history,sandbox: send_to_sandbox(files['index.html']),history_output: history_to_chatbot_messages(_history),}elif has_existing_content:1 unmodified linelast_content = _history[-1][1] if _history and len(_history[-1]) > 1 else ""modified_content = apply_transformers_js_search_replace_changes(last_content, content)_history.append([query, modified_content])yield {code_output: modified_content,history: _history,sandbox: send_to_sandbox(parse_transformers_js_output(modified_content)['index.html']),history_output: history_to_chatbot_messages(_history),}else:1083 unmodified linesif language == "transformers.js":files = parse_transformers_js_output(code)if files['index.html']:return send_to_sandbox(files['index.html'])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>"if language == "svelte":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>"804 unmodified lines8058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999001407 unmodified lines2308230923102311231223132314237 unmodified lines2552255325542555255625572558255925602561256225632564256525662567256871 unmodified lines26402641264226432644264526461 unmodified line264826492650265126522653265426552656265726581083 unmodified lines3742374337443745374637473748804 unmodified linesoutput.append(files['style.css'])return '\n'.join(output)def build_transformers_inline_html(files: dict) -> str:"""Merge transformers.js three-file output into a single self-contained HTML document.- Inlines style.css into a <style> tag- Inlines index.js into a <script type="module"> tag- Rewrites ESM imports for transformers.js to a stable CDN URL so it works in data: iframes"""import re as _rehtml = files.get('index.html') or ''js = files.get('index.js') or ''css = files.get('style.css') or ''# Normalize JS imports to CDN (handle both @huggingface/transformers and legacy @xenova/transformers)cdn_url = "https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.7.1"js = _re.sub(r"from\s+['\"]@huggingface/transformers['\"]", f"from '{cdn_url}'", js)js = _re.sub(r"from\s+['\"]@xenova/transformers['\"]", f"from '{cdn_url}'", js)# Prepend a small prelude to reduce persistent caching during preview# Note: importing env alongside user's own imports is fine in ESMif js.strip():prelude = (f"import {{ env }} from '{cdn_url}';\n""try { env.useBrowserCache = false; } catch (e) {}\n")js = prelude + js# If index.html missing or doesn't look like a full document, create a minimal shelldoc = html.strip()if not doc or ('<html' not in doc.lower()):doc = ("<!DOCTYPE html>\n""<html>\n<head>\n<meta charset=\"UTF-8\">\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n<title>Transformers.js App</title>\n</head>\n""<body>\n<div id=\"app\"></div>\n</body>\n</html>")# Remove local references to style.css and index.js to avoid duplicates when inliningdoc = _re.sub(r"<link[^>]+href=\"[^\"]*style\.css\"[^>]*>\s*", "", doc, flags=_re.IGNORECASE)doc = _re.sub(r"<script[^>]+src=\"[^\"]*index\.js\"[^>]*>\s*</script>\s*", "", doc, flags=_re.IGNORECASE)# Inline CSS: insert before </head> or create a <head>style_tag = f"<style>\n{css}\n</style>" if css else ""if style_tag:if '</head>' in doc.lower():# Preserve original casing by finding closing head case-insensitivelymatch = _re.search(r"</head>", doc, flags=_re.IGNORECASE)if match:idx = match.start()doc = doc[:idx] + style_tag + doc[idx:]else:# No head; insert at top of bodymatch = _re.search(r"<body[^>]*>", doc, flags=_re.IGNORECASE)if match:idx = match.end()doc = doc[:idx] + "\n" + style_tag + doc[idx:]else:# Append at beginningdoc = style_tag + doc# Inline JS: insert before </body>script_tag = f"<script type=\"module\">\n{js}\n</script>" if js else ""# Cleanup script to clear Cache Storage and IndexedDB on unload to free model weightscleanup_tag = ("<script>\n""(function(){\n"" function cleanup(){\n"" try { if (window.caches && caches.keys) { caches.keys().then(keys => keys.forEach(k => caches.delete(k))); } } catch(e){}\n"" try { if (window.indexedDB && indexedDB.databases) { indexedDB.databases().then(dbs => dbs.forEach(db => db && db.name && indexedDB.deleteDatabase(db.name))); } } catch(e){}\n"" }\n"" window.addEventListener('pagehide', cleanup, { once: true });\n"" window.addEventListener('beforeunload', cleanup, { once: true });\n""})();\n""</script>")if script_tag:match = _re.search(r"</body>", doc, flags=_re.IGNORECASE)if match:idx = match.start()doc = doc[:idx] + script_tag + cleanup_tag + doc[idx:]else:# Append at enddoc = doc + script_tag + cleanup_tagreturn docdef send_transformers_to_sandbox(files: dict) -> str:"""Build a self-contained HTML document from transformers.js files and return an iframe preview."""merged_html = build_transformers_inline_html(files)return send_to_sandbox(merged_html)def parse_svelte_output(text):"""Parse Svelte output to extract individual files"""files = {1407 unmodified linesyield {code_output: formatted_output,history: _history,sandbox: send_transformers_to_sandbox(files),history_output: history_to_chatbot_messages(_history),}else:237 unmodified linesyield {code_output: gr.update(value=formatted_output, language="html"),history_output: history_to_chatbot_messages(_history),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>",}elif has_existing_content:# Model is returning search/replace changes for transformers.js - apply themlast_content = _history[-1][1] if _history and len(_history[-1]) > 1 else ""modified_content = apply_transformers_js_search_replace_changes(last_content, content)_mf = parse_transformers_js_output(modified_content)yield {code_output: gr.update(value=modified_content, language="html"),history_output: history_to_chatbot_messages(_history),sandbox: send_transformers_to_sandbox(_mf) if _mf['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>",}else:# Still streaming, show partial content71 unmodified linesyield {code_output: formatted_output,history: _history,sandbox: send_transformers_to_sandbox(files),history_output: history_to_chatbot_messages(_history),}elif has_existing_content:1 unmodified linelast_content = _history[-1][1] if _history and len(_history[-1]) > 1 else ""modified_content = apply_transformers_js_search_replace_changes(last_content, content)_history.append([query, modified_content])_mf = parse_transformers_js_output(modified_content)yield {code_output: modified_content,history: _history,sandbox: send_transformers_to_sandbox(_mf),history_output: history_to_chatbot_messages(_history),}else:1083 unmodified linesif 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>"if language == "svelte":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>"
app.py+98-6