Update app.py
Browse files
app.py
CHANGED
@@ -1,112 +1,41 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
import
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
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([])
|
47 |
-
gr.Markdown("# 🚀 AnyCoder AI - Multi-Provider Code Generator")
|
48 |
-
|
49 |
-
with gr.Row():
|
50 |
-
with gr.Column(scale=1):
|
51 |
-
input_prompt = gr.Textbox(label="Prompt", placeholder="e.g., a login form with a blue button", lines=4)
|
52 |
-
model_dropdown = gr.Dropdown(choices=[m['name'] for m in AVAILABLE_MODELS], value=AVAILABLE_MODELS[0]['name'], label="Select Model")
|
53 |
-
language_dropdown = gr.Dropdown(choices=["html", "python", "javascript", "css", "react"], value="html", label="Language")
|
54 |
-
website_url_input = gr.Textbox(label="URL to Redesign", placeholder="https://example.com")
|
55 |
-
|
56 |
-
with gr.Row():
|
57 |
-
file_input = gr.File(label="Reference File", scale=2)
|
58 |
-
image_input = gr.Image(label="UI Image", type="numpy", visible=False, scale=1)
|
59 |
-
|
60 |
-
search_toggle = gr.Checkbox(label="Enable Web Search", value=False)
|
61 |
-
|
62 |
-
with gr.Row():
|
63 |
-
clear_btn = gr.Button("Clear")
|
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"):
|
71 |
-
with gr.Accordion("🚀 Deploy to Hugging Face", open=False):
|
72 |
-
login_button = gr.LoginButton()
|
73 |
-
space_name_input = gr.Textbox(label="New App Name", placeholder="my-anycoder-app")
|
74 |
-
sdk_dropdown = gr.Dropdown(choices=["Static (HTML)", "Gradio (Python)"], value="Static (HTML)", label="App Type")
|
75 |
-
deploy_btn = gr.Button("Deploy")
|
76 |
-
deploy_status = gr.Markdown(visible=False)
|
77 |
-
|
78 |
-
with gr.Column(scale=2):
|
79 |
-
with gr.Tabs() as tabs:
|
80 |
-
preview_tab = gr.Tab("Preview")
|
81 |
-
code_tab = gr.Tab("Code")
|
82 |
-
history_tab = gr.Tab("History")
|
83 |
-
with preview_tab:
|
84 |
-
sandbox = gr.HTML(label="Live Preview", elem_id="sandbox-preview", visible=False)
|
85 |
-
placeholder = gr.Markdown("### Your live preview will appear here after generation.")
|
86 |
-
with code_tab:
|
87 |
-
code_output = gr.Code(label="Generated Code", language="html", interactive=True)
|
88 |
-
with history_tab:
|
89 |
-
history_output = gr.Chatbot(label="Conversation History", type="messages", height=600)
|
90 |
-
|
91 |
-
# --- Event Wiring ---
|
92 |
-
model_id = gr.State()
|
93 |
-
generate_btn.click(on_generate_click,
|
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)),
|
100 |
-
outputs=[history_state, history_output, input_prompt, image_input, file_input, website_url_input, code_output, model_id, sandbox, placeholder])
|
101 |
-
|
102 |
-
def on_model_change(model_name):
|
103 |
-
mid = next((m['id'] for m in AVAILABLE_MODELS if m['name'] == model_name), None)
|
104 |
-
is_multimodal = mid in MULTIMODAL_MODELS if mid else False
|
105 |
-
return mid, gr.update(visible=is_multimodal)
|
106 |
-
model_dropdown.change(on_model_change, inputs=model_dropdown, outputs=[model_id, image_input])
|
107 |
-
|
108 |
-
if os.getenv("SPACE_ID"):
|
109 |
-
deploy_btn.click(on_deploy_click, inputs=[code_output, space_name_input, sdk_dropdown], outputs=deploy_status)
|
110 |
-
|
111 |
-
if __name__ == "__main__":
|
112 |
-
demo.queue().launch()
|
|
|
1 |
+
from constants import *
|
2 |
+
from hf_client import get_inference_client, tavily_client
|
3 |
+
from tavily_search import enhance_query_with_search
|
4 |
+
from utils import *
|
5 |
+
from search_replace import *
|
6 |
+
from web_scraper import *
|
7 |
+
from deploy import *
|
8 |
+
|
9 |
+
with gr.Blocks(
|
10 |
+
theme=gr.themes.Base(
|
11 |
+
primary_hue="blue",
|
12 |
+
secondary_hue="gray",
|
13 |
+
neutral_hue="gray",
|
14 |
+
font=gr.themes.GoogleFont("Inter"),
|
15 |
+
font_mono=gr.themes.GoogleFont("JetBrains Mono"),
|
16 |
+
text_size=gr.themes.sizes.text_md,
|
17 |
+
spacing_size=gr.themes.sizes.spacing_md,
|
18 |
+
radius_size=gr.themes.sizes.radius_md
|
19 |
+
),
|
20 |
+
title="AnyCoder - AI Code Generator"
|
21 |
+
) as demo:
|
22 |
+
history=gr.State([])
|
23 |
+
setting=gr.State({"system": HTML_SYSTEM_PROMPT})
|
24 |
+
current_model=gr.State(AVAILABLE_MODELS[9])
|
25 |
+
open_panel=gr.State(None)
|
26 |
+
last_login_state=gr.State(None)
|
27 |
+
|
28 |
+
# Sidebar definition, inputs, buttons...
|
29 |
+
# (exactly as in the monolith)
|
30 |
+
|
31 |
+
def generation_code(query, image, file, website_url, _setting, _history, _current_model, enable_search, language, provider):
|
32 |
+
...
|
33 |
+
# (exactly the full function from the monolith)
|
34 |
+
|
35 |
+
# Event handlers: load_project_btn.click, btn.click -> generation_code, clear_btn.click, etc.
|
36 |
+
|
37 |
+
# Deploy button hookup
|
38 |
+
|
39 |
+
if __name__=="__main__":
|
40 |
+
demo.queue(api_open=False, default_concurrency_limit=20)\
|
41 |
+
.launch(show_api=False, ssr_mode=True, mcp_server=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|