Spaces:
Running
Running
update
Browse files
app.py
CHANGED
|
@@ -771,7 +771,6 @@ def generate(prompt):
|
|
| 771 |
- + FP8 Quantization: Additional 1.2x speedup
|
| 772 |
- + FlashAttention-3: Additional attention speedup
|
| 773 |
- Total potential: 2x-3x faster inference
|
| 774 |
-
|
| 775 |
**Hardware Requirements:**
|
| 776 |
- FP8 quantization requires CUDA compute capability ≥ 9.0 (H200 ✅)
|
| 777 |
- FlashAttention-3 works on H200 hardware via kernels library
|
|
@@ -789,7 +788,7 @@ To make your Gradio app function as an MCP (Model Control Protocol) server:
|
|
| 789 |
4. Use type hints for all function parameters
|
| 790 |
|
| 791 |
**Example:**
|
| 792 |
-
```
|
| 793 |
import gradio as gr
|
| 794 |
|
| 795 |
def letter_counter(word: str, letter: str) -> int:
|
|
@@ -841,7 +840,7 @@ if __name__ == "__main__":
|
|
| 841 |
- Use gr.Progress() for long-running operations
|
| 842 |
|
| 843 |
**Multiple Tools Example:**
|
| 844 |
-
```
|
| 845 |
import gradio as gr
|
| 846 |
|
| 847 |
def add_numbers(a: str, b: str) -> str:
|
|
@@ -973,7 +972,7 @@ ALWAYS use ZeroGPU for GPU-dependent functions in Gradio apps:
|
|
| 973 |
- Heavy processing (120-180s): `@spaces.GPU(duration=180)`
|
| 974 |
|
| 975 |
Example usage:
|
| 976 |
-
```
|
| 977 |
import spaces
|
| 978 |
from diffusers import DiffusionPipeline
|
| 979 |
|
|
@@ -1028,7 +1027,7 @@ YOU MUST USE THIS EXACT PATTERN for any diffusion model (FLUX, Stable Diffusion,
|
|
| 1028 |
For production Spaces with heavy models, use ahead-of-time (AoT) compilation for 1.3x-1.8x speedups:
|
| 1029 |
|
| 1030 |
### Basic AoT Compilation
|
| 1031 |
-
```
|
| 1032 |
import spaces
|
| 1033 |
import torch
|
| 1034 |
from diffusers import DiffusionPipeline
|
|
@@ -1065,7 +1064,7 @@ def generate(prompt):
|
|
| 1065 |
### Advanced Optimizations
|
| 1066 |
|
| 1067 |
#### FP8 Quantization (Additional 1.2x speedup on H200)
|
| 1068 |
-
```
|
| 1069 |
from torchao.quantization import quantize_, Float8DynamicActivationFloat8WeightConfig
|
| 1070 |
|
| 1071 |
@spaces.GPU(duration=1500)
|
|
@@ -1085,7 +1084,7 @@ def compile_transformer_with_quantization():
|
|
| 1085 |
```
|
| 1086 |
|
| 1087 |
#### Dynamic Shapes (Variable input sizes)
|
| 1088 |
-
```
|
| 1089 |
from torch.utils._pytree import tree_map
|
| 1090 |
|
| 1091 |
@spaces.GPU(duration=1500)
|
|
@@ -1116,7 +1115,7 @@ def compile_transformer_dynamic():
|
|
| 1116 |
```
|
| 1117 |
|
| 1118 |
#### Multi-Compile for Different Resolutions
|
| 1119 |
-
```
|
| 1120 |
@spaces.GPU(duration=1500)
|
| 1121 |
def compile_multiple_resolutions():
|
| 1122 |
compiled_models = {}
|
|
@@ -1149,7 +1148,7 @@ def generate_with_resolution(prompt, width=1024, height=1024):
|
|
| 1149 |
```
|
| 1150 |
|
| 1151 |
#### FlashAttention-3 Integration
|
| 1152 |
-
```
|
| 1153 |
from kernels import get_kernel
|
| 1154 |
|
| 1155 |
# Load pre-built FA3 kernel compatible with H200
|
|
@@ -1173,7 +1172,7 @@ if 'vllm_flash_attn3' in locals():
|
|
| 1173 |
```
|
| 1174 |
|
| 1175 |
### Complete Optimized Example
|
| 1176 |
-
```
|
| 1177 |
import spaces
|
| 1178 |
import torch
|
| 1179 |
from diffusers import DiffusionPipeline
|
|
@@ -1232,7 +1231,7 @@ To make your Gradio app function as an MCP (Model Control Protocol) server:
|
|
| 1232 |
4. Use type hints for all function parameters
|
| 1233 |
|
| 1234 |
**Example:**
|
| 1235 |
-
```
|
| 1236 |
import gradio as gr
|
| 1237 |
|
| 1238 |
def letter_counter(word: str, letter: str) -> int:
|
|
@@ -1284,7 +1283,7 @@ if __name__ == "__main__":
|
|
| 1284 |
- Use gr.Progress() for long-running operations
|
| 1285 |
|
| 1286 |
**Multiple Tools Example:**
|
| 1287 |
-
```
|
| 1288 |
import gradio as gr
|
| 1289 |
|
| 1290 |
def add_numbers(a: str, b: str) -> str:
|
|
@@ -1550,7 +1549,6 @@ Hard constraints:
|
|
| 1550 |
- Use ONLY plain HTML, CSS, and vanilla JavaScript.
|
| 1551 |
- Allowed external resources: Tailwind CSS CDN, Font Awesome CDN, Google Fonts.
|
| 1552 |
- Do NOT escape characters (no \\n, \\t, or escaped quotes). Output raw HTML/JS/CSS.
|
| 1553 |
-
|
| 1554 |
Structural requirements:
|
| 1555 |
- Include <!DOCTYPE html>, <html>, <head>, and <body> with proper nesting
|
| 1556 |
- Include required <link> tags for any CSS you reference (e.g., Tailwind, Font Awesome, Google Fonts)
|
|
@@ -2292,7 +2290,6 @@ body {{
|
|
| 2292 |
}}
|
| 2293 |
{REPLACE_END}
|
| 2294 |
```
|
| 2295 |
-
|
| 2296 |
Example Fixing Library Loading Error:
|
| 2297 |
```
|
| 2298 |
Fixing transformers.js CDN loading error...
|
|
@@ -3015,7 +3012,6 @@ def validate_and_autofix_files(files: Dict[str, str]) -> Dict[str, str]:
|
|
| 3015 |
# This prevents unwanted SVG stub files from being generated during image generation
|
| 3016 |
|
| 3017 |
return normalized
|
| 3018 |
-
|
| 3019 |
def inline_multipage_into_single_preview(files: Dict[str, str]) -> str:
|
| 3020 |
"""Inline local CSS/JS referenced by index.html for preview inside a data: iframe.
|
| 3021 |
|
|
@@ -3723,9 +3719,6 @@ def cleanup_temp_media_files():
|
|
| 3723 |
|
| 3724 |
except Exception as e:
|
| 3725 |
print(f"[TempCleanup] Error during cleanup: {str(e)}")
|
| 3726 |
-
|
| 3727 |
-
|
| 3728 |
-
|
| 3729 |
def generate_image_to_image(input_image_data, prompt: str, token: gr.OAuthToken | None = None) -> str:
|
| 3730 |
"""Generate an image using image-to-image via OpenRouter.
|
| 3731 |
|
|
@@ -4519,7 +4512,6 @@ def extract_image_prompts_from_text(text: str, num_images_needed: int = 1) -> li
|
|
| 4519 |
prompts.append(variations[variation_index])
|
| 4520 |
|
| 4521 |
return prompts
|
| 4522 |
-
|
| 4523 |
def create_image_replacement_blocks(html_content: str, user_prompt: str) -> str:
|
| 4524 |
"""Create search/replace blocks to replace placeholder images with generated Qwen images"""
|
| 4525 |
if not user_prompt:
|
|
@@ -8088,7 +8080,6 @@ def get_saved_theme():
|
|
| 8088 |
except:
|
| 8089 |
pass
|
| 8090 |
return "Developer"
|
| 8091 |
-
|
| 8092 |
def save_theme_preference(theme_name):
|
| 8093 |
"""Save theme preference to file"""
|
| 8094 |
try:
|
|
@@ -8611,7 +8602,7 @@ with gr.Blocks(
|
|
| 8611 |
with gr.Group(visible=False) as react_group:
|
| 8612 |
with gr.Tabs():
|
| 8613 |
with gr.Tab("Dockerfile"):
|
| 8614 |
-
react_code_dockerfile = gr.Code(language="
|
| 8615 |
with gr.Tab("package.json"):
|
| 8616 |
react_code_package_json = gr.Code(language="json", lines=20, interactive=True, label="package.json")
|
| 8617 |
with gr.Tab("next.config.js"):
|
|
@@ -8842,7 +8833,6 @@ with gr.Blocks(
|
|
| 8842 |
inputs=[language_dropdown, code_output],
|
| 8843 |
outputs=[code_output, tjs_group, tjs_html_code, tjs_js_code, tjs_css_code, react_group, react_code_dockerfile, react_code_package_json, react_code_next_config, react_code_postcss_config, react_code_tailwind_config, react_code_pages_app, react_code_pages_index, react_code_components, react_code_styles],
|
| 8844 |
)
|
| 8845 |
-
|
| 8846 |
# Toggle Python multi-file editors for Gradio/Streamlit
|
| 8847 |
def toggle_python_editors(language, code_text):
|
| 8848 |
if language not in ["gradio", "streamlit"]:
|
|
@@ -9317,7 +9307,6 @@ with gr.Blocks(
|
|
| 9317 |
restart_message = f"""
|
| 9318 |
🎨 **Theme saved:** {theme_name}
|
| 9319 |
⚠️ **Restart required** to fully apply the new theme.
|
| 9320 |
-
|
| 9321 |
**Why restart is needed:** Gradio themes are set during application startup and cannot be changed dynamically at runtime. This ensures all components are properly styled with consistent theming.
|
| 9322 |
|
| 9323 |
**To apply your new theme:**
|
|
@@ -10116,7 +10105,6 @@ with gr.Blocks(
|
|
| 10116 |
if files['index.html'] and files['index.js'] and files['style.css']:
|
| 10117 |
return format_transformers_js_output(files)
|
| 10118 |
return code_text
|
| 10119 |
-
|
| 10120 |
deploy_btn.click(
|
| 10121 |
gather_code_for_deploy,
|
| 10122 |
inputs=[code_output, language_dropdown, tjs_html_code, tjs_js_code, tjs_css_code],
|
|
|
|
| 771 |
- + FP8 Quantization: Additional 1.2x speedup
|
| 772 |
- + FlashAttention-3: Additional attention speedup
|
| 773 |
- Total potential: 2x-3x faster inference
|
|
|
|
| 774 |
**Hardware Requirements:**
|
| 775 |
- FP8 quantization requires CUDA compute capability ≥ 9.0 (H200 ✅)
|
| 776 |
- FlashAttention-3 works on H200 hardware via kernels library
|
|
|
|
| 788 |
4. Use type hints for all function parameters
|
| 789 |
|
| 790 |
**Example:**
|
| 791 |
+
```
|
| 792 |
import gradio as gr
|
| 793 |
|
| 794 |
def letter_counter(word: str, letter: str) -> int:
|
|
|
|
| 840 |
- Use gr.Progress() for long-running operations
|
| 841 |
|
| 842 |
**Multiple Tools Example:**
|
| 843 |
+
```
|
| 844 |
import gradio as gr
|
| 845 |
|
| 846 |
def add_numbers(a: str, b: str) -> str:
|
|
|
|
| 972 |
- Heavy processing (120-180s): `@spaces.GPU(duration=180)`
|
| 973 |
|
| 974 |
Example usage:
|
| 975 |
+
```
|
| 976 |
import spaces
|
| 977 |
from diffusers import DiffusionPipeline
|
| 978 |
|
|
|
|
| 1027 |
For production Spaces with heavy models, use ahead-of-time (AoT) compilation for 1.3x-1.8x speedups:
|
| 1028 |
|
| 1029 |
### Basic AoT Compilation
|
| 1030 |
+
```
|
| 1031 |
import spaces
|
| 1032 |
import torch
|
| 1033 |
from diffusers import DiffusionPipeline
|
|
|
|
| 1064 |
### Advanced Optimizations
|
| 1065 |
|
| 1066 |
#### FP8 Quantization (Additional 1.2x speedup on H200)
|
| 1067 |
+
```
|
| 1068 |
from torchao.quantization import quantize_, Float8DynamicActivationFloat8WeightConfig
|
| 1069 |
|
| 1070 |
@spaces.GPU(duration=1500)
|
|
|
|
| 1084 |
```
|
| 1085 |
|
| 1086 |
#### Dynamic Shapes (Variable input sizes)
|
| 1087 |
+
```
|
| 1088 |
from torch.utils._pytree import tree_map
|
| 1089 |
|
| 1090 |
@spaces.GPU(duration=1500)
|
|
|
|
| 1115 |
```
|
| 1116 |
|
| 1117 |
#### Multi-Compile for Different Resolutions
|
| 1118 |
+
```
|
| 1119 |
@spaces.GPU(duration=1500)
|
| 1120 |
def compile_multiple_resolutions():
|
| 1121 |
compiled_models = {}
|
|
|
|
| 1148 |
```
|
| 1149 |
|
| 1150 |
#### FlashAttention-3 Integration
|
| 1151 |
+
```
|
| 1152 |
from kernels import get_kernel
|
| 1153 |
|
| 1154 |
# Load pre-built FA3 kernel compatible with H200
|
|
|
|
| 1172 |
```
|
| 1173 |
|
| 1174 |
### Complete Optimized Example
|
| 1175 |
+
```
|
| 1176 |
import spaces
|
| 1177 |
import torch
|
| 1178 |
from diffusers import DiffusionPipeline
|
|
|
|
| 1231 |
4. Use type hints for all function parameters
|
| 1232 |
|
| 1233 |
**Example:**
|
| 1234 |
+
```
|
| 1235 |
import gradio as gr
|
| 1236 |
|
| 1237 |
def letter_counter(word: str, letter: str) -> int:
|
|
|
|
| 1283 |
- Use gr.Progress() for long-running operations
|
| 1284 |
|
| 1285 |
**Multiple Tools Example:**
|
| 1286 |
+
```
|
| 1287 |
import gradio as gr
|
| 1288 |
|
| 1289 |
def add_numbers(a: str, b: str) -> str:
|
|
|
|
| 1549 |
- Use ONLY plain HTML, CSS, and vanilla JavaScript.
|
| 1550 |
- Allowed external resources: Tailwind CSS CDN, Font Awesome CDN, Google Fonts.
|
| 1551 |
- Do NOT escape characters (no \\n, \\t, or escaped quotes). Output raw HTML/JS/CSS.
|
|
|
|
| 1552 |
Structural requirements:
|
| 1553 |
- Include <!DOCTYPE html>, <html>, <head>, and <body> with proper nesting
|
| 1554 |
- Include required <link> tags for any CSS you reference (e.g., Tailwind, Font Awesome, Google Fonts)
|
|
|
|
| 2290 |
}}
|
| 2291 |
{REPLACE_END}
|
| 2292 |
```
|
|
|
|
| 2293 |
Example Fixing Library Loading Error:
|
| 2294 |
```
|
| 2295 |
Fixing transformers.js CDN loading error...
|
|
|
|
| 3012 |
# This prevents unwanted SVG stub files from being generated during image generation
|
| 3013 |
|
| 3014 |
return normalized
|
|
|
|
| 3015 |
def inline_multipage_into_single_preview(files: Dict[str, str]) -> str:
|
| 3016 |
"""Inline local CSS/JS referenced by index.html for preview inside a data: iframe.
|
| 3017 |
|
|
|
|
| 3719 |
|
| 3720 |
except Exception as e:
|
| 3721 |
print(f"[TempCleanup] Error during cleanup: {str(e)}")
|
|
|
|
|
|
|
|
|
|
| 3722 |
def generate_image_to_image(input_image_data, prompt: str, token: gr.OAuthToken | None = None) -> str:
|
| 3723 |
"""Generate an image using image-to-image via OpenRouter.
|
| 3724 |
|
|
|
|
| 4512 |
prompts.append(variations[variation_index])
|
| 4513 |
|
| 4514 |
return prompts
|
|
|
|
| 4515 |
def create_image_replacement_blocks(html_content: str, user_prompt: str) -> str:
|
| 4516 |
"""Create search/replace blocks to replace placeholder images with generated Qwen images"""
|
| 4517 |
if not user_prompt:
|
|
|
|
| 8080 |
except:
|
| 8081 |
pass
|
| 8082 |
return "Developer"
|
|
|
|
| 8083 |
def save_theme_preference(theme_name):
|
| 8084 |
"""Save theme preference to file"""
|
| 8085 |
try:
|
|
|
|
| 8602 |
with gr.Group(visible=False) as react_group:
|
| 8603 |
with gr.Tabs():
|
| 8604 |
with gr.Tab("Dockerfile"):
|
| 8605 |
+
react_code_dockerfile = gr.Code(language="dockerfile", lines=15, interactive=True, label="Dockerfile")
|
| 8606 |
with gr.Tab("package.json"):
|
| 8607 |
react_code_package_json = gr.Code(language="json", lines=20, interactive=True, label="package.json")
|
| 8608 |
with gr.Tab("next.config.js"):
|
|
|
|
| 8833 |
inputs=[language_dropdown, code_output],
|
| 8834 |
outputs=[code_output, tjs_group, tjs_html_code, tjs_js_code, tjs_css_code, react_group, react_code_dockerfile, react_code_package_json, react_code_next_config, react_code_postcss_config, react_code_tailwind_config, react_code_pages_app, react_code_pages_index, react_code_components, react_code_styles],
|
| 8835 |
)
|
|
|
|
| 8836 |
# Toggle Python multi-file editors for Gradio/Streamlit
|
| 8837 |
def toggle_python_editors(language, code_text):
|
| 8838 |
if language not in ["gradio", "streamlit"]:
|
|
|
|
| 9307 |
restart_message = f"""
|
| 9308 |
🎨 **Theme saved:** {theme_name}
|
| 9309 |
⚠️ **Restart required** to fully apply the new theme.
|
|
|
|
| 9310 |
**Why restart is needed:** Gradio themes are set during application startup and cannot be changed dynamically at runtime. This ensures all components are properly styled with consistent theming.
|
| 9311 |
|
| 9312 |
**To apply your new theme:**
|
|
|
|
| 10105 |
if files['index.html'] and files['index.js'] and files['style.css']:
|
| 10106 |
return format_transformers_js_output(files)
|
| 10107 |
return code_text
|
|
|
|
| 10108 |
deploy_btn.click(
|
| 10109 |
gather_code_for_deploy,
|
| 10110 |
inputs=[code_output, language_dropdown, tjs_html_code, tjs_js_code, tjs_css_code],
|