Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
"""
|
| 2 |
================================================================================
|
| 3 |
-
SCRIPT 2: THE HYSE SERVER (HUGGINGFACE SPACE NATIVE) - V9.
|
| 4 |
-
|
| 5 |
Requires: pip install llama-cpp-python sentence-transformers fastapi uvicorn requests
|
| 6 |
================================================================================
|
| 7 |
"""
|
|
@@ -141,7 +141,7 @@ class DualModelManager:
|
|
| 141 |
gc.collect()
|
| 142 |
|
| 143 |
def run_7b_extract(self, text: str) -> str:
|
| 144 |
-
""" V9.
|
| 145 |
prompt = (
|
| 146 |
f"<|im_start|>system\nYou translate specific problems into pure abstract geometry and systems dynamics. "
|
| 147 |
f"CRITICAL: Strip ALL domain nouns (e.g., biology, finance). Output exactly 1 sentence.<|im_end|>\n"
|
|
@@ -154,7 +154,7 @@ class DualModelManager:
|
|
| 154 |
except: return "Extraction failed."
|
| 155 |
|
| 156 |
def run_7b_blind_brainstorm(self, abstract_mechanism: str, axioms_dict: Dict[str, str], mode: str) -> List[str]:
|
| 157 |
-
""" V9.
|
| 158 |
axiom_str = "\n".join([f"- {k}: {v}" for k, v in axioms_dict.items()])
|
| 159 |
|
| 160 |
prompts = {
|
|
@@ -175,7 +175,6 @@ class DualModelManager:
|
|
| 175 |
output = self.active_llm(prompt, max_tokens=150, temperature=0.6, stop=["<|im_end|>"])
|
| 176 |
text = output['choices'][0]['text'].strip()
|
| 177 |
|
| 178 |
-
# Simple, bulletproof parsing
|
| 179 |
topics = [t.strip().replace('"', '').replace("'", "") for t in text.split(',')]
|
| 180 |
topics = [t for t in topics if len(t) > 3 and not "Here are" in t]
|
| 181 |
|
|
@@ -192,7 +191,7 @@ class DualModelManager:
|
|
| 192 |
return fallbacks.get(mode, fallbacks["mirrors"])
|
| 193 |
|
| 194 |
def run_3b_extract(self, text: str) -> str:
|
| 195 |
-
""" V9.
|
| 196 |
prompt = (
|
| 197 |
f"<|im_start|>system\nYou translate textbook descriptions into abstract structural mechanisms. "
|
| 198 |
f"CRITICAL: IGNORE ALL HUMAN NAMES (e.g., Ilya Prigogine, Lev Landau), dates, and historical filler. Focus ONLY on the math/physics. Output exactly 1 sentence.<|im_end|>\n"
|
|
@@ -219,7 +218,7 @@ def fetch_wiki_articles(search_queries: List[str]) -> List[Dict]:
|
|
| 219 |
chunks = []
|
| 220 |
session = requests.Session()
|
| 221 |
session.headers.update({
|
| 222 |
-
'User-Agent': 'IsomorphicEngineBot/9.
|
| 223 |
})
|
| 224 |
|
| 225 |
for q in set(search_queries):
|
|
@@ -279,11 +278,20 @@ def fetch_wiki_articles(search_queries: List[str]) -> List[Dict]:
|
|
| 279 |
class Substrate:
|
| 280 |
doc_id: str; title: str; text: str; radii: Dict[str, float]; embedding: np.ndarray
|
| 281 |
def r(self, a: str) -> float: return self.radii.get(a, 0.0)
|
| 282 |
-
def dominant(self, t: float = 0.
|
| 283 |
|
| 284 |
def calculate_gaps(q: Substrate, c: Substrate) -> Dict:
|
| 285 |
g1 = [{"axiom": a, "direction": "gain" if c.r(a)-q.r(a) > 0 else "release", "delta": round(c.r(a)-q.r(a), 3)} for a in q.radii if abs(c.r(a)-q.r(a)) >= 0.15]
|
| 286 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
|
| 288 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 289 |
# THE HySE PIPELINE
|
|
@@ -300,8 +308,13 @@ def run_hyse_pipeline(query: str):
|
|
| 300 |
q_rad, q_emb = MANAGER.score_mechanism(q_mech)
|
| 301 |
q_sub = Substrate("QUERY", "User Query", q_mech, q_rad, q_emb)
|
| 302 |
|
| 303 |
-
|
| 304 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
blindfold_dict = {name: SEED_AXIOMS[name] for name in top_axiom_names}
|
| 306 |
|
| 307 |
set_status("7B Generating Wikipedia Targets (Named Laws/Models)...")
|
|
@@ -326,21 +339,33 @@ def run_hyse_pipeline(query: str):
|
|
| 326 |
for chunk in chunks:
|
| 327 |
mech = MANAGER.run_3b_extract(chunk["text"])
|
| 328 |
if mech == "Extraction failed.": continue
|
| 329 |
-
|
| 330 |
rad, emb = MANAGER.score_mechanism(mech)
|
| 331 |
sub = Substrate(chunk["doc_id"], chunk["title"], mech, rad, emb)
|
| 332 |
|
|
|
|
| 333 |
vec_score = max(0.0, float(np.dot(q_sub.embedding, sub.embedding)))
|
|
|
|
| 334 |
|
| 335 |
-
# V9.
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
else:
|
| 339 |
-
gate = sum(1 for a in top_axiom_names if sub.r(a) >= 0.35) / max(len(top_axiom_names), 1)
|
| 340 |
score = vec_score * 0.6 + gate * 0.4
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 344 |
results.sort(key=lambda x: x["score"], reverse=True)
|
| 345 |
|
| 346 |
unique_results = []
|
|
@@ -379,7 +404,7 @@ MANAGER = DualModelManager()
|
|
| 379 |
|
| 380 |
@asynccontextmanager
|
| 381 |
async def lifespan(app: FastAPI):
|
| 382 |
-
log("Booting HySE Engine V9.
|
| 383 |
MANAGER.load_st()
|
| 384 |
|
| 385 |
def pre_fetch():
|
|
@@ -397,7 +422,7 @@ async def lifespan(app: FastAPI):
|
|
| 397 |
app = FastAPI(title="HySE Reverse Engineering Engine", lifespan=lifespan)
|
| 398 |
|
| 399 |
HTML = r"""<!DOCTYPE html>
|
| 400 |
-
<html><head><title>HySE V9.
|
| 401 |
<style>
|
| 402 |
body{background:#070707;color:#e0e0e0;font-family:monospace;display:flex;height:100vh;margin:0;}
|
| 403 |
.side{width:320px;background:#0b0b0b;padding:15px;border-right:1px solid #1a1a1a;overflow-y:auto;}
|
|
@@ -409,8 +434,8 @@ button{width:100%;background:#0a2014;color:#7ee37e;border:1px solid #184028;padd
|
|
| 409 |
.log{font-size:10px;color:#606060;height:150px;overflow-y:auto;border:1px solid #1a1a1a;padding:5px;}
|
| 410 |
</style></head><body>
|
| 411 |
<div class="side">
|
| 412 |
-
<h2 style="color:#7ec8e3;">HySE V9.
|
| 413 |
-
<div style="font-size:10px;color:#606060;margin-bottom:15px;">
|
| 414 |
<textarea id="q" rows="5">A virus infects a host, rapidly replicating and jumping to new hosts until immunity acts as a wall causing collapse.</textarea>
|
| 415 |
<button onclick="run()">βΆ Start Reverse Engineering</button>
|
| 416 |
<div id="prog" style="margin:15px 0;color:#7ee37e;">Idle</div>
|
|
@@ -463,8 +488,10 @@ function render(data){
|
|
| 463 |
<div style="display:flex;justify-content:space-between;font-weight:bold;"><span>π ${r.title}</span><span style="color:#e3c07e;">Score: ${(r.score*100).toFixed(1)}%</span></div>
|
| 464 |
<div style="color:#7ee3c8;margin:8px 0;font-style:italic;">${r.mechanism}</div>
|
| 465 |
<div style="font-size:10px;color:#606060;">
|
| 466 |
-
<
|
| 467 |
-
<
|
|
|
|
|
|
|
| 468 |
</div>
|
| 469 |
</div>`;
|
| 470 |
}
|
|
|
|
| 1 |
"""
|
| 2 |
================================================================================
|
| 3 |
+
SCRIPT 2: THE HYSE SERVER (HUGGINGFACE SPACE NATIVE) - V9.8 TRUE ONTOLOGY
|
| 4 |
+
Dynamic Axiom Gates | SR4 Complement Scoring | 1-Shot Semantic Scrubbing
|
| 5 |
Requires: pip install llama-cpp-python sentence-transformers fastapi uvicorn requests
|
| 6 |
================================================================================
|
| 7 |
"""
|
|
|
|
| 141 |
gc.collect()
|
| 142 |
|
| 143 |
def run_7b_extract(self, text: str) -> str:
|
| 144 |
+
""" V9.8: 1-Shot De-Semanticization explicitly showing how to strip nouns """
|
| 145 |
prompt = (
|
| 146 |
f"<|im_start|>system\nYou translate specific problems into pure abstract geometry and systems dynamics. "
|
| 147 |
f"CRITICAL: Strip ALL domain nouns (e.g., biology, finance). Output exactly 1 sentence.<|im_end|>\n"
|
|
|
|
| 154 |
except: return "Extraction failed."
|
| 155 |
|
| 156 |
def run_7b_blind_brainstorm(self, abstract_mechanism: str, axioms_dict: Dict[str, str], mode: str) -> List[str]:
|
| 157 |
+
""" V9.8: 1-Shot Comma-Separated Output to guarantee robust Wikipedia targeting """
|
| 158 |
axiom_str = "\n".join([f"- {k}: {v}" for k, v in axioms_dict.items()])
|
| 159 |
|
| 160 |
prompts = {
|
|
|
|
| 175 |
output = self.active_llm(prompt, max_tokens=150, temperature=0.6, stop=["<|im_end|>"])
|
| 176 |
text = output['choices'][0]['text'].strip()
|
| 177 |
|
|
|
|
| 178 |
topics = [t.strip().replace('"', '').replace("'", "") for t in text.split(',')]
|
| 179 |
topics = [t for t in topics if len(t) > 3 and not "Here are" in t]
|
| 180 |
|
|
|
|
| 191 |
return fallbacks.get(mode, fallbacks["mirrors"])
|
| 192 |
|
| 193 |
def run_3b_extract(self, text: str) -> str:
|
| 194 |
+
""" V9.8: 1-Shot Anti-History and Name Stripping """
|
| 195 |
prompt = (
|
| 196 |
f"<|im_start|>system\nYou translate textbook descriptions into abstract structural mechanisms. "
|
| 197 |
f"CRITICAL: IGNORE ALL HUMAN NAMES (e.g., Ilya Prigogine, Lev Landau), dates, and historical filler. Focus ONLY on the math/physics. Output exactly 1 sentence.<|im_end|>\n"
|
|
|
|
| 218 |
chunks = []
|
| 219 |
session = requests.Session()
|
| 220 |
session.headers.update({
|
| 221 |
+
'User-Agent': 'IsomorphicEngineBot/9.8 (https://huggingface.co/spaces/everydaytok/Small_llm; contact@example.com)'
|
| 222 |
})
|
| 223 |
|
| 224 |
for q in set(search_queries):
|
|
|
|
| 278 |
class Substrate:
|
| 279 |
doc_id: str; title: str; text: str; radii: Dict[str, float]; embedding: np.ndarray
|
| 280 |
def r(self, a: str) -> float: return self.radii.get(a, 0.0)
|
| 281 |
+
def dominant(self, t: float = 0.55) -> List[str]: return [a for a in self.radii if self.r(a) >= t]
|
| 282 |
|
| 283 |
def calculate_gaps(q: Substrate, c: Substrate) -> Dict:
|
| 284 |
g1 = [{"axiom": a, "direction": "gain" if c.r(a)-q.r(a) > 0 else "release", "delta": round(c.r(a)-q.r(a), 3)} for a in q.radii if abs(c.r(a)-q.r(a)) >= 0.15]
|
| 285 |
+
|
| 286 |
+
q_dom = set(q.dominant(0.55)) or set(sorted(q.radii, key=q.radii.get, reverse=True)[:4])
|
| 287 |
+
c_dom = set(c.dominant(0.55)) or set(sorted(c.radii, key=c.radii.get, reverse=True)[:4])
|
| 288 |
+
|
| 289 |
+
return {
|
| 290 |
+
"G1_gains": [f"{s['axiom']} (+{s['delta']})" for s in g1 if s["direction"] == "gain"][:3],
|
| 291 |
+
"G1_releases": [f"{s['axiom']} ({s['delta']})" for s in g1 if s["direction"] == "release"][:3],
|
| 292 |
+
"G4_shared": sorted(list(q_dom & c_dom)),
|
| 293 |
+
"G4_borrow": sorted(list(c_dom - q_dom))
|
| 294 |
+
}
|
| 295 |
|
| 296 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 297 |
# THE HySE PIPELINE
|
|
|
|
| 308 |
q_rad, q_emb = MANAGER.score_mechanism(q_mech)
|
| 309 |
q_sub = Substrate("QUERY", "User Query", q_mech, q_rad, q_emb)
|
| 310 |
|
| 311 |
+
# V9.8 FIX 1: Robust Dominant Axis Extraction
|
| 312 |
+
top_axiom_names = [a for a in q_rad if q_rad[a] >= 0.55]
|
| 313 |
+
if len(top_axiom_names) < 4:
|
| 314 |
+
top_axiom_names = sorted(q_rad, key=q_rad.get, reverse=True)[:5]
|
| 315 |
+
else:
|
| 316 |
+
top_axiom_names = top_axiom_names[:6]
|
| 317 |
+
|
| 318 |
blindfold_dict = {name: SEED_AXIOMS[name] for name in top_axiom_names}
|
| 319 |
|
| 320 |
set_status("7B Generating Wikipedia Targets (Named Laws/Models)...")
|
|
|
|
| 339 |
for chunk in chunks:
|
| 340 |
mech = MANAGER.run_3b_extract(chunk["text"])
|
| 341 |
if mech == "Extraction failed.": continue
|
|
|
|
| 342 |
rad, emb = MANAGER.score_mechanism(mech)
|
| 343 |
sub = Substrate(chunk["doc_id"], chunk["title"], mech, rad, emb)
|
| 344 |
|
| 345 |
+
gaps = calculate_gaps(q_sub, sub)
|
| 346 |
vec_score = max(0.0, float(np.dot(q_sub.embedding, sub.embedding)))
|
| 347 |
+
gate = sum(1 for a in top_axiom_names if sub.r(a) >= 0.45) / max(len(top_axiom_names), 1)
|
| 348 |
|
| 349 |
+
# V9.8 FIX 2 & 3: Dynamic Scoring and Gates
|
| 350 |
+
score = 0.0
|
| 351 |
+
if mode == "mirrors":
|
|
|
|
|
|
|
| 352 |
score = vec_score * 0.6 + gate * 0.4
|
| 353 |
+
elif mode == "more_completes":
|
| 354 |
+
if len(gaps["G4_borrow"]) == 0:
|
| 355 |
+
score = 0.0 # Must add something new
|
| 356 |
+
else:
|
| 357 |
+
score = vec_score * 0.5 + gate * 0.5
|
| 358 |
+
elif mode == "opposites":
|
| 359 |
+
if len(gaps["G4_shared"]) == 0:
|
| 360 |
+
score = 0.0 # Must share a foundation to be an opposite
|
| 361 |
+
else:
|
| 362 |
+
# SR4 Complement Logic
|
| 363 |
+
complement = sum((1.0 - q_sub.r(a)) * sub.r(a) for a in AXIOM_NAMES) / N_AXIOMS
|
| 364 |
+
score = complement * 0.7 + (1.0 - vec_score) * 0.3
|
| 365 |
+
|
| 366 |
+
if score > 0.05:
|
| 367 |
+
results.append({"title": sub.title, "score": round(score, 3), "mechanism": mech, **gaps})
|
| 368 |
+
|
| 369 |
results.sort(key=lambda x: x["score"], reverse=True)
|
| 370 |
|
| 371 |
unique_results = []
|
|
|
|
| 404 |
|
| 405 |
@asynccontextmanager
|
| 406 |
async def lifespan(app: FastAPI):
|
| 407 |
+
log("Booting HySE Engine V9.8 (True Ontology)...")
|
| 408 |
MANAGER.load_st()
|
| 409 |
|
| 410 |
def pre_fetch():
|
|
|
|
| 422 |
app = FastAPI(title="HySE Reverse Engineering Engine", lifespan=lifespan)
|
| 423 |
|
| 424 |
HTML = r"""<!DOCTYPE html>
|
| 425 |
+
<html><head><title>HySE V9.8 - True Ontology</title>
|
| 426 |
<style>
|
| 427 |
body{background:#070707;color:#e0e0e0;font-family:monospace;display:flex;height:100vh;margin:0;}
|
| 428 |
.side{width:320px;background:#0b0b0b;padding:15px;border-right:1px solid #1a1a1a;overflow-y:auto;}
|
|
|
|
| 434 |
.log{font-size:10px;color:#606060;height:150px;overflow-y:auto;border:1px solid #1a1a1a;padding:5px;}
|
| 435 |
</style></head><body>
|
| 436 |
<div class="side">
|
| 437 |
+
<h2 style="color:#7ec8e3;">HySE V9.8</h2>
|
| 438 |
+
<div style="font-size:10px;color:#606060;margin-bottom:15px;">Dynamic Axiom Gates | SR4 Complement Scores</div>
|
| 439 |
<textarea id="q" rows="5">A virus infects a host, rapidly replicating and jumping to new hosts until immunity acts as a wall causing collapse.</textarea>
|
| 440 |
<button onclick="run()">βΆ Start Reverse Engineering</button>
|
| 441 |
<div id="prog" style="margin:15px 0;color:#7ee37e;">Idle</div>
|
|
|
|
| 488 |
<div style="display:flex;justify-content:space-between;font-weight:bold;"><span>π ${r.title}</span><span style="color:#e3c07e;">Score: ${(r.score*100).toFixed(1)}%</span></div>
|
| 489 |
<div style="color:#7ee3c8;margin:8px 0;font-style:italic;">${r.mechanism}</div>
|
| 490 |
<div style="font-size:10px;color:#606060;">
|
| 491 |
+
<div style="color:#7ec8e3; margin-bottom:2px;">Shared: ${r.G4_shared.join(', ') || 'None'}</div>
|
| 492 |
+
<div style="color:#e3c07e; margin-bottom:4px;">Borrow: ${r.G4_borrow.join(', ') || 'None'}</div>
|
| 493 |
+
<span style="color:#7ee37e">G1 Gains: ${r.G1_gains.join(', ') || 'None'}</span> |
|
| 494 |
+
<span style="color:#e37e7e">G1 Releases: ${r.G1_releases.join(', ') || 'None'}</span>
|
| 495 |
</div>
|
| 496 |
</div>`;
|
| 497 |
}
|