Update app.py
Browse files
app.py
CHANGED
|
@@ -1,201 +1,182 @@
|
|
| 1 |
# app.py
|
| 2 |
-
|
| 3 |
"""
|
| 4 |
-
|
| 5 |
|
| 6 |
-
|
| 7 |
-
Supports
|
| 8 |
-
|
| 9 |
"""
|
| 10 |
|
|
|
|
| 11 |
import gradio as gr
|
| 12 |
-
from
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
from constants import (
|
| 16 |
HTML_SYSTEM_PROMPT,
|
| 17 |
TRANSFORMERS_JS_SYSTEM_PROMPT,
|
|
|
|
| 18 |
AVAILABLE_MODELS,
|
| 19 |
DEMO_LIST,
|
|
|
|
|
|
|
| 20 |
)
|
| 21 |
-
|
| 22 |
-
from
|
| 23 |
-
from
|
| 24 |
-
|
| 25 |
-
extract_website_content,
|
| 26 |
-
apply_search_replace_changes,
|
| 27 |
history_to_messages,
|
| 28 |
history_to_chatbot_messages,
|
| 29 |
remove_code_block,
|
| 30 |
parse_transformers_js_output,
|
| 31 |
format_transformers_js_output,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
)
|
| 33 |
-
from deploy
|
| 34 |
|
| 35 |
-
#
|
| 36 |
History = List[Tuple[str, str]]
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"r", "sql", "sql-msSQL", "sql-mySQL", "sql-mariaDB", "sql-sqlite",
|
| 44 |
-
"sql-cassandra", "sql-plSQL", "sql-hive", "sql-pgSQL", "sql-gql",
|
| 45 |
-
"sql-gpSQL", "sql-sparkSQL", "sql-esper"
|
| 46 |
-
]
|
| 47 |
-
|
| 48 |
-
def get_model_details(name: str) -> Optional[Model]:
|
| 49 |
-
for m in AVAILABLE_MODELS:
|
| 50 |
-
if m["name"] == name:
|
| 51 |
-
return m
|
| 52 |
-
return None
|
| 53 |
-
|
| 54 |
-
def generation_code(
|
| 55 |
-
query: Optional[str],
|
| 56 |
-
file: Optional[str],
|
| 57 |
website_url: Optional[str],
|
| 58 |
-
|
| 59 |
enable_search: bool,
|
| 60 |
language: str,
|
| 61 |
history: Optional[History],
|
| 62 |
) -> Tuple[str, History, str, List[Dict[str, str]]]:
|
|
|
|
| 63 |
query = query or ""
|
| 64 |
history = history or []
|
| 65 |
-
try:
|
| 66 |
-
# Choose system prompt based on language
|
| 67 |
-
if language == "html":
|
| 68 |
-
system_prompt = HTML_SYSTEM_PROMPT
|
| 69 |
-
elif language == "transformers.js":
|
| 70 |
-
system_prompt = TRANSFORMERS_JS_SYSTEM_PROMPT
|
| 71 |
-
else:
|
| 72 |
-
# Generic fallback prompt
|
| 73 |
-
system_prompt = (
|
| 74 |
-
f"You are an expert {language} developer. "
|
| 75 |
-
f"Write clean, idiomatic {language} code based on the user's request."
|
| 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 |
-
content = resp.choices[0].message.content
|
| 111 |
-
|
| 112 |
-
except Exception as e:
|
| 113 |
-
err = f"❌ **Error:**\n```\n{e}\n```"
|
| 114 |
-
history.append((query, err))
|
| 115 |
-
return "", history, "", history_to_chatbot_messages(history)
|
| 116 |
-
|
| 117 |
-
# Process model output
|
| 118 |
if language == "transformers.js":
|
| 119 |
-
files = parse_transformers_js_output(
|
| 120 |
-
code
|
| 121 |
preview = send_to_sandbox(files.get("index.html", ""))
|
| 122 |
else:
|
| 123 |
-
|
| 124 |
-
if history and
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
code = cleaned
|
| 128 |
preview = send_to_sandbox(code) if language == "html" else ""
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
return code,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
-
# --- Custom CSS ---
|
| 135 |
CUSTOM_CSS = """
|
| 136 |
-
body {
|
| 137 |
-
#
|
| 138 |
-
#subtitle { text-align: center; color: #4a5568; margin-bottom: 2.5rem; }
|
| 139 |
-
.gradio-container { background-color: #f7fafc; }
|
| 140 |
-
#gen_btn { box-shadow: 0 4px 6px rgba(0,0,0,0.1); }
|
| 141 |
"""
|
| 142 |
|
| 143 |
-
with gr.Blocks(
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
model_state = gr.State(initial_model)
|
| 147 |
|
| 148 |
-
|
| 149 |
-
gr.
|
|
|
|
|
|
|
| 150 |
|
|
|
|
| 151 |
with gr.Row():
|
| 152 |
-
with gr.Column(scale=1):
|
| 153 |
-
|
| 154 |
-
|
|
|
|
| 155 |
choices=[m["name"] for m in AVAILABLE_MODELS],
|
| 156 |
-
value=
|
| 157 |
-
label="AI Model"
|
| 158 |
)
|
| 159 |
|
| 160 |
-
|
| 161 |
with gr.Tabs():
|
| 162 |
-
with gr.Tab("
|
| 163 |
-
|
| 164 |
-
with gr.Tab("
|
| 165 |
-
|
| 166 |
-
with gr.Tab("
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
|
|
|
|
| 177 |
with gr.Column(scale=2):
|
| 178 |
with gr.Tabs():
|
| 179 |
-
with gr.Tab("
|
| 180 |
-
|
| 181 |
-
with gr.Tab("
|
| 182 |
-
|
| 183 |
-
with gr.Tab("
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
)
|
| 193 |
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
outputs=[prompt_in, file_in, url_in, history_state, code_out, preview_out, chat_out],
|
| 197 |
-
queue=False,
|
| 198 |
-
)
|
| 199 |
|
| 200 |
if __name__ == "__main__":
|
| 201 |
demo.queue().launch()
|
|
|
|
| 1 |
# app.py
|
|
|
|
| 2 |
"""
|
| 3 |
+
ShashaCode Builder – AI code‑generation playground.
|
| 4 |
|
| 5 |
+
• Hugging Face Spaces + Gradio front‑end
|
| 6 |
+
• Supports prompts, file upload, web‑site scraping, optional web search
|
| 7 |
+
• Streams code back, shows live HTML preview, can deploy to a user Space
|
| 8 |
"""
|
| 9 |
|
| 10 |
+
# ───────────────────────────────────────── Imports
|
| 11 |
import gradio as gr
|
| 12 |
+
from pathlib import Path
|
| 13 |
+
from typing import Dict, List, Optional, Tuple, Any
|
| 14 |
|
| 15 |
+
from constants import ( # ← all constants live here
|
|
|
|
| 16 |
HTML_SYSTEM_PROMPT,
|
| 17 |
TRANSFORMERS_JS_SYSTEM_PROMPT,
|
| 18 |
+
SYSTEM_PROMPTS,
|
| 19 |
AVAILABLE_MODELS,
|
| 20 |
DEMO_LIST,
|
| 21 |
+
GRADIO_SUPPORTED_LANGUAGES, # ← new import
|
| 22 |
+
SEARCH_START, DIVIDER, REPLACE_END,
|
| 23 |
)
|
| 24 |
+
|
| 25 |
+
from hf_client import get_inference_client
|
| 26 |
+
from tavily_search import enhance_query_with_search
|
| 27 |
+
from utils import ( # helpers split into utils.py
|
|
|
|
|
|
|
| 28 |
history_to_messages,
|
| 29 |
history_to_chatbot_messages,
|
| 30 |
remove_code_block,
|
| 31 |
parse_transformers_js_output,
|
| 32 |
format_transformers_js_output,
|
| 33 |
+
parse_svelte_output,
|
| 34 |
+
format_svelte_output,
|
| 35 |
+
apply_search_replace_changes,
|
| 36 |
+
apply_transformers_js_search_replace_changes,
|
| 37 |
+
extract_text_from_file,
|
| 38 |
+
extract_website_content,
|
| 39 |
+
get_gradio_language,
|
| 40 |
)
|
| 41 |
+
from deploy import send_to_sandbox
|
| 42 |
|
| 43 |
+
# ───────────────────────────────────────── Type Aliases
|
| 44 |
History = List[Tuple[str, str]]
|
| 45 |
+
ModelInfo = Dict[str, Any]
|
| 46 |
+
|
| 47 |
+
# ───────────────────────────────────────── Core Function
|
| 48 |
+
def generate_code(
|
| 49 |
+
query: str,
|
| 50 |
+
file_path: Optional[str],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
website_url: Optional[str],
|
| 52 |
+
model: ModelInfo,
|
| 53 |
enable_search: bool,
|
| 54 |
language: str,
|
| 55 |
history: Optional[History],
|
| 56 |
) -> Tuple[str, History, str, List[Dict[str, str]]]:
|
| 57 |
+
"""Main inference pipeline: build prompt → call model → post‑process."""
|
| 58 |
query = query or ""
|
| 59 |
history = history or []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
# 1. pick system prompt
|
| 62 |
+
if language == "html": system = HTML_SYSTEM_PROMPT
|
| 63 |
+
elif language == "transformers.js": system = TRANSFORMERS_JS_SYSTEM_PROMPT
|
| 64 |
+
else: system = SYSTEM_PROMPTS.get(language, HTML_SYSTEM_PROMPT)
|
| 65 |
+
|
| 66 |
+
# 2. build message list
|
| 67 |
+
messages = history_to_messages(history, system)
|
| 68 |
+
|
| 69 |
+
ctx_parts = [query.strip()]
|
| 70 |
+
|
| 71 |
+
if file_path: ctx_parts += ["[File]", extract_text_from_file(file_path)[:5000]]
|
| 72 |
+
if website_url:
|
| 73 |
+
html = extract_website_content(website_url)
|
| 74 |
+
if not html.startswith("Error"):
|
| 75 |
+
ctx_parts += ["[Website]", html[:8000]]
|
| 76 |
+
|
| 77 |
+
user_query = "\n\n".join(ctx_parts)
|
| 78 |
+
user_query = enhance_query_with_search(user_query, enable_search)
|
| 79 |
+
messages.append({"role": "user", "content": user_query})
|
| 80 |
+
|
| 81 |
+
# 3. call model
|
| 82 |
+
client = get_inference_client(model["id"])
|
| 83 |
+
resp = client.chat.completions.create(
|
| 84 |
+
model=model["id"],
|
| 85 |
+
messages=messages,
|
| 86 |
+
max_tokens=16000,
|
| 87 |
+
temperature=0.15,
|
| 88 |
+
)
|
| 89 |
+
answer = resp.choices[0].message.content
|
| 90 |
+
|
| 91 |
+
# 4. post‑process
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
if language == "transformers.js":
|
| 93 |
+
files = parse_transformers_js_output(answer)
|
| 94 |
+
code = format_transformers_js_output(files)
|
| 95 |
preview = send_to_sandbox(files.get("index.html", ""))
|
| 96 |
else:
|
| 97 |
+
clean = remove_code_block(answer)
|
| 98 |
+
if history and not history[-1][1].startswith("❌"):
|
| 99 |
+
clean = apply_search_replace_changes(history[-1][1], clean)
|
| 100 |
+
code = clean
|
|
|
|
| 101 |
preview = send_to_sandbox(code) if language == "html" else ""
|
| 102 |
|
| 103 |
+
history.append((query, code))
|
| 104 |
+
chat_msgs = history_to_chatbot_messages(history)
|
| 105 |
+
return code, history, preview, chat_msgs
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
# ───────────────────────────────────────── UI
|
| 109 |
+
LOGO_PATH = "assets/logo.png" # ensure this file exists
|
| 110 |
|
|
|
|
| 111 |
CUSTOM_CSS = """
|
| 112 |
+
body {font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;}
|
| 113 |
+
#logo {max-height:64px;margin:auto;}
|
|
|
|
|
|
|
|
|
|
| 114 |
"""
|
| 115 |
|
| 116 |
+
with gr.Blocks(css=CUSTOM_CSS, title="ShashaCode Builder") as demo:
|
| 117 |
+
state_history = gr.State([])
|
| 118 |
+
state_model = gr.State(AVAILABLE_MODELS[0])
|
|
|
|
| 119 |
|
| 120 |
+
# Header
|
| 121 |
+
with gr.Row():
|
| 122 |
+
gr.Image(LOGO_PATH, elem_id="logo", show_label=False, height=64)
|
| 123 |
+
gr.Markdown("## **AnyCoder AI**\nYour AI partner for generating, modifying & understanding code.")
|
| 124 |
|
| 125 |
+
# Sidebar (inputs)
|
| 126 |
with gr.Row():
|
| 127 |
+
with gr.Column(scale=1, min_width=300):
|
| 128 |
+
# Model
|
| 129 |
+
dd_model = gr.Dropdown(
|
| 130 |
+
label="AI Model",
|
| 131 |
choices=[m["name"] for m in AVAILABLE_MODELS],
|
| 132 |
+
value=AVAILABLE_MODELS[0]["name"],
|
|
|
|
| 133 |
)
|
| 134 |
|
| 135 |
+
# Prompt / File / Website tabs
|
| 136 |
with gr.Tabs():
|
| 137 |
+
with gr.Tab("Prompt"):
|
| 138 |
+
tb_prompt = gr.Textbox(label="Describe what you'd like to build…", lines=6)
|
| 139 |
+
with gr.Tab("File"):
|
| 140 |
+
inp_file = gr.File(label="Reference file", type="filepath")
|
| 141 |
+
with gr.Tab("Website"):
|
| 142 |
+
tb_url = gr.Textbox(label="URL to redesign")
|
| 143 |
+
|
| 144 |
+
# Output config
|
| 145 |
+
dd_lang = gr.Dropdown(
|
| 146 |
+
label="Target language",
|
| 147 |
+
choices=[l for l in GRADIO_SUPPORTED_LANGUAGES if l], # ← fixed list
|
| 148 |
+
value="html",
|
| 149 |
+
)
|
| 150 |
+
chk_search = gr.Checkbox(label="Enable Tavily Web Search")
|
| 151 |
|
| 152 |
+
# Buttons
|
| 153 |
+
btn_generate = gr.Button("Generate Code", variant="primary")
|
| 154 |
+
btn_clear = gr.Button("Clear Session", variant="secondary")
|
| 155 |
|
| 156 |
+
# Main panel (outputs)
|
| 157 |
with gr.Column(scale=2):
|
| 158 |
with gr.Tabs():
|
| 159 |
+
with gr.Tab("Code"):
|
| 160 |
+
out_code = gr.Code(language="html", show_label=False)
|
| 161 |
+
with gr.Tab("Preview"):
|
| 162 |
+
out_prev = gr.HTML()
|
| 163 |
+
with gr.Tab("History"):
|
| 164 |
+
out_hist = gr.Chatbot(type="messages")
|
| 165 |
+
|
| 166 |
+
# ─── Callbacks ─────────────────────────────────────────────
|
| 167 |
+
def _model_from_name(name):
|
| 168 |
+
return next((m for m in AVAILABLE_MODELS if m["name"] == name), AVAILABLE_MODELS[0])
|
| 169 |
+
|
| 170 |
+
dd_model.change(lambda n: _model_from_name(n), inputs=dd_model, outputs=state_model)
|
| 171 |
+
|
| 172 |
+
btn_generate.click(
|
| 173 |
+
fn=generate_code,
|
| 174 |
+
inputs=[tb_prompt, inp_file, tb_url, state_model, chk_search, dd_lang, state_history],
|
| 175 |
+
outputs=[out_code, state_history, out_prev, out_hist],
|
| 176 |
)
|
| 177 |
|
| 178 |
+
btn_clear.click(lambda: ("", None, "", [], "", []),
|
| 179 |
+
outputs=[tb_prompt, inp_file, tb_url, state_history, out_code, out_prev])
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
if __name__ == "__main__":
|
| 182 |
demo.queue().launch()
|