Update app.py
Browse files
app.py
CHANGED
|
@@ -4,21 +4,18 @@ from typing import Optional, Dict, List, Tuple
|
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
from constants import HTML_SYSTEM_PROMPT, AVAILABLE_MODELS, DEMO_LIST
|
| 7 |
-
from hf_client import get_inference_client
|
| 8 |
from tavily_search import enhance_query_with_search
|
| 9 |
from utils import (
|
| 10 |
extract_text_from_file,
|
| 11 |
extract_website_content,
|
| 12 |
apply_search_replace_changes,
|
| 13 |
-
apply_transformers_js_search_replace_changes,
|
| 14 |
history_to_messages,
|
| 15 |
history_to_chatbot_messages,
|
| 16 |
remove_code_block,
|
| 17 |
parse_transformers_js_output,
|
| 18 |
format_transformers_js_output
|
| 19 |
)
|
| 20 |
-
from search_replace import SEARCH_START, DIVIDER, REPLACE_END
|
| 21 |
-
from web_scraper import extract_text_from_image
|
| 22 |
from deploy import send_to_sandbox, handle_load_project
|
| 23 |
|
| 24 |
# Type aliases
|
|
@@ -37,33 +34,28 @@ def generation_code(
|
|
| 37 |
language: str,
|
| 38 |
provider: str
|
| 39 |
) -> Tuple[str, History, str, List[Dict[str, str]]]:
|
| 40 |
-
|
| 41 |
if query is None:
|
| 42 |
query = ''
|
| 43 |
if _history is None:
|
| 44 |
_history = []
|
| 45 |
|
| 46 |
-
# Prepare system prompt and history
|
| 47 |
system_prompt = _setting.get('system', HTML_SYSTEM_PROMPT)
|
| 48 |
messages = history_to_messages(_history, system_prompt)
|
| 49 |
|
| 50 |
-
# Append file content if provided
|
| 51 |
if file:
|
| 52 |
file_text = extract_text_from_file(file)
|
| 53 |
if file_text:
|
| 54 |
query += f"\n\n[Reference file content below]\n{file_text[:5000]}"
|
| 55 |
|
| 56 |
-
# Append website content if provided
|
| 57 |
if website_url:
|
| 58 |
website_text = extract_website_content(website_url)
|
| 59 |
if not website_text.startswith("Error"):
|
| 60 |
query += f"\n\n[Website content below]\n{website_text[:8000]}"
|
| 61 |
|
| 62 |
-
# Enhance with web search if enabled
|
| 63 |
final_query = enhance_query_with_search(query, enable_search)
|
| 64 |
messages.append({'role': 'user', 'content': final_query})
|
| 65 |
|
| 66 |
-
# Call HF inference
|
| 67 |
client = get_inference_client(_current_model['id'], provider)
|
| 68 |
completion = client.chat.completions.create(
|
| 69 |
model=_current_model['id'],
|
|
@@ -72,7 +64,6 @@ def generation_code(
|
|
| 72 |
)
|
| 73 |
content = completion.choices[0].message.content
|
| 74 |
|
| 75 |
-
# Process output based on language and existing content
|
| 76 |
has_existing = bool(_history and _history[-1][1])
|
| 77 |
if language == 'transformers.js':
|
| 78 |
files = parse_transformers_js_output(content)
|
|
@@ -85,20 +76,17 @@ def generation_code(
|
|
| 85 |
code_str = clean
|
| 86 |
sandbox_html = send_to_sandbox(clean) if language == 'html' else ''
|
| 87 |
|
| 88 |
-
# Update history and prepare chatbot messages
|
| 89 |
new_history = _history + [(query, code_str)]
|
| 90 |
chat_msgs = history_to_chatbot_messages(new_history)
|
| 91 |
|
| 92 |
-
# Return exactly four outputs: code, history state, preview HTML, and chat history
|
| 93 |
return code_str, new_history, sandbox_html, chat_msgs
|
| 94 |
|
| 95 |
-
# Build Gradio UI
|
| 96 |
with gr.Blocks(
|
| 97 |
theme=gr.themes.Base(),
|
| 98 |
title="AnyCoder - AI Code Generator"
|
| 99 |
) as demo:
|
| 100 |
history_state = gr.State([])
|
| 101 |
-
setting_state = gr.State({
|
| 102 |
current_model = gr.State(AVAILABLE_MODELS[9])
|
| 103 |
|
| 104 |
with gr.Sidebar():
|
|
@@ -108,7 +96,7 @@ with gr.Blocks(
|
|
| 108 |
load_project_status = gr.Markdown(visible=False)
|
| 109 |
|
| 110 |
input_box = gr.Textbox(label="What to build?", lines=3)
|
| 111 |
-
language_dropdown = gr.Dropdown(choices=["html","python","transformers.js"], value="html")
|
| 112 |
website_input = gr.Textbox(label="Website URL")
|
| 113 |
file_input = gr.File(label="Reference file")
|
| 114 |
image_input = gr.Image(label="Design image")
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
from constants import HTML_SYSTEM_PROMPT, AVAILABLE_MODELS, DEMO_LIST
|
| 7 |
+
from hf_client import get_inference_client
|
| 8 |
from tavily_search import enhance_query_with_search
|
| 9 |
from utils import (
|
| 10 |
extract_text_from_file,
|
| 11 |
extract_website_content,
|
| 12 |
apply_search_replace_changes,
|
|
|
|
| 13 |
history_to_messages,
|
| 14 |
history_to_chatbot_messages,
|
| 15 |
remove_code_block,
|
| 16 |
parse_transformers_js_output,
|
| 17 |
format_transformers_js_output
|
| 18 |
)
|
|
|
|
|
|
|
| 19 |
from deploy import send_to_sandbox, handle_load_project
|
| 20 |
|
| 21 |
# Type aliases
|
|
|
|
| 34 |
language: str,
|
| 35 |
provider: str
|
| 36 |
) -> Tuple[str, History, str, List[Dict[str, str]]]:
|
| 37 |
+
|
| 38 |
if query is None:
|
| 39 |
query = ''
|
| 40 |
if _history is None:
|
| 41 |
_history = []
|
| 42 |
|
|
|
|
| 43 |
system_prompt = _setting.get('system', HTML_SYSTEM_PROMPT)
|
| 44 |
messages = history_to_messages(_history, system_prompt)
|
| 45 |
|
|
|
|
| 46 |
if file:
|
| 47 |
file_text = extract_text_from_file(file)
|
| 48 |
if file_text:
|
| 49 |
query += f"\n\n[Reference file content below]\n{file_text[:5000]}"
|
| 50 |
|
|
|
|
| 51 |
if website_url:
|
| 52 |
website_text = extract_website_content(website_url)
|
| 53 |
if not website_text.startswith("Error"):
|
| 54 |
query += f"\n\n[Website content below]\n{website_text[:8000]}"
|
| 55 |
|
|
|
|
| 56 |
final_query = enhance_query_with_search(query, enable_search)
|
| 57 |
messages.append({'role': 'user', 'content': final_query})
|
| 58 |
|
|
|
|
| 59 |
client = get_inference_client(_current_model['id'], provider)
|
| 60 |
completion = client.chat.completions.create(
|
| 61 |
model=_current_model['id'],
|
|
|
|
| 64 |
)
|
| 65 |
content = completion.choices[0].message.content
|
| 66 |
|
|
|
|
| 67 |
has_existing = bool(_history and _history[-1][1])
|
| 68 |
if language == 'transformers.js':
|
| 69 |
files = parse_transformers_js_output(content)
|
|
|
|
| 76 |
code_str = clean
|
| 77 |
sandbox_html = send_to_sandbox(clean) if language == 'html' else ''
|
| 78 |
|
|
|
|
| 79 |
new_history = _history + [(query, code_str)]
|
| 80 |
chat_msgs = history_to_chatbot_messages(new_history)
|
| 81 |
|
|
|
|
| 82 |
return code_str, new_history, sandbox_html, chat_msgs
|
| 83 |
|
|
|
|
| 84 |
with gr.Blocks(
|
| 85 |
theme=gr.themes.Base(),
|
| 86 |
title="AnyCoder - AI Code Generator"
|
| 87 |
) as demo:
|
| 88 |
history_state = gr.State([])
|
| 89 |
+
setting_state = gr.State({'system': HTML_SYSTEM_PROMPT})
|
| 90 |
current_model = gr.State(AVAILABLE_MODELS[9])
|
| 91 |
|
| 92 |
with gr.Sidebar():
|
|
|
|
| 96 |
load_project_status = gr.Markdown(visible=False)
|
| 97 |
|
| 98 |
input_box = gr.Textbox(label="What to build?", lines=3)
|
| 99 |
+
language_dropdown = gr.Dropdown(choices=["html", "python", "transformers.js"], value="html")
|
| 100 |
website_input = gr.Textbox(label="Website URL")
|
| 101 |
file_input = gr.File(label="Reference file")
|
| 102 |
image_input = gr.Image(label="Design image")
|