Spaces:
Running
Running
update to use gradio docs
Browse files
app.py
CHANGED
@@ -35,6 +35,9 @@ import urllib.parse
|
|
35 |
import mimetypes
|
36 |
import threading
|
37 |
import atexit
|
|
|
|
|
|
|
38 |
|
39 |
# Gradio supported languages for syntax highlighting
|
40 |
GRADIO_SUPPORTED_LANGUAGES = [
|
@@ -54,6 +57,165 @@ SEARCH_START = "<<<<<<< SEARCH"
|
|
54 |
DIVIDER = "======="
|
55 |
REPLACE_END = ">>>>>>> REPLACE"
|
56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
# Configuration
|
58 |
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
|
59 |
|
@@ -482,6 +644,14 @@ The style.css should contain all the styling for the application.
|
|
482 |
|
483 |
Always output only the three code blocks as shown above, and do not include any explanations or extra text."""
|
484 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
485 |
GENERIC_SYSTEM_PROMPT = """You are an expert {language} developer. Write clean, idiomatic, and runnable {language} code for the user's request. If possible, include comments and best practices. Output ONLY the code inside a ``` code block, and do not include any explanations or extra text. If the user provides a file or other context, use it as a reference. If the code is for a script or app, make it as self-contained as possible. Do NOT add the language name at the top of the code output."""
|
486 |
|
487 |
# System prompt with search capability
|
@@ -4567,6 +4737,8 @@ Generate the exact search/replace blocks needed to make these changes."""
|
|
4567 |
system_prompt = TRANSFORMERS_JS_SYSTEM_PROMPT_WITH_SEARCH if enable_search else TRANSFORMERS_JS_SYSTEM_PROMPT
|
4568 |
elif language == "svelte":
|
4569 |
system_prompt = SVELTE_SYSTEM_PROMPT_WITH_SEARCH if enable_search else SVELTE_SYSTEM_PROMPT
|
|
|
|
|
4570 |
else:
|
4571 |
system_prompt = GENERIC_SYSTEM_PROMPT_WITH_SEARCH.format(language=language) if enable_search else GENERIC_SYSTEM_PROMPT.format(language=language)
|
4572 |
|
@@ -8109,6 +8281,9 @@ with gr.Blocks(
|
|
8109 |
# Optionally, you can keep the old deploy_btn.click for the default method as a secondary button.
|
8110 |
|
8111 |
if __name__ == "__main__":
|
|
|
|
|
|
|
8112 |
# Clean up any orphaned temporary files from previous runs
|
8113 |
cleanup_all_temp_media_on_startup()
|
8114 |
|
|
|
35 |
import mimetypes
|
36 |
import threading
|
37 |
import atexit
|
38 |
+
import asyncio
|
39 |
+
from datetime import datetime, timedelta
|
40 |
+
from typing import Optional
|
41 |
|
42 |
# Gradio supported languages for syntax highlighting
|
43 |
GRADIO_SUPPORTED_LANGUAGES = [
|
|
|
57 |
DIVIDER = "======="
|
58 |
REPLACE_END = ">>>>>>> REPLACE"
|
59 |
|
60 |
+
# Gradio Documentation Auto-Update System
|
61 |
+
GRADIO_LLMS_TXT_URL = "https://www.gradio.app/llms.txt"
|
62 |
+
GRADIO_DOCS_CACHE_FILE = ".gradio_docs_cache.txt"
|
63 |
+
GRADIO_DOCS_LAST_UPDATE_FILE = ".gradio_docs_last_update.txt"
|
64 |
+
GRADIO_DOCS_UPDATE_ON_APP_UPDATE = True # Only update when app is updated, not on a timer
|
65 |
+
|
66 |
+
# Global variable to store the current Gradio documentation
|
67 |
+
_gradio_docs_content: Optional[str] = None
|
68 |
+
_gradio_docs_last_fetched: Optional[datetime] = None
|
69 |
+
|
70 |
+
def fetch_gradio_docs() -> Optional[str]:
|
71 |
+
"""Fetch the latest Gradio documentation from llms.txt"""
|
72 |
+
try:
|
73 |
+
response = requests.get(GRADIO_LLMS_TXT_URL, timeout=10)
|
74 |
+
response.raise_for_status()
|
75 |
+
return response.text
|
76 |
+
except Exception as e:
|
77 |
+
print(f"Warning: Failed to fetch Gradio docs from {GRADIO_LLMS_TXT_URL}: {e}")
|
78 |
+
return None
|
79 |
+
|
80 |
+
def load_cached_gradio_docs() -> Optional[str]:
|
81 |
+
"""Load cached Gradio documentation from file"""
|
82 |
+
try:
|
83 |
+
if os.path.exists(GRADIO_DOCS_CACHE_FILE):
|
84 |
+
with open(GRADIO_DOCS_CACHE_FILE, 'r', encoding='utf-8') as f:
|
85 |
+
return f.read()
|
86 |
+
except Exception as e:
|
87 |
+
print(f"Warning: Failed to load cached Gradio docs: {e}")
|
88 |
+
return None
|
89 |
+
|
90 |
+
def save_gradio_docs_cache(content: str):
|
91 |
+
"""Save Gradio documentation to cache file"""
|
92 |
+
try:
|
93 |
+
with open(GRADIO_DOCS_CACHE_FILE, 'w', encoding='utf-8') as f:
|
94 |
+
f.write(content)
|
95 |
+
with open(GRADIO_DOCS_LAST_UPDATE_FILE, 'w', encoding='utf-8') as f:
|
96 |
+
f.write(datetime.now().isoformat())
|
97 |
+
except Exception as e:
|
98 |
+
print(f"Warning: Failed to save Gradio docs cache: {e}")
|
99 |
+
|
100 |
+
def get_last_update_time() -> Optional[datetime]:
|
101 |
+
"""Get the last update time from file"""
|
102 |
+
try:
|
103 |
+
if os.path.exists(GRADIO_DOCS_LAST_UPDATE_FILE):
|
104 |
+
with open(GRADIO_DOCS_LAST_UPDATE_FILE, 'r', encoding='utf-8') as f:
|
105 |
+
return datetime.fromisoformat(f.read().strip())
|
106 |
+
except Exception as e:
|
107 |
+
print(f"Warning: Failed to read last update time: {e}")
|
108 |
+
return None
|
109 |
+
|
110 |
+
def should_update_gradio_docs() -> bool:
|
111 |
+
"""Check if Gradio documentation should be updated"""
|
112 |
+
# Only update if we don't have cached content (first run or cache deleted)
|
113 |
+
return not os.path.exists(GRADIO_DOCS_CACHE_FILE)
|
114 |
+
|
115 |
+
def force_update_gradio_docs():
|
116 |
+
"""
|
117 |
+
Force an update of Gradio documentation (useful when app is updated).
|
118 |
+
|
119 |
+
To manually refresh docs, you can call this function or simply delete the cache file:
|
120 |
+
rm .gradio_docs_cache.txt && restart the app
|
121 |
+
"""
|
122 |
+
global _gradio_docs_content, _gradio_docs_last_fetched
|
123 |
+
|
124 |
+
print("π Forcing Gradio documentation update...")
|
125 |
+
latest_content = fetch_gradio_docs()
|
126 |
+
|
127 |
+
if latest_content:
|
128 |
+
_gradio_docs_content = latest_content
|
129 |
+
_gradio_docs_last_fetched = datetime.now()
|
130 |
+
save_gradio_docs_cache(latest_content)
|
131 |
+
update_gradio_system_prompts()
|
132 |
+
print("β
Gradio documentation updated successfully")
|
133 |
+
return True
|
134 |
+
else:
|
135 |
+
print("β Failed to update Gradio documentation")
|
136 |
+
return False
|
137 |
+
|
138 |
+
def get_gradio_docs_content() -> str:
|
139 |
+
"""Get the current Gradio documentation content, updating if necessary"""
|
140 |
+
global _gradio_docs_content, _gradio_docs_last_fetched
|
141 |
+
|
142 |
+
# Check if we need to update
|
143 |
+
if (_gradio_docs_content is None or
|
144 |
+
_gradio_docs_last_fetched is None or
|
145 |
+
should_update_gradio_docs()):
|
146 |
+
|
147 |
+
print("Updating Gradio documentation...")
|
148 |
+
|
149 |
+
# Try to fetch latest content
|
150 |
+
latest_content = fetch_gradio_docs()
|
151 |
+
|
152 |
+
if latest_content:
|
153 |
+
_gradio_docs_content = latest_content
|
154 |
+
_gradio_docs_last_fetched = datetime.now()
|
155 |
+
save_gradio_docs_cache(latest_content)
|
156 |
+
print("β
Gradio documentation updated successfully")
|
157 |
+
else:
|
158 |
+
# Fallback to cached content
|
159 |
+
cached_content = load_cached_gradio_docs()
|
160 |
+
if cached_content:
|
161 |
+
_gradio_docs_content = cached_content
|
162 |
+
_gradio_docs_last_fetched = datetime.now()
|
163 |
+
print("β οΈ Using cached Gradio documentation (network fetch failed)")
|
164 |
+
else:
|
165 |
+
# Fallback to minimal content
|
166 |
+
_gradio_docs_content = """
|
167 |
+
# Gradio API Reference (Offline Fallback)
|
168 |
+
|
169 |
+
This is a minimal fallback when documentation cannot be fetched.
|
170 |
+
Please check your internet connection for the latest API reference.
|
171 |
+
|
172 |
+
Basic Gradio components: Button, Textbox, Slider, Image, Audio, Video, File, etc.
|
173 |
+
Use gr.Blocks() for custom layouts and gr.Interface() for simple apps.
|
174 |
+
"""
|
175 |
+
print("β Using minimal fallback documentation")
|
176 |
+
|
177 |
+
return _gradio_docs_content or ""
|
178 |
+
|
179 |
+
def update_gradio_system_prompts():
|
180 |
+
"""Update the global Gradio system prompts with latest documentation"""
|
181 |
+
global GRADIO_SYSTEM_PROMPT, GRADIO_SYSTEM_PROMPT_WITH_SEARCH
|
182 |
+
|
183 |
+
docs_content = get_gradio_docs_content()
|
184 |
+
|
185 |
+
# Base system prompt
|
186 |
+
base_prompt = """You are an expert Gradio developer. Write clean, idiomatic, and runnable Gradio applications for the user's request. Use the latest Gradio API and best practices. Output ONLY the code inside a ``` code block, and do not include any explanations or extra text. If the user provides a file or other context, use it as a reference. Make the app as self-contained as possible. Do NOT add the language name at the top of the code output.
|
187 |
+
|
188 |
+
## Complete Gradio API Reference
|
189 |
+
|
190 |
+
This reference is automatically synced from https://www.gradio.app/llms.txt to ensure accuracy.
|
191 |
+
|
192 |
+
"""
|
193 |
+
|
194 |
+
# Search-enabled prompt
|
195 |
+
search_prompt = """You are an expert Gradio developer with access to real-time web search. Write clean, idiomatic, and runnable Gradio applications for the user's request. Use the latest Gradio API and best practices. When needed, use web search to find current best practices or verify latest Gradio features. Output ONLY the code inside a ``` code block, and do not include any explanations or extra text. If the user provides a file or other context, use it as a reference. Make the app as self-contained as possible. Do NOT add the language name at the top of the code output.
|
196 |
+
|
197 |
+
## Complete Gradio API Reference
|
198 |
+
|
199 |
+
This reference is automatically synced from https://www.gradio.app/llms.txt to ensure accuracy.
|
200 |
+
|
201 |
+
"""
|
202 |
+
|
203 |
+
# Update the prompts
|
204 |
+
GRADIO_SYSTEM_PROMPT = base_prompt + docs_content + "\n\nAlways use the exact function signatures from this API reference and follow modern Gradio patterns."
|
205 |
+
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."
|
206 |
+
|
207 |
+
# Initialize Gradio documentation on startup
|
208 |
+
def initialize_gradio_docs():
|
209 |
+
"""Initialize Gradio documentation on application startup"""
|
210 |
+
try:
|
211 |
+
update_gradio_system_prompts()
|
212 |
+
if should_update_gradio_docs():
|
213 |
+
print("π Gradio documentation system initialized (fetched fresh content)")
|
214 |
+
else:
|
215 |
+
print("π Gradio documentation system initialized (using cached content)")
|
216 |
+
except Exception as e:
|
217 |
+
print(f"Warning: Failed to initialize Gradio documentation: {e}")
|
218 |
+
|
219 |
# Configuration
|
220 |
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
|
221 |
|
|
|
644 |
|
645 |
Always output only the three code blocks as shown above, and do not include any explanations or extra text."""
|
646 |
|
647 |
+
# Gradio system prompts will be dynamically populated by update_gradio_system_prompts()
|
648 |
+
GRADIO_SYSTEM_PROMPT = ""
|
649 |
+
GRADIO_SYSTEM_PROMPT_WITH_SEARCH = ""
|
650 |
+
|
651 |
+
# GRADIO_SYSTEM_PROMPT_WITH_SEARCH will be dynamically populated by update_gradio_system_prompts()
|
652 |
+
|
653 |
+
# All Gradio API documentation is now dynamically loaded from https://www.gradio.app/llms.txt
|
654 |
+
|
655 |
GENERIC_SYSTEM_PROMPT = """You are an expert {language} developer. Write clean, idiomatic, and runnable {language} code for the user's request. If possible, include comments and best practices. Output ONLY the code inside a ``` code block, and do not include any explanations or extra text. If the user provides a file or other context, use it as a reference. If the code is for a script or app, make it as self-contained as possible. Do NOT add the language name at the top of the code output."""
|
656 |
|
657 |
# System prompt with search capability
|
|
|
4737 |
system_prompt = TRANSFORMERS_JS_SYSTEM_PROMPT_WITH_SEARCH if enable_search else TRANSFORMERS_JS_SYSTEM_PROMPT
|
4738 |
elif language == "svelte":
|
4739 |
system_prompt = SVELTE_SYSTEM_PROMPT_WITH_SEARCH if enable_search else SVELTE_SYSTEM_PROMPT
|
4740 |
+
elif language == "gradio":
|
4741 |
+
system_prompt = GRADIO_SYSTEM_PROMPT_WITH_SEARCH if enable_search else GRADIO_SYSTEM_PROMPT
|
4742 |
else:
|
4743 |
system_prompt = GENERIC_SYSTEM_PROMPT_WITH_SEARCH.format(language=language) if enable_search else GENERIC_SYSTEM_PROMPT.format(language=language)
|
4744 |
|
|
|
8281 |
# Optionally, you can keep the old deploy_btn.click for the default method as a secondary button.
|
8282 |
|
8283 |
if __name__ == "__main__":
|
8284 |
+
# Initialize Gradio documentation system
|
8285 |
+
initialize_gradio_docs()
|
8286 |
+
|
8287 |
# Clean up any orphaned temporary files from previous runs
|
8288 |
cleanup_all_temp_media_on_startup()
|
8289 |
|