add gradio lite for preview
Browse files37 unmodified lines3839404142431559 unmodified lines160316041605160616071608847 unmodified lines2456245724582459246024619 unmodified lines2471247224732474247524765 unmodified lines248224832484248524862487120 unmodified lines260826092610261126122613744 unmodified lines335833593360336133623363336433653366167 unmodified lines35343535353635373538353934 unmodified lines3574357535763577357835795 unmodified lines35853586358735883589359037 unmodified lines# Map composite options to a supported syntax highlightingif language == "streamlit":return "python"return language if language in GRADIO_SUPPORTED_LANGUAGES else None# Search/Replace Constants1559 unmodified linesiframe = 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 iframedef demo_card_click(e: gr.EventData):try:# Get the index from the event data847 unmodified linespreview_val = send_to_sandbox(clean_code)elif language == "python" and is_streamlit_code(clean_code):preview_val = send_streamlit_to_stlite(clean_code)yield {code_output: gr.update(value=clean_code, language=get_gradio_language(language)),history_output: history_to_chatbot_messages(_history),9 unmodified linespreview_val = send_to_sandbox(clean_content)elif language == "python" and is_streamlit_code(clean_content):preview_val = send_streamlit_to_stlite(clean_content)yield {code_output: gr.update(value=clean_content, language=get_gradio_language(language)),history_output: history_to_chatbot_messages(_history),5 unmodified linespreview_val = send_to_sandbox(clean_code)elif language == "python" and is_streamlit_code(clean_code):preview_val = send_streamlit_to_stlite(clean_code)yield {code_output: gr.update(value=clean_code, language=get_gradio_language(language)),history_output: history_to_chatbot_messages(_history),120 unmodified linespreview_val = send_to_sandbox(final_content)elif language == "python" and is_streamlit_code(final_content):preview_val = send_streamlit_to_stlite(final_content)yield {code_output: final_content,history: _history,744 unmodified lineslines=3,visible=True)# Language dropdown for code generation (add Streamlit as a first-class option)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"]language_dropdown = gr.Dropdown(choices=language_choices,167 unmodified lineselif is_streamlit_code(code):preview_html = send_streamlit_to_stlite(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 linesreturn gr.update(value="Static (HTML)")elif language == "streamlit":return gr.update(value="Streamlit (Python)")else:return gr.update(value="Gradio (Python)")5 unmodified linesreturn 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 == "python" or is_streamlit_code(code):if is_streamlit_code(code):return send_streamlit_to_stlite(code)37 unmodified lines38394041424344451559 unmodified lines16051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651847 unmodified lines249925002501250225032504250525069 unmodified lines251625172518251925202521252225235 unmodified lines25292530253125322533253425352536120 unmodified lines26572658265926602661266226632664744 unmodified lines340934103411341234133414341534163417167 unmodified lines35853586358735883589359035913592359334 unmodified lines362836293630363136323633363436355 unmodified lines3641364236433644364536463647364837 unmodified lines# Map composite options to a supported syntax highlightingif language == "streamlit":return "python"if language == "gradio":return "python"return language if language in GRADIO_SUPPORTED_LANGUAGES else None# Search/Replace Constants1559 unmodified linesiframe = 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 iframedef is_gradio_code(code: str) -> bool:"""Heuristic check to determine if Python code is a Gradio app."""if not code:return Falselowered = code.lower()return ("import gradio" in loweredor "from gradio" in loweredor "gr.Interface(" in codeor "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 iframedef demo_card_click(e: gr.EventData):try:# Get the index from the event data847 unmodified linespreview_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 linespreview_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 linespreview_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 linespreview_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 lineslines=3,visible=True)# Language dropdown for code generation (add Streamlit and Gradio as first-class options)language_choices = ["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 lineselif 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 linesreturn 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 linesreturn 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