Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# /app.py (Corrected)
|
| 2 |
|
| 3 |
"""
|
| 4 |
Main Gradio application for AnyCoder.
|
|
@@ -7,11 +7,12 @@ This file defines the user interface, handles user interactions, and orchestrate
|
|
| 7 |
the calls to the backend logic in other modules.
|
| 8 |
"""
|
| 9 |
import gradio as gr
|
|
|
|
| 10 |
|
| 11 |
from config import AVAILABLE_MODELS, MULTIMODAL_MODELS
|
| 12 |
from core import generate_code
|
| 13 |
from deployment import deploy_to_hf_space
|
| 14 |
-
from utils import get_gradio_language, history_to_chatbot_messages
|
| 15 |
|
| 16 |
# --- UI Helper Functions ---
|
| 17 |
def send_to_sandbox(code: str, language: str) -> str:
|
|
@@ -29,7 +30,7 @@ def on_generate_click(
|
|
| 29 |
if not query and not image and not file and not website_url:
|
| 30 |
yield {
|
| 31 |
code_output: gr.update(value="Please provide a prompt, image, file, or URL.", language="text"),
|
| 32 |
-
history_output: history_to_chatbot_messages(history)
|
| 33 |
}
|
| 34 |
return
|
| 35 |
|
|
@@ -43,7 +44,6 @@ def on_generate_click(
|
|
| 43 |
}
|
| 44 |
if "history" in response:
|
| 45 |
ui_update[history_state] = response["history"]
|
| 46 |
-
# <--- FIX: Use the helper function to format history for the chatbot
|
| 47 |
ui_update[history_output] = history_to_chatbot_messages(response["history"])
|
| 48 |
yield ui_update
|
| 49 |
|
|
@@ -54,10 +54,10 @@ def on_model_change(model_name: str):
|
|
| 54 |
def on_language_change(language: str):
|
| 55 |
return gr.update(language=get_gradio_language(language))
|
| 56 |
|
| 57 |
-
# <--- FIX:
|
| 58 |
-
def on_deploy_click(code: str, space_name: str, sdk_name: str,
|
| 59 |
-
"""Handler for the deploy button."""
|
| 60 |
-
hf_token =
|
| 61 |
if not hf_token:
|
| 62 |
return gr.update(value="β οΈ Please log in with your Hugging Face account to deploy.", visible=True)
|
| 63 |
|
|
@@ -68,7 +68,6 @@ def on_deploy_click(code: str, space_name: str, sdk_name: str, auth_token: gr.OA
|
|
| 68 |
# --- Gradio UI Layout ---
|
| 69 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue"), title="AnyCoder") as demo:
|
| 70 |
history_state = gr.State([])
|
| 71 |
-
# hf_token_state = gr.State() # <--- FIX: No longer needed
|
| 72 |
|
| 73 |
gr.Markdown("# π€ AnyCoder - AI Code Generator")
|
| 74 |
gr.Markdown("Describe what you want to build, upload a design, or provide a URL to redesign. The AI will generate the code for you.")
|
|
@@ -89,13 +88,22 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue"), title="AnyCoder") as
|
|
| 89 |
clear_btn = gr.Button("Clear")
|
| 90 |
generate_btn = gr.Button("Generate", variant="primary", scale=2)
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
with gr.Column(scale=2):
|
| 100 |
with gr.Tabs():
|
| 101 |
with gr.Tab("Preview"):
|
|
@@ -103,7 +111,6 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue"), title="AnyCoder") as
|
|
| 103 |
with gr.Tab("Code"):
|
| 104 |
code_output = gr.Code(label="Generated Code", language="html", interactive=True)
|
| 105 |
with gr.Tab("History"):
|
| 106 |
-
# <--- FIX: Update Chatbot component to modern standards
|
| 107 |
history_output = gr.Chatbot(
|
| 108 |
label="Conversation History",
|
| 109 |
type="messages",
|
|
@@ -116,22 +123,12 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue"), title="AnyCoder") as
|
|
| 116 |
inputs=[input_prompt, image_input, file_input, website_url_input, history_state, model_dropdown, search_toggle, language_dropdown],
|
| 117 |
outputs=[code_output, sandbox, history_state, history_output]
|
| 118 |
)
|
| 119 |
-
|
| 120 |
-
clear_btn.click(lambda: ([], [], None, None, None, "", []),
|
| 121 |
outputs=[history_state, history_output, input_prompt, image_input, file_input, website_url_input, code_output])
|
| 122 |
|
| 123 |
model_dropdown.change(on_model_change, inputs=model_dropdown, outputs=image_input)
|
| 124 |
language_dropdown.change(on_language_change, inputs=language_dropdown, outputs=code_output)
|
| 125 |
language_dropdown.change(lambda code, lang: send_to_sandbox(code, lang), inputs=[code_output, language_dropdown], outputs=sandbox)
|
| 126 |
-
|
| 127 |
-
# <--- FIX: REMOVED the incorrect .login() event handler
|
| 128 |
-
|
| 129 |
-
# <--- FIX: Correct way to handle deployment click
|
| 130 |
-
deploy_btn.click(
|
| 131 |
-
on_deploy_click,
|
| 132 |
-
inputs=[code_output, space_name_input, sdk_dropdown, login_button], # Pass the button itself as input
|
| 133 |
-
outputs=deploy_status
|
| 134 |
-
)
|
| 135 |
|
| 136 |
if __name__ == "__main__":
|
| 137 |
demo.queue().launch()
|
|
|
|
| 1 |
+
# /app.py (Final Corrected Version)
|
| 2 |
|
| 3 |
"""
|
| 4 |
Main Gradio application for AnyCoder.
|
|
|
|
| 7 |
the calls to the backend logic in other modules.
|
| 8 |
"""
|
| 9 |
import gradio as gr
|
| 10 |
+
import os # <--- FIX: Import the os module
|
| 11 |
|
| 12 |
from config import AVAILABLE_MODELS, MULTIMODAL_MODELS
|
| 13 |
from core import generate_code
|
| 14 |
from deployment import deploy_to_hf_space
|
| 15 |
+
from utils import get_gradio_language, history_to_chatbot_messages
|
| 16 |
|
| 17 |
# --- UI Helper Functions ---
|
| 18 |
def send_to_sandbox(code: str, language: str) -> str:
|
|
|
|
| 30 |
if not query and not image and not file and not website_url:
|
| 31 |
yield {
|
| 32 |
code_output: gr.update(value="Please provide a prompt, image, file, or URL.", language="text"),
|
| 33 |
+
history_output: history_to_chatbot_messages(history)
|
| 34 |
}
|
| 35 |
return
|
| 36 |
|
|
|
|
| 44 |
}
|
| 45 |
if "history" in response:
|
| 46 |
ui_update[history_state] = response["history"]
|
|
|
|
| 47 |
ui_update[history_output] = history_to_chatbot_messages(response["history"])
|
| 48 |
yield ui_update
|
| 49 |
|
|
|
|
| 54 |
def on_language_change(language: str):
|
| 55 |
return gr.update(language=get_gradio_language(language))
|
| 56 |
|
| 57 |
+
# <--- FIX: Use gr.Request to get the auth token
|
| 58 |
+
def on_deploy_click(code: str, space_name: str, sdk_name: str, request: gr.Request):
|
| 59 |
+
"""Handler for the deploy button, now correctly using gr.Request."""
|
| 60 |
+
hf_token = request.token
|
| 61 |
if not hf_token:
|
| 62 |
return gr.update(value="β οΈ Please log in with your Hugging Face account to deploy.", visible=True)
|
| 63 |
|
|
|
|
| 68 |
# --- Gradio UI Layout ---
|
| 69 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue"), title="AnyCoder") as demo:
|
| 70 |
history_state = gr.State([])
|
|
|
|
| 71 |
|
| 72 |
gr.Markdown("# π€ AnyCoder - AI Code Generator")
|
| 73 |
gr.Markdown("Describe what you want to build, upload a design, or provide a URL to redesign. The AI will generate the code for you.")
|
|
|
|
| 88 |
clear_btn = gr.Button("Clear")
|
| 89 |
generate_btn = gr.Button("Generate", variant="primary", scale=2)
|
| 90 |
|
| 91 |
+
# <--- FIX: Conditionally render the deployment UI only when on a HF Space
|
| 92 |
+
if os.getenv("SPACE_ID"):
|
| 93 |
+
with gr.Accordion("π Deploy to Hugging Face", open=False):
|
| 94 |
+
login_button = gr.LoginButton()
|
| 95 |
+
space_name_input = gr.Textbox(label="New App Name", placeholder="my-cool-app")
|
| 96 |
+
sdk_dropdown = gr.Dropdown(choices=["Static (HTML)", "Gradio (Python)"], value="Static (HTML)", label="App Type")
|
| 97 |
+
deploy_btn = gr.Button("Deploy")
|
| 98 |
+
deploy_status = gr.Markdown(visible=False)
|
| 99 |
+
|
| 100 |
+
# <--- FIX: Correct event handler wiring
|
| 101 |
+
deploy_btn.click(
|
| 102 |
+
on_deploy_click,
|
| 103 |
+
inputs=[code_output, space_name_input, sdk_dropdown],
|
| 104 |
+
outputs=deploy_status
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
with gr.Column(scale=2):
|
| 108 |
with gr.Tabs():
|
| 109 |
with gr.Tab("Preview"):
|
|
|
|
| 111 |
with gr.Tab("Code"):
|
| 112 |
code_output = gr.Code(label="Generated Code", language="html", interactive=True)
|
| 113 |
with gr.Tab("History"):
|
|
|
|
| 114 |
history_output = gr.Chatbot(
|
| 115 |
label="Conversation History",
|
| 116 |
type="messages",
|
|
|
|
| 123 |
inputs=[input_prompt, image_input, file_input, website_url_input, history_state, model_dropdown, search_toggle, language_dropdown],
|
| 124 |
outputs=[code_output, sandbox, history_state, history_output]
|
| 125 |
)
|
| 126 |
+
clear_btn.click(lambda: ([], [], None, None, None, "", ""),
|
|
|
|
| 127 |
outputs=[history_state, history_output, input_prompt, image_input, file_input, website_url_input, code_output])
|
| 128 |
|
| 129 |
model_dropdown.change(on_model_change, inputs=model_dropdown, outputs=image_input)
|
| 130 |
language_dropdown.change(on_language_change, inputs=language_dropdown, outputs=code_output)
|
| 131 |
language_dropdown.change(lambda code, lang: send_to_sandbox(code, lang), inputs=[code_output, language_dropdown], outputs=sandbox)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
if __name__ == "__main__":
|
| 134 |
demo.queue().launch()
|