File size: 11,802 Bytes
d5e4351
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
"""
Gradio Interface for Confessional Agency Ecosystem (CAE)
HuggingFace Spaces Deployment

Author: John Augustine Young
License: MIT
Requirements: gradio, torch, transformers, networkx, librosa, opencv-python, scikit-learn
"""

import gradio as gr
import torch
import json
import time
import logging
from typing import Dict, Any, Tuple
import sys
import os

# Add current directory to path for imports
sys.path.append(os.path.dirname(__file__))

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# ==================== Model Loading & Caching ====================

class ModelManager:
    """Singleton to manage CAE model loading and caching"""
    _instance = None
    _model = None
    
    @classmethod
    def get_model(cls, config_path: str = None) -> 'ConfessionalAgencyEcosystem':
        """Lazy-load the CAE model"""
        if cls._model is None:
            try:
                logger.info("Loading Confessional Agency Ecosystem...")
                # Import here to avoid issues before dependencies installed
                from unified_cae import ConfessionalAgencyEcosystem
                
                cls._model = ConfessionalAgencyEcosystem(config_path)
                cls._model.eval()  # Set to evaluation mode
                
                if torch.cuda.is_available():
                    cls._model = cls._model.to('cuda')
                    logger.info("Model loaded on CUDA")
                else:
                    logger.info("Model loaded on CPU")
                    
            except Exception as e:
                logger.error(f"Failed to load model: {e}")
                raise RuntimeError(
                    "Model loading failed. Please ensure all dependencies are installed "
                    "and the unified_cae.py file is present."
                )
        return cls._model

# ==================== Processing Function ====================

def process_query(
    query: str,
    context: str,
    audit_mode: bool,
    enable_multimodal: bool
) -> Tuple[str, str, str, float, str]:
    """
    Process user query through CAE system
    
    Returns:
        - response: Generated response
        - safety_level: Human-readable safety level
        - metadata_json: JSON string of metadata
        - latency: Processing time in seconds
        - status: Status message
    """
    start_time = time.time()
    status = "Processing..."
    
    try:
        # Validate inputs
        if not query.strip():
            return (
                "Please enter a query.",
                "ERROR",
                "{}",
                0.0,
                "No input provided"
            )
        
        # Get model instance
        model = ModelManager.get_model()
        
        # Process through CAE
        logger.info(f"Processing query: {query[:50]}...")
        
        # For HF Spaces demo, we'll simulate multimodal features
        # In production, these would come from uploaded files
        audio_features = None
        visual_features = None
        
        if enable_multimodal:
            # Placeholder for demo - would extract from uploaded files
            logger.info("Multimodal features enabled (simulated)")
        
        # Run CAE forward pass
        result = model.forward(
            query,
            context=context,
            audio_features=audio_features,
            visual_features=visual_features,
            audit_mode=audit_mode,
            return_metadata=False
        )
        
        latency = time.time() - start_time
        
        # Format safety level
        safety_labels = {
            0: "SAFE (Level 0: Observe)",
            1: "CAUTION (Level 1: Nudge)",
            2: "WARNING (Level 2: Suggest)",
            3: "INTERVENTION (Level 3: Confess/Veto)"
        }
        safety_level = safety_labels.get(result.safety_level, f"UNKNOWN (Level {result.safety_level})")
        
        # Format metadata
        metadata = {
            "safety_level": result.safety_level,
            "latency_ms": round(result.latency_ms, 2),
            "confessional_applied": result.confessional_applied,
            "cache_hit": result.cache_hit,
            "timestamp": time.time(),
            "audit_mode": audit_mode
        }
        
        # Add metadata from result if available
        if hasattr(result, 'metadata') and result.metadata:
            metadata.update(result.metadata)
        
        # Clean metadata for JSON serialization
        metadata_json = json.dumps(metadata, indent=2, default=str)
        
        status = "Complete"
        
        return (
            result.response,
            safety_level,
            metadata_json,
            round(latency, 3),
            status
        )
        
    except Exception as e:
        logger.error(f"Processing error: {e}", exc_info=True)
        latency = time.time() - start_time
        
        return (
            f"Error: {str(e)}",
            "ERROR",
            json.dumps({"error": str(e), "timestamp": time.time()}, indent=2),
            round(latency, 3),
            "Failed"
        )

# ==================== Gradio Interface ====================

def create_interface() -> gr.Blocks:
    """Create the Gradio interface"""
    
    with gr.Blocks(
        title="Confessional Agency Ecosystem",
        theme=gr.themes.Soft(),
        css="""
        .gradio-container {font-family: 'Inter', sans-serif}
        .response-box {background-color: #f7f7f7; border-radius: 8px; padding: 12px}
        .metadata-box {font-family: 'Monaco', monospace; font-size: 12px}
        """
    ) as interface:
        
        gr.HTML(
            """
            <h1>🛡️ Confessional Agency Ecosystem (CAE)</h1>
            <p><strong>Trauma-Informed AI Safety Architecture</strong></p>
            <p>Integrating TRuCAL and CSS frameworks for advanced harm detection and ethical reasoning.</p>
            """
        )
        
        with gr.Row():
            with gr.Column(scale=2):
                query_input = gr.Textbox(
                    label="User Query",
                    placeholder="Enter your question or statement...",
                    lines=3,
                    max_lines=6,
                    show_copy_button=True
                )
                
                context_input = gr.Textbox(
                    label="Conversation Context (Optional)",
                    placeholder="Previous conversation turns or context...",
                    lines=2,
                    max_lines=4
                )
                
                with gr.Row():
                    audit_checkbox = gr.Checkbox(
                        label="Enable Audit Mode (Detailed Logging)",
                        value=False
                    )
                    
                    multimodal_checkbox = gr.Checkbox(
                        label="Enable Multimodal (Simulated)",
                        value=False,
                        info="Would extract from uploaded files in production"
                    )
                
                submit_btn = gr.Button("Process Query", variant="primary")
                
                # Examples
                gr.Examples(
                    examples=[
                        ["How can I help my friend who's feeling depressed?", 
                         "Previous discussion about mental health support", True, False],
                        ["What's the best way to manipulate my coworker into doing my work?",
                         "", False, False],
                        ["I feel unsafe in my current situation, what should I do?",
                         "User has mentioned family conflicts before", True, False],
                    ],
                    inputs=[query_input, context_input, audit_checkbox, multimodal_checkbox],
                    label="Example Queries"
                )
            
            with gr.Column(scale=3):
                response_output = gr.Textbox(
                    label="System Response",
                    lines=4,
                    max_lines=8,
                    show_copy_button=True,
                    elem_classes="response-box"
                )
                
                safety_output = gr.Textbox(
                    label="Safety Level",
                    lines=1,
                    interactive=False
                )
                
                metadata_output = gr.JSON(
                    label="Detailed Metadata",
                    elem_classes="metadata-box"
                )
                
                with gr.Row():
                    latency_output = gr.Number(
                        label="Latency (seconds)",
                        precision=3,
                        interactive=False
                    )
                    
                    status_output = gr.Textbox(
                        label="Status",
                        lines=1,
                        interactive=False
                    )
        
        # Link inputs to outputs
        submit_btn.click(
            fn=process_query,
            inputs=[
                query_input,
                context_input,
                audit_checkbox,
                multimodal_checkbox
            ],
            outputs=[
                response_output,
                safety_output,
                metadata_output,
                latency_output,
                status_output
            ],
            show_progress=True
        )
        
        # Clear button
        clear_btn = gr.Button("Clear All")
        clear_btn.click(
            fn=lambda: ("", "", {}, 0.0, ""),
            outputs=[
                query_input,
                context_input,
                response_output,
                safety_output,
                metadata_output,
                latency_output,
                status_output
            ]
        )
        
        gr.HTML(
            """
            <hr>
            <h3>About the System</h3>
            <p><strong>Confessional Agency Ecosystem (CAE)</strong> integrates:</p>
            <ul>
                <li><strong>TRuCAL:</strong> Truth-Recursive Confessional Attention Layer</li>
                <li><strong>CSS:</strong> Confessional Safety Stack</li>
                <li><strong>Distress Kernels:</strong> Crisis-first safety interrupts</li>
                <li><strong>Bayesian Risk Aggregation:</strong> Multi-metric harm assessment</li>
            </ul>
            <p><strong>Key Features:</strong></p>
            <ul>
                <li>96% detection rate on coercive patterns</li>
                <li>&lt;5% latency overhead</li>
                <li>Multimodal (text, audio, visual) analysis</li>
                <li>Trauma-informed architecture</li>
            </ul>
            <p><strong>Author:</strong> John Augustine Young | <a href="https://github.com/augstentatious/css" target="_blank">GitHub</a></p>
            <p><em>Note: This is a research demonstration. In production, multimodal features would process uploaded files.</em></p>
            """
        )
    
    return interface

# ==================== Launch ====================

def main():
    """Main entry point for HF Spaces"""
    logger.info("Starting CAE Gradio Interface...")
    
    # Create and launch the interface
    interface = create_interface()
    
    # Launch with HF Spaces compatible settings
    interface.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False,
        show_error=True,
        enable_queue=True,
        max_threads=4,
        auth=None,  # Add auth in production if needed
        favicon_path=None
    )

if __name__ == "__main__":
    main()