Update app.py
Browse files
app.py
CHANGED
@@ -8,17 +8,14 @@ from deployment import deploy_to_hf_space
|
|
8 |
from utils import get_gradio_language, history_to_chatbot_messages
|
9 |
|
10 |
# --- Event Handlers ---
|
|
|
11 |
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):
|
12 |
-
# This is the section with the error
|
13 |
if not any([query, image, file, website_url]):
|
14 |
yield {
|
15 |
-
# <--- FIX: Change language="text" to language="markdown" ---
|
16 |
code_output: gr.update(value="## Please provide a prompt, image, file, or URL.", language="markdown"),
|
17 |
history_output: history_to_chatbot_messages(history)
|
18 |
}
|
19 |
return
|
20 |
-
|
21 |
-
# The rest of the function is correct and needs no changes
|
22 |
model_config = next((m for m in AVAILABLE_MODELS if m['name'] == model_name), AVAILABLE_MODELS[0])
|
23 |
for response in generate_code(query, image, file.name if file else None, website_url, history, model_config, enable_search, language):
|
24 |
ui_update = {code_output: gr.update(value=response.get("code_output"), language=get_gradio_language(language))}
|
@@ -27,13 +24,25 @@ def on_generate_click(query: str, image: gr.Image, file: gr.File, website_url: s
|
|
27 |
ui_update[history_output] = history_to_chatbot_messages(response["history"])
|
28 |
yield ui_update
|
29 |
|
30 |
-
# ... (The rest of the file is correct and does not need to be changed) ...
|
31 |
def on_deploy_click(code: str, space_name: str, sdk_name: str, request: gr.Request):
|
32 |
hf_token = request.token
|
33 |
if not hf_token: return gr.update(value="⚠️ Please log in with your Hugging Face account to deploy.", visible=True)
|
34 |
sdk = {"Static (HTML)": "static", "Gradio (Python)": "gradio"}.get(sdk_name, "static")
|
35 |
return gr.update(value=deploy_to_hf_space(code, space_name, sdk, hf_token), visible=True)
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
# --- UI Layout ---
|
38 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue"), title="AnyCoder AI") as demo:
|
39 |
history_state = gr.State([])
|
@@ -57,7 +66,7 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue"), title="AnyCoder AI")
|
|
57 |
generate_btn = gr.Button("Generate", variant="primary", scale=2)
|
58 |
|
59 |
gr.Markdown("--- \n**Quick Examples**")
|
60 |
-
for item in DEMO_LIST:
|
61 |
gr.Button(item['title']).click(lambda d=item['description']: d, outputs=input_prompt)
|
62 |
|
63 |
if os.getenv("SPACE_ID"):
|
@@ -87,15 +96,15 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue"), title="AnyCoder AI")
|
|
87 |
inputs=[input_prompt, image_input, file_input, website_url_input, history_state, model_dropdown, search_toggle, language_dropdown],
|
88 |
outputs=[code_output, history_state, history_output])
|
89 |
|
90 |
-
|
91 |
-
|
92 |
|
93 |
clear_btn.click(lambda: ([], [], None, None, None, "", "", None, gr.HTML(visible=False), gr.Markdown(visible=True)),
|
94 |
outputs=[history_state, history_output, input_prompt, image_input, file_input, website_url_input, code_output, model_id, sandbox, placeholder])
|
95 |
|
96 |
def on_model_change(model_name):
|
97 |
mid = next((m['id'] for m in AVAILABLE_MODELS if m['name'] == model_name), None)
|
98 |
-
is_multimodal = mid in MULTIMODAL_MODELS
|
99 |
return mid, gr.update(visible=is_multimodal)
|
100 |
model_dropdown.change(on_model_change, inputs=model_dropdown, outputs=[model_id, image_input])
|
101 |
|
|
|
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 {
|
|
|
15 |
code_output: gr.update(value="## Please provide a prompt, image, file, or URL.", language="markdown"),
|
16 |
history_output: history_to_chatbot_messages(history)
|
17 |
}
|
18 |
return
|
|
|
|
|
19 |
model_config = next((m for m in AVAILABLE_MODELS if m['name'] == model_name), AVAILABLE_MODELS[0])
|
20 |
for response in generate_code(query, image, file.name if file else None, website_url, history, model_config, enable_search, language):
|
21 |
ui_update = {code_output: gr.update(value=response.get("code_output"), language=get_gradio_language(language))}
|
|
|
24 |
ui_update[history_output] = history_to_chatbot_messages(response["history"])
|
25 |
yield ui_update
|
26 |
|
|
|
27 |
def on_deploy_click(code: str, space_name: str, sdk_name: str, request: gr.Request):
|
28 |
hf_token = request.token
|
29 |
if not hf_token: return gr.update(value="⚠️ Please log in with your Hugging Face account to deploy.", visible=True)
|
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 |
generate_btn = gr.Button("Generate", variant="primary", scale=2)
|
67 |
|
68 |
gr.Markdown("--- \n**Quick Examples**")
|
69 |
+
for item in DEMO_LIST: # Display all demos
|
70 |
gr.Button(item['title']).click(lambda d=item['description']: d, outputs=input_prompt)
|
71 |
|
72 |
if os.getenv("SPACE_ID"):
|
|
|
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)),
|
103 |
outputs=[history_state, history_output, input_prompt, image_input, file_input, website_url_input, code_output, model_id, sandbox, placeholder])
|
104 |
|
105 |
def on_model_change(model_name):
|
106 |
mid = next((m['id'] for m in AVAILABLE_MODELS if m['name'] == model_name), None)
|
107 |
+
is_multimodal = mid in MULTIMODAL_MODELS if mid else False
|
108 |
return mid, gr.update(visible=is_multimodal)
|
109 |
model_dropdown.change(on_model_change, inputs=model_dropdown, outputs=[model_id, image_input])
|
110 |
|