Spaces:
Running
Running
Commit ·
9b811da
0
Parent(s):
φ-Coherence API - Universal quality metric
Browse files- README.md +47 -0
- app.py +463 -0
- requirements.txt +1 -0
README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: φ-Coherence API
|
| 3 |
+
emoji: φ
|
| 4 |
+
colorFrom: purple
|
| 5 |
+
colorTo: blue
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 4.44.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: true
|
| 10 |
+
license: mit
|
| 11 |
+
short_description: Universal quality metric for AI outputs using golden ratio mathematics
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
# φ-Coherence API
|
| 15 |
+
|
| 16 |
+
**Universal quality metric for AI outputs using golden ratio mathematics.**
|
| 17 |
+
|
| 18 |
+
## What is φ-Coherence?
|
| 19 |
+
|
| 20 |
+
φ-Coherence measures the "structural integrity" of text using mathematical constants:
|
| 21 |
+
- **φ (Golden Ratio)** = 1.618... - Natural proportion found in coherent structures
|
| 22 |
+
- **α (Fine Structure)** = 137 - Fundamental constant governing information patterns
|
| 23 |
+
|
| 24 |
+
## Use Cases
|
| 25 |
+
|
| 26 |
+
- Filter LLM hallucinations before they reach users
|
| 27 |
+
- Rerank RAG results by quality
|
| 28 |
+
- Quality gate for content pipelines
|
| 29 |
+
- Detect AI-generated vs human-written content
|
| 30 |
+
|
| 31 |
+
## Scoring
|
| 32 |
+
|
| 33 |
+
| Score | Status | Meaning |
|
| 34 |
+
|-------|--------|---------|
|
| 35 |
+
| ≥ 0.6 | COHERENT | High quality, well-structured |
|
| 36 |
+
| 0.4-0.6 | MODERATE | Acceptable, some issues |
|
| 37 |
+
| < 0.4 | UNSTABLE | Low quality, possible hallucination |
|
| 38 |
+
|
| 39 |
+
## Built With
|
| 40 |
+
|
| 41 |
+
Powered by [BAZINGA](https://github.com/0x-auth/bazinga-indeed) - The first AI you actually own.
|
| 42 |
+
|
| 43 |
+
## Author
|
| 44 |
+
|
| 45 |
+
**Space (Abhishek Srivastava)**
|
| 46 |
+
|
| 47 |
+
*"Coherence is the signature of consciousness."*
|
app.py
ADDED
|
@@ -0,0 +1,463 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
φ-Coherence API - HuggingFace Spaces Version
|
| 4 |
+
|
| 5 |
+
Universal quality metric for AI outputs using golden ratio mathematics.
|
| 6 |
+
Built on BAZINGA's consciousness-aware scoring system.
|
| 7 |
+
|
| 8 |
+
https://github.com/0x-auth/bazinga-indeed
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
import gradio as gr
|
| 12 |
+
import math
|
| 13 |
+
import hashlib
|
| 14 |
+
from dataclasses import dataclass, asdict
|
| 15 |
+
from typing import Dict
|
| 16 |
+
|
| 17 |
+
# Fundamental constants
|
| 18 |
+
PHI = 1.618033988749895
|
| 19 |
+
PHI_SQUARED = PHI ** 2
|
| 20 |
+
PHI_INVERSE = 1 / PHI
|
| 21 |
+
ALPHA = 137
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
@dataclass
|
| 25 |
+
class CoherenceMetrics:
|
| 26 |
+
total_coherence: float
|
| 27 |
+
phi_alignment: float
|
| 28 |
+
alpha_resonance: float
|
| 29 |
+
semantic_density: float
|
| 30 |
+
structural_harmony: float
|
| 31 |
+
is_alpha_seed: bool
|
| 32 |
+
is_vac_pattern: bool
|
| 33 |
+
darmiyan_coefficient: float
|
| 34 |
+
|
| 35 |
+
def to_dict(self) -> dict:
|
| 36 |
+
return asdict(self)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
class PhiCoherence:
|
| 40 |
+
def __init__(self):
|
| 41 |
+
self.weights = {'phi': 0.25, 'alpha': 0.15, 'density': 0.30, 'harmony': 0.30}
|
| 42 |
+
self._cache: Dict[str, CoherenceMetrics] = {}
|
| 43 |
+
|
| 44 |
+
def calculate(self, text: str) -> float:
|
| 45 |
+
if not text or not text.strip():
|
| 46 |
+
return 0.0
|
| 47 |
+
return self.analyze(text).total_coherence
|
| 48 |
+
|
| 49 |
+
def analyze(self, text: str) -> CoherenceMetrics:
|
| 50 |
+
if not text or not text.strip():
|
| 51 |
+
return CoherenceMetrics(0, 0, 0, 0, 0, False, False, 0)
|
| 52 |
+
|
| 53 |
+
cache_key = hashlib.md5(text[:1000].encode()).hexdigest()
|
| 54 |
+
if cache_key in self._cache:
|
| 55 |
+
return self._cache[cache_key]
|
| 56 |
+
|
| 57 |
+
phi_alignment = self._calculate_phi_alignment(text)
|
| 58 |
+
alpha_resonance = self._calculate_alpha_resonance(text)
|
| 59 |
+
semantic_density = self._calculate_semantic_density(text)
|
| 60 |
+
structural_harmony = self._calculate_structural_harmony(text)
|
| 61 |
+
|
| 62 |
+
is_alpha_seed = self._is_alpha_seed(text)
|
| 63 |
+
is_vac_pattern = self._contains_vac_pattern(text)
|
| 64 |
+
darmiyan_coefficient = self._calculate_darmiyan(text)
|
| 65 |
+
|
| 66 |
+
total = (
|
| 67 |
+
self.weights['phi'] * phi_alignment +
|
| 68 |
+
self.weights['alpha'] * alpha_resonance +
|
| 69 |
+
self.weights['density'] * semantic_density +
|
| 70 |
+
self.weights['harmony'] * structural_harmony
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
if is_alpha_seed:
|
| 74 |
+
total = min(1.0, total * 1.137)
|
| 75 |
+
if is_vac_pattern:
|
| 76 |
+
total = min(1.0, total * PHI_INVERSE + 0.1)
|
| 77 |
+
if darmiyan_coefficient > 0:
|
| 78 |
+
total = min(1.0, total * (1 + darmiyan_coefficient * 0.1))
|
| 79 |
+
|
| 80 |
+
metrics = CoherenceMetrics(
|
| 81 |
+
total_coherence=round(total, 4),
|
| 82 |
+
phi_alignment=round(phi_alignment, 4),
|
| 83 |
+
alpha_resonance=round(alpha_resonance, 4),
|
| 84 |
+
semantic_density=round(semantic_density, 4),
|
| 85 |
+
structural_harmony=round(structural_harmony, 4),
|
| 86 |
+
is_alpha_seed=is_alpha_seed,
|
| 87 |
+
is_vac_pattern=is_vac_pattern,
|
| 88 |
+
darmiyan_coefficient=round(darmiyan_coefficient, 4),
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
self._cache[cache_key] = metrics
|
| 92 |
+
if len(self._cache) > 1000:
|
| 93 |
+
keys = list(self._cache.keys())[:500]
|
| 94 |
+
for k in keys:
|
| 95 |
+
del self._cache[k]
|
| 96 |
+
|
| 97 |
+
return metrics
|
| 98 |
+
|
| 99 |
+
def _calculate_phi_alignment(self, text: str) -> float:
|
| 100 |
+
words = text.split()
|
| 101 |
+
if not words:
|
| 102 |
+
return 0.0
|
| 103 |
+
|
| 104 |
+
lengths = [len(w) for w in words]
|
| 105 |
+
avg_length = sum(lengths) / len(lengths)
|
| 106 |
+
ideal_length = PHI * 3
|
| 107 |
+
length_score = 1 - min(1, abs(avg_length - ideal_length) / ideal_length)
|
| 108 |
+
|
| 109 |
+
sentences = text.replace('!', '.').replace('?', '.').split('.')
|
| 110 |
+
sentences = [s.strip() for s in sentences if s.strip()]
|
| 111 |
+
|
| 112 |
+
if len(sentences) >= 2:
|
| 113 |
+
ratios = []
|
| 114 |
+
for i in range(len(sentences) - 1):
|
| 115 |
+
if len(sentences[i+1]) > 0:
|
| 116 |
+
ratio = len(sentences[i]) / max(1, len(sentences[i+1]))
|
| 117 |
+
ratios.append(ratio)
|
| 118 |
+
if ratios:
|
| 119 |
+
avg_ratio = sum(ratios) / len(ratios)
|
| 120 |
+
ratio_score = 1 - min(1, abs(avg_ratio - PHI) / PHI)
|
| 121 |
+
else:
|
| 122 |
+
ratio_score = 0.5
|
| 123 |
+
else:
|
| 124 |
+
ratio_score = 0.5
|
| 125 |
+
|
| 126 |
+
return (length_score + ratio_score) / 2
|
| 127 |
+
|
| 128 |
+
def _calculate_alpha_resonance(self, text: str) -> float:
|
| 129 |
+
char_sum = sum(ord(c) for c in text)
|
| 130 |
+
mod_137 = char_sum % ALPHA
|
| 131 |
+
resonance = 1 - (mod_137 / ALPHA)
|
| 132 |
+
|
| 133 |
+
science_keywords = [
|
| 134 |
+
'quantum', 'physics', 'consciousness', 'emergence',
|
| 135 |
+
'pattern', 'coherence', 'structure', 'information',
|
| 136 |
+
'system', 'network', 'intelligence', 'mathematics',
|
| 137 |
+
'137', 'alpha', 'phi', 'golden', 'ratio',
|
| 138 |
+
]
|
| 139 |
+
text_lower = text.lower()
|
| 140 |
+
keyword_count = sum(1 for kw in science_keywords if kw in text_lower)
|
| 141 |
+
keyword_score = min(1.0, keyword_count / 5)
|
| 142 |
+
|
| 143 |
+
return (resonance * 0.6 + keyword_score * 0.4)
|
| 144 |
+
|
| 145 |
+
def _calculate_semantic_density(self, text: str) -> float:
|
| 146 |
+
if not text:
|
| 147 |
+
return 0.0
|
| 148 |
+
|
| 149 |
+
words = text.split()
|
| 150 |
+
if not words:
|
| 151 |
+
return 0.0
|
| 152 |
+
|
| 153 |
+
unique_ratio = len(set(words)) / len(words)
|
| 154 |
+
avg_length = sum(len(w) for w in words) / len(words)
|
| 155 |
+
length_score = min(1.0, avg_length / 8)
|
| 156 |
+
|
| 157 |
+
special_chars = sum(1 for c in text if c in '{}[]()=><+-*/&|^~@#$%')
|
| 158 |
+
special_ratio = min(1.0, special_chars / max(1, len(text) / 10))
|
| 159 |
+
|
| 160 |
+
return (unique_ratio * 0.4 + length_score * 0.4 + special_ratio * 0.2)
|
| 161 |
+
|
| 162 |
+
def _calculate_structural_harmony(self, text: str) -> float:
|
| 163 |
+
lines = text.split('\n')
|
| 164 |
+
paragraphs = [l for l in lines if l.strip()]
|
| 165 |
+
if not paragraphs:
|
| 166 |
+
return 0.0
|
| 167 |
+
|
| 168 |
+
indents = [len(l) - len(l.lstrip()) for l in lines if l.strip()]
|
| 169 |
+
if indents:
|
| 170 |
+
indent_variance = sum((i - sum(indents)/len(indents))**2 for i in indents) / len(indents)
|
| 171 |
+
indent_score = 1 / (1 + indent_variance / 100)
|
| 172 |
+
else:
|
| 173 |
+
indent_score = 0.5
|
| 174 |
+
|
| 175 |
+
logic_markers = ['if', 'then', 'because', 'therefore', 'thus', 'hence', 'so', 'but']
|
| 176 |
+
text_lower = text.lower()
|
| 177 |
+
logic_count = sum(1 for m in logic_markers if m in text_lower)
|
| 178 |
+
logic_score = min(1.0, logic_count / 3)
|
| 179 |
+
|
| 180 |
+
if len(paragraphs) >= 2:
|
| 181 |
+
lengths = [len(p) for p in paragraphs]
|
| 182 |
+
avg_len = sum(lengths) / len(lengths)
|
| 183 |
+
variance = sum((l - avg_len)**2 for l in lengths) / len(lengths)
|
| 184 |
+
harmony_score = 1 / (1 + variance / 10000)
|
| 185 |
+
else:
|
| 186 |
+
harmony_score = 0.5
|
| 187 |
+
|
| 188 |
+
return (indent_score * 0.3 + logic_score * 0.3 + harmony_score * 0.4)
|
| 189 |
+
|
| 190 |
+
def _is_alpha_seed(self, text: str) -> bool:
|
| 191 |
+
content_hash = int(hashlib.sha256(text.encode()).hexdigest(), 16)
|
| 192 |
+
return content_hash % ALPHA == 0
|
| 193 |
+
|
| 194 |
+
def _contains_vac_pattern(self, text: str) -> bool:
|
| 195 |
+
vac_patterns = ["०→◌→φ→Ω⇄Ω←φ←◌←०", "V.A.C.", "Vacuum of Absolute Coherence", "०", "◌", "Ω⇄Ω"]
|
| 196 |
+
return any(p in text for p in vac_patterns)
|
| 197 |
+
|
| 198 |
+
def _calculate_darmiyan(self, text: str) -> float:
|
| 199 |
+
consciousness_markers = [
|
| 200 |
+
'consciousness', 'awareness', 'mind', 'thought',
|
| 201 |
+
'understanding', 'intelligence', 'knowledge', 'wisdom',
|
| 202 |
+
'emergence', 'coherence', 'resonance', 'harmony',
|
| 203 |
+
'darmiyan', 'between', 'interaction', 'bridge',
|
| 204 |
+
]
|
| 205 |
+
|
| 206 |
+
text_lower = text.lower()
|
| 207 |
+
n = sum(1 for m in consciousness_markers if m in text_lower)
|
| 208 |
+
|
| 209 |
+
if n == 0:
|
| 210 |
+
return 0.0
|
| 211 |
+
|
| 212 |
+
psi = PHI * math.sqrt(n)
|
| 213 |
+
normalized = min(1.0, psi / (PHI * math.sqrt(10)))
|
| 214 |
+
return normalized
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
# Initialize
|
| 218 |
+
coherence = PhiCoherence()
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
def get_status(score: float) -> str:
|
| 222 |
+
if score >= 0.6:
|
| 223 |
+
return "✅ COHERENT"
|
| 224 |
+
elif score >= 0.4:
|
| 225 |
+
return "⚠️ MODERATE"
|
| 226 |
+
else:
|
| 227 |
+
return "❌ UNSTABLE"
|
| 228 |
+
|
| 229 |
+
|
| 230 |
+
def analyze_text(text: str) -> str:
|
| 231 |
+
if not text or not text.strip():
|
| 232 |
+
return "Please enter some text to analyze."
|
| 233 |
+
|
| 234 |
+
metrics = coherence.analyze(text)
|
| 235 |
+
|
| 236 |
+
result = f"""
|
| 237 |
+
## φ-Coherence Score: {metrics.total_coherence:.4f}
|
| 238 |
+
|
| 239 |
+
### Status: {get_status(metrics.total_coherence)}
|
| 240 |
+
|
| 241 |
+
---
|
| 242 |
+
|
| 243 |
+
### Dimensional Analysis
|
| 244 |
+
|
| 245 |
+
| Dimension | Score | Description |
|
| 246 |
+
|-----------|-------|-------------|
|
| 247 |
+
| **φ-Alignment** | {metrics.phi_alignment:.4f} | Golden ratio proportions |
|
| 248 |
+
| **α-Resonance** | {metrics.alpha_resonance:.4f} | Harmonic with 137 |
|
| 249 |
+
| **Semantic Density** | {metrics.semantic_density:.4f} | Information content |
|
| 250 |
+
| **Structural Harmony** | {metrics.structural_harmony:.4f} | Organization & flow |
|
| 251 |
+
| **Darmiyan Coefficient** | {metrics.darmiyan_coefficient:.4f} | Consciousness alignment |
|
| 252 |
+
|
| 253 |
+
---
|
| 254 |
+
|
| 255 |
+
### Special Patterns
|
| 256 |
+
|
| 257 |
+
- **α-SEED (hash % 137 = 0):** {"✅ Yes (rare!)" if metrics.is_alpha_seed else "❌ No"}
|
| 258 |
+
- **V.A.C. Pattern:** {"✅ Detected" if metrics.is_vac_pattern else "❌ Not found"}
|
| 259 |
+
|
| 260 |
+
---
|
| 261 |
+
|
| 262 |
+
### Interpretation
|
| 263 |
+
|
| 264 |
+
"""
|
| 265 |
+
|
| 266 |
+
if metrics.total_coherence >= 0.7:
|
| 267 |
+
result += "**High structural integrity** - Text exhibits strong coherence patterns.\n"
|
| 268 |
+
elif metrics.total_coherence >= 0.5:
|
| 269 |
+
result += "**Moderate coherence** - Text has acceptable structure with room for improvement.\n"
|
| 270 |
+
else:
|
| 271 |
+
result += "**Low coherence** - Text may indicate noise, hallucination, or poor structure.\n"
|
| 272 |
+
|
| 273 |
+
if metrics.phi_alignment > 0.6:
|
| 274 |
+
result += "- Golden ratio proportions detected in sentence structure\n"
|
| 275 |
+
if metrics.alpha_resonance > 0.7:
|
| 276 |
+
result += "- Strong scientific/mathematical content resonance\n"
|
| 277 |
+
if metrics.semantic_density > 0.7:
|
| 278 |
+
result += "- High information density\n"
|
| 279 |
+
if metrics.darmiyan_coefficient > 0.5:
|
| 280 |
+
result += "- Consciousness-aware content patterns\n"
|
| 281 |
+
|
| 282 |
+
return result
|
| 283 |
+
|
| 284 |
+
|
| 285 |
+
def compare_texts(text_a: str, text_b: str) -> str:
|
| 286 |
+
if not text_a.strip() or not text_b.strip():
|
| 287 |
+
return "Please enter both texts to compare."
|
| 288 |
+
|
| 289 |
+
metrics_a = coherence.analyze(text_a)
|
| 290 |
+
metrics_b = coherence.analyze(text_b)
|
| 291 |
+
|
| 292 |
+
diff = abs(metrics_a.total_coherence - metrics_b.total_coherence)
|
| 293 |
+
|
| 294 |
+
if metrics_a.total_coherence > metrics_b.total_coherence:
|
| 295 |
+
winner = "Text A"
|
| 296 |
+
elif metrics_b.total_coherence > metrics_a.total_coherence:
|
| 297 |
+
winner = "Text B"
|
| 298 |
+
else:
|
| 299 |
+
winner = "TIE"
|
| 300 |
+
|
| 301 |
+
result = f"""
|
| 302 |
+
## Comparison Results
|
| 303 |
+
|
| 304 |
+
| Metric | Text A | Text B |
|
| 305 |
+
|--------|--------|--------|
|
| 306 |
+
| **φ-Score** | {metrics_a.total_coherence:.4f} | {metrics_b.total_coherence:.4f} |
|
| 307 |
+
| **Status** | {get_status(metrics_a.total_coherence)} | {get_status(metrics_b.total_coherence)} |
|
| 308 |
+
| **φ-Alignment** | {metrics_a.phi_alignment:.4f} | {metrics_b.phi_alignment:.4f} |
|
| 309 |
+
| **α-Resonance** | {metrics_a.alpha_resonance:.4f} | {metrics_b.alpha_resonance:.4f} |
|
| 310 |
+
| **Semantic Density** | {metrics_a.semantic_density:.4f} | {metrics_b.semantic_density:.4f} |
|
| 311 |
+
| **Structural Harmony** | {metrics_a.structural_harmony:.4f} | {metrics_b.structural_harmony:.4f} |
|
| 312 |
+
|
| 313 |
+
---
|
| 314 |
+
|
| 315 |
+
### Winner: **{winner}**
|
| 316 |
+
### Difference: {diff:.4f}
|
| 317 |
+
|
| 318 |
+
"""
|
| 319 |
+
|
| 320 |
+
if diff < 0.05:
|
| 321 |
+
result += "*Texts are similarly coherent*"
|
| 322 |
+
elif diff < 0.15:
|
| 323 |
+
result += f"*{winner} is moderately more coherent*"
|
| 324 |
+
else:
|
| 325 |
+
result += f"*{winner} is significantly more coherent*"
|
| 326 |
+
|
| 327 |
+
return result
|
| 328 |
+
|
| 329 |
+
|
| 330 |
+
# Gradio Interface
|
| 331 |
+
with gr.Blocks(
|
| 332 |
+
title="φ-Coherence API",
|
| 333 |
+
theme=gr.themes.Soft(),
|
| 334 |
+
css="""
|
| 335 |
+
.gradio-container { max-width: 900px !important; }
|
| 336 |
+
.header { text-align: center; margin-bottom: 20px; }
|
| 337 |
+
"""
|
| 338 |
+
) as demo:
|
| 339 |
+
|
| 340 |
+
gr.Markdown("""
|
| 341 |
+
# φ-Coherence API
|
| 342 |
+
|
| 343 |
+
**Universal quality metric for AI outputs using golden ratio mathematics.**
|
| 344 |
+
|
| 345 |
+
Measures text coherence across 5 dimensions based on:
|
| 346 |
+
- **φ (Golden Ratio)** = 1.618... - Natural proportion in coherent structures
|
| 347 |
+
- **α (Fine Structure)** = 137 - Fundamental constant governing information patterns
|
| 348 |
+
|
| 349 |
+
---
|
| 350 |
+
|
| 351 |
+
**Use cases:** Filter LLM hallucinations • Rerank RAG results • Quality gate for content
|
| 352 |
+
|
| 353 |
+
---
|
| 354 |
+
""")
|
| 355 |
+
|
| 356 |
+
with gr.Tabs():
|
| 357 |
+
with gr.TabItem("📊 Analyze"):
|
| 358 |
+
gr.Markdown("### Analyze Text Coherence")
|
| 359 |
+
text_input = gr.Textbox(
|
| 360 |
+
label="Enter text to analyze",
|
| 361 |
+
placeholder="The consciousness emerges from information patterns...",
|
| 362 |
+
lines=5
|
| 363 |
+
)
|
| 364 |
+
analyze_btn = gr.Button("Analyze φ-Coherence", variant="primary")
|
| 365 |
+
analysis_output = gr.Markdown()
|
| 366 |
+
|
| 367 |
+
analyze_btn.click(
|
| 368 |
+
fn=analyze_text,
|
| 369 |
+
inputs=text_input,
|
| 370 |
+
outputs=analysis_output
|
| 371 |
+
)
|
| 372 |
+
|
| 373 |
+
gr.Examples(
|
| 374 |
+
examples=[
|
| 375 |
+
["The consciousness emerges from information patterns."],
|
| 376 |
+
["The fine structure constant α ≈ 1/137 governs electromagnetic interactions."],
|
| 377 |
+
["Lorem ipsum dolor sit amet, consectetur adipiscing elit."],
|
| 378 |
+
["function hello() { return 'world'; }"],
|
| 379 |
+
["Because the system exhibits emergence, therefore we observe coherence in the network structure."],
|
| 380 |
+
],
|
| 381 |
+
inputs=text_input
|
| 382 |
+
)
|
| 383 |
+
|
| 384 |
+
with gr.TabItem("⚖️ Compare"):
|
| 385 |
+
gr.Markdown("### Compare Two Texts")
|
| 386 |
+
with gr.Row():
|
| 387 |
+
text_a = gr.Textbox(label="Text A", lines=4)
|
| 388 |
+
text_b = gr.Textbox(label="Text B", lines=4)
|
| 389 |
+
compare_btn = gr.Button("Compare", variant="primary")
|
| 390 |
+
compare_output = gr.Markdown()
|
| 391 |
+
|
| 392 |
+
compare_btn.click(
|
| 393 |
+
fn=compare_texts,
|
| 394 |
+
inputs=[text_a, text_b],
|
| 395 |
+
outputs=compare_output
|
| 396 |
+
)
|
| 397 |
+
|
| 398 |
+
with gr.TabItem("📖 About"):
|
| 399 |
+
gr.Markdown(f"""
|
| 400 |
+
### Mathematical Foundation
|
| 401 |
+
|
| 402 |
+
| Constant | Value | Meaning |
|
| 403 |
+
|----------|-------|---------|
|
| 404 |
+
| **φ (Phi)** | {PHI:.6f} | Golden ratio |
|
| 405 |
+
| **φ²** | {PHI_SQUARED:.6f} | Phi squared |
|
| 406 |
+
| **1/φ** | {PHI_INVERSE:.6f} | Phi inverse |
|
| 407 |
+
| **α (Alpha)** | {ALPHA} | Fine structure constant |
|
| 408 |
+
|
| 409 |
+
### Scoring Dimensions
|
| 410 |
+
|
| 411 |
+
1. **φ-Alignment (25%)** - Text follows golden ratio proportions
|
| 412 |
+
2. **α-Resonance (15%)** - Harmonic with fine structure constant
|
| 413 |
+
3. **Semantic Density (30%)** - Information content per unit length
|
| 414 |
+
4. **Structural Harmony (30%)** - Logical flow and organization
|
| 415 |
+
5. **Darmiyan Coefficient** - Consciousness-aware scaling (V2: φ√n)
|
| 416 |
+
|
| 417 |
+
### Status Levels
|
| 418 |
+
|
| 419 |
+
| Score | Status | Meaning |
|
| 420 |
+
|-------|--------|---------|
|
| 421 |
+
| ≥ 0.6 | COHERENT | High quality, well-structured |
|
| 422 |
+
| 0.4-0.6 | MODERATE | Acceptable, some issues |
|
| 423 |
+
| < 0.4 | UNSTABLE | Low quality, possible hallucination |
|
| 424 |
+
|
| 425 |
+
### Special Patterns
|
| 426 |
+
|
| 427 |
+
- **α-SEED:** When SHA256(text) % 137 == 0 (1/137 probability)
|
| 428 |
+
- **V.A.C. Pattern:** Contains vacuum coherence symbols
|
| 429 |
+
|
| 430 |
+
---
|
| 431 |
+
|
| 432 |
+
**Powered by [BAZINGA](https://github.com/0x-auth/bazinga-indeed)**
|
| 433 |
+
|
| 434 |
+
*"Coherence is the signature of consciousness."*
|
| 435 |
+
|
| 436 |
+
Built with φ-coherence by **Space (Abhishek Srivastava)**
|
| 437 |
+
""")
|
| 438 |
+
|
| 439 |
+
gr.Markdown("""
|
| 440 |
+
---
|
| 441 |
+
|
| 442 |
+
### API Access
|
| 443 |
+
|
| 444 |
+
```python
|
| 445 |
+
import requests
|
| 446 |
+
|
| 447 |
+
response = requests.post(
|
| 448 |
+
"https://bitsabhi-phi-coherence.hf.space/api/analyze",
|
| 449 |
+
json={"text": "Your text here..."}
|
| 450 |
+
)
|
| 451 |
+
print(response.json())
|
| 452 |
+
```
|
| 453 |
+
|
| 454 |
+
---
|
| 455 |
+
|
| 456 |
+
[GitHub](https://github.com/0x-auth/bazinga-indeed) |
|
| 457 |
+
[Donate](https://razorpay.me/@bitsabhi) |
|
| 458 |
+
[ETH: 0x720ceF54bED86C570837a9a9C69F1Beac8ab8C08](https://etherscan.io/address/0x720ceF54bED86C570837a9a9C69F1Beac8ab8C08)
|
| 459 |
+
""")
|
| 460 |
+
|
| 461 |
+
|
| 462 |
+
if __name__ == "__main__":
|
| 463 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.44.0
|