Spaces:
Running
Running
update transformer js followup
Browse files
app.py
CHANGED
@@ -192,6 +192,64 @@ Removing the paragraph...
|
|
192 |
{REPLACE_END}
|
193 |
```"""
|
194 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
195 |
# Available models
|
196 |
AVAILABLE_MODELS = [
|
197 |
{
|
@@ -575,6 +633,83 @@ def apply_search_replace_changes(original_content: str, changes_text: str) -> st
|
|
575 |
|
576 |
return modified_content
|
577 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
578 |
# Updated for faster Tavily search and closer prompt usage
|
579 |
# Uses 'advanced' search_depth and auto_parameters=True for speed and relevance
|
580 |
|
@@ -1103,13 +1238,19 @@ def generation_code(query: Optional[str], image: Optional[gr.Image], file: Optio
|
|
1103 |
'import gradio' in last_assistant_msg or
|
1104 |
'import streamlit' in last_assistant_msg or
|
1105 |
'def ' in last_assistant_msg and 'app' in last_assistant_msg or
|
1106 |
-
'IMPORTED PROJECT FROM HUGGING FACE SPACE' in last_assistant_msg
|
|
|
|
|
|
|
1107 |
has_existing_content = True
|
1108 |
|
1109 |
# Choose system prompt based on context
|
1110 |
if has_existing_content:
|
1111 |
# Use follow-up prompt for modifying existing content
|
1112 |
-
|
|
|
|
|
|
|
1113 |
else:
|
1114 |
# Use language-specific prompt
|
1115 |
if language == "html":
|
@@ -1182,12 +1323,22 @@ This will help me create a better design for you."""
|
|
1182 |
if language == "transformers.js":
|
1183 |
files = parse_transformers_js_output(content)
|
1184 |
if files['index.html'] and files['index.js'] and files['style.css']:
|
|
|
1185 |
formatted_output = format_transformers_js_output(files)
|
1186 |
yield {
|
1187 |
code_output: gr.update(value=formatted_output, language="html"),
|
1188 |
history_output: history_to_chatbot_messages(_history),
|
1189 |
sandbox: send_to_sandbox(files['index.html']) if files['index.html'] else "<div style='padding:1em;color:#888;text-align:center;'>Preview is only available for HTML. Please download your code using the download button above.</div>",
|
1190 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1191 |
else:
|
1192 |
# Still streaming, show partial content
|
1193 |
yield {
|
@@ -1229,6 +1380,7 @@ This will help me create a better design for you."""
|
|
1229 |
# Handle transformers.js output
|
1230 |
files = parse_transformers_js_output(content)
|
1231 |
if files['index.html'] and files['index.js'] and files['style.css']:
|
|
|
1232 |
formatted_output = format_transformers_js_output(files)
|
1233 |
_history.append([query, formatted_output])
|
1234 |
yield {
|
@@ -1237,6 +1389,17 @@ This will help me create a better design for you."""
|
|
1237 |
sandbox: send_to_sandbox(files['index.html']),
|
1238 |
history_output: history_to_chatbot_messages(_history),
|
1239 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1240 |
else:
|
1241 |
# Fallback if parsing failed
|
1242 |
_history.append([query, content])
|
@@ -1583,7 +1746,7 @@ with gr.Blocks(
|
|
1583 |
code_output = gr.Code(
|
1584 |
language="html",
|
1585 |
lines=25,
|
1586 |
-
interactive=
|
1587 |
label="Generated code"
|
1588 |
)
|
1589 |
with gr.Tab("Preview"):
|
|
|
192 |
{REPLACE_END}
|
193 |
```"""
|
194 |
|
195 |
+
# Follow-up system prompt for modifying existing transformers.js applications
|
196 |
+
TransformersJSFollowUpSystemPrompt = f"""You are an expert web developer modifying an existing transformers.js application.
|
197 |
+
The user wants to apply changes based on their request.
|
198 |
+
You MUST output ONLY the changes required using the following SEARCH/REPLACE block format. Do NOT output the entire file.
|
199 |
+
Explain the changes briefly *before* the blocks if necessary, but the code changes THEMSELVES MUST be within the blocks.
|
200 |
+
|
201 |
+
The transformers.js application consists of three files: index.html, index.js, and style.css.
|
202 |
+
When making changes, specify which file you're modifying by starting your search/replace blocks with the file name.
|
203 |
+
|
204 |
+
Format Rules:
|
205 |
+
1. Start with {SEARCH_START}
|
206 |
+
2. Provide the exact lines from the current code that need to be replaced.
|
207 |
+
3. Use {DIVIDER} to separate the search block from the replacement.
|
208 |
+
4. Provide the new lines that should replace the original lines.
|
209 |
+
5. End with {REPLACE_END}
|
210 |
+
6. You can use multiple SEARCH/REPLACE blocks if changes are needed in different parts of the file.
|
211 |
+
7. To insert code, use an empty SEARCH block (only {SEARCH_START} and {DIVIDER} on their lines) if inserting at the very beginning, otherwise provide the line *before* the insertion point in the SEARCH block and include that line plus the new lines in the REPLACE block.
|
212 |
+
8. To delete code, provide the lines to delete in the SEARCH block and leave the REPLACE block empty (only {DIVIDER} and {REPLACE_END} on their lines).
|
213 |
+
9. IMPORTANT: The SEARCH block must *exactly* match the current code, including indentation and whitespace.
|
214 |
+
|
215 |
+
Example Modifying HTML:
|
216 |
+
```
|
217 |
+
Changing the title in index.html...
|
218 |
+
{SEARCH_START}
|
219 |
+
<title>Old Title</title>
|
220 |
+
{DIVIDER}
|
221 |
+
<title>New Title</title>
|
222 |
+
{REPLACE_END}
|
223 |
+
```
|
224 |
+
|
225 |
+
Example Modifying JavaScript:
|
226 |
+
```
|
227 |
+
Adding a new function to index.js...
|
228 |
+
{SEARCH_START}
|
229 |
+
// Existing code
|
230 |
+
{DIVIDER}
|
231 |
+
// Existing code
|
232 |
+
|
233 |
+
function newFunction() {{
|
234 |
+
console.log("New function added");
|
235 |
+
}}
|
236 |
+
{REPLACE_END}
|
237 |
+
```
|
238 |
+
|
239 |
+
Example Modifying CSS:
|
240 |
+
```
|
241 |
+
Changing background color in style.css...
|
242 |
+
{SEARCH_START}
|
243 |
+
body {{
|
244 |
+
background-color: white;
|
245 |
+
}}
|
246 |
+
{DIVIDER}
|
247 |
+
body {{
|
248 |
+
background-color: #f0f0f0;
|
249 |
+
}}
|
250 |
+
{REPLACE_END}
|
251 |
+
```"""
|
252 |
+
|
253 |
# Available models
|
254 |
AVAILABLE_MODELS = [
|
255 |
{
|
|
|
633 |
|
634 |
return modified_content
|
635 |
|
636 |
+
def apply_transformers_js_search_replace_changes(original_formatted_content: str, changes_text: str) -> str:
|
637 |
+
"""Apply search/replace changes to transformers.js formatted content (three files)"""
|
638 |
+
if not changes_text.strip():
|
639 |
+
return original_formatted_content
|
640 |
+
|
641 |
+
# Parse the original formatted content to get the three files
|
642 |
+
files = parse_transformers_js_output(original_formatted_content)
|
643 |
+
|
644 |
+
# Split the changes text into individual search/replace blocks
|
645 |
+
blocks = []
|
646 |
+
current_block = ""
|
647 |
+
lines = changes_text.split('\n')
|
648 |
+
|
649 |
+
for line in lines:
|
650 |
+
if line.strip() == SEARCH_START:
|
651 |
+
if current_block.strip():
|
652 |
+
blocks.append(current_block.strip())
|
653 |
+
current_block = line + '\n'
|
654 |
+
elif line.strip() == REPLACE_END:
|
655 |
+
current_block += line + '\n'
|
656 |
+
blocks.append(current_block.strip())
|
657 |
+
current_block = ""
|
658 |
+
else:
|
659 |
+
current_block += line + '\n'
|
660 |
+
|
661 |
+
if current_block.strip():
|
662 |
+
blocks.append(current_block.strip())
|
663 |
+
|
664 |
+
# Process each block and apply changes to the appropriate file
|
665 |
+
for block in blocks:
|
666 |
+
if not block.strip():
|
667 |
+
continue
|
668 |
+
|
669 |
+
# Parse the search/replace block
|
670 |
+
lines = block.split('\n')
|
671 |
+
search_lines = []
|
672 |
+
replace_lines = []
|
673 |
+
in_search = False
|
674 |
+
in_replace = False
|
675 |
+
target_file = None
|
676 |
+
|
677 |
+
for line in lines:
|
678 |
+
if line.strip() == SEARCH_START:
|
679 |
+
in_search = True
|
680 |
+
in_replace = False
|
681 |
+
elif line.strip() == DIVIDER:
|
682 |
+
in_search = False
|
683 |
+
in_replace = True
|
684 |
+
elif line.strip() == REPLACE_END:
|
685 |
+
in_replace = False
|
686 |
+
elif in_search:
|
687 |
+
search_lines.append(line)
|
688 |
+
elif in_replace:
|
689 |
+
replace_lines.append(line)
|
690 |
+
|
691 |
+
# Determine which file this change targets based on the search content
|
692 |
+
if search_lines:
|
693 |
+
search_text = '\n'.join(search_lines).strip()
|
694 |
+
replace_text = '\n'.join(replace_lines).strip()
|
695 |
+
|
696 |
+
# Check which file contains the search text
|
697 |
+
if search_text in files['index.html']:
|
698 |
+
target_file = 'index.html'
|
699 |
+
elif search_text in files['index.js']:
|
700 |
+
target_file = 'index.js'
|
701 |
+
elif search_text in files['style.css']:
|
702 |
+
target_file = 'style.css'
|
703 |
+
|
704 |
+
# Apply the change to the target file
|
705 |
+
if target_file and search_text in files[target_file]:
|
706 |
+
files[target_file] = files[target_file].replace(search_text, replace_text)
|
707 |
+
else:
|
708 |
+
print(f"Warning: Search text not found in any transformers.js file: {search_text[:100]}...")
|
709 |
+
|
710 |
+
# Reformat the modified files
|
711 |
+
return format_transformers_js_output(files)
|
712 |
+
|
713 |
# Updated for faster Tavily search and closer prompt usage
|
714 |
# Uses 'advanced' search_depth and auto_parameters=True for speed and relevance
|
715 |
|
|
|
1238 |
'import gradio' in last_assistant_msg or
|
1239 |
'import streamlit' in last_assistant_msg or
|
1240 |
'def ' in last_assistant_msg and 'app' in last_assistant_msg or
|
1241 |
+
'IMPORTED PROJECT FROM HUGGING FACE SPACE' in last_assistant_msg or
|
1242 |
+
'=== index.html ===' in last_assistant_msg or
|
1243 |
+
'=== index.js ===' in last_assistant_msg or
|
1244 |
+
'=== style.css ===' in last_assistant_msg):
|
1245 |
has_existing_content = True
|
1246 |
|
1247 |
# Choose system prompt based on context
|
1248 |
if has_existing_content:
|
1249 |
# Use follow-up prompt for modifying existing content
|
1250 |
+
if language == "transformers.js":
|
1251 |
+
system_prompt = TransformersJSFollowUpSystemPrompt
|
1252 |
+
else:
|
1253 |
+
system_prompt = FollowUpSystemPrompt
|
1254 |
else:
|
1255 |
# Use language-specific prompt
|
1256 |
if language == "html":
|
|
|
1323 |
if language == "transformers.js":
|
1324 |
files = parse_transformers_js_output(content)
|
1325 |
if files['index.html'] and files['index.js'] and files['style.css']:
|
1326 |
+
# Model returned complete transformers.js output
|
1327 |
formatted_output = format_transformers_js_output(files)
|
1328 |
yield {
|
1329 |
code_output: gr.update(value=formatted_output, language="html"),
|
1330 |
history_output: history_to_chatbot_messages(_history),
|
1331 |
sandbox: send_to_sandbox(files['index.html']) if files['index.html'] else "<div style='padding:1em;color:#888;text-align:center;'>Preview is only available for HTML. Please download your code using the download button above.</div>",
|
1332 |
}
|
1333 |
+
elif has_existing_content:
|
1334 |
+
# Model is returning search/replace changes for transformers.js - apply them
|
1335 |
+
last_content = _history[-1][1] if _history and len(_history[-1]) > 1 else ""
|
1336 |
+
modified_content = apply_transformers_js_search_replace_changes(last_content, content)
|
1337 |
+
yield {
|
1338 |
+
code_output: gr.update(value=modified_content, language="html"),
|
1339 |
+
history_output: history_to_chatbot_messages(_history),
|
1340 |
+
sandbox: send_to_sandbox(parse_transformers_js_output(modified_content)['index.html']) if parse_transformers_js_output(modified_content)['index.html'] else "<div style='padding:1em;color:#888;text-align:center;'>Preview is only available for HTML. Please download your code using the download button above.</div>",
|
1341 |
+
}
|
1342 |
else:
|
1343 |
# Still streaming, show partial content
|
1344 |
yield {
|
|
|
1380 |
# Handle transformers.js output
|
1381 |
files = parse_transformers_js_output(content)
|
1382 |
if files['index.html'] and files['index.js'] and files['style.css']:
|
1383 |
+
# Model returned complete transformers.js output
|
1384 |
formatted_output = format_transformers_js_output(files)
|
1385 |
_history.append([query, formatted_output])
|
1386 |
yield {
|
|
|
1389 |
sandbox: send_to_sandbox(files['index.html']),
|
1390 |
history_output: history_to_chatbot_messages(_history),
|
1391 |
}
|
1392 |
+
elif has_existing_content:
|
1393 |
+
# Model returned search/replace changes for transformers.js - apply them
|
1394 |
+
last_content = _history[-1][1] if _history and len(_history[-1]) > 1 else ""
|
1395 |
+
modified_content = apply_transformers_js_search_replace_changes(last_content, content)
|
1396 |
+
_history.append([query, modified_content])
|
1397 |
+
yield {
|
1398 |
+
code_output: modified_content,
|
1399 |
+
history: _history,
|
1400 |
+
sandbox: send_to_sandbox(parse_transformers_js_output(modified_content)['index.html']),
|
1401 |
+
history_output: history_to_chatbot_messages(_history),
|
1402 |
+
}
|
1403 |
else:
|
1404 |
# Fallback if parsing failed
|
1405 |
_history.append([query, content])
|
|
|
1746 |
code_output = gr.Code(
|
1747 |
language="html",
|
1748 |
lines=25,
|
1749 |
+
interactive=True,
|
1750 |
label="Generated code"
|
1751 |
)
|
1752 |
with gr.Tab("Preview"):
|