Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# /app.py
|
| 2 |
""" Main Gradio application for AnyCoder. """
|
| 3 |
import gradio as gr
|
|
@@ -8,7 +11,6 @@ from deployment import deploy_to_hf_space
|
|
| 8 |
from utils import get_gradio_language, history_to_chatbot_messages
|
| 9 |
|
| 10 |
# --- Event Handlers ---
|
| 11 |
-
|
| 12 |
def on_generate_click(query: str, image: gr.Image, file: gr.File, website_url: str, history: list, model_name: str, enable_search: bool, language: str):
|
| 13 |
if not any([query, image, file, website_url]):
|
| 14 |
yield {
|
|
@@ -30,19 +32,15 @@ def on_deploy_click(code: str, space_name: str, sdk_name: str, request: gr.Reque
|
|
| 30 |
sdk = {"Static (HTML)": "static", "Gradio (Python)": "gradio"}.get(sdk_name, "static")
|
| 31 |
return gr.update(value=deploy_to_hf_space(code, space_name, sdk, hf_token), visible=True)
|
| 32 |
|
| 33 |
-
# <--- FIX: Create a dedicated helper function for the preview logic ---
|
| 34 |
def update_preview(code: str):
|
| 35 |
"""Updates the preview pane based on the generated code."""
|
| 36 |
if code and code.strip():
|
| 37 |
-
# Sanitize the code for the iframe's srcdoc attribute
|
| 38 |
safe_code = code.replace('"', '"')
|
| 39 |
iframe_html = f'<iframe srcdoc="{safe_code}" width="100%" height="920px" style="border:0;"></iframe>'
|
| 40 |
return gr.HTML(iframe_html, visible=True), gr.Markdown(visible=False)
|
| 41 |
else:
|
| 42 |
-
# If there's no code, show the placeholder
|
| 43 |
return gr.HTML(visible=False), gr.Markdown(visible=True)
|
| 44 |
|
| 45 |
-
|
| 46 |
# --- UI Layout ---
|
| 47 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue"), title="AnyCoder AI") as demo:
|
| 48 |
history_state = gr.State([])
|
|
@@ -66,7 +64,7 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue"), title="AnyCoder AI")
|
|
| 66 |
generate_btn = gr.Button("Generate", variant="primary", scale=2)
|
| 67 |
|
| 68 |
gr.Markdown("--- \n**Quick Examples**")
|
| 69 |
-
for item in DEMO_LIST:
|
| 70 |
gr.Button(item['title']).click(lambda d=item['description']: d, outputs=input_prompt)
|
| 71 |
|
| 72 |
if os.getenv("SPACE_ID"):
|
|
@@ -96,7 +94,6 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue"), title="AnyCoder AI")
|
|
| 96 |
inputs=[input_prompt, image_input, file_input, website_url_input, history_state, model_dropdown, search_toggle, language_dropdown],
|
| 97 |
outputs=[code_output, history_state, history_output])
|
| 98 |
|
| 99 |
-
# <--- FIX: Use the new, clean helper function for the change event ---
|
| 100 |
code_output.change(fn=update_preview, inputs=code_output, outputs=[sandbox, placeholder])
|
| 101 |
|
| 102 |
clear_btn.click(lambda: ([], [], None, None, None, "", "", None, gr.HTML(visible=False), gr.Markdown(visible=True)),
|
|
|
|
| 1 |
+
|
| 2 |
+
#### 2. Corrected `app.py`
|
| 3 |
+
|
| 4 |
# /app.py
|
| 5 |
""" Main Gradio application for AnyCoder. """
|
| 6 |
import gradio as gr
|
|
|
|
| 11 |
from utils import get_gradio_language, history_to_chatbot_messages
|
| 12 |
|
| 13 |
# --- Event Handlers ---
|
|
|
|
| 14 |
def on_generate_click(query: str, image: gr.Image, file: gr.File, website_url: str, history: list, model_name: str, enable_search: bool, language: str):
|
| 15 |
if not any([query, image, file, website_url]):
|
| 16 |
yield {
|
|
|
|
| 32 |
sdk = {"Static (HTML)": "static", "Gradio (Python)": "gradio"}.get(sdk_name, "static")
|
| 33 |
return gr.update(value=deploy_to_hf_space(code, space_name, sdk, hf_token), visible=True)
|
| 34 |
|
|
|
|
| 35 |
def update_preview(code: str):
|
| 36 |
"""Updates the preview pane based on the generated code."""
|
| 37 |
if code and code.strip():
|
|
|
|
| 38 |
safe_code = code.replace('"', '"')
|
| 39 |
iframe_html = f'<iframe srcdoc="{safe_code}" width="100%" height="920px" style="border:0;"></iframe>'
|
| 40 |
return gr.HTML(iframe_html, visible=True), gr.Markdown(visible=False)
|
| 41 |
else:
|
|
|
|
| 42 |
return gr.HTML(visible=False), gr.Markdown(visible=True)
|
| 43 |
|
|
|
|
| 44 |
# --- UI Layout ---
|
| 45 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue"), title="AnyCoder AI") as demo:
|
| 46 |
history_state = gr.State([])
|
|
|
|
| 64 |
generate_btn = gr.Button("Generate", variant="primary", scale=2)
|
| 65 |
|
| 66 |
gr.Markdown("--- \n**Quick Examples**")
|
| 67 |
+
for item in DEMO_LIST:
|
| 68 |
gr.Button(item['title']).click(lambda d=item['description']: d, outputs=input_prompt)
|
| 69 |
|
| 70 |
if os.getenv("SPACE_ID"):
|
|
|
|
| 94 |
inputs=[input_prompt, image_input, file_input, website_url_input, history_state, model_dropdown, search_toggle, language_dropdown],
|
| 95 |
outputs=[code_output, history_state, history_output])
|
| 96 |
|
|
|
|
| 97 |
code_output.change(fn=update_preview, inputs=code_output, outputs=[sandbox, placeholder])
|
| 98 |
|
| 99 |
clear_btn.click(lambda: ([], [], None, None, None, "", "", None, gr.HTML(visible=False), gr.Markdown(visible=True)),
|