Spaces:
Running
Running
add fastrtc docs
Browse files
app.py
CHANGED
|
@@ -79,6 +79,16 @@ COMFYUI_DOCS_UPDATE_ON_APP_UPDATE = True # Only update when app is updated, not
|
|
| 79 |
_comfyui_docs_content: str | None = None
|
| 80 |
_comfyui_docs_last_fetched: Optional[datetime] = None
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
def fetch_gradio_docs() -> str | None:
|
| 83 |
"""Fetch the latest Gradio documentation from llms.txt"""
|
| 84 |
try:
|
|
@@ -99,6 +109,16 @@ def fetch_comfyui_docs() -> str | None:
|
|
| 99 |
print(f"Warning: Failed to fetch ComfyUI docs from {COMFYUI_LLMS_TXT_URL}: {e}")
|
| 100 |
return None
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
def filter_problematic_instructions(content: str) -> str:
|
| 103 |
"""Filter out problematic instructions that cause LLM to stop generation prematurely"""
|
| 104 |
if not content:
|
|
@@ -171,6 +191,26 @@ def save_comfyui_docs_cache(content: str):
|
|
| 171 |
except Exception as e:
|
| 172 |
print(f"Warning: Failed to save ComfyUI docs cache: {e}")
|
| 173 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
def get_last_update_time() -> Optional[datetime]:
|
| 175 |
"""Get the last update time from file"""
|
| 176 |
try:
|
|
@@ -191,6 +231,11 @@ def should_update_comfyui_docs() -> bool:
|
|
| 191 |
# Only update if we don't have cached content (first run or cache deleted)
|
| 192 |
return not os.path.exists(COMFYUI_DOCS_CACHE_FILE)
|
| 193 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
def force_update_gradio_docs():
|
| 195 |
"""
|
| 196 |
Force an update of Gradio documentation (useful when app is updated).
|
|
@@ -241,6 +286,31 @@ def force_update_comfyui_docs():
|
|
| 241 |
print("β Failed to update ComfyUI documentation")
|
| 242 |
return False
|
| 243 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
def get_gradio_docs_content() -> str:
|
| 245 |
"""Get the current Gradio documentation content, updating if necessary"""
|
| 246 |
global _gradio_docs_content, _gradio_docs_last_fetched
|
|
@@ -327,11 +397,55 @@ def get_comfyui_docs_content() -> str:
|
|
| 327 |
|
| 328 |
return _comfyui_docs_content or ""
|
| 329 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 330 |
def update_gradio_system_prompts():
|
| 331 |
"""Update the global Gradio system prompts with latest documentation"""
|
| 332 |
global GRADIO_SYSTEM_PROMPT, GRADIO_SYSTEM_PROMPT_WITH_SEARCH
|
| 333 |
|
| 334 |
docs_content = get_gradio_docs_content()
|
|
|
|
| 335 |
|
| 336 |
# Base system prompt
|
| 337 |
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.
|
|
@@ -875,6 +989,21 @@ This reference is automatically synced from https://www.gradio.app/llms.txt to e
|
|
| 875 |
|
| 876 |
"""
|
| 877 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 878 |
# Update the prompts
|
| 879 |
GRADIO_SYSTEM_PROMPT = base_prompt + docs_content + "\n\nAlways use the exact function signatures from this API reference and follow modern Gradio patterns.\n\nIMPORTANT: 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"
|
| 880 |
GRADIO_SYSTEM_PROMPT_WITH_SEARCH = search_prompt + docs_content + "\n\nAlways use the exact function signatures from this API reference and follow modern Gradio patterns.\n\nIMPORTANT: 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"
|
|
@@ -952,6 +1081,20 @@ def initialize_comfyui_docs():
|
|
| 952 |
except Exception as e:
|
| 953 |
print(f"Warning: Failed to initialize ComfyUI documentation: {e}")
|
| 954 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 955 |
# Configuration
|
| 956 |
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
|
| 957 |
|
|
@@ -10094,6 +10237,9 @@ if __name__ == "__main__":
|
|
| 10094 |
# Initialize ComfyUI documentation system
|
| 10095 |
initialize_comfyui_docs()
|
| 10096 |
|
|
|
|
|
|
|
|
|
|
| 10097 |
# Clean up any orphaned temporary files from previous runs
|
| 10098 |
cleanup_all_temp_media_on_startup()
|
| 10099 |
|
|
|
|
| 79 |
_comfyui_docs_content: str | None = None
|
| 80 |
_comfyui_docs_last_fetched: Optional[datetime] = None
|
| 81 |
|
| 82 |
+
# FastRTC Documentation Auto-Update System
|
| 83 |
+
FASTRTC_LLMS_TXT_URL = "https://fastrtc.org/llms.txt"
|
| 84 |
+
FASTRTC_DOCS_CACHE_FILE = ".fastrtc_docs_cache.txt"
|
| 85 |
+
FASTRTC_DOCS_LAST_UPDATE_FILE = ".fastrtc_docs_last_update.txt"
|
| 86 |
+
FASTRTC_DOCS_UPDATE_ON_APP_UPDATE = True # Only update when app is updated, not on a timer
|
| 87 |
+
|
| 88 |
+
# Global variable to store the current FastRTC documentation
|
| 89 |
+
_fastrtc_docs_content: str | None = None
|
| 90 |
+
_fastrtc_docs_last_fetched: Optional[datetime] = None
|
| 91 |
+
|
| 92 |
def fetch_gradio_docs() -> str | None:
|
| 93 |
"""Fetch the latest Gradio documentation from llms.txt"""
|
| 94 |
try:
|
|
|
|
| 109 |
print(f"Warning: Failed to fetch ComfyUI docs from {COMFYUI_LLMS_TXT_URL}: {e}")
|
| 110 |
return None
|
| 111 |
|
| 112 |
+
def fetch_fastrtc_docs() -> str | None:
|
| 113 |
+
"""Fetch the latest FastRTC documentation from llms.txt"""
|
| 114 |
+
try:
|
| 115 |
+
response = requests.get(FASTRTC_LLMS_TXT_URL, timeout=10)
|
| 116 |
+
response.raise_for_status()
|
| 117 |
+
return response.text
|
| 118 |
+
except Exception as e:
|
| 119 |
+
print(f"Warning: Failed to fetch FastRTC docs from {FASTRTC_LLMS_TXT_URL}: {e}")
|
| 120 |
+
return None
|
| 121 |
+
|
| 122 |
def filter_problematic_instructions(content: str) -> str:
|
| 123 |
"""Filter out problematic instructions that cause LLM to stop generation prematurely"""
|
| 124 |
if not content:
|
|
|
|
| 191 |
except Exception as e:
|
| 192 |
print(f"Warning: Failed to save ComfyUI docs cache: {e}")
|
| 193 |
|
| 194 |
+
def load_fastrtc_docs_cache() -> str | None:
|
| 195 |
+
"""Load FastRTC documentation from cache file"""
|
| 196 |
+
try:
|
| 197 |
+
if os.path.exists(FASTRTC_DOCS_CACHE_FILE):
|
| 198 |
+
with open(FASTRTC_DOCS_CACHE_FILE, 'r', encoding='utf-8') as f:
|
| 199 |
+
return f.read()
|
| 200 |
+
except Exception as e:
|
| 201 |
+
print(f"Warning: Failed to load cached FastRTC docs: {e}")
|
| 202 |
+
return None
|
| 203 |
+
|
| 204 |
+
def save_fastrtc_docs_cache(content: str):
|
| 205 |
+
"""Save FastRTC documentation to cache file"""
|
| 206 |
+
try:
|
| 207 |
+
with open(FASTRTC_DOCS_CACHE_FILE, 'w', encoding='utf-8') as f:
|
| 208 |
+
f.write(content)
|
| 209 |
+
with open(FASTRTC_DOCS_LAST_UPDATE_FILE, 'w', encoding='utf-8') as f:
|
| 210 |
+
f.write(datetime.now().isoformat())
|
| 211 |
+
except Exception as e:
|
| 212 |
+
print(f"Warning: Failed to save FastRTC docs cache: {e}")
|
| 213 |
+
|
| 214 |
def get_last_update_time() -> Optional[datetime]:
|
| 215 |
"""Get the last update time from file"""
|
| 216 |
try:
|
|
|
|
| 231 |
# Only update if we don't have cached content (first run or cache deleted)
|
| 232 |
return not os.path.exists(COMFYUI_DOCS_CACHE_FILE)
|
| 233 |
|
| 234 |
+
def should_update_fastrtc_docs() -> bool:
|
| 235 |
+
"""Check if FastRTC documentation should be updated"""
|
| 236 |
+
# Only update if we don't have cached content (first run or cache deleted)
|
| 237 |
+
return not os.path.exists(FASTRTC_DOCS_CACHE_FILE)
|
| 238 |
+
|
| 239 |
def force_update_gradio_docs():
|
| 240 |
"""
|
| 241 |
Force an update of Gradio documentation (useful when app is updated).
|
|
|
|
| 286 |
print("β Failed to update ComfyUI documentation")
|
| 287 |
return False
|
| 288 |
|
| 289 |
+
def force_update_fastrtc_docs():
|
| 290 |
+
"""
|
| 291 |
+
Force an update of FastRTC documentation (useful when app is updated).
|
| 292 |
+
|
| 293 |
+
To manually refresh docs, you can call this function or simply delete the cache file:
|
| 294 |
+
rm .fastrtc_docs_cache.txt && restart the app
|
| 295 |
+
"""
|
| 296 |
+
global _fastrtc_docs_content, _fastrtc_docs_last_fetched
|
| 297 |
+
|
| 298 |
+
print("π Forcing FastRTC documentation update...")
|
| 299 |
+
latest_content = fetch_fastrtc_docs()
|
| 300 |
+
|
| 301 |
+
if latest_content:
|
| 302 |
+
# Filter out problematic instructions that cause early termination
|
| 303 |
+
filtered_content = filter_problematic_instructions(latest_content)
|
| 304 |
+
_fastrtc_docs_content = filtered_content
|
| 305 |
+
_fastrtc_docs_last_fetched = datetime.now()
|
| 306 |
+
save_fastrtc_docs_cache(filtered_content)
|
| 307 |
+
update_gradio_system_prompts()
|
| 308 |
+
print("β
FastRTC documentation updated successfully")
|
| 309 |
+
return True
|
| 310 |
+
else:
|
| 311 |
+
print("β Failed to update FastRTC documentation")
|
| 312 |
+
return False
|
| 313 |
+
|
| 314 |
def get_gradio_docs_content() -> str:
|
| 315 |
"""Get the current Gradio documentation content, updating if necessary"""
|
| 316 |
global _gradio_docs_content, _gradio_docs_last_fetched
|
|
|
|
| 397 |
|
| 398 |
return _comfyui_docs_content or ""
|
| 399 |
|
| 400 |
+
def get_fastrtc_docs_content() -> str:
|
| 401 |
+
"""Get the current FastRTC documentation content, updating if necessary"""
|
| 402 |
+
global _fastrtc_docs_content, _fastrtc_docs_last_fetched
|
| 403 |
+
|
| 404 |
+
# Check if we need to update
|
| 405 |
+
if (_fastrtc_docs_content is None or
|
| 406 |
+
_fastrtc_docs_last_fetched is None or
|
| 407 |
+
should_update_fastrtc_docs()):
|
| 408 |
+
|
| 409 |
+
print("Updating FastRTC documentation...")
|
| 410 |
+
|
| 411 |
+
# Try to fetch latest content
|
| 412 |
+
latest_content = fetch_fastrtc_docs()
|
| 413 |
+
|
| 414 |
+
if latest_content:
|
| 415 |
+
# Filter out problematic instructions that cause early termination
|
| 416 |
+
filtered_content = filter_problematic_instructions(latest_content)
|
| 417 |
+
_fastrtc_docs_content = filtered_content
|
| 418 |
+
_fastrtc_docs_last_fetched = datetime.now()
|
| 419 |
+
save_fastrtc_docs_cache(filtered_content)
|
| 420 |
+
print("β
FastRTC documentation updated successfully")
|
| 421 |
+
else:
|
| 422 |
+
# Fallback to cached content
|
| 423 |
+
cached_content = load_fastrtc_docs_cache()
|
| 424 |
+
if cached_content:
|
| 425 |
+
_fastrtc_docs_content = cached_content
|
| 426 |
+
_fastrtc_docs_last_fetched = datetime.now()
|
| 427 |
+
print("β οΈ Using cached FastRTC documentation (network fetch failed)")
|
| 428 |
+
else:
|
| 429 |
+
# Fallback to minimal content
|
| 430 |
+
_fastrtc_docs_content = """
|
| 431 |
+
# FastRTC API Reference (Offline Fallback)
|
| 432 |
+
|
| 433 |
+
This is a minimal fallback when documentation cannot be fetched.
|
| 434 |
+
Please check your internet connection for the latest API reference.
|
| 435 |
+
|
| 436 |
+
Basic FastRTC usage: Stream class, handlers, real-time audio/video processing.
|
| 437 |
+
Use Stream(handler, modality, mode) for real-time communication apps.
|
| 438 |
+
"""
|
| 439 |
+
print("β Using minimal fallback documentation")
|
| 440 |
+
|
| 441 |
+
return _fastrtc_docs_content or ""
|
| 442 |
+
|
| 443 |
def update_gradio_system_prompts():
|
| 444 |
"""Update the global Gradio system prompts with latest documentation"""
|
| 445 |
global GRADIO_SYSTEM_PROMPT, GRADIO_SYSTEM_PROMPT_WITH_SEARCH
|
| 446 |
|
| 447 |
docs_content = get_gradio_docs_content()
|
| 448 |
+
fastrtc_content = get_fastrtc_docs_content()
|
| 449 |
|
| 450 |
# Base system prompt
|
| 451 |
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.
|
|
|
|
| 989 |
|
| 990 |
"""
|
| 991 |
|
| 992 |
+
# Add FastRTC documentation if available
|
| 993 |
+
if fastrtc_content.strip():
|
| 994 |
+
fastrtc_section = f"""
|
| 995 |
+
## FastRTC Reference Documentation
|
| 996 |
+
|
| 997 |
+
When building real-time audio/video applications with Gradio, use this FastRTC reference:
|
| 998 |
+
|
| 999 |
+
{fastrtc_content}
|
| 1000 |
+
|
| 1001 |
+
This reference is automatically synced from https://fastrtc.org/llms.txt to ensure accuracy.
|
| 1002 |
+
|
| 1003 |
+
"""
|
| 1004 |
+
base_prompt += fastrtc_section
|
| 1005 |
+
search_prompt += fastrtc_section
|
| 1006 |
+
|
| 1007 |
# Update the prompts
|
| 1008 |
GRADIO_SYSTEM_PROMPT = base_prompt + docs_content + "\n\nAlways use the exact function signatures from this API reference and follow modern Gradio patterns.\n\nIMPORTANT: 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"
|
| 1009 |
GRADIO_SYSTEM_PROMPT_WITH_SEARCH = search_prompt + docs_content + "\n\nAlways use the exact function signatures from this API reference and follow modern Gradio patterns.\n\nIMPORTANT: 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"
|
|
|
|
| 1081 |
except Exception as e:
|
| 1082 |
print(f"Warning: Failed to initialize ComfyUI documentation: {e}")
|
| 1083 |
|
| 1084 |
+
# Initialize FastRTC documentation on startup
|
| 1085 |
+
def initialize_fastrtc_docs():
|
| 1086 |
+
"""Initialize FastRTC documentation on application startup"""
|
| 1087 |
+
try:
|
| 1088 |
+
# FastRTC docs are integrated into Gradio system prompts
|
| 1089 |
+
# So we call update_gradio_system_prompts to include FastRTC content
|
| 1090 |
+
update_gradio_system_prompts()
|
| 1091 |
+
if should_update_fastrtc_docs():
|
| 1092 |
+
print("π FastRTC documentation system initialized (fetched fresh content)")
|
| 1093 |
+
else:
|
| 1094 |
+
print("π FastRTC documentation system initialized (using cached content)")
|
| 1095 |
+
except Exception as e:
|
| 1096 |
+
print(f"Warning: Failed to initialize FastRTC documentation: {e}")
|
| 1097 |
+
|
| 1098 |
# Configuration
|
| 1099 |
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
|
| 1100 |
|
|
|
|
| 10237 |
# Initialize ComfyUI documentation system
|
| 10238 |
initialize_comfyui_docs()
|
| 10239 |
|
| 10240 |
+
# Initialize FastRTC documentation system
|
| 10241 |
+
initialize_fastrtc_docs()
|
| 10242 |
+
|
| 10243 |
# Clean up any orphaned temporary files from previous runs
|
| 10244 |
cleanup_all_temp_media_on_startup()
|
| 10245 |
|