builder / app.py
mgbam's picture
Update app.py
2deb7a7 verified
raw
history blame
4.77 kB
# app.py
from constants import *
from hf_client import get_inference_client, tavily_client
from tavily_search import enhance_query_with_search
from utils import *
from search_replace import *
from web_scraper import *
from deploy import *
import gradio as gr
def generation_code(
query: Optional[str],
image: Optional[gr.Image],
file: Optional[str],
website_url: Optional[str],
_setting: Dict[str, str],
_history: Optional[History],
_current_model: Dict,
enable_search: bool,
language: str,
provider: str
):
# (Exact same logic as in your monolith; omitted here for brevity)
# It should handle multimodal input, web scraping, file extraction,
# search/replace modifications, and stream responses via HF client.
...
with gr.Blocks(
theme=gr.themes.Base(
primary_hue="blue",
secondary_hue="gray",
neutral_hue="gray",
font=gr.themes.GoogleFont("Inter"),
font_mono=gr.themes.GoogleFont("JetBrains Mono"),
text_size=gr.themes.sizes.text_md,
spacing_size=gr.themes.sizes.spacing_md,
radius_size=gr.themes.sizes.radius_md
),
title="AnyCoder - AI Code Generator"
) as demo:
history = gr.State([])
setting = gr.State({"system": HTML_SYSTEM_PROMPT})
current_model = gr.State(AVAILABLE_MODELS[9])
open_panel = gr.State(None)
last_login_state = gr.State(None)
# Sidebar
with gr.Sidebar():
login_button = gr.LoginButton()
gr.Markdown("📥 Load Existing Project")
load_project_url = gr.Textbox(label="Hugging Face Space URL", placeholder="https://huggingface.co/spaces/username/project", lines=1)
load_project_btn = gr.Button("Import Project", variant="secondary", size="sm")
load_project_status = gr.Markdown(visible=False)
gr.Markdown("---")
input_box = gr.Textbox(label="What would you like to build?", placeholder="Describe your application...", lines=3)
language_dropdown = gr.Dropdown(
choices=[
"html", "python", "c", "cpp", "markdown", "latex", "json",
"css", "javascript", "jinja2", "typescript", "yaml", "dockerfile",
"shell", "r", "sql", "transformers.js"
],
value="html",
label="Code Language"
)
website_url_input = gr.Textbox(label="Website for redesign", placeholder="https://example.com", lines=1)
file_input = gr.File(
label="Reference file",
file_types=[".pdf", ".txt", ".md", ".csv", ".docx", ".jpg", ".png"]
)
image_input = gr.Image(label="UI design image", visible=False)
search_toggle = gr.Checkbox(label="🔍 Web search", value=False)
model_dropdown = gr.Dropdown(
choices=[m["name"] for m in AVAILABLE_MODELS],
value=AVAILABLE_MODELS[9]["name"],
label="Model"
)
btn = gr.Button("Generate", variant="primary", size="lg")
clear_btn = gr.Button("Clear", variant="secondary", size="sm")
# Main panel with tabs
with gr.Column():
with gr.Tabs():
with gr.Tab("Code"):
code_output = gr.Code(language="html", lines=25, interactive=True, label="Generated code")
with gr.Tab("Preview"):
sandbox = gr.HTML(label="Live preview")
with gr.Tab("History"):
history_output = gr.Chatbot(show_label=False, height=400, type="messages")
# Event hookups
load_project_btn.click(
fn=lambda url: handle_load_project(url),
inputs=[load_project_url],
outputs=[load_project_status, code_output, sandbox, load_project_url, history, history_output]
)
btn.click(
fn=generation_code,
inputs=[
input_box, image_input, file_input, website_url_input,
setting, history, current_model, search_toggle,
language_dropdown, gr.State("auto")
],
outputs=[code_output, history, sandbox, history_output]
)
clear_btn.click(
fn=lambda: ([], [], None, ""),
outputs=[history, history_output, file_input, website_url_input]
)
# Preview update
code_output.change(
fn=lambda code, lang: send_to_sandbox(code) if lang=="html" else "<div>No preview</div>",
inputs=[code_output, language_dropdown],
outputs=sandbox
)
# Model switch
model_dropdown.change(
fn=lambda name: (next(m for m in AVAILABLE_MODELS if m["name"]==name),),
inputs=model_dropdown,
outputs=current_model
)
if __name__ == "__main__":
demo.queue(api_open=False, default_concurrency_limit=20) \
.launch(show_api=False, ssr_mode=True, mcp_server=False)