File size: 4,767 Bytes
2deb7a7 6dcd973 2deb7a7 6dcd973 10686a9 2deb7a7 9b171dd 2deb7a7 e7d5ce8 2deb7a7 f7cf3be 2deb7a7 f7cf3be 2deb7a7 83257f1 2deb7a7 83257f1 2deb7a7 6dcd973 2deb7a7 f7cf3be 2deb7a7 f7cf3be 2deb7a7 f7cf3be 2deb7a7 f7cf3be 2deb7a7 |
1 2 3 4 5 6 7 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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# 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)
|