Spaces:
Sleeping
Sleeping
Commit
·
952fb23
1
Parent(s):
e041d24
init
Browse files
app.py
CHANGED
|
@@ -90,22 +90,127 @@ class DreamLoRAInference:
|
|
| 90 |
# CSS is exactly the same as your original script
|
| 91 |
CSS = """
|
| 92 |
/* Enhanced modern styling */
|
| 93 |
-
.main-container {
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
.
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
.
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
.token.prompt { background: linear-gradient(135deg, #f1f5f9 0%, #e2e8f0 100%); color: #475569; border: 1px solid #cbd5e1; }
|
| 110 |
.token.gen-0 { background: linear-gradient(135deg, #dbeafe 0%, #bfdbfe 100%); color: #1e40af; border: 1px solid #60a5fa; }
|
| 111 |
.token.gen-1 { background: linear-gradient(135deg, #d1fae5 0%, #a7f3d0 100%); color: #065f46; border: 1px solid #34d399; }
|
|
@@ -113,21 +218,96 @@ class DreamLoRAInference:
|
|
| 113 |
.token.gen-3 { background: linear-gradient(135deg, #fee2e2 0%, #fecaca 100%); color: #991b1b; border: 1px solid #f87171; }
|
| 114 |
.token.gen-4 { background: linear-gradient(135deg, #e0e7ff 0%, #c7d2fe 100%); color: #3730a3; border: 1px solid #818cf8; }
|
| 115 |
.token.gen-5 { background: linear-gradient(135deg, #f3e8ff 0%, #e9d5ff 100%); color: #6b21a8; border: 1px solid #c084fc; }
|
| 116 |
-
.token.mask {
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
.
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
"""
|
| 132 |
|
| 133 |
def __init__(self, **kwargs):
|
|
@@ -184,35 +364,60 @@ class DreamLoRAInference:
|
|
| 184 |
block_states[next_block_id]['is_complete'] = True
|
| 185 |
|
| 186 |
def _render_visualization_html(self, step: int, x_t: torch.Tensor, block_states: Dict, cache_length: int, updated_block_ids: Set[int]) -> str:
|
|
|
|
| 187 |
timestamp = int(time.time() * 1000)
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
| 189 |
for block_id in sorted(k for k in block_states.keys() if k > 0):
|
| 190 |
state = block_states[block_id]
|
| 191 |
-
container_classes = ["block-container"
|
|
|
|
| 192 |
html_parts.append(f'<div class="{" ".join(container_classes)}" id="block-{block_id}-{timestamp}">')
|
| 193 |
block_tokens = x_t[0, state['start_pos']:state['end_pos']]
|
| 194 |
for token_id in block_tokens:
|
| 195 |
token_id_int = token_id.item()
|
| 196 |
token_classes = ["token"]
|
| 197 |
if token_id_int == self.mask_token_id:
|
| 198 |
-
token_str
|
| 199 |
else:
|
| 200 |
token_str = self.tokenizer.decode([token_id_int], skip_special_tokens=False)
|
| 201 |
token_str = token_str.replace('&', '&').replace('<', '<').replace('>', '>')
|
| 202 |
token_classes.append(f"gen-{(block_id - 1) % 6}")
|
| 203 |
html_parts.append(f'<span class="{" ".join(token_classes)}">{token_str}</span>')
|
| 204 |
html_parts.append('</div>')
|
|
|
|
| 205 |
html_parts.append(f'<div class="scroll-anchor" id="viz-anchor-{timestamp}"></div>')
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
@spaces.GPU
|
| 209 |
@torch.inference_mode()
|
| 210 |
def stream_and_capture_for_gradio(
|
| 211 |
-
self,
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
) -> Iterator[Tuple[str, List[str], str, bool]]:
|
| 214 |
|
| 215 |
-
|
|
|
|
| 216 |
|
| 217 |
start_time = time.time()
|
| 218 |
captured_frames: List[str] = []
|
|
@@ -295,12 +500,14 @@ class DreamLoRAInference:
|
|
| 295 |
|
| 296 |
current_viz_html = self._render_visualization_html(step, x_t, block_states, cache_length, updated_block_ids)
|
| 297 |
captured_frames.append(current_viz_html)
|
|
|
|
| 298 |
yield live_text, captured_frames, f'<div class="generating-indicator">Generating... Step {step}</div>', False
|
| 299 |
|
| 300 |
total_time = time.time() - start_time
|
| 301 |
final_generated_ids = x_t[0, prompt_length:]
|
| 302 |
eos_positions = (final_generated_ids == self.tokenizer.eos_token_id).nonzero()
|
| 303 |
-
if eos_positions.numel() > 0:
|
|
|
|
| 304 |
|
| 305 |
final_text = self.tokenizer.decode(final_generated_ids, skip_special_tokens=True)
|
| 306 |
final_viz_html = self._render_visualization_html(step, x_t, block_states, cache_length, set())
|
|
@@ -313,10 +520,22 @@ class DreamLoRAInference:
|
|
| 313 |
<div class="stats-card">
|
| 314 |
<h3>✅ Generation Complete!</h3>
|
| 315 |
<div class="stats-grid">
|
| 316 |
-
<div class="stat-item"
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
</div>
|
| 321 |
</div>
|
| 322 |
"""
|
|
@@ -344,46 +563,29 @@ if __name__ == "__main__":
|
|
| 344 |
yield frame
|
| 345 |
time.sleep(delay)
|
| 346 |
|
| 347 |
-
#
|
| 348 |
auto_scroll_js = """
|
| 349 |
<script>
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
}
|
| 362 |
}
|
| 363 |
-
|
| 364 |
-
function setupObserver(containerId) {
|
| 365 |
-
const targetNode = document.getElementById(containerId);
|
| 366 |
-
if (!targetNode) return;
|
| 367 |
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
scrollElementToBottom(containerId);
|
| 371 |
-
};
|
| 372 |
-
const observer = new MutationObserver(callback);
|
| 373 |
-
observer.observe(targetNode, config);
|
| 374 |
-
}
|
| 375 |
-
|
| 376 |
-
document.addEventListener('DOMContentLoaded', function() {
|
| 377 |
setTimeout(() => {
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
const ta = liveTextComponent.querySelector('textarea');
|
| 382 |
-
if (ta) setupObserver(ta.id);
|
| 383 |
-
}
|
| 384 |
-
// Target the visualization container
|
| 385 |
-
setupObserver('viz-container');
|
| 386 |
-
}, 1500); // Delay to ensure Gradio renders elements
|
| 387 |
});
|
| 388 |
</script>
|
| 389 |
"""
|
|
@@ -392,11 +594,10 @@ if __name__ == "__main__":
|
|
| 392 |
html_frames_state = gr.State([])
|
| 393 |
generation_complete_state = gr.State(False)
|
| 394 |
|
| 395 |
-
#
|
| 396 |
-
# gr.HTML(auto_scroll_js) # This can be problematic, JS in HTML render is better.
|
| 397 |
|
|
|
|
| 398 |
with gr.Column(elem_classes=["main-container"]):
|
| 399 |
-
# UI structure is identical to your original script
|
| 400 |
gr.Markdown("# ✨ D2F: Faster-than-AR Inference for Diffusion LLMs")
|
| 401 |
gr.Markdown(
|
| 402 |
"""
|
|
@@ -413,7 +614,13 @@ if __name__ == "__main__":
|
|
| 413 |
|
| 414 |
with gr.Row():
|
| 415 |
with gr.Column(scale=2):
|
| 416 |
-
prompt_input = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 417 |
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
| 418 |
with gr.Row():
|
| 419 |
max_new_tokens_slider = gr.Slider(minimum=64, maximum=2048, value=1024, step=64, label="Max Tokens", info="Maximum number of tokens to generate")
|
|
@@ -424,6 +631,7 @@ if __name__ == "__main__":
|
|
| 424 |
with gr.Row():
|
| 425 |
skip_thresh_slider = gr.Slider(minimum=0.0, maximum=1.0, value=0.9, step=0.01, label="Skip Threshold", info="Token selection threshold")
|
| 426 |
delay_slider = gr.Slider(minimum=0.0, maximum=1.0, value=0.15, step=0.05, label="Playback Speed", info="Slow-motion playback delay (seconds)")
|
|
|
|
| 427 |
generate_button = gr.Button("🚀 Generate Text", variant="primary", size="lg")
|
| 428 |
|
| 429 |
with gr.Column(scale=3):
|
|
@@ -435,7 +643,7 @@ if __name__ == "__main__":
|
|
| 435 |
with gr.Row():
|
| 436 |
with gr.Column():
|
| 437 |
slowmo_button = gr.Button("🎬 Watch Slow-Motion Generation Process", variant="secondary", size="lg", elem_classes=["control-button"], visible=False, interactive=False)
|
| 438 |
-
with gr.Group(elem_classes=["viz-container"], visible=False
|
| 439 |
visualization_output = gr.HTML(label="")
|
| 440 |
|
| 441 |
# Examples are identical to your original script
|
|
@@ -449,7 +657,7 @@ if __name__ == "__main__":
|
|
| 449 |
label="💡 Try these examples"
|
| 450 |
)
|
| 451 |
|
| 452 |
-
# Event handling is identical to your original script
|
| 453 |
def update_slowmo_button_visibility(is_complete):
|
| 454 |
return gr.update(visible=is_complete, interactive=is_complete)
|
| 455 |
|
|
@@ -461,9 +669,10 @@ if __name__ == "__main__":
|
|
| 461 |
block_add_thresh_slider, decoded_token_thresh_slider, skip_thresh_slider
|
| 462 |
]
|
| 463 |
|
|
|
|
| 464 |
generation_event = generate_button.click(
|
| 465 |
-
fn=lambda: [gr.update(
|
| 466 |
-
outputs=[
|
| 467 |
).then(
|
| 468 |
fn=inference_engine.stream_and_capture_for_gradio,
|
| 469 |
inputs=inputs_list,
|
|
@@ -472,9 +681,6 @@ if __name__ == "__main__":
|
|
| 472 |
fn=update_slowmo_button_visibility,
|
| 473 |
inputs=[generation_complete_state],
|
| 474 |
outputs=[slowmo_button]
|
| 475 |
-
).then(
|
| 476 |
-
fn=lambda: gr.update(interactive=True),
|
| 477 |
-
outputs=[prompt_input]
|
| 478 |
)
|
| 479 |
|
| 480 |
slowmo_event = slowmo_button.click(
|
|
|
|
| 90 |
# CSS is exactly the same as your original script
|
| 91 |
CSS = """
|
| 92 |
/* Enhanced modern styling */
|
| 93 |
+
.main-container {
|
| 94 |
+
max-width: 1400px;
|
| 95 |
+
margin: 0 auto;
|
| 96 |
+
padding: 20px;
|
| 97 |
+
}
|
| 98 |
+
.output-text-container {
|
| 99 |
+
background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
|
| 100 |
+
border: 2px solid #e2e8f0;
|
| 101 |
+
border-radius: 12px;
|
| 102 |
+
padding: 20px;
|
| 103 |
+
margin: 15px 0;
|
| 104 |
+
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
|
| 105 |
+
}
|
| 106 |
+
.output-text-container textarea {
|
| 107 |
+
background: white !important;
|
| 108 |
+
border: 1px solid #cbd5e1 !important;
|
| 109 |
+
border-radius: 8px !important;
|
| 110 |
+
font-family: 'Inter', 'Segoe UI', sans-serif !important;
|
| 111 |
+
font-size: 14px !important;
|
| 112 |
+
line-height: 1.6 !important;
|
| 113 |
+
padding: 16px !important;
|
| 114 |
+
box-shadow: inset 0 2px 4px 0 rgba(0, 0, 0, 0.06) !important;
|
| 115 |
+
}
|
| 116 |
+
.stats-card {
|
| 117 |
+
background: linear-gradient(135deg, #ecfdf5 0%, #f0fdf4 100%);
|
| 118 |
+
border: 2px solid #10b981;
|
| 119 |
+
border-radius: 12px;
|
| 120 |
+
padding: 20px;
|
| 121 |
+
margin: 15px 0;
|
| 122 |
+
box-shadow: 0 4px 6px -1px rgba(16, 185, 129, 0.1);
|
| 123 |
+
}
|
| 124 |
+
.stats-card h3 {
|
| 125 |
+
color: #065f46;
|
| 126 |
+
margin-top: 0;
|
| 127 |
+
margin-bottom: 15px;
|
| 128 |
+
font-weight: 600;
|
| 129 |
+
}
|
| 130 |
+
.stats-grid {
|
| 131 |
+
display: grid;
|
| 132 |
+
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
| 133 |
+
gap: 12px;
|
| 134 |
+
margin-top: 10px;
|
| 135 |
+
}
|
| 136 |
+
.stat-item {
|
| 137 |
+
background: white;
|
| 138 |
+
padding: 12px 16px;
|
| 139 |
+
border-radius: 8px;
|
| 140 |
+
border-left: 4px solid #10b981;
|
| 141 |
+
box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1);
|
| 142 |
+
}
|
| 143 |
+
.stat-label {
|
| 144 |
+
font-size: 12px;
|
| 145 |
+
color: #6b7280;
|
| 146 |
+
text-transform: uppercase;
|
| 147 |
+
letter-spacing: 0.5px;
|
| 148 |
+
margin-bottom: 4px;
|
| 149 |
+
}
|
| 150 |
+
.stat-value {
|
| 151 |
+
font-size: 18px;
|
| 152 |
+
font-weight: 600;
|
| 153 |
+
color: #065f46;
|
| 154 |
+
font-family: 'Monaco', 'Menlo', monospace;
|
| 155 |
+
}
|
| 156 |
+
.viz-container {
|
| 157 |
+
background: linear-gradient(135deg, #fefefe 0%, #f9fafb 100%);
|
| 158 |
+
border: 2px solid #e5e7eb;
|
| 159 |
+
border-radius: 12px;
|
| 160 |
+
padding: 20px;
|
| 161 |
+
margin: 15px 0;
|
| 162 |
+
height: 600px;
|
| 163 |
+
overflow-y: auto;
|
| 164 |
+
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
|
| 165 |
+
position: relative;
|
| 166 |
+
}
|
| 167 |
+
.viz-header {
|
| 168 |
+
background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
|
| 169 |
+
color: white;
|
| 170 |
+
padding: 12px 20px;
|
| 171 |
+
margin: -20px -20px 20px -20px;
|
| 172 |
+
border-radius: 12px 12px 0 0;
|
| 173 |
+
font-weight: 600;
|
| 174 |
+
font-size: 16px;
|
| 175 |
+
display: flex;
|
| 176 |
+
align-items: center;
|
| 177 |
+
gap: 8px;
|
| 178 |
+
}
|
| 179 |
+
.viz-header::before {
|
| 180 |
+
content: "🎬";
|
| 181 |
+
font-size: 18px;
|
| 182 |
+
}
|
| 183 |
+
.block-container {
|
| 184 |
+
display: inline-block;
|
| 185 |
+
border: 2px solid transparent;
|
| 186 |
+
border-radius: 10px;
|
| 187 |
+
padding: 8px;
|
| 188 |
+
margin: 6px 2px;
|
| 189 |
+
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
|
| 190 |
+
background: rgba(255, 255, 255, 0.8);
|
| 191 |
+
backdrop-filter: blur(10px);
|
| 192 |
+
}
|
| 193 |
+
.block-updating {
|
| 194 |
+
border-color: #ff4500 !important;
|
| 195 |
+
box-shadow: 0 0 20px rgba(255, 69, 0, 0.4);
|
| 196 |
+
transform: scale(1.02);
|
| 197 |
+
background: rgba(255, 245, 238, 0.9) !important;
|
| 198 |
+
}
|
| 199 |
+
.token {
|
| 200 |
+
padding: 4px 8px;
|
| 201 |
+
margin: 2px;
|
| 202 |
+
border-radius: 6px;
|
| 203 |
+
display: inline-block;
|
| 204 |
+
line-height: 1.5;
|
| 205 |
+
font-family: 'Monaco', 'Menlo', monospace;
|
| 206 |
+
font-size: 13px;
|
| 207 |
+
font-weight: 500;
|
| 208 |
+
transition: all 0.2s ease;
|
| 209 |
+
}
|
| 210 |
+
.token:hover {
|
| 211 |
+
transform: translateY(-1px);
|
| 212 |
+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
|
| 213 |
+
}
|
| 214 |
.token.prompt { background: linear-gradient(135deg, #f1f5f9 0%, #e2e8f0 100%); color: #475569; border: 1px solid #cbd5e1; }
|
| 215 |
.token.gen-0 { background: linear-gradient(135deg, #dbeafe 0%, #bfdbfe 100%); color: #1e40af; border: 1px solid #60a5fa; }
|
| 216 |
.token.gen-1 { background: linear-gradient(135deg, #d1fae5 0%, #a7f3d0 100%); color: #065f46; border: 1px solid #34d399; }
|
|
|
|
| 218 |
.token.gen-3 { background: linear-gradient(135deg, #fee2e2 0%, #fecaca 100%); color: #991b1b; border: 1px solid #f87171; }
|
| 219 |
.token.gen-4 { background: linear-gradient(135deg, #e0e7ff 0%, #c7d2fe 100%); color: #3730a3; border: 1px solid #818cf8; }
|
| 220 |
.token.gen-5 { background: linear-gradient(135deg, #f3e8ff 0%, #e9d5ff 100%); color: #6b21a8; border: 1px solid #c084fc; }
|
| 221 |
+
.token.mask {
|
| 222 |
+
background: linear-gradient(135deg, #f9fafb 0%, #f3f4f6 100%);
|
| 223 |
+
color: #9ca3af;
|
| 224 |
+
border: 2px dashed #d1d5db;
|
| 225 |
+
animation: pulse 2s infinite;
|
| 226 |
+
}
|
| 227 |
+
@keyframes pulse {
|
| 228 |
+
0%, 100% { opacity: 1; }
|
| 229 |
+
50% { opacity: 0.6; }
|
| 230 |
+
}
|
| 231 |
+
.control-button {
|
| 232 |
+
background: linear-gradient(135deg, #8b5cf6 0%, #7c3aed 100%) !important;
|
| 233 |
+
border: none !important;
|
| 234 |
+
color: white !important;
|
| 235 |
+
padding: 12px 24px !important;
|
| 236 |
+
border-radius: 10px !important;
|
| 237 |
+
font-weight: 600 !important;
|
| 238 |
+
font-size: 14px !important;
|
| 239 |
+
box-shadow: 0 4px 6px -1px rgba(139, 92, 246, 0.3) !important;
|
| 240 |
+
transition: all 0.3s ease !important;
|
| 241 |
+
display: flex !important;
|
| 242 |
+
align-items: center !important;
|
| 243 |
+
gap: 8px !important;
|
| 244 |
+
margin: 10px 0 !important;
|
| 245 |
+
}
|
| 246 |
+
.control-button:hover {
|
| 247 |
+
transform: translateY(-2px) !important;
|
| 248 |
+
box-shadow: 0 8px 15px -3px rgba(139, 92, 246, 0.4) !important;
|
| 249 |
+
}
|
| 250 |
+
.control-button:active {
|
| 251 |
+
transform: translateY(0) !important;
|
| 252 |
+
}
|
| 253 |
+
.control-button::before {
|
| 254 |
+
content: "🎮";
|
| 255 |
+
font-size: 16px;
|
| 256 |
+
}
|
| 257 |
+
.param-card {
|
| 258 |
+
background: white;
|
| 259 |
+
border: 1px solid #e5e7eb;
|
| 260 |
+
border-radius: 10px;
|
| 261 |
+
padding: 16px;
|
| 262 |
+
margin: 8px 0;
|
| 263 |
+
box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1);
|
| 264 |
+
}
|
| 265 |
+
.viz-container::-webkit-scrollbar {
|
| 266 |
+
width: 12px;
|
| 267 |
+
}
|
| 268 |
+
.viz-container::-webkit-scrollbar-track {
|
| 269 |
+
background: #f1f5f9;
|
| 270 |
+
border-radius: 6px;
|
| 271 |
+
}
|
| 272 |
+
.viz-container::-webkit-scrollbar-thumb {
|
| 273 |
+
background: linear-gradient(135deg, #94a3b8 0%, #64748b 100%);
|
| 274 |
+
border-radius: 6px;
|
| 275 |
+
border: 2px solid #f1f5f9;
|
| 276 |
+
}
|
| 277 |
+
.viz-container::-webkit-scrollbar-thumb:hover {
|
| 278 |
+
background: linear-gradient(135deg, #64748b 0%, #475569 100%);
|
| 279 |
+
}
|
| 280 |
+
.generating-indicator {
|
| 281 |
+
display: inline-flex;
|
| 282 |
+
align-items: center;
|
| 283 |
+
gap: 8px;
|
| 284 |
+
color: #6366f1;
|
| 285 |
+
font-weight: 500;
|
| 286 |
+
}
|
| 287 |
+
.generating-indicator::after {
|
| 288 |
+
content: "";
|
| 289 |
+
width: 12px;
|
| 290 |
+
height: 12px;
|
| 291 |
+
border: 2px solid #6366f1;
|
| 292 |
+
border-top: 2px solid transparent;
|
| 293 |
+
border-radius: 50%;
|
| 294 |
+
animation: spin 1s linear infinite;
|
| 295 |
+
}
|
| 296 |
+
@keyframes spin {
|
| 297 |
+
0% { transform: rotate(0deg); }
|
| 298 |
+
100% { transform: rotate(360deg); }
|
| 299 |
+
}
|
| 300 |
+
@media (max-width: 768px) {
|
| 301 |
+
.main-container {
|
| 302 |
+
padding: 10px;
|
| 303 |
+
}
|
| 304 |
+
.stats-grid {
|
| 305 |
+
grid-template-columns: 1fr;
|
| 306 |
+
}
|
| 307 |
+
.viz-container {
|
| 308 |
+
height: 400px;
|
| 309 |
+
}
|
| 310 |
+
}
|
| 311 |
"""
|
| 312 |
|
| 313 |
def __init__(self, **kwargs):
|
|
|
|
| 364 |
block_states[next_block_id]['is_complete'] = True
|
| 365 |
|
| 366 |
def _render_visualization_html(self, step: int, x_t: torch.Tensor, block_states: Dict, cache_length: int, updated_block_ids: Set[int]) -> str:
|
| 367 |
+
# This function is identical to your original one, with HTML escaping.
|
| 368 |
timestamp = int(time.time() * 1000)
|
| 369 |
+
|
| 370 |
+
html_parts = []
|
| 371 |
+
html_parts.append('<div class="viz-header">Slow-Motion Generation Process</div>')
|
| 372 |
+
|
| 373 |
for block_id in sorted(k for k in block_states.keys() if k > 0):
|
| 374 |
state = block_states[block_id]
|
| 375 |
+
container_classes = ["block-container"]
|
| 376 |
+
if block_id in updated_block_ids: container_classes.append("block-updating")
|
| 377 |
html_parts.append(f'<div class="{" ".join(container_classes)}" id="block-{block_id}-{timestamp}">')
|
| 378 |
block_tokens = x_t[0, state['start_pos']:state['end_pos']]
|
| 379 |
for token_id in block_tokens:
|
| 380 |
token_id_int = token_id.item()
|
| 381 |
token_classes = ["token"]
|
| 382 |
if token_id_int == self.mask_token_id:
|
| 383 |
+
token_str = '░'; token_classes.append("mask")
|
| 384 |
else:
|
| 385 |
token_str = self.tokenizer.decode([token_id_int], skip_special_tokens=False)
|
| 386 |
token_str = token_str.replace('&', '&').replace('<', '<').replace('>', '>')
|
| 387 |
token_classes.append(f"gen-{(block_id - 1) % 6}")
|
| 388 |
html_parts.append(f'<span class="{" ".join(token_classes)}">{token_str}</span>')
|
| 389 |
html_parts.append('</div>')
|
| 390 |
+
|
| 391 |
html_parts.append(f'<div class="scroll-anchor" id="viz-anchor-{timestamp}"></div>')
|
| 392 |
+
|
| 393 |
+
# Script part from original for scrolling
|
| 394 |
+
complete_html = f"""
|
| 395 |
+
<div class="viz-content" id="viz-content-{timestamp}">
|
| 396 |
+
{''.join(html_parts)}
|
| 397 |
+
</div>
|
| 398 |
+
<script>
|
| 399 |
+
(function() {{
|
| 400 |
+
const container = document.querySelector('.viz-container');
|
| 401 |
+
if (container) {{ container.scrollTop = container.scrollHeight; }}
|
| 402 |
+
}})();
|
| 403 |
+
</script>
|
| 404 |
+
"""
|
| 405 |
+
return complete_html
|
| 406 |
|
| 407 |
@spaces.GPU
|
| 408 |
@torch.inference_mode()
|
| 409 |
def stream_and_capture_for_gradio(
|
| 410 |
+
self,
|
| 411 |
+
prompt_text: str,
|
| 412 |
+
max_new_tokens: int,
|
| 413 |
+
block_size: int,
|
| 414 |
+
block_add_threshold: float,
|
| 415 |
+
decoded_token_threshold: float,
|
| 416 |
+
skip_threshold: float
|
| 417 |
) -> Iterator[Tuple[str, List[str], str, bool]]:
|
| 418 |
|
| 419 |
+
# This is the core generation algorithm, now identical to your original script
|
| 420 |
+
self._ensure_model_loaded()
|
| 421 |
|
| 422 |
start_time = time.time()
|
| 423 |
captured_frames: List[str] = []
|
|
|
|
| 500 |
|
| 501 |
current_viz_html = self._render_visualization_html(step, x_t, block_states, cache_length, updated_block_ids)
|
| 502 |
captured_frames.append(current_viz_html)
|
| 503 |
+
|
| 504 |
yield live_text, captured_frames, f'<div class="generating-indicator">Generating... Step {step}</div>', False
|
| 505 |
|
| 506 |
total_time = time.time() - start_time
|
| 507 |
final_generated_ids = x_t[0, prompt_length:]
|
| 508 |
eos_positions = (final_generated_ids == self.tokenizer.eos_token_id).nonzero()
|
| 509 |
+
if eos_positions.numel() > 0:
|
| 510 |
+
final_generated_ids = final_generated_ids[:eos_positions[0, 0] + 1]
|
| 511 |
|
| 512 |
final_text = self.tokenizer.decode(final_generated_ids, skip_special_tokens=True)
|
| 513 |
final_viz_html = self._render_visualization_html(step, x_t, block_states, cache_length, set())
|
|
|
|
| 520 |
<div class="stats-card">
|
| 521 |
<h3>✅ Generation Complete!</h3>
|
| 522 |
<div class="stats-grid">
|
| 523 |
+
<div class="stat-item">
|
| 524 |
+
<div class="stat-label">Total Time</div>
|
| 525 |
+
<div class="stat-value">{total_time:.2f}s</div>
|
| 526 |
+
</div>
|
| 527 |
+
<div class="stat-item">
|
| 528 |
+
<div class="stat-label">Tokens (incl. EOS)</div>
|
| 529 |
+
<div class="stat-value">{tokens_incl_eos}</div>
|
| 530 |
+
</div>
|
| 531 |
+
<div class="stat-item">
|
| 532 |
+
<div class="stat-label">Tokens (excl. EOS)</div>
|
| 533 |
+
<div class="stat-value">{tokens_excl_eos}</div>
|
| 534 |
+
</div>
|
| 535 |
+
<div class="stat-item">
|
| 536 |
+
<div class="stat-label">Tokens/Second</div>
|
| 537 |
+
<div class="stat-value">{(tokens_incl_eos / total_time):.1f}</div>
|
| 538 |
+
</div>
|
| 539 |
</div>
|
| 540 |
</div>
|
| 541 |
"""
|
|
|
|
| 563 |
yield frame
|
| 564 |
time.sleep(delay)
|
| 565 |
|
| 566 |
+
# Simplified auto-scroll JS from your original script
|
| 567 |
auto_scroll_js = """
|
| 568 |
<script>
|
| 569 |
+
function setupAutoScroll(containerSelector, contentSelector) {
|
| 570 |
+
const container = document.querySelector(containerSelector);
|
| 571 |
+
if (!container) return;
|
| 572 |
+
|
| 573 |
+
const observer = new MutationObserver(() => {
|
| 574 |
+
container.scrollTop = container.scrollHeight;
|
| 575 |
+
});
|
| 576 |
+
|
| 577 |
+
observer.observe(container, {
|
| 578 |
+
childList: true,
|
| 579 |
+
subtree: true
|
| 580 |
+
});
|
| 581 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 582 |
|
| 583 |
+
document.addEventListener('DOMContentLoaded', () => {
|
| 584 |
+
// Use a timeout to ensure Gradio elements are rendered
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 585 |
setTimeout(() => {
|
| 586 |
+
setupAutoScroll('#live-text-output', 'textarea');
|
| 587 |
+
setupAutoScroll('.viz-container', '.viz-content');
|
| 588 |
+
}, 1500);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 589 |
});
|
| 590 |
</script>
|
| 591 |
"""
|
|
|
|
| 594 |
html_frames_state = gr.State([])
|
| 595 |
generation_complete_state = gr.State(False)
|
| 596 |
|
| 597 |
+
gr.HTML(auto_scroll_js) # Keep the JS injection
|
|
|
|
| 598 |
|
| 599 |
+
# The entire UI layout is now identical to your original script
|
| 600 |
with gr.Column(elem_classes=["main-container"]):
|
|
|
|
| 601 |
gr.Markdown("# ✨ D2F: Faster-than-AR Inference for Diffusion LLMs")
|
| 602 |
gr.Markdown(
|
| 603 |
"""
|
|
|
|
| 614 |
|
| 615 |
with gr.Row():
|
| 616 |
with gr.Column(scale=2):
|
| 617 |
+
prompt_input = gr.Textbox(
|
| 618 |
+
label="🤔 Enter your question",
|
| 619 |
+
placeholder="Ask me anything! Try: 'Explain quantum physics' or 'Write a story about...'",
|
| 620 |
+
lines=4,
|
| 621 |
+
elem_classes=["param-card"]
|
| 622 |
+
)
|
| 623 |
+
|
| 624 |
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
| 625 |
with gr.Row():
|
| 626 |
max_new_tokens_slider = gr.Slider(minimum=64, maximum=2048, value=1024, step=64, label="Max Tokens", info="Maximum number of tokens to generate")
|
|
|
|
| 631 |
with gr.Row():
|
| 632 |
skip_thresh_slider = gr.Slider(minimum=0.0, maximum=1.0, value=0.9, step=0.01, label="Skip Threshold", info="Token selection threshold")
|
| 633 |
delay_slider = gr.Slider(minimum=0.0, maximum=1.0, value=0.15, step=0.05, label="Playback Speed", info="Slow-motion playback delay (seconds)")
|
| 634 |
+
|
| 635 |
generate_button = gr.Button("🚀 Generate Text", variant="primary", size="lg")
|
| 636 |
|
| 637 |
with gr.Column(scale=3):
|
|
|
|
| 643 |
with gr.Row():
|
| 644 |
with gr.Column():
|
| 645 |
slowmo_button = gr.Button("🎬 Watch Slow-Motion Generation Process", variant="secondary", size="lg", elem_classes=["control-button"], visible=False, interactive=False)
|
| 646 |
+
with gr.Group(elem_classes=["viz-container"], visible=False) as viz_group:
|
| 647 |
visualization_output = gr.HTML(label="")
|
| 648 |
|
| 649 |
# Examples are identical to your original script
|
|
|
|
| 657 |
label="💡 Try these examples"
|
| 658 |
)
|
| 659 |
|
| 660 |
+
# Event handling is now identical to your original, correct script
|
| 661 |
def update_slowmo_button_visibility(is_complete):
|
| 662 |
return gr.update(visible=is_complete, interactive=is_complete)
|
| 663 |
|
|
|
|
| 669 |
block_add_thresh_slider, decoded_token_thresh_slider, skip_thresh_slider
|
| 670 |
]
|
| 671 |
|
| 672 |
+
# This is the original, correct event chain
|
| 673 |
generation_event = generate_button.click(
|
| 674 |
+
fn=lambda: [gr.update(visible=False, interactive=False), gr.update(visible=False), gr.update(value=None), gr.update(value="")],
|
| 675 |
+
outputs=[slowmo_button, viz_group, stats_output, live_text_output]
|
| 676 |
).then(
|
| 677 |
fn=inference_engine.stream_and_capture_for_gradio,
|
| 678 |
inputs=inputs_list,
|
|
|
|
| 681 |
fn=update_slowmo_button_visibility,
|
| 682 |
inputs=[generation_complete_state],
|
| 683 |
outputs=[slowmo_button]
|
|
|
|
|
|
|
|
|
|
| 684 |
)
|
| 685 |
|
| 686 |
slowmo_event = slowmo_button.click(
|