""" PHANTASM Demo — Hugging Face Space Probabilistic Hallucination-Aware Neural Transformation with Adaptive Synthesis Method """ import gradio as gr _pipeline = None def get_pipeline(): global _pipeline if _pipeline is None: from phantasm import PHANTASMPipeline _pipeline = PHANTASMPipeline.from_pretrained("gpt2", device="cpu") return _pipeline def analyze(text: str, domain: str, reference: str): if not text or not text.strip(): return ("Please enter some text to analyze.",) * 5 try: pipe = get_pipeline() ref = reference.strip() if reference and reference.strip() else None report = pipe.analyze(text, reference_text=ref, domain=domain) d = report.to_dict() # ── Summary ────────────────────────────────────────────────────────── risk = d["hallucination_risk"] tier = d["uncertainty"]["tier"] cal = d["uncertainty"]["calibrated_confidence"] interval = d["uncertainty"]["interval"] risk_icon = "🔴" if risk > 0.7 else "🟡" if risk > 0.4 else "🟢" summary_md = f"""## PHANTASM Analysis Summary | Metric | Value | |--------|-------| | **Hallucination Risk** | `{risk:.3f}` {risk_icon} | | **Reliability Tier** | `{tier.upper()}` | | **Calibrated Confidence** | `{cal:.3f}` | | **Conformal Interval** | `[{interval[0]:.3f}, {interval[1]:.3f}]` | | **Domain** | `{domain}` | ### Synthesis {d["synthesis"]} """ # ── HGT ────────────────────────────────────────────────────────────── atlas = report.competency_atlas boundary = atlas.boundary_tokens[:10] if atlas.boundary_tokens else [] hgt_md = f"""## Hallucination Gradient Tracing (HGT) — Pillar I **Overall Hallucination Risk:** `{atlas.overall_hallucination_risk:.4f}` **Boundary Tokens** (knowledge limits detected): {", ".join(f"`{t}`" for t in boundary) if boundary else "_None detected — text appears factually grounded._"} > HGT uses gradient norms to pinpoint exactly which tokens the model is uncertain about, > creating a per-token knowledge-boundary oracle. """ # ── CMN ────────────────────────────────────────────────────────────── hyps = d.get("hypotheses", []) if hyps: hyp_rows = "\n".join( f"| {h['text'][:80]}{'…' if len(h['text']) > 80 else ''} " f"| {h['novelty']:.3f} | {h['plausibility']:.3f} | {h['domain']} |" for h in hyps[:5] ) else: hyp_rows = "| _No hypotheses generated_ | — | — | — |" cmn_md = f"""## Confabulation Mining Network (CMN) — Pillar II **{len(hyps)} hypothesis(es) mined** from confabulation patterns: | Hypothesis | Novelty | Plausibility | Domain | |-----------|---------|-------------|--------| {hyp_rows} > CMN extracts structured, falsifiable hypotheses from model confabulations — > turning "hallucinations" into research leads for scientific discovery. """ # ── UC ──────────────────────────────────────────────────────────────── uc = d["uncertainty"] uc_md = f"""## Uncertainty Crystallization (UC) — Pillar III | Component | Value | |-----------|-------| | **Raw Confidence** | `{uc["raw_confidence"]:.4f}` | | **Calibrated Confidence** | `{uc["calibrated_confidence"]:.4f}` | | **Epistemic Uncertainty** | `{uc["epistemic"]:.4f}` | | **Aleatoric Uncertainty** | `{uc["aleatoric"]:.4f}` | | **Reliability Tier** | **{uc["tier"].upper()}** | | **Conformal Interval** | `[{uc["interval"][0]:.3f}, {uc["interval"][1]:.3f}]` | ### Recommendation {uc["recommendation"]} **Tiers:** CRYSTAL ≥ 0.85 confidence · SOLID ≥ 0.65 · FLUID ≥ 0.45 · VAPOR < 0.45 """ # ── Insights ────────────────────────────────────────────────────────── insights = d.get("insights", []) if insights: insights_md = "## Actionable Insights\n\n" + "\n".join( f"- {ins}" for ins in insights ) else: insights_md = "## Actionable Insights\n\n_No specific insights generated._" return summary_md, hgt_md, cmn_md, uc_md, insights_md except Exception as e: err = ( f"Error during analysis: `{type(e).__name__}: {e}`\n\n" "Please try a different text or check the logs." ) return (err,) * 5 EXAMPLES = [ ["The moon is made of green cheese and orbits Earth every 24 hours.", "science", ""], ["Penicillin was discovered by Alexander Fleming in 1928.", "medicine", ""], ["Bitcoin uses proof-of-work consensus where miners solve cryptographic puzzles.", "finance", ""], ["Napoleon Bonaparte was born in Corsica in 1769.", "history", ""], ["The Great Wall of China is approximately 500 km long.", "general", ""], ] with gr.Blocks( title="PHANTASM — Hallucination Inversion Framework", theme=gr.themes.Soft(), ) as demo: gr.Markdown(""" # 🔮 PHANTASM ### Probabilistic Hallucination-Aware Neural Transformation with Adaptive Synthesis Method **The first ML framework that mathematically inverts LLM "failures" into productive features.** Three pillars: **HGT** (gradient knowledge boundary) · **CMN** (hypothesis mining) · **UC** (uncertainty crystallization) [![PyPI](https://img.shields.io/pypi/v/phantasm-llm?color=purple)](https://pypi.org/project/phantasm-llm/) [![GitHub](https://img.shields.io/badge/GitHub-PHANTASM-black)](https://github.com/vignesh2027/PHANTASM) """) with gr.Row(): with gr.Column(scale=2): text_input = gr.Textbox( label="Text to Analyze", placeholder="Enter any text — factual claims, model outputs, research hypotheses…", lines=5, ) with gr.Column(scale=1): domain_input = gr.Dropdown( choices=["general", "medicine", "finance", "science", "history", "legal", "education", "code"], value="general", label="Domain", ) ref_input = gr.Textbox( label="Reference Text (optional)", placeholder="Ground truth or reference…", lines=3, ) analyze_btn = gr.Button("🔮 Analyze with PHANTASM", variant="primary", size="lg") with gr.Tabs(): with gr.Tab("Summary"): summary_out = gr.Markdown() with gr.Tab("HGT — Knowledge Boundaries"): hgt_out = gr.Markdown() with gr.Tab("CMN — Hypothesis Mining"): cmn_out = gr.Markdown() with gr.Tab("UC — Uncertainty"): uc_out = gr.Markdown() with gr.Tab("Actionable Insights"): insights_out = gr.Markdown() analyze_btn.click( fn=analyze, inputs=[text_input, domain_input, ref_input], outputs=[summary_out, hgt_out, cmn_out, uc_out, insights_out], ) gr.Examples( examples=EXAMPLES, inputs=[text_input, domain_input, ref_input], label="Quick Examples", ) gr.Markdown(""" --- **PHANTASM** · Apache 2.0 · Built by [Vignesh S](https://github.com/vignesh2027) · [Paper](https://github.com/vignesh2027/PHANTASM/blob/main/paper/PHANTASM_paper.md) · [Docs](https://vignesh2027.github.io/PHANTASM) """) if __name__ == "__main__": demo.launch()