Spaces:
Running
Running
File size: 28,810 Bytes
d1c02ef d6a6b23 35022df d1c02ef d6a6b23 35022df d1c02ef d6a6b23 35022df d1c02ef d6a6b23 35022df d1c02ef d6a6b23 35022df d1c02ef 254f500 92ee151 254f500 92ee151 254f500 92ee151 254f500 92ee151 254f500 d1c02ef 254f500 92ee151 254f500 92ee151 d1c02ef d6a6b23 35022df d1c02ef d6a6b23 d1c02ef d6a6b23 35022df d6a6b23 d1c02ef |
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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 |
"""
Documentation management for backend system prompts.
Handles fetching, caching, and updating documentation from llms.txt files.
No dependencies on Gradio or other heavy libraries - pure Python only.
"""
import os
import re
from datetime import datetime
from typing import Optional
try:
import requests
HAS_REQUESTS = True
except ImportError:
HAS_REQUESTS = False
print("Warning: requests library not available, using minimal fallback")
# Configuration
GRADIO_LLMS_TXT_URL = "https://www.gradio.app/llms.txt"
GRADIO_DOCS_CACHE_FILE = ".backend_gradio_docs_cache.txt"
GRADIO_DOCS_LAST_UPDATE_FILE = ".backend_gradio_docs_last_update.txt"
TRANSFORMERSJS_DOCS_URL = "https://huggingface.co/docs/transformers.js/llms.txt"
TRANSFORMERSJS_DOCS_CACHE_FILE = ".backend_transformersjs_docs_cache.txt"
TRANSFORMERSJS_DOCS_LAST_UPDATE_FILE = ".backend_transformersjs_docs_last_update.txt"
COMFYUI_LLMS_TXT_URL = "https://docs.comfy.org/llms.txt"
COMFYUI_DOCS_CACHE_FILE = ".backend_comfyui_docs_cache.txt"
COMFYUI_DOCS_LAST_UPDATE_FILE = ".backend_comfyui_docs_last_update.txt"
# Global variable to store the current Gradio documentation
_gradio_docs_content: Optional[str] = None
_gradio_docs_last_fetched: Optional[datetime] = None
# Global variable to store the current transformers.js documentation
_transformersjs_docs_content: Optional[str] = None
_transformersjs_docs_last_fetched: Optional[datetime] = None
# Global variable to store the current ComfyUI documentation
_comfyui_docs_content: Optional[str] = None
_comfyui_docs_last_fetched: Optional[datetime] = None
def fetch_gradio_docs() -> Optional[str]:
"""Fetch the latest Gradio documentation from llms.txt"""
if not HAS_REQUESTS:
return None
try:
response = requests.get(GRADIO_LLMS_TXT_URL, timeout=10)
response.raise_for_status()
return response.text
except Exception as e:
print(f"Warning: Failed to fetch Gradio docs from {GRADIO_LLMS_TXT_URL}: {e}")
return None
def fetch_transformersjs_docs() -> Optional[str]:
"""Fetch the latest transformers.js documentation from llms.txt"""
if not HAS_REQUESTS:
return None
try:
response = requests.get(TRANSFORMERSJS_DOCS_URL, timeout=10)
response.raise_for_status()
return response.text
except Exception as e:
print(f"Warning: Failed to fetch transformers.js docs from {TRANSFORMERSJS_DOCS_URL}: {e}")
return None
def fetch_comfyui_docs() -> Optional[str]:
"""Fetch the latest ComfyUI documentation from llms.txt"""
if not HAS_REQUESTS:
return None
try:
response = requests.get(COMFYUI_LLMS_TXT_URL, timeout=10)
response.raise_for_status()
return response.text
except Exception as e:
print(f"Warning: Failed to fetch ComfyUI docs from {COMFYUI_LLMS_TXT_URL}: {e}")
return None
def filter_problematic_instructions(content: str) -> str:
"""Filter out problematic instructions that cause LLM to stop generation prematurely"""
if not content:
return content
# List of problematic phrases that cause early termination when LLM encounters ``` in user code
problematic_patterns = [
r"Output ONLY the code inside a ``` code block, and do not include any explanations or extra text",
r"output only the code inside a ```.*?``` code block",
r"Always output only the.*?code.*?inside.*?```.*?```.*?block",
r"Return ONLY the code inside a.*?```.*?``` code block",
r"Do NOT add the language name at the top of the code output",
r"do not include any explanations or extra text",
r"Always output only the.*?code blocks.*?shown above, and do not include any explanations",
r"Output.*?ONLY.*?code.*?inside.*?```.*?```",
r"Return.*?ONLY.*?code.*?inside.*?```.*?```",
r"Generate.*?ONLY.*?code.*?inside.*?```.*?```",
r"Provide.*?ONLY.*?code.*?inside.*?```.*?```",
]
# Remove problematic patterns
filtered_content = content
for pattern in problematic_patterns:
# Use case-insensitive matching
filtered_content = re.sub(pattern, "", filtered_content, flags=re.IGNORECASE | re.DOTALL)
# Clean up any double newlines or extra whitespace left by removals
filtered_content = re.sub(r'\n\s*\n\s*\n', '\n\n', filtered_content)
filtered_content = re.sub(r'^\s+', '', filtered_content, flags=re.MULTILINE)
return filtered_content
def load_cached_gradio_docs() -> Optional[str]:
"""Load cached Gradio documentation from file"""
try:
if os.path.exists(GRADIO_DOCS_CACHE_FILE):
with open(GRADIO_DOCS_CACHE_FILE, 'r', encoding='utf-8') as f:
return f.read()
except Exception as e:
print(f"Warning: Failed to load cached Gradio docs: {e}")
return None
def save_gradio_docs_cache(content: str):
"""Save Gradio documentation to cache file"""
try:
with open(GRADIO_DOCS_CACHE_FILE, 'w', encoding='utf-8') as f:
f.write(content)
with open(GRADIO_DOCS_LAST_UPDATE_FILE, 'w', encoding='utf-8') as f:
f.write(datetime.now().isoformat())
except Exception as e:
print(f"Warning: Failed to save Gradio docs cache: {e}")
def should_update_gradio_docs() -> bool:
"""Check if Gradio documentation should be updated"""
# Only update if we don't have cached content (first run or cache deleted)
return not os.path.exists(GRADIO_DOCS_CACHE_FILE)
def load_cached_transformersjs_docs() -> Optional[str]:
"""Load cached transformers.js documentation from file"""
try:
if os.path.exists(TRANSFORMERSJS_DOCS_CACHE_FILE):
with open(TRANSFORMERSJS_DOCS_CACHE_FILE, 'r', encoding='utf-8') as f:
return f.read()
except Exception as e:
print(f"Warning: Failed to load cached transformers.js docs: {e}")
return None
def save_transformersjs_docs_cache(content: str):
"""Save transformers.js documentation to cache file"""
try:
with open(TRANSFORMERSJS_DOCS_CACHE_FILE, 'w', encoding='utf-8') as f:
f.write(content)
with open(TRANSFORMERSJS_DOCS_LAST_UPDATE_FILE, 'w', encoding='utf-8') as f:
f.write(datetime.now().isoformat())
except Exception as e:
print(f"Warning: Failed to save transformers.js docs cache: {e}")
def should_update_transformersjs_docs() -> bool:
"""Check if transformers.js documentation should be updated"""
# Only update if we don't have cached content (first run or cache deleted)
return not os.path.exists(TRANSFORMERSJS_DOCS_CACHE_FILE)
def load_cached_comfyui_docs() -> Optional[str]:
"""Load cached ComfyUI documentation from file"""
try:
if os.path.exists(COMFYUI_DOCS_CACHE_FILE):
with open(COMFYUI_DOCS_CACHE_FILE, 'r', encoding='utf-8') as f:
return f.read()
except Exception as e:
print(f"Warning: Failed to load cached ComfyUI docs: {e}")
return None
def save_comfyui_docs_cache(content: str):
"""Save ComfyUI documentation to cache file"""
try:
with open(COMFYUI_DOCS_CACHE_FILE, 'w', encoding='utf-8') as f:
f.write(content)
with open(COMFYUI_DOCS_LAST_UPDATE_FILE, 'w', encoding='utf-8') as f:
f.write(datetime.now().isoformat())
except Exception as e:
print(f"Warning: Failed to save ComfyUI docs cache: {e}")
def should_update_comfyui_docs() -> bool:
"""Check if ComfyUI documentation should be updated"""
# Only update if we don't have cached content (first run or cache deleted)
return not os.path.exists(COMFYUI_DOCS_CACHE_FILE)
def get_gradio_docs_content() -> str:
"""Get the current Gradio documentation content, updating if necessary"""
global _gradio_docs_content, _gradio_docs_last_fetched
# Check if we need to update
if (_gradio_docs_content is None or
_gradio_docs_last_fetched is None or
should_update_gradio_docs()):
print("π Loading Gradio 6 documentation...")
# Try to fetch latest content
latest_content = fetch_gradio_docs()
if latest_content:
# Filter out problematic instructions that cause early termination
filtered_content = filter_problematic_instructions(latest_content)
_gradio_docs_content = filtered_content
_gradio_docs_last_fetched = datetime.now()
save_gradio_docs_cache(filtered_content)
print(f"β
Gradio 6 documentation loaded successfully ({len(filtered_content)} chars)")
else:
# Fallback to cached content
cached_content = load_cached_gradio_docs()
if cached_content:
_gradio_docs_content = cached_content
_gradio_docs_last_fetched = datetime.now()
print(f"β οΈ Using cached Gradio documentation (network fetch failed) ({len(cached_content)} chars)")
else:
# Fallback to minimal content
_gradio_docs_content = """
# Gradio API Reference (Offline Fallback)
This is a minimal fallback when documentation cannot be fetched.
Please check your internet connection for the latest API reference.
Basic Gradio components: Button, Textbox, Slider, Image, Audio, Video, File, etc.
Use gr.Blocks() for custom layouts and gr.Interface() for simple apps.
For the latest documentation, visit: https://www.gradio.app/llms.txt
"""
print("β Using minimal fallback documentation")
return _gradio_docs_content or ""
def get_transformersjs_docs_content() -> str:
"""Get the current transformers.js documentation content, updating if necessary"""
global _transformersjs_docs_content, _transformersjs_docs_last_fetched
# Check if we need to update
if (_transformersjs_docs_content is None or
_transformersjs_docs_last_fetched is None or
should_update_transformersjs_docs()):
print("π Loading transformers.js documentation...")
# Try to fetch latest content
latest_content = fetch_transformersjs_docs()
if latest_content:
# Filter out problematic instructions that cause early termination
filtered_content = filter_problematic_instructions(latest_content)
_transformersjs_docs_content = filtered_content
_transformersjs_docs_last_fetched = datetime.now()
save_transformersjs_docs_cache(filtered_content)
print(f"β
transformers.js documentation loaded successfully ({len(filtered_content)} chars)")
else:
# Fallback to cached content
cached_content = load_cached_transformersjs_docs()
if cached_content:
_transformersjs_docs_content = cached_content
_transformersjs_docs_last_fetched = datetime.now()
print(f"β οΈ Using cached transformers.js documentation (network fetch failed) ({len(cached_content)} chars)")
else:
# Fallback to minimal content
_transformersjs_docs_content = """
# Transformers.js API Reference (Offline Fallback)
This is a minimal fallback when documentation cannot be fetched.
Please check your internet connection for the latest API reference.
Transformers.js allows you to run π€ Transformers models directly in the browser using ONNX Runtime.
Key features:
- pipeline() API for common tasks (sentiment-analysis, text-generation, etc.)
- Support for custom models via model ID or path
- WebGPU support for GPU acceleration
- Quantization support (fp32, fp16, q8, q4)
Basic usage:
```javascript
import { pipeline } from '@huggingface/transformers';
const pipe = await pipeline('sentiment-analysis');
const out = await pipe('I love transformers!');
```
For the latest documentation, visit: https://huggingface.co/docs/transformers.js
"""
print("β Using minimal fallback transformers.js documentation")
return _transformersjs_docs_content or ""
def get_comfyui_docs_content() -> str:
"""Get the current ComfyUI documentation content, updating if necessary"""
global _comfyui_docs_content, _comfyui_docs_last_fetched
# Check if we need to update
if (_comfyui_docs_content is None or
_comfyui_docs_last_fetched is None or
should_update_comfyui_docs()):
print("π Loading ComfyUI documentation...")
# Try to fetch latest content
latest_content = fetch_comfyui_docs()
if latest_content:
# Filter out problematic instructions that cause early termination
filtered_content = filter_problematic_instructions(latest_content)
_comfyui_docs_content = filtered_content
_comfyui_docs_last_fetched = datetime.now()
save_comfyui_docs_cache(filtered_content)
print(f"β
ComfyUI documentation loaded successfully ({len(filtered_content)} chars)")
else:
# Fallback to cached content
cached_content = load_cached_comfyui_docs()
if cached_content:
_comfyui_docs_content = cached_content
_comfyui_docs_last_fetched = datetime.now()
print(f"β οΈ Using cached ComfyUI documentation (network fetch failed) ({len(cached_content)} chars)")
else:
# Fallback to minimal content
_comfyui_docs_content = """
# ComfyUI API Reference (Offline Fallback)
This is a minimal fallback when documentation cannot be fetched.
Please check your internet connection for the latest API reference.
Basic ComfyUI workflow structure: nodes, connections, inputs, outputs.
Use CheckpointLoaderSimple, CLIPTextEncode, KSampler for basic workflows.
For the latest documentation, visit: https://docs.comfy.org/llms.txt
"""
print("β Using minimal fallback ComfyUI documentation")
return _comfyui_docs_content or ""
def build_gradio_system_prompt() -> str:
"""Build the complete Gradio system prompt with full documentation"""
# Get the full Gradio 6 documentation
docs_content = get_gradio_docs_content()
# Base system prompt with anycoder-specific instructions
base_prompt = """π¨ CRITICAL: You are an expert Gradio 6 developer. You MUST use Gradio 6 syntax and API.
## Key Gradio 6 Changes (MUST FOLLOW):
- π¨ **BREAKING CHANGE**: `theme`, `css`, `js`, `head` parameters moved from `gr.Blocks()` to `demo.launch()`
- π¨ **gr.Blocks() has NO parameters** - use `with gr.Blocks() as demo:` (no args!)
- π¨ **ALL app-level params go in demo.launch()**: `theme=`, `css=`, `footer_links=`, etc.
- Use `footer_links` parameter in `demo.launch()` (NOT show_api)
- Use `api_visibility` instead of `api_name` in event listeners
- Use modern Gradio 6 component syntax (check documentation below)
- Gradio 6 has updated component APIs - always refer to the documentation below
- DO NOT use deprecated Gradio 5 or older syntax
Create a complete, working Gradio 6 application based on the user's request. Generate all necessary code to make the application functional and runnable.
## Gradio 6 Themes (Modern UI Design):
Gradio 6 provides powerful theming capabilities. Use themes to create beautiful, professional interfaces:
**Built-in Themes:**
```python
import gradio as gr
# Use predefined themes in launch() - Gradio 6 syntax
with gr.Blocks() as demo:
gr.Textbox(label="Input")
demo.launch(theme=gr.themes.Soft()) # Soft, rounded design
# demo.launch(theme=gr.themes.Glass()) # Modern glass morphism
# demo.launch(theme=gr.themes.Monochrome()) # Clean monochrome
# demo.launch(theme=gr.themes.Base()) # Default base theme
```
**Custom Themes:**
```python
import gradio as gr
# Create custom theme
custom_theme = gr.themes.Soft(
primary_hue="blue",
secondary_hue="indigo",
neutral_hue="slate",
font=gr.themes.GoogleFont("Inter"),
text_size="lg",
spacing_size="lg",
radius_size="md"
).set(
button_primary_background_fill="*primary_600",
button_primary_background_fill_hover="*primary_700",
block_title_text_weight="600",
)
with gr.Blocks() as demo:
gr.Textbox(label="Input")
demo.launch(theme=custom_theme) # Apply theme in launch() - Gradio 6!
```
**Best Practices:**
- π¨ **CRITICAL**: In Gradio 6, `theme` goes in `demo.launch()`, NOT in `gr.Blocks()`
- Use `gr.themes.Soft()` for modern, friendly apps
- Use `gr.themes.Glass()` for sleek, contemporary designs
- Customize colors with `primary_hue`, `secondary_hue`, `neutral_hue`
- Use Google Fonts: `font=gr.themes.GoogleFont("Roboto")`
- Adjust sizing: `text_size`, `spacing_size`, `radius_size` (sm/md/lg)
- Fine-tune with `.set()` for specific CSS variables
## Gradio 6 Example (Your Code Should Follow This Pattern):
```python
import gradio as gr
def process(text):
return f"Processed: {text}"
# Gradio 6 - NO parameters in gr.Blocks() constructor!
with gr.Blocks() as demo:
gr.Markdown("# My App")
with gr.Row():
input_text = gr.Textbox(label="Input")
output_text = gr.Textbox(label="Output")
btn = gr.Button("Process")
# Gradio 6 events use api_visibility (NOT just api_name)
btn.click(
fn=process,
inputs=[input_text],
outputs=[output_text],
api_visibility="public" # Gradio 6 syntax
)
# Gradio 6 - ALL app parameters go in launch()!
demo.launch(
theme=gr.themes.Soft(primary_hue="blue"),
footer_links=[{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}]
)
```
## Multi-File Application Structure
When creating Gradio applications, organize your code into multiple files for proper deployment:
**File Organization:**
- `app.py` - Main application entry point (REQUIRED)
- `requirements.txt` - Python dependencies (REQUIRED, auto-generated from imports)
- `utils.py` - Utility functions and helpers (optional)
- `models.py` - Model loading and inference functions (optional)
- `config.py` - Configuration and constants (optional)
**Output Format:**
You MUST use this exact format with file separators:
=== app.py ===
[complete app.py content]
=== utils.py ===
[utility functions - if needed]
**π¨ CRITICAL: DO NOT GENERATE requirements.txt or README.md**
- requirements.txt is automatically generated from your app.py imports
- README.md is automatically provided by the template
- Generating these files will break the deployment process
Requirements:
1. Create a modern, intuitive Gradio application
2. Use appropriate Gradio components (gr.Textbox, gr.Slider, etc.)
3. Include proper error handling and loading states
4. Use gr.Interface or gr.Blocks as appropriate
5. Add helpful descriptions and examples
6. Follow Gradio best practices
7. Make the UI user-friendly with clear labels
8. Include proper documentation in docstrings
IMPORTANT: Always include "Built with anycoder" as clickable text in the header/top section of your application that links to https://huggingface.co/spaces/akhaliq/anycoder
---
## Complete Gradio 6 Documentation
Below is the complete, official Gradio 6 documentation automatically synced from https://www.gradio.app/llms.txt:
"""
# Combine base prompt with full documentation
full_prompt = base_prompt + docs_content
# Add final instructions
final_instructions = """
---
## π¨ CRITICAL FINAL INSTRUCTIONS - GRADIO 6 ONLY
YOU MUST USE GRADIO 6 SYNTAX. This is MANDATORY:
1. **ONLY use Gradio 6 API** - Do NOT use Gradio 5 or older syntax
2. **Reference the documentation above** - All function signatures and patterns are from Gradio 6
3. **Use modern Gradio 6 patterns:**
- π¨ **CRITICAL**: `theme`, `css`, `js`, `head` go in `demo.launch()`, NOT in `gr.Blocks()`
- Use `footer_links` parameter in `demo.launch()` (NOT show_api in Blocks)
- Use `api_visibility` in event listeners (NOT api_name alone)
- Use updated component syntax from Gradio 6 documentation
- **Use themes for professional UI design** (gr.themes.Soft(), gr.themes.Glass(), etc.)
4. **Always use themes** - Modern Gradio 6 apps should use `theme=gr.themes.Soft()` in `demo.launch()`
5. **Follow Gradio 6 migration guide** if you see any deprecated patterns
6. **Generate production-ready Gradio 6 code** that follows all best practices
7. **Always include "Built with anycoder"** as clickable text in the header linking to https://huggingface.co/spaces/akhaliq/anycoder
**Gradio 6 Structure Checklist:**
β
`with gr.Blocks() as demo:` - NO parameters here!
β
`demo.launch(theme=..., css=..., footer_links=...)` - ALL app parameters here!
β
Use `theme=` parameter in `demo.launch()` (NOT in gr.Blocks())
β
Choose appropriate theme: Soft (friendly), Glass (modern), Monochrome (minimal)
β
Customize with primary_hue, font, text_size, spacing_size
β
Use `.set()` for advanced customization
REMINDER: You are writing Gradio 6 code with modern themes. In Gradio 6, `gr.Blocks()` has NO parameters - everything goes in `demo.launch()`. Double-check all syntax against the Gradio 6 documentation provided above.
"""
return full_prompt + final_instructions
def build_transformersjs_system_prompt() -> str:
"""Build the complete transformers.js system prompt with full documentation"""
# Get the full transformers.js documentation
docs_content = get_transformersjs_docs_content()
# Base system prompt with anycoder-specific instructions
base_prompt = """You are an expert transformers.js developer. Create a complete, working browser-based ML application using transformers.js based on the user's request. Generate all necessary code to make the application functional and runnable in the browser.
## Multi-File Application Structure
When creating transformers.js applications, organize your code into multiple files for proper deployment:
**File Organization:**
- `index.html` - Main HTML entry point (REQUIRED)
- `app.js` - Main JavaScript application logic (REQUIRED)
- `styles.css` - Styling (optional)
- `worker.js` - Web Worker for model loading (recommended for better performance)
- `package.json` - Node.js dependencies if using bundler (optional)
**Output Format:**
You MUST use this exact format with file separators:
=== index.html ===
[complete HTML content]
=== app.js ===
[complete JavaScript content]
=== worker.js ===
[web worker content - if needed]
**π¨ CRITICAL: Best Practices**
- Use CDN for transformers.js: https://cdn.jsdelivr.net/npm/@huggingface/transformers
- Implement loading states and progress indicators
- Use Web Workers for model loading to avoid blocking UI
- Handle errors gracefully with user-friendly messages
- Show model download progress when applicable
- Use quantized models (q8, q4) for faster loading in browser
Requirements:
1. Create a modern, responsive web application
2. Use appropriate transformers.js pipelines and models
3. Include proper error handling and loading states
4. Implement progress indicators for model loading
5. Add helpful descriptions and examples
6. Follow browser best practices (async/await, Web Workers, etc.)
7. Make the UI user-friendly with clear labels
8. Include proper comments in code
IMPORTANT: Always include "Built with anycoder" as clickable text in the header/top section of your application that links to https://huggingface.co/spaces/akhaliq/anycoder
---
## Complete transformers.js Documentation
Below is the complete, official transformers.js documentation automatically synced from https://huggingface.co/docs/transformers.js/llms.txt:
"""
# Combine base prompt with full documentation
full_prompt = base_prompt + docs_content
# Add final instructions
final_instructions = """
---
## Final Instructions
- Always use the exact function signatures and patterns from the transformers.js documentation above
- Use the pipeline() API for common tasks
- Implement WebGPU support when appropriate for better performance
- Use quantized models by default (q8 or q4) for faster browser loading
- Generate production-ready code that follows all best practices
- Always include the "Built with anycoder" attribution in the header
- Consider using Web Workers for heavy computation to keep UI responsive
"""
return full_prompt + final_instructions
def build_comfyui_system_prompt() -> str:
"""Build the complete ComfyUI system prompt with full documentation"""
# Get the full ComfyUI documentation
docs_content = get_comfyui_docs_content()
# Base system prompt with anycoder-specific instructions
base_prompt = """You are an expert ComfyUI developer. Generate clean, valid JSON workflows for ComfyUI based on the user's request.
π¨ CRITICAL: READ THE USER'S REQUEST CAREFULLY AND GENERATE A WORKFLOW THAT MATCHES THEIR SPECIFIC NEEDS.
ComfyUI workflows are JSON structures that define:
- Nodes: Individual processing units with specific functions (e.g., CheckpointLoaderSimple, CLIPTextEncode, KSampler, VAEDecode, SaveImage)
- Connections: Links between nodes that define data flow
- Parameters: Configuration values for each node (prompts, steps, cfg, sampler_name, etc.)
- Inputs/Outputs: Data flow between nodes using numbered inputs/outputs
**π¨ YOUR PRIMARY TASK:**
1. **UNDERSTAND what the user is asking for** in their message
2. **CREATE a ComfyUI workflow** that accomplishes their goal
3. **GENERATE ONLY the JSON workflow** - no HTML, no applications, no explanations outside the JSON
**JSON Syntax Rules:**
- Use double quotes for strings
- No trailing commas
- Proper nesting and structure
- Valid data types (string, number, boolean, null, object, array)
**Output Requirements:**
- Generate ONLY the ComfyUI workflow JSON
- The output should be pure, valid JSON that can be loaded directly into ComfyUI
- Do NOT wrap in markdown code fences (no ```json```)
- Do NOT add explanatory text before or after the JSON
- The JSON should be complete and functional
---
## Complete ComfyUI Documentation
Below is the complete, official ComfyUI documentation automatically synced from https://docs.comfy.org/llms.txt:
"""
# Combine base prompt with full documentation
full_prompt = base_prompt + docs_content
# Add final instructions
final_instructions = """
---
## Final Instructions
- Always use the exact node types, parameters, and workflow structures from the ComfyUI documentation above
- Pay close attention to the user's specific request and generate a workflow that fulfills it
- Use appropriate nodes for the task (CheckpointLoader, KSampler, VAEDecode, SaveImage, etc.)
- Ensure all node connections are properly defined
- Generate production-ready JSON that can be loaded directly into ComfyUI
- Do NOT generate random or example workflows - create workflows based on the user's actual request
- Always include "Built with anycoder - https://huggingface.co/spaces/akhaliq/anycoder" as a comment in workflow metadata if possible
π¨ REMINDER: Your workflow should directly address what the user asked for. Don't ignore their message!
"""
return full_prompt + final_instructions
def initialize_backend_docs():
"""Initialize backend documentation system on startup"""
try:
# Pre-load the Gradio documentation
gradio_docs = get_gradio_docs_content()
if gradio_docs:
print(f"π Gradio documentation initialized ({len(gradio_docs)} chars loaded)")
else:
print("β οΈ Gradio documentation initialized with fallback content")
# Pre-load the transformers.js documentation
transformersjs_docs = get_transformersjs_docs_content()
if transformersjs_docs:
print(f"π transformers.js documentation initialized ({len(transformersjs_docs)} chars loaded)")
else:
print("β οΈ transformers.js documentation initialized with fallback content")
# Pre-load the ComfyUI documentation
comfyui_docs = get_comfyui_docs_content()
if comfyui_docs:
print(f"π ComfyUI documentation initialized ({len(comfyui_docs)} chars loaded)")
else:
print("β οΈ ComfyUI documentation initialized with fallback content")
except Exception as e:
print(f"Warning: Failed to initialize backend documentation: {e}")
# Initialize on import
if __name__ != "__main__":
# Only initialize if being imported (not run directly)
try:
initialize_backend_docs()
except Exception as e:
print(f"Warning: Failed to auto-initialize backend docs: {e}")
|