add gradio lite for preview
Browse files37 unmodified lines38394041424344451559 unmodified lines16051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651847 unmodified lines249925002501250225032504250525069 unmodified lines251625172518251925202521252225235 unmodified lines25292530253125322533253425352536120 unmodified lines26572658265926602661266226632664744 unmodified lines34093410341133613412341333633414341534163417167 unmodified lines35853586358735883589359035913592359334 unmodified lines362836293630363136323633363436355 unmodified lines3641364236433644364536463647364837 unmodified lines # Map composite options to a supported syntax highlighting if language == "streamlit": return "python" if language == "gradio": return "python" return language if language in GRADIO_SUPPORTED_LANGUAGES else None
# Search/Replace Constants1559 unmodified lines iframe = f'<iframe src="{data_uri}" width="100%" height="920px" sandbox="allow-scripts allow-same-origin allow-forms allow-popups allow-modals allow-presentation" allow="display-capture"></iframe>' return iframe
def is_gradio_code(code: str) -> bool: """Heuristic check to determine if Python code is a Gradio app.""" if not code: return False lowered = code.lower() return ( "import gradio" in lowered or "from gradio" in lowered or "gr.Interface(" in code or "gr.Blocks(" in code )
def send_gradio_to_lite(code: str) -> str: """Render Gradio code using gradio-lite inside a sandboxed iframe for preview.""" html_doc = ( """<!doctype html><html> <head> <meta charset=\"UTF-8\" /> <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\" /> <meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\" /> <title>Gradio Preview</title> <script type=\"module\" crossorigin src=\"https://cdn.jsdelivr.net/npm/@gradio/lite/dist/lite.js\"></script> <link rel=\"stylesheet\" href=\"https://cdn.jsdelivr.net/npm/@gradio/lite/dist/lite.css\" /> <style>html,body{margin:0;padding:0;height:100%;} gradio-lite{display:block;height:100%;}</style> </head> <body> <gradio-lite>""" + (code or "") + """ </gradio-lite> </body></html>""" ) encoded_html = base64.b64encode(html_doc.encode('utf-8')).decode('utf-8') data_uri = f"data:text/html;charset=utf-8;base64,{encoded_html}" iframe = f'<iframe src="{data_uri}" width="100%" height="920px" sandbox="allow-scripts allow-same-origin allow-forms allow-popups allow-modals allow-presentation" allow="display-capture"></iframe>' return iframe
def demo_card_click(e: gr.EventData): try: # Get the index from the event data847 unmodified lines preview_val = send_to_sandbox(clean_code) elif language == "python" and is_streamlit_code(clean_code): preview_val = send_streamlit_to_stlite(clean_code) elif language == "gradio" or (language == "python" and is_gradio_code(clean_code)): preview_val = send_gradio_to_lite(clean_code) yield { code_output: gr.update(value=clean_code, language=get_gradio_language(language)), history_output: history_to_chatbot_messages(_history),9 unmodified lines preview_val = send_to_sandbox(clean_content) elif language == "python" and is_streamlit_code(clean_content): preview_val = send_streamlit_to_stlite(clean_content) elif language == "gradio" or (language == "python" and is_gradio_code(clean_content)): preview_val = send_gradio_to_lite(clean_content) yield { code_output: gr.update(value=clean_content, language=get_gradio_language(language)), history_output: history_to_chatbot_messages(_history),5 unmodified lines preview_val = send_to_sandbox(clean_code) elif language == "python" and is_streamlit_code(clean_code): preview_val = send_streamlit_to_stlite(clean_code) elif language == "gradio" or (language == "python" and is_gradio_code(clean_code)): preview_val = send_gradio_to_lite(clean_code) yield { code_output: gr.update(value=clean_code, language=get_gradio_language(language)), history_output: history_to_chatbot_messages(_history),120 unmodified lines preview_val = send_to_sandbox(final_content) elif language == "python" and is_streamlit_code(final_content): preview_val = send_streamlit_to_stlite(final_content) elif language == "gradio" or (language == "python" and is_gradio_code(final_content)): preview_val = send_gradio_to_lite(final_content) yield { code_output: final_content, history: _history,744 unmodified lines lines=3, visible=True ) # Language dropdown for code generation (add Streamlit as a first-class option) # Language dropdown for code generation (add Streamlit and Gradio as first-class options) language_choices = [ "html", "streamlit", "python", "transformers.js", "svelte", "c", "cpp", "markdown", "latex", "json", "css", "javascript", "jinja2", "typescript", "yaml", "dockerfile", "shell", "r", "sql", "sql-msSQL", "sql-mySQL", "sql-mariaDB", "sql-sqlite", "sql-cassandra", "sql-plSQL", "sql-hive", "sql-pgSQL", "sql-gql", "sql-gpSQL", "sql-sparkSQL", "sql-esper" "html", "streamlit", "gradio", "python", "transformers.js", "svelte", "c", "cpp", "markdown", "latex", "json", "css", "javascript", "jinja2", "typescript", "yaml", "dockerfile", "shell", "r", "sql", "sql-msSQL", "sql-mySQL", "sql-mariaDB", "sql-sqlite", "sql-cassandra", "sql-plSQL", "sql-hive", "sql-pgSQL", "sql-gql", "sql-gpSQL", "sql-sparkSQL", "sql-esper" ] language_dropdown = gr.Dropdown( choices=language_choices,167 unmodified lines elif is_streamlit_code(code): preview_html = send_streamlit_to_stlite(code) code_lang = "python" elif is_gradio_code(code): preview_html = send_gradio_to_lite(code) code_lang = "python" else: preview_html = "<div style='padding:1em;color:#888;text-align:center;'>Preview not available for this file type.</div>" code_lang = "html"34 unmodified lines return gr.update(value="Static (HTML)") elif language == "streamlit": return gr.update(value="Streamlit (Python)") elif language == "gradio": return gr.update(value="Gradio (Python)") else: return gr.update(value="Gradio (Python)")
5 unmodified lines return send_to_sandbox(code) if language == "streamlit": return send_streamlit_to_stlite(code) if is_streamlit_code(code) else "<div style='padding:1em;color:#888;text-align:center;'>Add `import streamlit as st` to enable Streamlit preview.</div>" if language == "gradio": return send_gradio_to_lite(code) if is_gradio_code(code) else "<div style='padding:1em;color:#888;text-align:center;'>Add `import gradio as gr` to enable Gradio preview.</div>" if language == "python" or is_streamlit_code(code): if is_streamlit_code(code): return send_streamlit_to_stlite(code)app.py+60-2