Spaces:
Running on Zero
Running on Zero
Remove placeholder text box and add graceful error handling
Browse files
app.py
CHANGED
|
@@ -47,7 +47,7 @@ default_schema = PRESETS[default_title]["schema"] if default_title else "{}"
|
|
| 47 |
def run_comparison(context_str: str, schema_json_str: str):
|
| 48 |
if not context_str or not context_str.strip():
|
| 49 |
yield (
|
| 50 |
-
"<div style='color: #dc2626; font-weight: 600;'>Please provide a context prompt.</div>",
|
| 51 |
"{}",
|
| 52 |
"0.0 ms",
|
| 53 |
"{}",
|
|
@@ -60,7 +60,7 @@ def run_comparison(context_str: str, schema_json_str: str):
|
|
| 60 |
schema = StructuredSchema(schema_dict)
|
| 61 |
except Exception as e:
|
| 62 |
yield (
|
| 63 |
-
f"<div style='color: #dc2626; font-weight: 600;'>Invalid Schema JSON: {e}</div>",
|
| 64 |
"{}",
|
| 65 |
"0.0 ms",
|
| 66 |
"{}",
|
|
@@ -68,51 +68,62 @@ def run_comparison(context_str: str, schema_json_str: str):
|
|
| 68 |
)
|
| 69 |
return
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
<
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
|
| 118 |
with gr.Blocks(title="Parallel Constrained Decision Engine") as demo:
|
|
@@ -128,11 +139,7 @@ with gr.Blocks(title="Parallel Constrained Decision Engine") as demo:
|
|
| 128 |
)
|
| 129 |
btn_run = gr.Button("⚡ Run Comparison", variant="primary", scale=1)
|
| 130 |
|
| 131 |
-
summary_banner = gr.HTML(value=""
|
| 132 |
-
<div style="background: #f8fafc; border: 1px solid #e2e8f0; border-radius: 9999px; padding: 6px 16px; display: inline-flex; align-items: center; font-family: monospace; font-size: 14px; color: #64748b;">
|
| 133 |
-
Select a preset and click "Run Comparison" to start
|
| 134 |
-
</div>
|
| 135 |
-
""")
|
| 136 |
|
| 137 |
with gr.Row():
|
| 138 |
with gr.Column(scale=1):
|
|
|
|
| 47 |
def run_comparison(context_str: str, schema_json_str: str):
|
| 48 |
if not context_str or not context_str.strip():
|
| 49 |
yield (
|
| 50 |
+
"<div style='color: #dc2626; font-weight: 600; padding: 6px 12px;'>Please provide a context prompt.</div>",
|
| 51 |
"{}",
|
| 52 |
"0.0 ms",
|
| 53 |
"{}",
|
|
|
|
| 60 |
schema = StructuredSchema(schema_dict)
|
| 61 |
except Exception as e:
|
| 62 |
yield (
|
| 63 |
+
f"<div style='color: #dc2626; font-weight: 600; padding: 6px 12px;'>Invalid Schema JSON: {e}</div>",
|
| 64 |
"{}",
|
| 65 |
"0.0 ms",
|
| 66 |
"{}",
|
|
|
|
| 68 |
)
|
| 69 |
return
|
| 70 |
|
| 71 |
+
try:
|
| 72 |
+
# 1. Run Parallel Constrained Decoding first
|
| 73 |
+
parallel_res = run_parallel_generation(context_str, schema)
|
| 74 |
+
parallel_ms = parallel_res["elapsed_ms"]
|
| 75 |
+
parallel_json_str = json.dumps(parallel_res["parsed_json"], indent=2)
|
| 76 |
+
parallel_time_badge = f"{parallel_ms:.1f} ms"
|
| 77 |
+
|
| 78 |
+
summary_intermediate = f"""
|
| 79 |
+
<div style="background: #f0fdf4; border: 1px solid #bbf7d0; border-radius: 9999px; padding: 6px 16px; display: inline-flex; align-items: center; gap: 8px; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; font-size: 14px;">
|
| 80 |
+
<span style="color: #16a34a; font-weight: 700;">Parallel Done: {parallel_time_badge}</span>
|
| 81 |
+
<span style="color: #94a3b8;">·</span>
|
| 82 |
+
<span style="color: #64748b;">Evaluating normal autoregressive baseline...</span>
|
| 83 |
+
</div>
|
| 84 |
+
"""
|
| 85 |
+
|
| 86 |
+
yield (
|
| 87 |
+
summary_intermediate,
|
| 88 |
+
parallel_json_str,
|
| 89 |
+
parallel_time_badge,
|
| 90 |
+
"// Running sequential autoregressive baseline forward passes...",
|
| 91 |
+
"Evaluating..."
|
| 92 |
+
)
|
| 93 |
|
| 94 |
+
# 2. Run Naive generation baseline
|
| 95 |
+
naive_res = run_naive_generation(context_str, schema)
|
| 96 |
+
naive_ms = naive_res["elapsed_ms"]
|
| 97 |
+
naive_json_str = json.dumps(naive_res["parsed_json"], indent=2) if naive_res.get("parsed_json") else naive_res.get("raw_text", "")
|
| 98 |
+
naive_time_badge = f"{naive_ms:.1f} ms"
|
| 99 |
+
|
| 100 |
+
speedup = round(naive_ms / max(parallel_ms, 1.0), 1)
|
| 101 |
+
|
| 102 |
+
final_summary_html = f"""
|
| 103 |
+
<div style="background: #f0fdf4; border: 1px solid #bbf7d0; border-radius: 9999px; padding: 8px 20px; display: inline-flex; align-items: center; gap: 10px; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; font-size: 15px; box-shadow: 0 1px 3px rgba(0,0,0,0.05);">
|
| 104 |
+
<span style="color: #16a34a; font-weight: 800; font-size: 16px; letter-spacing: 0.5px;">{speedup}x FASTER</span>
|
| 105 |
+
<span style="color: #cbd5e1; font-weight: 600;">·</span>
|
| 106 |
+
<span style="color: #334155; font-weight: 600; font-family: monospace;">{parallel_time_badge} vs {naive_time_badge}</span>
|
| 107 |
+
</div>
|
| 108 |
+
"""
|
| 109 |
+
|
| 110 |
+
yield (
|
| 111 |
+
final_summary_html,
|
| 112 |
+
parallel_json_str,
|
| 113 |
+
parallel_time_badge,
|
| 114 |
+
naive_json_str,
|
| 115 |
+
naive_time_badge
|
| 116 |
+
)
|
| 117 |
+
except Exception as err:
|
| 118 |
+
import traceback
|
| 119 |
+
err_msg = f"{err}\n{traceback.format_exc()}"
|
| 120 |
+
yield (
|
| 121 |
+
f"<div style='color: #dc2626; background: #fef2f2; border: 1px solid #fecaca; border-radius: 8px; padding: 10px 14px; font-family: monospace; font-size: 13px;'>Error: {err}</div>",
|
| 122 |
+
"{}",
|
| 123 |
+
"0.0 ms",
|
| 124 |
+
f"Error details:\n{err_msg}",
|
| 125 |
+
"0.0 ms"
|
| 126 |
+
)
|
| 127 |
|
| 128 |
|
| 129 |
with gr.Blocks(title="Parallel Constrained Decision Engine") as demo:
|
|
|
|
| 139 |
)
|
| 140 |
btn_run = gr.Button("⚡ Run Comparison", variant="primary", scale=1)
|
| 141 |
|
| 142 |
+
summary_banner = gr.HTML(value="")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
with gr.Row():
|
| 145 |
with gr.Column(scale=1):
|