akhaliq HF Staff commited on
Commit
d1c02ef
Β·
1 Parent(s): 739ee1e

gradio 6 support

Browse files
Files changed (4) hide show
  1. .gitignore +10 -0
  2. backend_api.py +4 -1
  3. backend_docs_manager.py +235 -0
  4. backend_prompts.py +19 -1
.gitignore CHANGED
@@ -71,6 +71,16 @@ coverage.xml
71
  log/
72
  logs/
73
 
 
 
 
 
 
 
 
 
 
 
74
  # System files
75
  .DS_Store
76
  Thumbs.db
 
71
  log/
72
  logs/
73
 
74
+ # Documentation cache files (backend)
75
+ .backend_gradio_docs_cache.txt
76
+ .backend_gradio_docs_last_update.txt
77
+ .gradio_docs_cache.txt
78
+ .gradio_docs_last_update.txt
79
+ .comfyui_docs_cache.txt
80
+ .comfyui_docs_last_update.txt
81
+ .fastrtc_docs_cache.txt
82
+ .fastrtc_docs_last_update.txt
83
+
84
  # System files
85
  .DS_Store
86
  Thumbs.db
backend_api.py CHANGED
@@ -42,11 +42,14 @@ try:
42
  TRANSFORMERS_JS_SYSTEM_PROMPT,
43
  STREAMLIT_SYSTEM_PROMPT,
44
  REACT_SYSTEM_PROMPT,
45
- GRADIO_SYSTEM_PROMPT,
46
  JSON_SYSTEM_PROMPT,
47
  GENERIC_SYSTEM_PROMPT
48
  )
 
 
49
  print("[Startup] βœ… All system prompts loaded successfully from backend_prompts.py")
 
50
  except Exception as e:
51
  import traceback
52
  print(f"[Startup] ❌ ERROR: Could not import from backend_prompts: {e}")
 
42
  TRANSFORMERS_JS_SYSTEM_PROMPT,
43
  STREAMLIT_SYSTEM_PROMPT,
44
  REACT_SYSTEM_PROMPT,
45
+ get_gradio_system_prompt, # Import the function to get dynamic prompt
46
  JSON_SYSTEM_PROMPT,
47
  GENERIC_SYSTEM_PROMPT
48
  )
49
+ # Get the Gradio system prompt (includes full Gradio 6 documentation)
50
+ GRADIO_SYSTEM_PROMPT = get_gradio_system_prompt()
51
  print("[Startup] βœ… All system prompts loaded successfully from backend_prompts.py")
52
+ print(f"[Startup] πŸ“š Gradio system prompt loaded with full documentation ({len(GRADIO_SYSTEM_PROMPT)} chars)")
53
  except Exception as e:
54
  import traceback
55
  print(f"[Startup] ❌ ERROR: Could not import from backend_prompts: {e}")
backend_docs_manager.py ADDED
@@ -0,0 +1,235 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Documentation management for backend system prompts.
3
+ Handles fetching, caching, and updating documentation from llms.txt files.
4
+ No dependencies on Gradio or other heavy libraries - pure Python only.
5
+ """
6
+ import os
7
+ import re
8
+ from datetime import datetime
9
+ from typing import Optional
10
+
11
+ try:
12
+ import requests
13
+ HAS_REQUESTS = True
14
+ except ImportError:
15
+ HAS_REQUESTS = False
16
+ print("Warning: requests library not available, using minimal fallback")
17
+
18
+ # Configuration
19
+ GRADIO_LLMS_TXT_URL = "https://www.gradio.app/llms.txt"
20
+ GRADIO_DOCS_CACHE_FILE = ".backend_gradio_docs_cache.txt"
21
+ GRADIO_DOCS_LAST_UPDATE_FILE = ".backend_gradio_docs_last_update.txt"
22
+
23
+ # Global variable to store the current Gradio documentation
24
+ _gradio_docs_content: Optional[str] = None
25
+ _gradio_docs_last_fetched: Optional[datetime] = None
26
+
27
+ def fetch_gradio_docs() -> Optional[str]:
28
+ """Fetch the latest Gradio documentation from llms.txt"""
29
+ if not HAS_REQUESTS:
30
+ return None
31
+
32
+ try:
33
+ response = requests.get(GRADIO_LLMS_TXT_URL, timeout=10)
34
+ response.raise_for_status()
35
+ return response.text
36
+ except Exception as e:
37
+ print(f"Warning: Failed to fetch Gradio docs from {GRADIO_LLMS_TXT_URL}: {e}")
38
+ return None
39
+
40
+ def filter_problematic_instructions(content: str) -> str:
41
+ """Filter out problematic instructions that cause LLM to stop generation prematurely"""
42
+ if not content:
43
+ return content
44
+
45
+ # List of problematic phrases that cause early termination when LLM encounters ``` in user code
46
+ problematic_patterns = [
47
+ r"Output ONLY the code inside a ``` code block, and do not include any explanations or extra text",
48
+ r"output only the code inside a ```.*?``` code block",
49
+ r"Always output only the.*?code.*?inside.*?```.*?```.*?block",
50
+ r"Return ONLY the code inside a.*?```.*?``` code block",
51
+ r"Do NOT add the language name at the top of the code output",
52
+ r"do not include any explanations or extra text",
53
+ r"Always output only the.*?code blocks.*?shown above, and do not include any explanations",
54
+ r"Output.*?ONLY.*?code.*?inside.*?```.*?```",
55
+ r"Return.*?ONLY.*?code.*?inside.*?```.*?```",
56
+ r"Generate.*?ONLY.*?code.*?inside.*?```.*?```",
57
+ r"Provide.*?ONLY.*?code.*?inside.*?```.*?```",
58
+ ]
59
+
60
+ # Remove problematic patterns
61
+ filtered_content = content
62
+ for pattern in problematic_patterns:
63
+ # Use case-insensitive matching
64
+ filtered_content = re.sub(pattern, "", filtered_content, flags=re.IGNORECASE | re.DOTALL)
65
+
66
+ # Clean up any double newlines or extra whitespace left by removals
67
+ filtered_content = re.sub(r'\n\s*\n\s*\n', '\n\n', filtered_content)
68
+ filtered_content = re.sub(r'^\s+', '', filtered_content, flags=re.MULTILINE)
69
+
70
+ return filtered_content
71
+
72
+ def load_cached_gradio_docs() -> Optional[str]:
73
+ """Load cached Gradio documentation from file"""
74
+ try:
75
+ if os.path.exists(GRADIO_DOCS_CACHE_FILE):
76
+ with open(GRADIO_DOCS_CACHE_FILE, 'r', encoding='utf-8') as f:
77
+ return f.read()
78
+ except Exception as e:
79
+ print(f"Warning: Failed to load cached Gradio docs: {e}")
80
+ return None
81
+
82
+ def save_gradio_docs_cache(content: str):
83
+ """Save Gradio documentation to cache file"""
84
+ try:
85
+ with open(GRADIO_DOCS_CACHE_FILE, 'w', encoding='utf-8') as f:
86
+ f.write(content)
87
+ with open(GRADIO_DOCS_LAST_UPDATE_FILE, 'w', encoding='utf-8') as f:
88
+ f.write(datetime.now().isoformat())
89
+ except Exception as e:
90
+ print(f"Warning: Failed to save Gradio docs cache: {e}")
91
+
92
+ def should_update_gradio_docs() -> bool:
93
+ """Check if Gradio documentation should be updated"""
94
+ # Only update if we don't have cached content (first run or cache deleted)
95
+ return not os.path.exists(GRADIO_DOCS_CACHE_FILE)
96
+
97
+ def get_gradio_docs_content() -> str:
98
+ """Get the current Gradio documentation content, updating if necessary"""
99
+ global _gradio_docs_content, _gradio_docs_last_fetched
100
+
101
+ # Check if we need to update
102
+ if (_gradio_docs_content is None or
103
+ _gradio_docs_last_fetched is None or
104
+ should_update_gradio_docs()):
105
+
106
+ print("πŸ“š Loading Gradio 6 documentation...")
107
+
108
+ # Try to fetch latest content
109
+ latest_content = fetch_gradio_docs()
110
+
111
+ if latest_content:
112
+ # Filter out problematic instructions that cause early termination
113
+ filtered_content = filter_problematic_instructions(latest_content)
114
+ _gradio_docs_content = filtered_content
115
+ _gradio_docs_last_fetched = datetime.now()
116
+ save_gradio_docs_cache(filtered_content)
117
+ print(f"βœ… Gradio 6 documentation loaded successfully ({len(filtered_content)} chars)")
118
+ else:
119
+ # Fallback to cached content
120
+ cached_content = load_cached_gradio_docs()
121
+ if cached_content:
122
+ _gradio_docs_content = cached_content
123
+ _gradio_docs_last_fetched = datetime.now()
124
+ print(f"⚠️ Using cached Gradio documentation (network fetch failed) ({len(cached_content)} chars)")
125
+ else:
126
+ # Fallback to minimal content
127
+ _gradio_docs_content = """
128
+ # Gradio API Reference (Offline Fallback)
129
+
130
+ This is a minimal fallback when documentation cannot be fetched.
131
+ Please check your internet connection for the latest API reference.
132
+
133
+ Basic Gradio components: Button, Textbox, Slider, Image, Audio, Video, File, etc.
134
+ Use gr.Blocks() for custom layouts and gr.Interface() for simple apps.
135
+
136
+ For the latest documentation, visit: https://www.gradio.app/llms.txt
137
+ """
138
+ print("❌ Using minimal fallback documentation")
139
+
140
+ return _gradio_docs_content or ""
141
+
142
+ def build_gradio_system_prompt() -> str:
143
+ """Build the complete Gradio system prompt with full documentation"""
144
+
145
+ # Get the full Gradio 6 documentation
146
+ docs_content = get_gradio_docs_content()
147
+
148
+ # Base system prompt with anycoder-specific instructions
149
+ base_prompt = """You are an expert Gradio developer. Create a complete, working Gradio application based on the user's request. Generate all necessary code to make the application functional and runnable.
150
+
151
+ ## Multi-File Application Structure
152
+
153
+ When creating Gradio applications, organize your code into multiple files for proper deployment:
154
+
155
+ **File Organization:**
156
+ - `app.py` - Main application entry point (REQUIRED)
157
+ - `requirements.txt` - Python dependencies (REQUIRED, auto-generated from imports)
158
+ - `utils.py` - Utility functions and helpers (optional)
159
+ - `models.py` - Model loading and inference functions (optional)
160
+ - `config.py` - Configuration and constants (optional)
161
+
162
+ **Output Format:**
163
+ You MUST use this exact format with file separators:
164
+
165
+ === app.py ===
166
+ [complete app.py content]
167
+
168
+ === utils.py ===
169
+ [utility functions - if needed]
170
+
171
+ **🚨 CRITICAL: DO NOT GENERATE requirements.txt or README.md**
172
+ - requirements.txt is automatically generated from your app.py imports
173
+ - README.md is automatically provided by the template
174
+ - Generating these files will break the deployment process
175
+
176
+ Requirements:
177
+ 1. Create a modern, intuitive Gradio application
178
+ 2. Use appropriate Gradio components (gr.Textbox, gr.Slider, etc.)
179
+ 3. Include proper error handling and loading states
180
+ 4. Use gr.Interface or gr.Blocks as appropriate
181
+ 5. Add helpful descriptions and examples
182
+ 6. Follow Gradio best practices
183
+ 7. Make the UI user-friendly with clear labels
184
+ 8. Include proper documentation in docstrings
185
+
186
+ 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
187
+
188
+ ---
189
+
190
+ ## Complete Gradio 6 Documentation
191
+
192
+ Below is the complete, official Gradio 6 documentation automatically synced from https://www.gradio.app/llms.txt:
193
+
194
+ """
195
+
196
+ # Combine base prompt with full documentation
197
+ full_prompt = base_prompt + docs_content
198
+
199
+ # Add final instructions
200
+ final_instructions = """
201
+
202
+ ---
203
+
204
+ ## Final Instructions
205
+
206
+ - Always use the exact function signatures and patterns from the Gradio 6 documentation above
207
+ - Follow Gradio 6 migration guidelines if you're familiar with older versions
208
+ - Use modern Gradio 6 API patterns (e.g., footer_links instead of show_api, api_visibility instead of show_api in events)
209
+ - Generate production-ready code that follows all best practices
210
+ - Always include the "Built with anycoder" attribution in the header
211
+
212
+ """
213
+
214
+ return full_prompt + final_instructions
215
+
216
+ def initialize_backend_docs():
217
+ """Initialize backend documentation system on startup"""
218
+ try:
219
+ # Pre-load the documentation
220
+ docs = get_gradio_docs_content()
221
+ if docs:
222
+ print(f"πŸš€ Backend documentation system initialized ({len(docs)} chars loaded)")
223
+ else:
224
+ print("⚠️ Backend documentation system initialized with fallback content")
225
+ except Exception as e:
226
+ print(f"Warning: Failed to initialize backend documentation: {e}")
227
+
228
+ # Initialize on import
229
+ if __name__ != "__main__":
230
+ # Only initialize if being imported (not run directly)
231
+ try:
232
+ initialize_backend_docs()
233
+ except Exception as e:
234
+ print(f"Warning: Failed to auto-initialize backend docs: {e}")
235
+
backend_prompts.py CHANGED
@@ -3,6 +3,14 @@ Standalone system prompts for AnyCoder backend.
3
  No dependencies on Gradio or other heavy libraries.
4
  """
5
 
 
 
 
 
 
 
 
 
6
  HTML_SYSTEM_PROMPT = """ONLY USE HTML, CSS AND JAVASCRIPT. If you want to use ICON make sure to import the library first. Try to create the best UI possible by using only HTML, CSS and JAVASCRIPT. MAKE IT RESPONSIVE USING MODERN CSS. Use as much as you can modern CSS for the styling, if you can't do something with modern CSS, then use custom CSS. Also, try to elaborate as much as you can, to create something unique. ALWAYS GIVE THE RESPONSE INTO A SINGLE HTML FILE
7
 
8
  **🚨 CRITICAL: DO NOT Generate README.md Files**
@@ -187,7 +195,14 @@ IMPORTANT: Always include "Built with anycoder" as clickable text in the header/
187
  """
188
 
189
 
190
- GRADIO_SYSTEM_PROMPT = """You are an expert Gradio developer. Create a complete, working Gradio application based on the user's request. Generate all necessary code to make the application functional and runnable.
 
 
 
 
 
 
 
191
 
192
  ## Multi-File Application Structure
193
 
@@ -227,6 +242,9 @@ Requirements:
227
  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
228
  """
229
 
 
 
 
230
 
231
  JSON_SYSTEM_PROMPT = """You are an expert at generating JSON configurations for ComfyUI workflows. Create valid, well-structured JSON that can be loaded into ComfyUI.
232
 
 
3
  No dependencies on Gradio or other heavy libraries.
4
  """
5
 
6
+ # Import the backend documentation manager for Gradio 6 docs
7
+ try:
8
+ from backend_docs_manager import build_gradio_system_prompt
9
+ HAS_BACKEND_DOCS = True
10
+ except ImportError:
11
+ HAS_BACKEND_DOCS = False
12
+ print("Warning: backend_docs_manager not available, using fallback Gradio prompt")
13
+
14
  HTML_SYSTEM_PROMPT = """ONLY USE HTML, CSS AND JAVASCRIPT. If you want to use ICON make sure to import the library first. Try to create the best UI possible by using only HTML, CSS and JAVASCRIPT. MAKE IT RESPONSIVE USING MODERN CSS. Use as much as you can modern CSS for the styling, if you can't do something with modern CSS, then use custom CSS. Also, try to elaborate as much as you can, to create something unique. ALWAYS GIVE THE RESPONSE INTO A SINGLE HTML FILE
15
 
16
  **🚨 CRITICAL: DO NOT Generate README.md Files**
 
195
  """
196
 
197
 
198
+ # Gradio system prompt - dynamically loaded with full Gradio 6 documentation
199
+ def get_gradio_system_prompt() -> str:
200
+ """Get the complete Gradio system prompt with full Gradio 6 documentation"""
201
+ if HAS_BACKEND_DOCS:
202
+ return build_gradio_system_prompt()
203
+ else:
204
+ # Fallback prompt if documentation manager is not available
205
+ return """You are an expert Gradio developer. Create a complete, working Gradio application based on the user's request. Generate all necessary code to make the application functional and runnable.
206
 
207
  ## Multi-File Application Structure
208
 
 
242
  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
243
  """
244
 
245
+ # Legacy variable for backward compatibility - now dynamically generated
246
+ GRADIO_SYSTEM_PROMPT = get_gradio_system_prompt()
247
+
248
 
249
  JSON_SYSTEM_PROMPT = """You are an expert at generating JSON configurations for ComfyUI workflows. Create valid, well-structured JSON that can be loaded into ComfyUI.
250