akhaliq HF Staff commited on
Commit
586f5a7
·
1 Parent(s): 5d2d97e

add gradio lite for preview

Browse files
Files changed (1) hide show
  1. app.py +60 -2
app.py CHANGED
@@ -38,6 +38,8 @@ def get_gradio_language(language):
38
  # Map composite options to a supported syntax highlighting
39
  if language == "streamlit":
40
  return "python"
 
 
41
  return language if language in GRADIO_SUPPORTED_LANGUAGES else None
42
 
43
  # Search/Replace Constants
@@ -1603,6 +1605,47 @@ def send_streamlit_to_stlite(code: str) -> str:
1603
  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>'
1604
  return iframe
1605
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1606
  def demo_card_click(e: gr.EventData):
1607
  try:
1608
  # Get the index from the event data
@@ -2456,6 +2499,8 @@ This will help me create a better design for you."""
2456
  preview_val = send_to_sandbox(clean_code)
2457
  elif language == "python" and is_streamlit_code(clean_code):
2458
  preview_val = send_streamlit_to_stlite(clean_code)
 
 
2459
  yield {
2460
  code_output: gr.update(value=clean_code, language=get_gradio_language(language)),
2461
  history_output: history_to_chatbot_messages(_history),
@@ -2471,6 +2516,8 @@ This will help me create a better design for you."""
2471
  preview_val = send_to_sandbox(clean_content)
2472
  elif language == "python" and is_streamlit_code(clean_content):
2473
  preview_val = send_streamlit_to_stlite(clean_content)
 
 
2474
  yield {
2475
  code_output: gr.update(value=clean_content, language=get_gradio_language(language)),
2476
  history_output: history_to_chatbot_messages(_history),
@@ -2482,6 +2529,8 @@ This will help me create a better design for you."""
2482
  preview_val = send_to_sandbox(clean_code)
2483
  elif language == "python" and is_streamlit_code(clean_code):
2484
  preview_val = send_streamlit_to_stlite(clean_code)
 
 
2485
  yield {
2486
  code_output: gr.update(value=clean_code, language=get_gradio_language(language)),
2487
  history_output: history_to_chatbot_messages(_history),
@@ -2608,6 +2657,8 @@ This will help me create a better design for you."""
2608
  preview_val = send_to_sandbox(final_content)
2609
  elif language == "python" and is_streamlit_code(final_content):
2610
  preview_val = send_streamlit_to_stlite(final_content)
 
 
2611
  yield {
2612
  code_output: final_content,
2613
  history: _history,
@@ -3358,9 +3409,9 @@ with gr.Blocks(
3358
  lines=3,
3359
  visible=True
3360
  )
3361
- # Language dropdown for code generation (add Streamlit as a first-class option)
3362
  language_choices = [
3363
- "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"
3364
  ]
3365
  language_dropdown = gr.Dropdown(
3366
  choices=language_choices,
@@ -3534,6 +3585,9 @@ with gr.Blocks(
3534
  elif is_streamlit_code(code):
3535
  preview_html = send_streamlit_to_stlite(code)
3536
  code_lang = "python"
 
 
 
3537
  else:
3538
  preview_html = "<div style='padding:1em;color:#888;text-align:center;'>Preview not available for this file type.</div>"
3539
  code_lang = "html"
@@ -3574,6 +3628,8 @@ with gr.Blocks(
3574
  return gr.update(value="Static (HTML)")
3575
  elif language == "streamlit":
3576
  return gr.update(value="Streamlit (Python)")
 
 
3577
  else:
3578
  return gr.update(value="Gradio (Python)")
3579
 
@@ -3585,6 +3641,8 @@ with gr.Blocks(
3585
  return send_to_sandbox(code)
3586
  if language == "streamlit":
3587
  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>"
 
 
3588
  if language == "python" or is_streamlit_code(code):
3589
  if is_streamlit_code(code):
3590
  return send_streamlit_to_stlite(code)
 
38
  # Map composite options to a supported syntax highlighting
39
  if language == "streamlit":
40
  return "python"
41
+ if language == "gradio":
42
+ return "python"
43
  return language if language in GRADIO_SUPPORTED_LANGUAGES else None
44
 
45
  # Search/Replace Constants
 
1605
  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>'
1606
  return iframe
1607
 
1608
+ def is_gradio_code(code: str) -> bool:
1609
+ """Heuristic check to determine if Python code is a Gradio app."""
1610
+ if not code:
1611
+ return False
1612
+ lowered = code.lower()
1613
+ return (
1614
+ "import gradio" in lowered
1615
+ or "from gradio" in lowered
1616
+ or "gr.Interface(" in code
1617
+ or "gr.Blocks(" in code
1618
+ )
1619
+
1620
+ def send_gradio_to_lite(code: str) -> str:
1621
+ """Render Gradio code using gradio-lite inside a sandboxed iframe for preview."""
1622
+ html_doc = (
1623
+ """<!doctype html>
1624
+ <html>
1625
+ <head>
1626
+ <meta charset=\"UTF-8\" />
1627
+ <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\" />
1628
+ <meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\" />
1629
+ <title>Gradio Preview</title>
1630
+ <script type=\"module\" crossorigin src=\"https://cdn.jsdelivr.net/npm/@gradio/lite/dist/lite.js\"></script>
1631
+ <link rel=\"stylesheet\" href=\"https://cdn.jsdelivr.net/npm/@gradio/lite/dist/lite.css\" />
1632
+ <style>html,body{margin:0;padding:0;height:100%;} gradio-lite{display:block;height:100%;}</style>
1633
+ </head>
1634
+ <body>
1635
+ <gradio-lite>
1636
+ """
1637
+ + (code or "")
1638
+ + """
1639
+ </gradio-lite>
1640
+ </body>
1641
+ </html>
1642
+ """
1643
+ )
1644
+ encoded_html = base64.b64encode(html_doc.encode('utf-8')).decode('utf-8')
1645
+ data_uri = f"data:text/html;charset=utf-8;base64,{encoded_html}"
1646
+ 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>'
1647
+ return iframe
1648
+
1649
  def demo_card_click(e: gr.EventData):
1650
  try:
1651
  # Get the index from the event data
 
2499
  preview_val = send_to_sandbox(clean_code)
2500
  elif language == "python" and is_streamlit_code(clean_code):
2501
  preview_val = send_streamlit_to_stlite(clean_code)
2502
+ elif language == "gradio" or (language == "python" and is_gradio_code(clean_code)):
2503
+ preview_val = send_gradio_to_lite(clean_code)
2504
  yield {
2505
  code_output: gr.update(value=clean_code, language=get_gradio_language(language)),
2506
  history_output: history_to_chatbot_messages(_history),
 
2516
  preview_val = send_to_sandbox(clean_content)
2517
  elif language == "python" and is_streamlit_code(clean_content):
2518
  preview_val = send_streamlit_to_stlite(clean_content)
2519
+ elif language == "gradio" or (language == "python" and is_gradio_code(clean_content)):
2520
+ preview_val = send_gradio_to_lite(clean_content)
2521
  yield {
2522
  code_output: gr.update(value=clean_content, language=get_gradio_language(language)),
2523
  history_output: history_to_chatbot_messages(_history),
 
2529
  preview_val = send_to_sandbox(clean_code)
2530
  elif language == "python" and is_streamlit_code(clean_code):
2531
  preview_val = send_streamlit_to_stlite(clean_code)
2532
+ elif language == "gradio" or (language == "python" and is_gradio_code(clean_code)):
2533
+ preview_val = send_gradio_to_lite(clean_code)
2534
  yield {
2535
  code_output: gr.update(value=clean_code, language=get_gradio_language(language)),
2536
  history_output: history_to_chatbot_messages(_history),
 
2657
  preview_val = send_to_sandbox(final_content)
2658
  elif language == "python" and is_streamlit_code(final_content):
2659
  preview_val = send_streamlit_to_stlite(final_content)
2660
+ elif language == "gradio" or (language == "python" and is_gradio_code(final_content)):
2661
+ preview_val = send_gradio_to_lite(final_content)
2662
  yield {
2663
  code_output: final_content,
2664
  history: _history,
 
3409
  lines=3,
3410
  visible=True
3411
  )
3412
+ # Language dropdown for code generation (add Streamlit and Gradio as first-class options)
3413
  language_choices = [
3414
+ "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"
3415
  ]
3416
  language_dropdown = gr.Dropdown(
3417
  choices=language_choices,
 
3585
  elif is_streamlit_code(code):
3586
  preview_html = send_streamlit_to_stlite(code)
3587
  code_lang = "python"
3588
+ elif is_gradio_code(code):
3589
+ preview_html = send_gradio_to_lite(code)
3590
+ code_lang = "python"
3591
  else:
3592
  preview_html = "<div style='padding:1em;color:#888;text-align:center;'>Preview not available for this file type.</div>"
3593
  code_lang = "html"
 
3628
  return gr.update(value="Static (HTML)")
3629
  elif language == "streamlit":
3630
  return gr.update(value="Streamlit (Python)")
3631
+ elif language == "gradio":
3632
+ return gr.update(value="Gradio (Python)")
3633
  else:
3634
  return gr.update(value="Gradio (Python)")
3635
 
 
3641
  return send_to_sandbox(code)
3642
  if language == "streamlit":
3643
  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>"
3644
+ if language == "gradio":
3645
+ 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>"
3646
  if language == "python" or is_streamlit_code(code):
3647
  if is_streamlit_code(code):
3648
  return send_streamlit_to_stlite(code)