File size: 11,075 Bytes
c0856bb
 
 
 
 
ca2955c
c0856bb
 
ca2955c
 
 
c0856bb
 
 
ca2955c
c0856bb
ca2955c
c0856bb
 
 
ca2955c
c0856bb
 
 
 
 
 
 
ca2955c
c0856bb
 
 
 
 
 
 
 
 
ca2955c
 
c0856bb
ca2955c
c0856bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import os
import gc
import gradio as gr
from huggingface_hub import hf_hub_download
from llama_cpp import Llama
import spaces  # <--- 1. Import spaces for ZeroGPU

CONFIG = {
    "UNEXPECTED": {"repo_id": "JPQ24/Contrarian-GGUF", "filename": "Meta-Llama-3.1-8B-Instruct-Contrarian-Q4_K_M.gguf"},
    "LOGIC":      {"repo_id": "JPQ24/Logic-4-GGUF",      "filename": "Meta-Llama-3.1-8B-Instruct-logic-v4-Q4_K_M.gguf"},
    "ORGANIC":    {"repo_id": "JPQ24/Llama-3.1-8b-Natural-Synthesis-merged-GGUF",    "filename": "source_model.Q4_K_M.gguf"}
}

def run_gguf_inference(repo_id: str, filename: str, prompt: str, max_tokens: int = 768, temperature: float = 0.7) -> str:
    model_path = hf_hub_download(repo_id=repo_id, filename=filename)

    # 2. Enable full GPU offloading (n_gpu_layers=-1)
    llm = Llama(
        model_path=model_path,
        n_ctx=4096,
        n_gpu_layers=-1,  # <--- Offloads ALL layers to the A100 GPU
        verbose=False
    )

    response = llm(
        prompt=prompt,
        max_tokens=max_tokens,
        temperature=temperature,
        stop=["<|endoftext|>", "<|eot_id|>", "### END"]
    )
    
    output_text = response["choices"][0]["text"].strip()

    del llm
    gc.collect()
    return output_text


# 3. Add @spaces.GPU decorator (allocates the A100 for up to 90 seconds)
@spaces.GPU(duration=90)
def execute_triad_pipeline(context: str, problem: str, progress=gr.Progress()):
    # ... (Keep the rest of your pipeline logic identical) ...
    if not context.strip() or not problem.strip():
        raise gr.Error("Both Context Documents and Problem Statement are required.")

    # --------------------------------------------------------------------------
    # STAGE 1: UNEXPECTED (Grounded Lateral Inversion)
    # --------------------------------------------------------------------------
    progress(0.1, desc="⚑ Stage 1/4: Unexpected Engine (Extracting Grounded Inversions)...")
    
    stage1_prompt = f"""<|system|>
You are an adversarial lateral strategist and red-teamer.
STRICT CLOSED-WORLD GROUNDING RULES:
1. You MUST NOT invent any physical assets, infrastructure, or technology not explicitly stated in the CONTEXT.
2. Innovation MUST come strictly from inverting the FUNCTIONAL RELATIONSHIPS of existing context entities.
3. Explicitly list the Context Entities used before generating hypotheses.
<|user|>
[CONTEXT]
{context}

[PROBLEM]
{problem}

### TASK:
1. Identify the fatal traps and rebound failure modes of past attempts.
2. List available context entities.
3. Provide 3 grounded, high-leverage unconventional hypotheses.
<|assistant|>
"""
    stage1_output = run_gguf_inference(
        CONFIG["UNEXPECTED"]["repo_id"],
        CONFIG["UNEXPECTED"]["filename"],
        stage1_prompt,
        max_tokens=650,
        temperature=0.75
    )

    # --------------------------------------------------------------------------
    # STAGE 2: LOGIC BOUNDS (Formal Axioms & Measurable Criteria)
    # --------------------------------------------------------------------------
    progress(0.35, desc="πŸ“ Stage 2/4: Logic Engine (Formulating Constraint Matrix)...")

    stage2_prompt = f"""<|system|>
You are a formal deductive logic engine. Convert qualitative requirements and proposed hypotheses into hard mathematical bounds, formal axioms, and verifiable success criteria (C1..Cn).
<|user|>
[CONTEXT]
{context}

[LATERAL HYPOTHESES]
{stage1_output}

### TASK:
1. Define Formal Axioms (P -> Q).
2. Set Feasibility & Grounding Boundaries (reject any non-context entities).
3. Establish Mandatory Success Criteria (C1..Cn).
<|assistant|>
"""
    stage2_output = run_gguf_inference(
        CONFIG["LOGIC"]["repo_id"],
        CONFIG["LOGIC"]["filename"],
        stage2_prompt,
        max_tokens=500,
        temperature=0.1
    )

    # --------------------------------------------------------------------------
    # STAGE 3: ORGANIC SYNTHESIS (Biomimetic Contrastive Pruning)
    # --------------------------------------------------------------------------
    progress(0.65, desc="🌿 Stage 3/4: Organic Engine (Biomimetic CoT & Pruning)...")

    stage3_prompt = f"""<|system|>
You are a biomimetic systems architect. Reason using the 5-phase organic cognitive trace:
1. Seed (Conceptual Core)
2. Germination (Divergent Loops)
3. Selective Nourishment (Explicitly NOURISH viable paths, explicitly WITHER weak/ungrounded paths)
4. Canopy Formation (Convergent System Design)
5. Homeostatic Review (Shock testing against heatwaves, floods, economic stress)
<|user|>
[CONTEXT]
{context}

[CRITERIA & BOUNDS]
{stage2_output}

[HYPOTHESES]
{stage1_output}

### TASK:
Produce the <thought> trace using the 5-phase method, then formulate the unified Multi-Component Mechanism.
<|assistant|>
"""
    stage3_output = run_gguf_inference(
        CONFIG["ORGANIC"]["repo_id"],
        CONFIG["ORGANIC"]["filename"],
        stage3_prompt,
        max_tokens=900,
        temperature=0.35
    )

    # --------------------------------------------------------------------------
    # STAGE 4: LOGICAL AUDIT & FINAL VERDICT
    # --------------------------------------------------------------------------
    progress(0.9, desc="βš–οΈ Stage 4/4: Logic Auditor (Verifying C1..Cn Compliance)...")

    stage4_prompt = f"""<|system|>
You are a ruthless logical verification auditor. Check the synthesized plan against each criterion (C1..Cn) defined in the Criteria list. Check for contradictions and ungrounded hallucinations.
<|user|>
[CRITERIA (C1..Cn)]
{stage2_output}

[PROPOSED SYSTEM PLAN]
{stage3_output}

### TASK:
1. Audit each criterion: [SATISFIED] or [FAILED] with justification.
2. Contradiction & Grounding Scan.
3. Logical Verdict (VALID & SOUND or INVALID).
<|assistant|>
"""
    stage4_output = run_gguf_inference(
        CONFIG["LOGIC"]["repo_id"],
        CONFIG["LOGIC"]["filename"],
        stage4_prompt,
        max_tokens=400,
        temperature=0.05
    )

    progress(1.0, desc="Execution Complete.")

    final_dashboard = f"""==============================================================================
FINAL AUDITED RESULT
==============================================================================
{stage4_output}

==============================================================================
FINAL SYSTEMIC INTERVENTION PLAN
==============================================================================
{stage3_output}
"""
    return stage1_output, stage2_output, stage3_output, final_dashboard


# ==============================================================================
# GRADIO UI SETUP
# ==============================================================================

sample_context = """[DOC 1: Hydrogeology & Climate Risk Report - District 9]
- The district faces severe aquifer depletion: water extraction currently exceeds recharge by 35% annually.
- Climate modeling projects a 40% increase in 5-day extreme heatwave events and a 20% risk of sudden flash floods in the low-lying southern valley.
- Emergency shock capacity: Current municipal water storage lasts only 48 hours in the event of an electrical grid blackout.

[DOC 2: Socio-Economic & Vulnerability Matrix]
- 70% of the population are smallholder farmers (under 2 hectares) living on less than $2.50/day.
- Smallholders rely on high-interest informal credit (40-60% annual rates) to buy diesel fuel for irrigation pumps. When diesel prices spike or pumps fail, default rates surge, causing land foreclosures.
- 30% of agricultural land is owned by 3 commercial agribusinesses that hold prioritized legal water rights and private solar arrays.

[DOC 3: Technology & Policy Pilot Evaluations]
- Pilot A (Subsidized Solar Pumps): Replaced diesel pumps with free solar pumps. Result: Farmers expanded pumping hours due to zero marginal energy cost, increasing aquifer depletion by an additional 18%.
- Pilot B (Automated Micro-Drip with Smart Metering): Reduced water consumption by 50% and doubled crop yields, but requires a continuous 4G/IoT connection and $1,200/hectare upfront capital cost.
- Pilot C (Community Water Quota & Insurance Co-op): Successfully prevented over-extraction, but collapsed within 8 months due to lack of transparent enforcement and elite capture by commercial agribusiness owners."""

sample_problem = """District 9 suffers from a compound crisis: extreme poverty among smallholders, escalating climate disaster risks (heatwaves/floods), and acute resource collapse (aquifer exhaustion). 

Design a systemic intervention that simultaneously:
1. Eliminates the smallholder debt-poverty trap caused by energy/pumping costs.
2. Halts aquifer over-extraction without reducing smallholder caloric/income baseline.
3. Establishes a localized crisis buffer for water/energy during sudden grid outages or climate shocks.

Constraints:
- You must ground your solution in the provided documents.
- You must explicitly avoid the failure modes identified in Pilots A, B, and C."""

theme = gr.themes.Monochrome(
    primary_hue="emerald",
    neutral_hue="slate",
    font=[gr.themes.GoogleFont("JetBrains Mono"), "monospace"]
)

with gr.Blocks(title="Triad: Cognitive SLM Pipeline", theme=theme) as demo:
    gr.Markdown("# 🧠 Triad: SLM Multi-Agent Cognitive Architecture")
    gr.Markdown(
        "Sequential orchestration of three specialized 8B parameter models (*Unexpected*, *Logic*, and *Organic*) "
        "designed to solve multi-constraint systemic problems without monolithic parameter bloat."
    )

    with gr.Row():
        with gr.Column(scale=5):
            context_input = gr.Textbox(
                label="Knowledge Base / Context Documents",
                lines=10,
                placeholder="Paste reference documents here..."
            )
            problem_input = gr.Textbox(
                label="Problem Statement & Constraints",
                lines=5,
                placeholder="Define the problem, objectives, and constraints..."
            )
            run_btn = gr.Button("πŸš€ Run Triad Pipeline", variant="primary", size="lg")

        with gr.Column(scale=6):
            with gr.Accordion("⚑ Stage 1: Contrarian Traps & Inversions (Unexpected Engine)", open=False):
                s1_out = gr.Markdown()
            
            with gr.Accordion("πŸ“ Stage 2: Formal Axioms & Success Bounds (Logic Engine)", open=False):
                s2_out = gr.Markdown()

            with gr.Accordion("🌿 Stage 3: Biomimetic CoT & Synthesis (Organic Engine)", open=False):
                s3_out = gr.Markdown()

            gr.Markdown("### πŸ“‹ Final Audited System Plan")
            final_out = gr.Markdown()

    # Preset Example Click
    gr.Examples(
        examples=[[sample_context, sample_problem]],
        inputs=[context_input, problem_input],
        label="Load Preset Case Study (District 9 Crisis)"
    )

    run_btn.click(
        fn=execute_triad_pipeline,
        inputs=[context_input, problem_input],
        outputs=[s1_out, s2_out, s3_out, final_out]
    )

if __name__ == "__main__":
    demo.launch()