Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
|
| 2 |
#!/usr/bin/env python3
|
| 3 |
"""
|
| 4 |
PRACTICALITY SYSTEM 24.3 β POSITIVE SPACE SKETCHING & L2 SMOOTH MANIFOLDS
|
|
@@ -10,14 +9,14 @@ ARCHITECTURE:
|
|
| 10 |
β [WISDOM LAYER] β Masked Deduction (L2 MSE + Homotopy)
|
| 11 |
β [GEMINI COLLAPSER] β Domain-language explanation
|
| 12 |
|
| 13 |
-
CRITICAL UPDATE
|
| 14 |
-
-
|
| 15 |
-
(`
|
| 16 |
-
|
| 17 |
-
smoothly decay to zero, breaking the CE plateau bug.
|
| 18 |
"""
|
| 19 |
|
| 20 |
import os, time, random, math, threading, warnings, json, textwrap, itertools
|
|
|
|
| 21 |
from functools import reduce
|
| 22 |
from typing import Optional, Callable, List, Dict, Tuple, Any, Set
|
| 23 |
from dataclasses import dataclass, field
|
|
@@ -44,13 +43,13 @@ except ImportError:
|
|
| 44 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 45 |
# AI CONFIGURATION
|
| 46 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 47 |
-
DEFAULT_GEMINI_API_KEY = os.environ.get("
|
| 48 |
-
GEMINI_MODEL = "gemini-
|
| 49 |
|
| 50 |
warnings.filterwarnings("ignore")
|
| 51 |
USE_GPU = torch.cuda.is_available()
|
| 52 |
DEVICE = torch.device("cuda" if USE_GPU else "cpu")
|
| 53 |
-
print(f"[SYSTEM 24.3] Compute: {DEVICE.type.upper()} | Smooth L2 Manifold
|
| 54 |
|
| 55 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 56 |
# SECTION 1: CONSTANTS
|
|
@@ -456,8 +455,7 @@ class Problem:
|
|
| 456 |
args = [X[:, self.var_idx[v]] if is_batched else X[self.var_idx[v]] for v in mc.syms_used]
|
| 457 |
val = mc.torch_func(*args)
|
| 458 |
|
| 459 |
-
#
|
| 460 |
-
# This mathematically allows gradients to vanish at 0, preventing infinite chatter.
|
| 461 |
if mc.kind == "equality":
|
| 462 |
total += (val ** 2) * eff_weight
|
| 463 |
elif mc.direction == "geq":
|
|
@@ -470,7 +468,7 @@ class Problem:
|
|
| 470 |
col = X[:, i] if is_batched else X[i]
|
| 471 |
margin = (hi - lo) * 0.1 * (1.0 - step_ratio)
|
| 472 |
out_of_bounds = torch.relu(lo - margin - col) + torch.relu(col - (hi + margin))
|
| 473 |
-
total += (out_of_bounds ** 2) * 10.0
|
| 474 |
|
| 475 |
return total.squeeze()
|
| 476 |
|
|
@@ -745,7 +743,8 @@ def generate_templates(problem: Problem, sketch: StructuralSketch) -> List[Hypot
|
|
| 745 |
nb = dict(b_mid)
|
| 746 |
nb[v_small] = vs; nb[v_large] = vl
|
| 747 |
box = {u: IV(*tb.get(u, problem.bounds.get(u, (-10, 10)))) for u in problem.variables}
|
| 748 |
-
box[v_small] = IV(vs - 1e-6, vs + 1e-6)
|
|
|
|
| 749 |
cont = _hc4(box, problem.compiled_constraints)
|
| 750 |
|
| 751 |
pinned = {v_small: vs, v_large: vl}
|
|
@@ -1413,7 +1412,7 @@ class SequenceRayTracer:
|
|
| 1413 |
inv.compile(base_problem.variables)
|
| 1414 |
|
| 1415 |
_update_state(phase="SKETCHING")
|
| 1416 |
-
_add_log("πΊ Computing structural sketch
|
| 1417 |
sketch = compute_sketch(base_problem)
|
| 1418 |
_update_state(sketch_summary="; ".join(sketch.sketch_notes[:4]), sympy_derivation=sketch.sympy_derivation)
|
| 1419 |
_add_log(f"β Sketch: {sketch.dominant_type} system, DOF={sketch.n_free_dof}")
|
|
@@ -1702,7 +1701,6 @@ def global_solve_task(original_problem_text, api_key, prebuilt_axl=None):
|
|
| 1702 |
_update_state(phase="ERROR", answer=f"Encoder API Failed: {str(e)}")
|
| 1703 |
return
|
| 1704 |
|
| 1705 |
-
_add_log(f"β `{axl_def.name}` | vars: {len(axl_def.variables)}")
|
| 1706 |
_update_state(phase="COMPILING")
|
| 1707 |
|
| 1708 |
try:
|
|
@@ -1917,7 +1915,7 @@ with gr.Blocks(css=CSS, title="Practicality 24.3") as demo:
|
|
| 1917 |
f"New: L2 MSE Smooth Gradients\n"
|
| 1918 |
f"New: Positive Space Grid Sweeping\n"
|
| 1919 |
f"New: 11-Layer QAOA Stress Test\n"
|
| 1920 |
-
f"New:
|
| 1921 |
lines=7, interactive=False, elem_classes=["solver-log"])
|
| 1922 |
|
| 1923 |
with gr.Row(elem_classes=["wrap-row"]):
|
|
@@ -1926,9 +1924,9 @@ with gr.Blocks(css=CSS, title="Practicality 24.3") as demo:
|
|
| 1926 |
with gr.Column(scale=1, min_width=300):
|
| 1927 |
answer_box = gr.Textbox(label="Isomorphic Collapse", lines=20, interactive=False, elem_classes=["answer-box"])
|
| 1928 |
|
| 1929 |
-
def fetch_ui():
|
| 1930 |
while True:
|
| 1931 |
-
|
| 1932 |
yield build_live_html(), GLOBAL_SOLVE_STATE["answer"]
|
| 1933 |
|
| 1934 |
solve_btn.click(fn=trigger_solve, inputs=[problem_input, api_key_input], outputs=None)
|
|
@@ -1941,4 +1939,4 @@ with gr.Blocks(css=CSS, title="Practicality 24.3") as demo:
|
|
| 1941 |
|
| 1942 |
if __name__ == "__main__":
|
| 1943 |
print(f"[SYSTEM 24.3] Compute: {DEVICE.type.upper()} | Feature Flags: Quantum={HAS_QUANTUM}")
|
| 1944 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
|
|
|
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
PRACTICALITY SYSTEM 24.3 β POSITIVE SPACE SKETCHING & L2 SMOOTH MANIFOLDS
|
|
|
|
| 9 |
β [WISDOM LAYER] β Masked Deduction (L2 MSE + Homotopy)
|
| 10 |
β [GEMINI COLLAPSER] β Domain-language explanation
|
| 11 |
|
| 12 |
+
CRITICAL UPDATE:
|
| 13 |
+
- ASGI Thread Exhaustion Fix: Moved the UI polling generator to the async
|
| 14 |
+
event loop (`asyncio.sleep`) to prevent Gradio/FastAPI worker thread starvation
|
| 15 |
+
which causes Hugging Face Spaces to hang endlessly in "Starting".
|
|
|
|
| 16 |
"""
|
| 17 |
|
| 18 |
import os, time, random, math, threading, warnings, json, textwrap, itertools
|
| 19 |
+
import asyncio
|
| 20 |
from functools import reduce
|
| 21 |
from typing import Optional, Callable, List, Dict, Tuple, Any, Set
|
| 22 |
from dataclasses import dataclass, field
|
|
|
|
| 43 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 44 |
# AI CONFIGURATION
|
| 45 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 46 |
+
DEFAULT_GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY", "")
|
| 47 |
+
GEMINI_MODEL = "gemini-2.5-pro-preview-06-05"
|
| 48 |
|
| 49 |
warnings.filterwarnings("ignore")
|
| 50 |
USE_GPU = torch.cuda.is_available()
|
| 51 |
DEVICE = torch.device("cuda" if USE_GPU else "cpu")
|
| 52 |
+
print(f"[SYSTEM 24.3] Compute: {DEVICE.type.upper()} | Smooth L2 Manifold + Async UI")
|
| 53 |
|
| 54 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 55 |
# SECTION 1: CONSTANTS
|
|
|
|
| 455 |
args = [X[:, self.var_idx[v]] if is_batched else X[self.var_idx[v]] for v in mc.syms_used]
|
| 456 |
val = mc.torch_func(*args)
|
| 457 |
|
| 458 |
+
# Smooth L2 (MSE) Loss
|
|
|
|
| 459 |
if mc.kind == "equality":
|
| 460 |
total += (val ** 2) * eff_weight
|
| 461 |
elif mc.direction == "geq":
|
|
|
|
| 468 |
col = X[:, i] if is_batched else X[i]
|
| 469 |
margin = (hi - lo) * 0.1 * (1.0 - step_ratio)
|
| 470 |
out_of_bounds = torch.relu(lo - margin - col) + torch.relu(col - (hi + margin))
|
| 471 |
+
total += (out_of_bounds ** 2) * 10.0
|
| 472 |
|
| 473 |
return total.squeeze()
|
| 474 |
|
|
|
|
| 743 |
nb = dict(b_mid)
|
| 744 |
nb[v_small] = vs; nb[v_large] = vl
|
| 745 |
box = {u: IV(*tb.get(u, problem.bounds.get(u, (-10, 10)))) for u in problem.variables}
|
| 746 |
+
box[v_small] = IV(vs - 1e-6, vs + 1e-6)
|
| 747 |
+
box[v_large] = IV(vl - 1e-6, vl + 1e-6)
|
| 748 |
cont = _hc4(box, problem.compiled_constraints)
|
| 749 |
|
| 750 |
pinned = {v_small: vs, v_large: vl}
|
|
|
|
| 1412 |
inv.compile(base_problem.variables)
|
| 1413 |
|
| 1414 |
_update_state(phase="SKETCHING")
|
| 1415 |
+
_add_log(f"πΊ Computing structural sketch... | vars: {len(base_problem.variables)}")
|
| 1416 |
sketch = compute_sketch(base_problem)
|
| 1417 |
_update_state(sketch_summary="; ".join(sketch.sketch_notes[:4]), sympy_derivation=sketch.sympy_derivation)
|
| 1418 |
_add_log(f"β Sketch: {sketch.dominant_type} system, DOF={sketch.n_free_dof}")
|
|
|
|
| 1701 |
_update_state(phase="ERROR", answer=f"Encoder API Failed: {str(e)}")
|
| 1702 |
return
|
| 1703 |
|
|
|
|
| 1704 |
_update_state(phase="COMPILING")
|
| 1705 |
|
| 1706 |
try:
|
|
|
|
| 1915 |
f"New: L2 MSE Smooth Gradients\n"
|
| 1916 |
f"New: Positive Space Grid Sweeping\n"
|
| 1917 |
f"New: 11-Layer QAOA Stress Test\n"
|
| 1918 |
+
f"New: Thread Crash Safety (Async UI)"),
|
| 1919 |
lines=7, interactive=False, elem_classes=["solver-log"])
|
| 1920 |
|
| 1921 |
with gr.Row(elem_classes=["wrap-row"]):
|
|
|
|
| 1924 |
with gr.Column(scale=1, min_width=300):
|
| 1925 |
answer_box = gr.Textbox(label="Isomorphic Collapse", lines=20, interactive=False, elem_classes=["answer-box"])
|
| 1926 |
|
| 1927 |
+
async def fetch_ui():
|
| 1928 |
while True:
|
| 1929 |
+
await asyncio.sleep(1.0)
|
| 1930 |
yield build_live_html(), GLOBAL_SOLVE_STATE["answer"]
|
| 1931 |
|
| 1932 |
solve_btn.click(fn=trigger_solve, inputs=[problem_input, api_key_input], outputs=None)
|
|
|
|
| 1939 |
|
| 1940 |
if __name__ == "__main__":
|
| 1941 |
print(f"[SYSTEM 24.3] Compute: {DEVICE.type.upper()} | Feature Flags: Quantum={HAS_QUANTUM}")
|
| 1942 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
|