import gradio as gr import numpy as np from sentence_transformers import SentenceTransformer import json import requests from bs4 import BeautifulSoup from urllib.parse import urljoin, urlparse # PHDM Configuration PHDM_DIM = 21 # 6D hyperbolic + 6D phase + 3D flux + 6D audit NEUROTRANSMITTER_WEIGHTS = { "KO": 1.0, "AV": 1.62, "RU": 2.62, "CA": 4.24, "UM": 6.85, "DR": 11.09, } # Load base model model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2") def exponential_map(v, c=1.0): """Map Euclidean vector to Poincare Ball via exponential map.""" norm = np.linalg.norm(v) if norm < 1e-10: return v coeff = np.tanh(np.sqrt(c) * norm) / (np.sqrt(c) * norm) return coeff * v def project_to_phdm(embedding): """Project 384D embedding into 21D PHDM Poincare Ball space.""" np.random.seed(42) proj_matrix = np.random.randn(384, PHDM_DIM) * 0.1 projected = embedding @ proj_matrix hyperbolic = projected[:6] phase = projected[6:12] flux = projected[12:15] audit = projected[15:21] hyperbolic = exponential_map(hyperbolic, c=1.0) phase = exponential_map(phase, c=0.5) flux = exponential_map(flux, c=0.25) audit = exponential_map(audit, c=1.0) phdm_embedding = np.concatenate([hyperbolic, phase, flux, audit]) return phdm_embedding def get_embedding(text): """Generate PHDM 21D embedding for input text.""" base_emb = model.encode(text) phdm_emb = project_to_phdm(base_emb) result = { "text": text, "phdm_embedding": phdm_emb.tolist(), "components": { "hyperbolic_6d": phdm_emb[:6].tolist(), "phase_6d": phdm_emb[6:12].tolist(), "flux_3d": phdm_emb[12:15].tolist(), "audit_6d": phdm_emb[15:21].tolist(), }, "poincare_norm": float(np.linalg.norm(phdm_emb)), "dimension": PHDM_DIM, } return json.dumps(result, indent=2) def compare_texts(text1, text2): """Compare two texts using PHDM embeddings.""" emb1 = model.encode(text1) emb2 = model.encode(text2) phdm1 = project_to_phdm(emb1) phdm2 = project_to_phdm(emb2) cos_sim = np.dot(phdm1, phdm2) / (np.linalg.norm(phdm1) * np.linalg.norm(phdm2) + 1e-10) diff = phdm1 - phdm2 hyp_dist = np.arccosh(1 + 2 * np.linalg.norm(diff)**2 / ((1 - np.linalg.norm(phdm1)**2) * (1 - np.linalg.norm(phdm2)**2) + 1e-10) + 1e-10) return json.dumps({ "cosine_similarity": float(cos_sim), "hyperbolic_distance": float(hyp_dist), "text1_norm": float(np.linalg.norm(phdm1)), "text2_norm": float(np.linalg.norm(phdm2)), }, indent=2) # ===== WEB CRAWLING TEST FUNCTIONS ===== def crawl_url(url, max_links=10): """Crawl a URL and extract text content and links.""" try: headers = { 'User-Agent': 'SCBE-AETHERMOORE-Crawler/1.0 (AI Governance Test Bot)' } response = requests.get(url, headers=headers, timeout=15) response.raise_for_status() soup = BeautifulSoup(response.text, 'html.parser') # Remove script and style elements for script in soup(["script", "style", "nav", "footer", "header"]): script.decompose() # Extract text text = soup.get_text(separator=' ', strip=True) text = ' '.join(text.split())[:2000] # Limit to 2000 chars # Extract links links = [] for link in soup.find_all('a', href=True)[:max_links]: href = link['href'] full_url = urljoin(url, href) if full_url.startswith('http'): links.append({ 'url': full_url, 'text': link.get_text(strip=True)[:100] }) # Get title title = soup.title.string if soup.title else 'No title' result = { 'status': 'success', 'url': url, 'title': title, 'text_length': len(text), 'text_preview': text[:500], 'links_found': len(links), 'links': links[:max_links] } return json.dumps(result, indent=2) except Exception as e: return json.dumps({ 'status': 'error', 'url': url, 'error': str(e) }, indent=2) def crawl_and_embed(url): """Crawl a URL, extract text, and generate PHDM embedding.""" try: headers = { 'User-Agent': 'SCBE-AETHERMOORE-Crawler/1.0 (AI Governance Test Bot)' } response = requests.get(url, headers=headers, timeout=15) response.raise_for_status() soup = BeautifulSoup(response.text, 'html.parser') for script in soup(["script", "style", "nav", "footer", "header"]): script.decompose() text = soup.get_text(separator=' ', strip=True) text = ' '.join(text.split())[:1000] # Limit for embedding title = soup.title.string if soup.title else 'No title' # Generate PHDM embedding base_emb = model.encode(text) phdm_emb = project_to_phdm(base_emb) result = { 'status': 'success', 'url': url, 'title': title, 'text_used': text[:300] + '...' if len(text) > 300 else text, 'phdm_embedding': phdm_emb.tolist(), 'poincare_norm': float(np.linalg.norm(phdm_emb)), 'dimension': PHDM_DIM, 'components': { 'hyperbolic_6d': phdm_emb[:6].tolist(), 'phase_6d': phdm_emb[6:12].tolist(), 'flux_3d': phdm_emb[12:15].tolist(), 'audit_6d': phdm_emb[15:21].tolist(), } } return json.dumps(result, indent=2) except Exception as e: return json.dumps({ 'status': 'error', 'url': url, 'error': str(e) }, indent=2) # Build Gradio interface with gr.Blocks(title="PHDM 21D Embedding Model", theme=gr.themes.Soft()) as demo: gr.Markdown("""# PHDM 21D Embedding Model Custom embedding model for the **SCBE-AETHERMOORE** framework. Maps text into a 21-dimensional Poincare Ball manifold for hyperbolic AI safety governance. **Architecture**: 21D (6D hyperbolic + 6D phase + 3D flux + 6D audit) **Geometry**: Poincare Ball B^n with Harmonic Wall containment """) with gr.Tab("Embed Text"): text_input = gr.Textbox(label="Input Text", placeholder="Enter text to embed...", lines=3) embed_btn = gr.Button("Generate PHDM Embedding", variant="primary") embed_output = gr.Code(label="PHDM 21D Embedding", language="json") embed_btn.click(get_embedding, inputs=text_input, outputs=embed_output) with gr.Tab("Compare Texts"): text1 = gr.Textbox(label="Text 1", placeholder="Enter first text...", lines=2) text2 = gr.Textbox(label="Text 2", placeholder="Enter second text...", lines=2) compare_btn = gr.Button("Compare in PHDM Space", variant="primary") compare_output = gr.Code(label="Similarity Results", language="json") compare_btn.click(compare_texts, inputs=[text1, text2], outputs=compare_output) with gr.Tab("Web Crawl Test"): gr.Markdown("""### AI Web Crawling Test Test basic web crawling capabilities. Enter a URL to fetch and analyze. """) crawl_url_input = gr.Textbox(label="URL to Crawl", placeholder="https://example.com", lines=1) with gr.Row(): crawl_btn = gr.Button("Crawl URL", variant="primary") embed_crawl_btn = gr.Button("Crawl & Embed", variant="secondary") crawl_output = gr.Code(label="Crawl Results", language="json") crawl_btn.click(crawl_url, inputs=crawl_url_input, outputs=crawl_output) embed_crawl_btn.click(crawl_and_embed, inputs=crawl_url_input, outputs=crawl_output) gr.Markdown("""--- **Model**: `issdandavis/phdm-21d-embedding` **License**: Apache 2.0 **Base**: all-MiniLM-L6-v2 """) demo.launch()