Update app.py
Browse files
app.py
CHANGED
@@ -1,212 +1,202 @@
|
|
|
|
|
|
|
|
|
|
1 |
"""
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
7 |
"""
|
8 |
|
|
|
|
|
|
|
|
|
|
|
9 |
import gradio as gr
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
from
|
14 |
-
|
15 |
-
|
16 |
-
AVAILABLE_MODELS,
|
17 |
-
DEMO_LIST,
|
18 |
-
)
|
19 |
-
from hf_client import get_inference_client
|
20 |
-
from tavily_search import enhance_query_with_search
|
21 |
-
from utils import (
|
22 |
extract_text_from_file,
|
23 |
extract_website_content,
|
24 |
-
apply_search_replace_changes,
|
25 |
history_to_messages,
|
26 |
history_to_chatbot_messages,
|
|
|
27 |
remove_code_block,
|
28 |
parse_transformers_js_output,
|
29 |
format_transformers_js_output,
|
30 |
)
|
31 |
-
from deploy
|
32 |
-
|
33 |
-
#
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
SUPPORTED_LANGUAGES = [
|
39 |
"python", "c", "cpp", "markdown", "latex", "json", "html", "css",
|
40 |
"javascript", "jinja2", "typescript", "yaml", "dockerfile", "shell",
|
41 |
"r", "sql", "sql-msSQL", "sql-mySQL", "sql-mariaDB", "sql-sqlite",
|
42 |
"sql-cassandra", "sql-plSQL", "sql-hive", "sql-pgSQL", "sql-gql",
|
43 |
-
"sql-gpSQL", "sql-sparkSQL", "sql-esper"
|
44 |
]
|
|
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
query: Optional[str],
|
54 |
-
file: Optional[str],
|
55 |
-
website_url: Optional[str],
|
56 |
-
current_model: Model,
|
57 |
-
enable_search: bool,
|
58 |
language: str,
|
59 |
-
|
|
|
60 |
) -> Tuple[str, History, str, List[Dict[str, str]]]:
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
try:
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
else:
|
70 |
-
# Generic fallback prompt
|
71 |
-
system_prompt = (
|
72 |
-
f"You are an expert {language} developer. "
|
73 |
-
f"Write clean, idiomatic {language} code based on the user's request."
|
74 |
-
)
|
75 |
|
76 |
-
|
77 |
-
# Determine provider
|
78 |
-
if model_id.startswith("openai/") or model_id in {"gpt-4", "gpt-3.5-turbo"}:
|
79 |
-
provider = "openai"
|
80 |
-
elif model_id.startswith("gemini/") or model_id.startswith("google/"):
|
81 |
-
provider = "gemini"
|
82 |
-
elif model_id.startswith("fireworks-ai/"):
|
83 |
-
provider = "fireworks-ai"
|
84 |
-
else:
|
85 |
-
provider = "auto"
|
86 |
-
|
87 |
-
# Build message history
|
88 |
-
msgs = history_to_messages(history, system_prompt)
|
89 |
-
context = query
|
90 |
-
if file:
|
91 |
-
ftext = extract_text_from_file(file)
|
92 |
-
context += f"\n\n[Attached file]\n{ftext[:5000]}"
|
93 |
-
if website_url:
|
94 |
-
wtext = extract_website_content(website_url)
|
95 |
-
if not wtext.startswith("Error"):
|
96 |
-
context += f"\n\n[Website content]\n{wtext[:8000]}"
|
97 |
-
final_q = enhance_query_with_search(context, enable_search)
|
98 |
-
msgs.append({"role": "user", "content": final_q})
|
99 |
-
|
100 |
-
# Call the model
|
101 |
-
client = get_inference_client(model_id, provider)
|
102 |
-
resp = client.chat.completions.create(
|
103 |
-
model=model_id,
|
104 |
-
messages=msgs,
|
105 |
-
max_tokens=16000,
|
106 |
-
temperature=0.1
|
107 |
-
)
|
108 |
-
content = resp.choices[0].message.content
|
109 |
-
|
110 |
-
except Exception as e:
|
111 |
-
err = f"❌ **Error:**\n```\n{e}\n```"
|
112 |
-
history.append((query, err))
|
113 |
-
return "", history, "", history_to_chatbot_messages(history)
|
114 |
-
|
115 |
-
# Process model output
|
116 |
if language == "transformers.js":
|
117 |
-
files = parse_transformers_js_output(
|
118 |
code = format_transformers_js_output(files)
|
119 |
preview = send_to_sandbox(files.get("index.html", ""))
|
120 |
else:
|
121 |
-
cleaned = remove_code_block(
|
122 |
-
if
|
123 |
-
|
124 |
-
|
125 |
-
code = cleaned
|
126 |
preview = send_to_sandbox(code) if language == "html" else ""
|
127 |
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
.gradio-container {
|
138 |
-
|
|
|
|
|
|
|
|
|
139 |
"""
|
140 |
|
141 |
-
with gr.Blocks(
|
142 |
-
|
143 |
-
initial_model = AVAILABLE_MODELS[0]
|
144 |
-
model_state = gr.State(initial_model)
|
145 |
|
146 |
-
|
147 |
-
gr.Markdown(
|
|
|
|
|
|
|
148 |
|
149 |
-
with gr.Row():
|
150 |
-
|
151 |
-
|
|
|
152 |
model_dd = gr.Dropdown(
|
153 |
-
choices=[m
|
154 |
-
value=
|
155 |
-
label="AI
|
156 |
)
|
157 |
|
158 |
-
gr.Markdown("### 2
|
159 |
with gr.Tabs():
|
160 |
-
with gr.Tab("
|
161 |
-
|
162 |
-
with gr.Tab("
|
163 |
-
|
164 |
-
with gr.Tab("
|
165 |
-
|
166 |
|
167 |
-
gr.Markdown("### 3
|
168 |
-
lang_dd = gr.Dropdown(SUPPORTED_LANGUAGES, value="html", label="Target
|
169 |
-
search_chk = gr.Checkbox(label="Enable Web
|
170 |
|
171 |
with gr.Row():
|
172 |
-
|
173 |
-
gen_btn
|
174 |
-
|
175 |
-
# --- Hugging Face Integration ---
|
176 |
-
gr.Markdown("---")
|
177 |
-
gr.Markdown("#### Hugging Face Integration")
|
178 |
-
hf_url = gr.Textbox(label="Load HF Space URL")
|
179 |
-
hf_load_btn = gr.Button("Import Project")
|
180 |
-
hf_status = gr.Markdown(visible=False)
|
181 |
-
|
182 |
-
hf_load_btn.click(
|
183 |
-
fn=lambda u: load_project_from_url(u),
|
184 |
-
inputs=hf_url,
|
185 |
-
outputs=[hf_status, code_out, preview_out, hf_url, history_state, chat_out]
|
186 |
-
)
|
187 |
|
188 |
-
|
|
|
189 |
with gr.Tabs():
|
190 |
-
with gr.Tab("
|
191 |
-
code_out = gr.Code(language="html"
|
192 |
-
with gr.Tab("
|
193 |
preview_out = gr.HTML()
|
194 |
-
with gr.Tab("
|
195 |
chat_out = gr.Chatbot(type="messages")
|
196 |
|
197 |
-
|
198 |
-
|
199 |
gen_btn.click(
|
200 |
-
|
201 |
-
inputs=[
|
202 |
-
|
|
|
|
|
|
|
203 |
)
|
204 |
|
205 |
-
|
206 |
-
lambda: ("", None, "", [],
|
207 |
-
outputs=[
|
|
|
|
|
|
|
|
|
208 |
queue=False,
|
209 |
)
|
210 |
|
|
|
|
|
211 |
if __name__ == "__main__":
|
212 |
demo.queue().launch()
|
|
|
1 |
+
# app.py
|
2 |
+
# ---------------------------------------------------------------------
|
3 |
+
# AnyCoder / Shasha AI – Gradio front‑end (no external static files)
|
4 |
+
# ---------------------------------------------------------------------
|
5 |
"""
|
6 |
+
Interactive UI for generating / modifying / previewing code with multiple
|
7 |
+
LLM back‑ends. Relies on:
|
8 |
+
|
9 |
+
• models.py – AVAILABLE_MODELS registry + find_model()
|
10 |
+
• inference.py – chat_completion() (provider routing handled there)
|
11 |
+
• utils.py – file / website extractors, history helpers, etc.
|
12 |
+
• tavily_search.py (optional) – enhance_query_with_search()
|
13 |
+
• deploy.py – send_to_sandbox() for live HTML preview
|
14 |
"""
|
15 |
|
16 |
+
from __future__ import annotations
|
17 |
+
|
18 |
+
from pathlib import Path
|
19 |
+
from typing import List, Tuple, Dict, Any, Optional
|
20 |
+
|
21 |
import gradio as gr
|
22 |
+
|
23 |
+
# ---------- local helpers --------------------------------------------------
|
24 |
+
from models import AVAILABLE_MODELS, find_model, ModelInfo
|
25 |
+
from inference import chat_completion
|
26 |
+
from tavily_search import enhance_query_with_search
|
27 |
+
from utils import (
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
extract_text_from_file,
|
29 |
extract_website_content,
|
|
|
30 |
history_to_messages,
|
31 |
history_to_chatbot_messages,
|
32 |
+
apply_search_replace_changes,
|
33 |
remove_code_block,
|
34 |
parse_transformers_js_output,
|
35 |
format_transformers_js_output,
|
36 |
)
|
37 |
+
from deploy import send_to_sandbox
|
38 |
+
|
39 |
+
# ---------- constants ------------------------------------------------------
|
40 |
+
SYSTEM_PROMPTS = {
|
41 |
+
"html": (
|
42 |
+
"ONLY USE HTML, CSS AND JAVASCRIPT. Produce ONE complete html file "
|
43 |
+
"wrapped in ```html … ```."
|
44 |
+
),
|
45 |
+
"transformers.js": (
|
46 |
+
"Generate THREE fenced blocks: index.html, index.js, style.css "
|
47 |
+
"for a transformers.js demo."
|
48 |
+
),
|
49 |
+
}
|
50 |
SUPPORTED_LANGUAGES = [
|
51 |
"python", "c", "cpp", "markdown", "latex", "json", "html", "css",
|
52 |
"javascript", "jinja2", "typescript", "yaml", "dockerfile", "shell",
|
53 |
"r", "sql", "sql-msSQL", "sql-mySQL", "sql-mariaDB", "sql-sqlite",
|
54 |
"sql-cassandra", "sql-plSQL", "sql-hive", "sql-pgSQL", "sql-gql",
|
55 |
+
"sql-gpSQL", "sql-sparkSQL", "sql-esper",
|
56 |
]
|
57 |
+
History = List[Tuple[str, str]] # [(prompt, code/result)]
|
58 |
|
59 |
+
|
60 |
+
# ---------- core generation callback --------------------------------------
|
61 |
+
def generate(
|
62 |
+
prompt: str,
|
63 |
+
file_path: str | None,
|
64 |
+
website_url: str | None,
|
65 |
+
model_name: str,
|
|
|
|
|
|
|
|
|
|
|
66 |
language: str,
|
67 |
+
use_search: bool,
|
68 |
+
hist: History | None,
|
69 |
) -> Tuple[str, History, str, List[Dict[str, str]]]:
|
70 |
+
"""Main callback wired to the “Generate Code” button."""
|
71 |
+
hist = hist or []
|
72 |
+
user_prompt = (prompt or "").strip()
|
73 |
+
|
74 |
+
# 1 · system + previous messages
|
75 |
+
sys = SYSTEM_PROMPTS.get(language, f"You are an expert {language} developer.")
|
76 |
+
messages = history_to_messages(hist, sys)
|
77 |
+
|
78 |
+
# 2 · gather extra context
|
79 |
+
ctx_parts: list[str] = [user_prompt]
|
80 |
+
|
81 |
+
if file_path:
|
82 |
+
ctx_parts.append("[Reference file]")
|
83 |
+
ctx_parts.append(extract_text_from_file(file_path)[:5000])
|
84 |
+
|
85 |
+
if website_url:
|
86 |
+
html = extract_website_content(website_url)
|
87 |
+
if not html.lower().startswith("error"):
|
88 |
+
ctx_parts.append("[Website content]")
|
89 |
+
ctx_parts.append(html[:8000])
|
90 |
+
|
91 |
+
user_query = "\n\n".join(filter(None, ctx_parts))
|
92 |
+
user_query = enhance_query_with_search(user_query, use_search)
|
93 |
+
messages.append({"role": "user", "content": user_query})
|
94 |
+
|
95 |
+
# 3 · call the model
|
96 |
+
model: ModelInfo = find_model(model_name) or AVAILABLE_MODELS[0]
|
97 |
try:
|
98 |
+
assistant = chat_completion(model.id, messages)
|
99 |
+
except Exception as exc:
|
100 |
+
err = f"❌ **Error**\n```{exc}```"
|
101 |
+
hist.append((user_prompt, err))
|
102 |
+
return "", hist, "", history_to_chatbot_messages(hist)
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
+
# 4 · post‑process
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
if language == "transformers.js":
|
106 |
+
files = parse_transformers_js_output(assistant)
|
107 |
code = format_transformers_js_output(files)
|
108 |
preview = send_to_sandbox(files.get("index.html", ""))
|
109 |
else:
|
110 |
+
cleaned = remove_code_block(assistant)
|
111 |
+
if hist and not hist[-1][1].startswith("❌"):
|
112 |
+
cleaned = apply_search_replace_changes(hist[-1][1], cleaned)
|
113 |
+
code = cleaned
|
|
|
114 |
preview = send_to_sandbox(code) if language == "html" else ""
|
115 |
|
116 |
+
# 5 · update history & chat
|
117 |
+
hist.append((user_prompt, code))
|
118 |
+
chat_messages = history_to_chatbot_messages(hist)
|
119 |
+
return code, hist, preview, chat_messages
|
120 |
+
|
121 |
+
|
122 |
+
# ---------- UI (Gradio 5) --------------------------------------------------
|
123 |
+
THEME = gr.themes.Soft(primary_hue="indigo")
|
124 |
+
CSS = """
|
125 |
+
.gradio-container {max-width: 1450px !important;}
|
126 |
+
@media (min-width: 960px){
|
127 |
+
.layout {display:flex; gap:32px}
|
128 |
+
.inputs {flex:1 0 360px}
|
129 |
+
.outputs {flex:2 0 0}
|
130 |
+
}
|
131 |
"""
|
132 |
|
133 |
+
with gr.Blocks(title="AnyCoder AI", theme=THEME, css=CSS) as demo:
|
134 |
+
state_hist: gr.State = gr.State([])
|
|
|
|
|
135 |
|
136 |
+
# ---- header ----------------------------------------------------------
|
137 |
+
gr.Markdown(
|
138 |
+
"## 🚀 **AnyCoder AI** \n"
|
139 |
+
"Your AI partner for generating, modifying & understanding code."
|
140 |
+
)
|
141 |
|
142 |
+
with gr.Row(elem_classes="layout"):
|
143 |
+
# -------- left column (inputs) ------------------------------------
|
144 |
+
with gr.Column(elem_classes="inputs"):
|
145 |
+
gr.Markdown("### 1 · Model")
|
146 |
model_dd = gr.Dropdown(
|
147 |
+
choices=[m.name for m in AVAILABLE_MODELS],
|
148 |
+
value=AVAILABLE_MODELS[0].name,
|
149 |
+
label="AI Model",
|
150 |
)
|
151 |
|
152 |
+
gr.Markdown("### 2 · Context")
|
153 |
with gr.Tabs():
|
154 |
+
with gr.Tab("Prompt"):
|
155 |
+
prompt_box = gr.Textbox(lines=6, placeholder="Describe what you need…")
|
156 |
+
with gr.Tab("File"):
|
157 |
+
file_box = gr.File(type="filepath")
|
158 |
+
with gr.Tab("Website"):
|
159 |
+
url_box = gr.Textbox(placeholder="https://example.com")
|
160 |
|
161 |
+
gr.Markdown("### 3 · Output")
|
162 |
+
lang_dd = gr.Dropdown(SUPPORTED_LANGUAGES, value="html", label="Target Language")
|
163 |
+
search_chk = gr.Checkbox(label="Enable Tavily Web Search")
|
164 |
|
165 |
with gr.Row():
|
166 |
+
clear_btn = gr.Button("Clear Session", variant="secondary")
|
167 |
+
gen_btn = gr.Button("Generate Code", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
|
169 |
+
# -------- right column (outputs) ----------------------------------
|
170 |
+
with gr.Column(elem_classes="outputs"):
|
171 |
with gr.Tabs():
|
172 |
+
with gr.Tab("Code"):
|
173 |
+
code_out = gr.Code(height=500, language="html")
|
174 |
+
with gr.Tab("Live Preview"):
|
175 |
preview_out = gr.HTML()
|
176 |
+
with gr.Tab("History"):
|
177 |
chat_out = gr.Chatbot(type="messages")
|
178 |
|
179 |
+
# ---- callbacks -------------------------------------------------------
|
|
|
180 |
gen_btn.click(
|
181 |
+
generate,
|
182 |
+
inputs=[
|
183 |
+
prompt_box, file_box, url_box,
|
184 |
+
model_dd, lang_dd, search_chk, state_hist
|
185 |
+
],
|
186 |
+
outputs=[code_out, state_hist, preview_out, chat_out],
|
187 |
)
|
188 |
|
189 |
+
clear_btn.click(
|
190 |
+
lambda: ("", None, "", "html", False, [], [], "", []),
|
191 |
+
outputs=[
|
192 |
+
prompt_box, file_box, url_box,
|
193 |
+
lang_dd, search_chk, state_hist,
|
194 |
+
code_out, preview_out, chat_out,
|
195 |
+
],
|
196 |
queue=False,
|
197 |
)
|
198 |
|
199 |
+
|
200 |
+
# ---------- launch ---------------------------------------------------------
|
201 |
if __name__ == "__main__":
|
202 |
demo.queue().launch()
|