Spaces:
Running
Running
Update zotheos_interface_public.py
Browse files- zotheos_interface_public.py +318 -855
zotheos_interface_public.py
CHANGED
@@ -1,855 +1,318 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import asyncio
|
3 |
-
import logging
|
4 |
-
import os
|
5 |
-
import sys
|
6 |
-
import time
|
7 |
-
import random
|
8 |
-
from typing import Optional, Dict, Any, List, Union, Tuple
|
9 |
-
import html
|
10 |
-
import json
|
11 |
-
import tempfile
|
12 |
-
|
13 |
-
# --- Constants ---
|
14 |
-
APP_TITLE = "ZOTHEOS - Ethical Fusion AI"
|
15 |
-
FAVICON_FILENAME = "favicon_blackBW.ico"
|
16 |
-
LOGO_FILENAME = "zotheos_logo.png"
|
17 |
-
|
18 |
-
# --- Logger
|
19 |
-
def init_logger():
|
20 |
-
logging.basicConfig(
|
21 |
-
|
22 |
-
|
23 |
-
datefmt="%Y-%m-%d %H:%M:%S"
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
return
|
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 |
-
--text-
|
132 |
-
--border-
|
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 |
-
color: var(--text-
|
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 |
-
color:
|
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 |
-
.gradio-button.secondary:hover, button#clear_button.gradio-button:hover {
|
321 |
-
background: var(--button-secondary-hover-bg) !important;
|
322 |
-
border-color: var(--button-secondary-hover-bg) !important;
|
323 |
-
}
|
324 |
-
.gradio-button.secondary:focus, button#clear_button.gradio-button:focus {
|
325 |
-
box-shadow: 0 0 0 2px var(--background-main),
|
326 |
-
0 0 0 4px var(--button-secondary-focus-ring) !important;
|
327 |
-
}
|
328 |
-
|
329 |
-
/* Pro Button Styling */
|
330 |
-
#pro_features_row .gradio-button {
|
331 |
-
font-size: 0.85rem !important;
|
332 |
-
padding: 8px 16px !important;
|
333 |
-
flex-grow: 0 !important;
|
334 |
-
background-color: var(--accent-secondary) !important;
|
335 |
-
color: var(--text-color-medium) !important;
|
336 |
-
opacity: 0.7;
|
337 |
-
border: 1px solid var(--accent-secondary) !important;
|
338 |
-
}
|
339 |
-
#pro_features_row .gradio-button.interactive_button_enabled {
|
340 |
-
background-color: var(--button-secondary-bg) !important;
|
341 |
-
color: var(--button-secondary-text) !important;
|
342 |
-
opacity: 1.0;
|
343 |
-
border: 1px solid var(--button-secondary-bg) !important;
|
344 |
-
}
|
345 |
-
#pro_features_row .gradio-button.interactive_button_enabled:hover {
|
346 |
-
background-color: var(--button-secondary-hover-bg) !important;
|
347 |
-
border-color: var(--button-secondary-hover-bg) !important;
|
348 |
-
}
|
349 |
-
#pro_features_row .gradio-button:disabled,
|
350 |
-
#pro_features_row .gradio-button[disabled] {
|
351 |
-
opacity: 0.4 !important;
|
352 |
-
cursor: not-allowed !important;
|
353 |
-
background-color: #202020 !important;
|
354 |
-
color: #555 !important;
|
355 |
-
border: 1px solid #202020 !important;
|
356 |
-
}
|
357 |
-
|
358 |
-
/* Attribution footer */
|
359 |
-
#footer_attribution {
|
360 |
-
font-size: 0.75rem;
|
361 |
-
color: var(--text-color-medium);
|
362 |
-
text-align: center;
|
363 |
-
padding-top: 1rem;
|
364 |
-
margin-top: 2rem;
|
365 |
-
opacity: 0.8;
|
366 |
-
line-height: 1.5;
|
367 |
-
}
|
368 |
-
|
369 |
-
/* ✅ FORCE FULL RESPONSIVE LAYOUT ON MOBILE */
|
370 |
-
html, body {
|
371 |
-
max-width: 100vw !important;
|
372 |
-
overflow-x: hidden !important;
|
373 |
-
}
|
374 |
-
|
375 |
-
.gradio-container,
|
376 |
-
.container,
|
377 |
-
.block,
|
378 |
-
.row,
|
379 |
-
.column,
|
380 |
-
.interface-section,
|
381 |
-
.centered-container,
|
382 |
-
#header_column,
|
383 |
-
#query_input,
|
384 |
-
#synthesized_summary_output,
|
385 |
-
#fusion_output,
|
386 |
-
#user_token_input,
|
387 |
-
#tier_status_display,
|
388 |
-
#memory_log,
|
389 |
-
#memory_display_panel,
|
390 |
-
#pro_features_row {
|
391 |
-
width: 100% !important;
|
392 |
-
max-width: 100% !important;
|
393 |
-
box-sizing: border-box !important;
|
394 |
-
overflow-wrap: break-word !important;
|
395 |
-
}
|
396 |
-
|
397 |
-
textarea, input, button {
|
398 |
-
max-width: 100% !important;
|
399 |
-
}
|
400 |
-
|
401 |
-
/* ✅🧠 CORRECTED: Force logo image to grayscale. Emojis (text) are unaffected. */
|
402 |
-
img {
|
403 |
-
max-width: 100%;
|
404 |
-
height: auto;
|
405 |
-
display: block;
|
406 |
-
filter: grayscale(100%);
|
407 |
-
}
|
408 |
-
|
409 |
-
/* ✅ FIX TEXTBOX TRUNCATION */
|
410 |
-
textarea {
|
411 |
-
resize: vertical !important;
|
412 |
-
}
|
413 |
-
|
414 |
-
/* ✅ UNIFY FONT SIZE & TIGHTEN UI ON SMALL DEVICES */
|
415 |
-
@media screen and (max-width: 480px) {
|
416 |
-
html, body {
|
417 |
-
font-size: 15px !important;
|
418 |
-
}
|
419 |
-
|
420 |
-
/* 🔧 TIGHTEN GRADIO UI SCALE ON MOBILE */
|
421 |
-
.gradio-container,
|
422 |
-
.block,
|
423 |
-
.row,
|
424 |
-
.column,
|
425 |
-
textarea,
|
426 |
-
input,
|
427 |
-
button {
|
428 |
-
transform: scale(0.95);
|
429 |
-
transform-origin: top center;
|
430 |
-
}
|
431 |
-
|
432 |
-
h1, h2, h3 {
|
433 |
-
font-size: 1.2rem !important;
|
434 |
-
}
|
435 |
-
|
436 |
-
#header_logo img {
|
437 |
-
max-height: 80px !important;
|
438 |
-
padding: 8px !important;
|
439 |
-
}
|
440 |
-
|
441 |
-
#footer_attribution {
|
442 |
-
font-size: 0.65rem !important;
|
443 |
-
}
|
444 |
-
}
|
445 |
-
|
446 |
-
/* Loading Indicator Styles */
|
447 |
-
.cosmic-loading { display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 20px; text-align: center; }
|
448 |
-
.orbital-spinner { width: 40px; height: 40px; border: 4px solid var(--text-color-medium); border-top: 4px solid var(--text-color-light); border-radius: 50%; animation: spin 1s linear infinite; margin-bottom: 15px; }
|
449 |
-
@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
|
450 |
-
.thinking-text { font-family: var(--font-body); color: var(--text-color-light); font-size: 0.9rem; }
|
451 |
-
|
452 |
-
/* Authentication Row and Tier Status Display */
|
453 |
-
#auth_row { margin-bottom: 1rem; align-items: center; }
|
454 |
-
#user_token_input textarea {
|
455 |
-
background-color: var(--input-bg) !important; color: var(--text-color-light) !important;
|
456 |
-
border: 1px solid var(--input-border) !important; border-radius: 8px !important;
|
457 |
-
padding: 0.75em !important;
|
458 |
-
}
|
459 |
-
#tier_status_display {
|
460 |
-
padding: 0.60rem 0.5rem !important; text-align: left; font-size: 0.9rem;
|
461 |
-
color: var(--text-color-medium); display: flex; align-items: flex-start;
|
462 |
-
min-height: 40px; line-height: 1.4;
|
463 |
-
}
|
464 |
-
#tier_status_display p { margin: 0 !important; }
|
465 |
-
#tier_status_display small { font-size: 0.9em; opacity: 0.9; }
|
466 |
-
|
467 |
-
/* Footer */
|
468 |
-
#footer_attribution {
|
469 |
-
font-size: 0.75rem; color: var(--text-color-medium); text-align: center;
|
470 |
-
margin-top: 2.5rem; padding-bottom: 1rem; opacity: 0.8; line-height: 1.5;
|
471 |
-
}
|
472 |
-
#footer_attribution strong { font-weight: 600; color: var(--text-color-light); }
|
473 |
-
|
474 |
-
/* Toast / Notification Styling */
|
475 |
-
.gradio-toast {
|
476 |
-
background-color: #1e1e1e !important;
|
477 |
-
color: var(--text-color-light) !important;
|
478 |
-
border: 1px solid var(--border-color) !important;
|
479 |
-
border-radius: 6px !important;
|
480 |
-
box-shadow: 0 4px 12px rgba(0,0,0,0.3) !important;
|
481 |
-
}
|
482 |
-
.gradio-toast .toast-body {
|
483 |
-
color: var(--text-color-light) !important;
|
484 |
-
}
|
485 |
-
.gradio-toast .toast-body svg { /* Style the icon if present */
|
486 |
-
fill: var(--text-color-light) !important;
|
487 |
-
}
|
488 |
-
.gradio-toast.toast-info, .gradio-toast.toast-error, .gradio-toast.toast-warning, .gradio-toast.toast-success {
|
489 |
-
/* General styles already applied, these are for any minor specific tweaks if needed */
|
490 |
-
}
|
491 |
-
|
492 |
-
/* Scrollable Output Boxes CSS */
|
493 |
-
#fusion_output, #synthesized_summary_output {
|
494 |
-
max-height: 70vh;
|
495 |
-
overflow-y: auto !important;
|
496 |
-
padding: 1em;
|
497 |
-
scroll-behavior: smooth;
|
498 |
-
border: 1px solid var(--border-color) !important;
|
499 |
-
border-radius: 10px !important;
|
500 |
-
background-color: var(--input-bg) !important;
|
501 |
-
word-wrap: break-word;
|
502 |
-
white-space: pre-wrap;
|
503 |
-
}
|
504 |
-
|
505 |
-
/* Memory Display Panel Scrollable CSS */
|
506 |
-
#memory_display_panel {
|
507 |
-
font-size: 0.85rem;
|
508 |
-
color: var(--text-color-medium);
|
509 |
-
background-color: #0A0A0A; padding: 0.75em;
|
510 |
-
border-radius: 6px; border: 1px solid var(--border-color); max-height: 400px; overflow-y: auto;
|
511 |
-
}
|
512 |
-
#memory_display_panel .memory-entry { margin-bottom: 1rem; padding: 0.75rem; border: 1px solid #333; border-radius: 6px; background-color: #101010; }
|
513 |
-
#memory_display_panel .memory-entry strong { color: var(--text-color-light); font-weight: bold; }
|
514 |
-
#memory_display_panel .memory-entry em { color: var(--text-color-medium); font-style: normal; }
|
515 |
-
#memory_display_panel .memory-entry .query-text,
|
516 |
-
#memory_display_panel .memory-entry .summary-text { display: block; white-space: pre-wrap; word-break: break-word; margin-top: 0.25em; background-color: #181818; padding: 0.3em 0.5em; border-radius: 4px; }
|
517 |
-
|
518 |
-
/* A general scrollable container class, if needed elsewhere */
|
519 |
-
.scrollable-container { max-height: 75vh; overflow-y: auto; }
|
520 |
-
"""
|
521 |
-
|
522 |
-
# --- render_memory_entries_as_html ---
|
523 |
-
def render_memory_entries_as_html(memory_entries: List[dict]) -> str:
|
524 |
-
if not memory_entries: return "<div class='memory-entry'>No stored memory entries or memory disabled.</div>"
|
525 |
-
html_blocks = [];
|
526 |
-
for entry in reversed(memory_entries[-5:]):
|
527 |
-
query = html.escape(entry.get("query", "N/A")); ts_iso = entry.get("metadata", {}).get("timestamp_iso", "Unknown");
|
528 |
-
try: dt_obj = time.strptime(ts_iso, '%Y-%m-%dT%H:%M:%SZ'); formatted_ts = time.strftime('%Y-%m-%d %H:%M UTC', dt_obj)
|
529 |
-
except: formatted_ts = html.escape(ts_iso)
|
530 |
-
models_list = entry.get("metadata", {}).get("active_models_queried", []); models_str = html.escape(", ".join(models_list)) if models_list else "N/A"
|
531 |
-
summary_raw = entry.get("metadata", {}).get("synthesized_summary_text", entry.get("response", "No summary.")[:500]); summary = html.escape(summary_raw[:300]) + ("..." if len(summary_raw) > 300 else "")
|
532 |
-
html_blocks.append(f"<div class='memory-entry'><p><strong>🕒 TS:</strong> {formatted_ts}</p><p><strong>🧠 Models:</strong> {models_str}</p><div><strong>❓ Q:</strong><div class='query-text'>{query}</div></div><div><strong>📌 Sum:</strong><div class='summary-text'>{summary}</div></div></div>")
|
533 |
-
return "\n".join(html_blocks) if html_blocks else "<div class='memory-entry'>No processable entries.</div>"
|
534 |
-
|
535 |
-
# --- Format Tier Status for UI ---
|
536 |
-
def format_tier_status_for_ui(tier_name: str) -> str:
|
537 |
-
features = TIER_FEATURES.get(tier_name, TIER_FEATURES["error (auth module failed)"])
|
538 |
-
status_md = f"**{features['display_name']}**\n"
|
539 |
-
status_md += f"<small>Memory: {features['emoji_map']['memory']} | "
|
540 |
-
status_md += f"Export: {features['emoji_map']['export']} | "
|
541 |
-
model_limit_from_backend = "N/A"
|
542 |
-
if ai_system and hasattr(ai_system, 'config') and isinstance(ai_system.config, dict) and \
|
543 |
-
'TIER_CONFIG' in ai_system.config and isinstance(ai_system.config['TIER_CONFIG'], dict) and \
|
544 |
-
tier_name in ai_system.config['TIER_CONFIG'] and isinstance(ai_system.config['TIER_CONFIG'][tier_name], dict):
|
545 |
-
backend_limit = ai_system.config['TIER_CONFIG'][tier_name].get('model_limit', 'N/A')
|
546 |
-
if isinstance(backend_limit, int) and backend_limit >= 999 :
|
547 |
-
model_limit_from_backend = "All"
|
548 |
-
else: model_limit_from_backend = str(backend_limit)
|
549 |
-
status_md += f"Models: Up to {model_limit_from_backend}"
|
550 |
-
status_md += "</small>"
|
551 |
-
return status_md
|
552 |
-
|
553 |
-
# --- Update Tier Display and Features UI ---
|
554 |
-
def update_tier_display_and_features_ui(user_token_str: Optional[str]) -> Tuple:
|
555 |
-
tier_name = get_user_tier(user_token_str if user_token_str else "")
|
556 |
-
tier_features = TIER_FEATURES.get(tier_name, TIER_FEATURES["free"])
|
557 |
-
logger.info(f"UI: Token changed. Tier: '{tier_name}'. Features: {tier_features}")
|
558 |
-
formatted_status = format_tier_status_for_ui(tier_name)
|
559 |
-
export_button_interactive = tier_features["export_enabled"]
|
560 |
-
export_button_classes = "interactive_button_enabled" if export_button_interactive else ""
|
561 |
-
return (gr.update(value=formatted_status), gr.update(interactive=export_button_interactive, elem_classes=export_button_classes))
|
562 |
-
|
563 |
-
# --- Clear All Fields Function ---
|
564 |
-
def clear_all_fields():
|
565 |
-
logger.info("Clearing all input fields and output areas.")
|
566 |
-
default_tier_name = get_user_tier("")
|
567 |
-
default_tier_features = TIER_FEATURES.get(default_tier_name, TIER_FEATURES["free"])
|
568 |
-
return (
|
569 |
-
"", # query_input_component
|
570 |
-
"", # user_token_input
|
571 |
-
gr.update(value=format_tier_status_for_ui(default_tier_name)), # tier_status_display
|
572 |
-
"Detailed perspectives will appear here...", # fused_response_output_component
|
573 |
-
"Synthesized insight will appear here...", # synthesized_summary_component
|
574 |
-
gr.update(value="**Status:** Awaiting Input"), # active_models_display_component
|
575 |
-
gr.update(value="<div class='memory-entry'>No memory entries loaded yet.</div>"), # memory_display_panel_html
|
576 |
-
gr.update(interactive=default_tier_features["export_enabled"],
|
577 |
-
elem_classes="interactive_button_enabled" if default_tier_features["export_enabled"] else ""),
|
578 |
-
None
|
579 |
-
)
|
580 |
-
|
581 |
-
# --- Function to prepare memory data for client-side download ---
|
582 |
-
async def export_user_memory_data_for_download(user_token_str: Optional[str]) -> Optional[str]:
|
583 |
-
try:
|
584 |
-
tier_name = get_user_tier(user_token_str if user_token_str else "")
|
585 |
-
tier_f = TIER_FEATURES.get(tier_name, TIER_FEATURES["free"])
|
586 |
-
|
587 |
-
if not tier_f["export_enabled"]:
|
588 |
-
gr.Warning("🚫 Export Memory is a Pro Tier feature. Please upgrade your plan.")
|
589 |
-
return None
|
590 |
-
|
591 |
-
if not ai_system or not hasattr(ai_system, 'memory_bank'):
|
592 |
-
gr.Error("⚠️ Memory system is not available in the backend.")
|
593 |
-
return None
|
594 |
-
|
595 |
-
memory_data = None
|
596 |
-
if hasattr(ai_system.memory_bank, 'get_all_memories_for_export_async'):
|
597 |
-
logger.info("Using asynchronous get_all_memories_for_export_async.")
|
598 |
-
memory_data = await ai_system.memory_bank.get_all_memories_for_export_async()
|
599 |
-
elif hasattr(ai_system.memory_bank, 'get_all_memories_for_export'):
|
600 |
-
logger.info("Using synchronous get_all_memories_for_export.")
|
601 |
-
memory_data = ai_system.memory_bank.get_all_memories_for_export()
|
602 |
-
else:
|
603 |
-
gr.Error("⚠️ Memory export data retrieval method not available in backend MemoryBank.")
|
604 |
-
return None
|
605 |
-
|
606 |
-
if not memory_data:
|
607 |
-
gr.Info("ℹ️ No memory entries to export.")
|
608 |
-
return None
|
609 |
-
|
610 |
-
json_data = json.dumps(memory_data, indent=2)
|
611 |
-
|
612 |
-
with tempfile.NamedTemporaryFile(delete=False, mode="w", suffix=".json", encoding="utf-8", prefix="zotheos_export_") as tmp_file:
|
613 |
-
tmp_file.write(json_data)
|
614 |
-
tmp_file_path = tmp_file.name
|
615 |
-
|
616 |
-
logger.info(f"✅ Memory data prepared for client download: {tmp_file_path}")
|
617 |
-
gr.Info(f"✅ Memory export ready. Download: {os.path.basename(tmp_file_path)}")
|
618 |
-
return tmp_file_path
|
619 |
-
except Exception as e:
|
620 |
-
logger.error(f"❌ Error preparing memory export for client: {e}", exc_info=True)
|
621 |
-
gr.Error(f"⚠️ Error preparing export: {str(e)}")
|
622 |
-
return None
|
623 |
-
|
624 |
-
# --- Build Gradio Interface ---
|
625 |
-
def build_interface(logo_path_param: Optional[str], favicon_path_param: Optional[str]) -> gr.Blocks:
|
626 |
-
# ✅🧠 DEFINITIVE FIX: Use a true neutral (grayscale) theme as the foundation.
|
627 |
-
theme = gr.themes.Base(
|
628 |
-
primary_hue=gr.themes.colors.neutral, # Changed from "slate"
|
629 |
-
secondary_hue=gr.themes.colors.neutral, # Changed from "gray"
|
630 |
-
neutral_hue=gr.themes.colors.neutral, # Changed from "gray"
|
631 |
-
font=[gr.themes.GoogleFont("Lato"),"ui-sans-serif"],
|
632 |
-
font_mono=[gr.themes.GoogleFont("Inconsolata"),"ui-monospace"]
|
633 |
-
).set(
|
634 |
-
body_background_fill="var(--background-main)",
|
635 |
-
)
|
636 |
-
|
637 |
-
initial_tier_name = get_user_tier("")
|
638 |
-
initial_tier_features = TIER_FEATURES.get(initial_tier_name, TIER_FEATURES["free"])
|
639 |
-
|
640 |
-
with gr.Blocks(theme=theme, css=zotheos_base_css, title=APP_TITLE) as demo:
|
641 |
-
with gr.Column(elem_classes="centered-container"):
|
642 |
-
with gr.Column(elem_id="header_column"):
|
643 |
-
if logo_path_param:
|
644 |
-
gr.Image(value=logo_path_param, elem_id="header_logo", show_label=False, container=False, interactive=False)
|
645 |
-
gr.Markdown("Ethical Fusion AI for Synthesized Intelligence", elem_id="header_subtitle")
|
646 |
-
gr.Markdown("### 💡 Fusing perspectives for deeper truth.", elem_id="welcome_message")
|
647 |
-
|
648 |
-
with gr.Row(elem_id="auth_row", elem_classes="interface-section", equal_height=False):
|
649 |
-
user_token_input = gr.Textbox(label="🔑 Access Token", placeholder="Enter token for tiered features", type="password", elem_id="user_token_input", scale=3, container=False)
|
650 |
-
tier_status_display = gr.Markdown(value=format_tier_status_for_ui(initial_tier_name), elem_id="tier_status_display")
|
651 |
-
|
652 |
-
with gr.Group(elem_classes="interface-section"):
|
653 |
-
query_input_component = gr.Textbox(label="Your Inquiry:", placeholder="e.g., Analyze the ethical implications of AI in art...", lines=6, elem_id="query_input")
|
654 |
-
status_indicator_component = gr.HTML(elem_id="status_indicator", visible=False)
|
655 |
-
with gr.Row(elem_classes="button-row"):
|
656 |
-
submit_button_component = gr.Button("Process Inquiry", elem_id="submit_button", variant="primary", scale=2)
|
657 |
-
clear_button_component = gr.Button("Clear All", elem_id="clear_button", variant="secondary", scale=1)
|
658 |
-
|
659 |
-
with gr.Group(elem_classes="interface-section"):
|
660 |
-
synthesized_summary_component = gr.Markdown(elem_id="synthesized_summary_output", value="Synthesized insight will appear here...", visible=True)
|
661 |
-
fused_response_output_component = gr.Markdown(elem_id="fusion_output", value="Detailed perspectives will appear here...", visible=True)
|
662 |
-
|
663 |
-
with gr.Row(elem_id="pro_features_row", elem_classes="interface-section", visible=True):
|
664 |
-
export_memory_button = gr.Button(
|
665 |
-
"⬇️ Export Memory Log",
|
666 |
-
interactive=initial_tier_features["export_enabled"],
|
667 |
-
elem_classes="interactive_button_enabled"
|
668 |
-
)
|
669 |
-
|
670 |
-
with gr.Accordion("💡 System Status & Active Models", open=False, elem_id="status_accordion"):
|
671 |
-
active_models_display_component = gr.Markdown("**Status:** Initializing... ", elem_id="active_models_info")
|
672 |
-
with gr.Accordion("🧠 Recent Interaction Chronicle (Last 5)", open=False, elem_id="memory_viewer_section"):
|
673 |
-
memory_display_panel_html = gr.HTML(elem_id="memory_display_panel", value="<div class='memory-entry'>No memory entries yet.</div>")
|
674 |
-
|
675 |
-
gr.Markdown(
|
676 |
-
"""---
|
677 |
-
ZOTHEOS © 2025 ZOTHEOS LLC. System Architect: David A. Garcia.
|
678 |
-
Built with open-source models: **Qwen** (Alibaba/Tongyi Qianwen License), **Mistral** (Mistral AI/Apache 2.0), **Gemma** (Google/Gemma T.O.U).
|
679 |
-
Powered by **llama-cpp-python** (MIT) & **Gradio** (Apache 2.0). Please see `LICENSE.txt` for full details.
|
680 |
-
Ethical Fusion AI for Humanity.
|
681 |
-
""", elem_id="footer_attribution"
|
682 |
-
)
|
683 |
-
|
684 |
-
async def process_query_wrapper_internal(query_text_internal: str, user_token_from_ui: Optional[str]) -> Tuple:
|
685 |
-
current_tier_name = get_user_tier(user_token_from_ui if user_token_from_ui else "")
|
686 |
-
tier_display_name = TIER_FEATURES.get(current_tier_name, TIER_FEATURES["free"])["display_name"]
|
687 |
-
loading_html = f"<div class='cosmic-loading'><div class='orbital-spinner'></div><div class='thinking-text'>ZOTHEOS is synthesizing (Tier: {tier_display_name})...</div></div>"
|
688 |
-
yield (
|
689 |
-
gr.update(value=loading_html, visible=True),
|
690 |
-
gr.update(value="Processing...", interactive=False),
|
691 |
-
gr.update(interactive=False),
|
692 |
-
gr.update(),
|
693 |
-
gr.update(value=""),
|
694 |
-
gr.update(value=""),
|
695 |
-
gr.update(),
|
696 |
-
gr.update(value="<div class='memory-entry'>Loading...</div>")
|
697 |
-
)
|
698 |
-
|
699 |
-
if not ai_system or initialization_error:
|
700 |
-
error_msg_html = f"<div style='color: var(--text-color-light); font-weight: bold; padding: 10px; border: 1px solid var(--border-color); background-color: #1a1a1a; border-radius: 6px;'>🚫 Core System Unresponsive. Details: {html.escape(str(initialization_error or 'Unknown Reason'))}. Please check logs or restart.</div>"
|
701 |
-
yield (
|
702 |
-
gr.update(value=error_msg_html,visible=True),
|
703 |
-
gr.update(value="Offline",interactive=False),
|
704 |
-
gr.update(interactive=True),
|
705 |
-
gr.update(),
|
706 |
-
gr.update(value="System error occurred."),
|
707 |
-
gr.update(value="Insight unavailable due to system error."),
|
708 |
-
gr.update(value="**Status:** `Offline - Critical Error`"),
|
709 |
-
gr.update(value="<div class='memory-entry'>Memory unavailable due to system error.</div>")
|
710 |
-
); return
|
711 |
-
query = query_text_internal.strip();
|
712 |
-
if not query:
|
713 |
-
yield (
|
714 |
-
gr.update(value="",visible=False),
|
715 |
-
gr.update(value="🔍 Process",interactive=True),
|
716 |
-
gr.update(interactive=True),
|
717 |
-
gr.update(),
|
718 |
-
gr.update(value="Enter query."),
|
719 |
-
gr.update(value="Insight awaits..."),
|
720 |
-
gr.update(value="**Status:** Awaiting Input"),
|
721 |
-
gr.update(value="<div class='memory-entry'>No query entered.</div>")
|
722 |
-
); return
|
723 |
-
|
724 |
-
fused_perspectives_content = ""; synthesized_summary_content = ""; rendered_memory_html = "<div class='memory-entry'>Memory processing...</div>"
|
725 |
-
try:
|
726 |
-
logger.info(f"UI: Query: '{query[:60]}...' Token: '{user_token_from_ui[:3] if user_token_from_ui else 'N/A'}...'")
|
727 |
-
current_mode_val = DEFAULT_INFERENCE_PRESET_INTERFACE
|
728 |
-
full_response_content = await ai_system.process_query_with_fusion(query, user_token=user_token_from_ui, fusion_mode_override=current_mode_val)
|
729 |
-
|
730 |
-
overall_header_marker = "## 🧠 ZOTHEOS Fused Perspectives 🧠"
|
731 |
-
final_insight_marker = "## ✨ ZOTHEOS Final Synthesized Insight ✨"
|
732 |
-
detailed_perspectives_marker = "### 💬 Detailed Individual Perspectives"
|
733 |
-
|
734 |
-
overall_header = ""
|
735 |
-
content_after_overall_header = full_response_content
|
736 |
-
|
737 |
-
if full_response_content.startswith(overall_header_marker):
|
738 |
-
first_double_newline = full_response_content.find("\n\n")
|
739 |
-
if first_double_newline != -1:
|
740 |
-
overall_header = full_response_content[:first_double_newline + 2]
|
741 |
-
content_after_overall_header = full_response_content[first_double_newline + 2:]
|
742 |
-
else:
|
743 |
-
overall_header = full_response_content
|
744 |
-
content_after_overall_header = ""
|
745 |
-
|
746 |
-
idx_final_insight = content_after_overall_header.find(final_insight_marker)
|
747 |
-
|
748 |
-
if idx_final_insight != -1 :
|
749 |
-
synthesized_summary_content = overall_header + content_after_overall_header[idx_final_insight:]
|
750 |
-
temp_content_before_insight = content_after_overall_header[:idx_final_insight]
|
751 |
-
idx_detailed_perspectives_in_prefix = temp_content_before_insight.rfind(detailed_perspectives_marker)
|
752 |
-
|
753 |
-
if idx_detailed_perspectives_in_prefix != -1:
|
754 |
-
fused_perspectives_content = overall_header + temp_content_before_insight[idx_detailed_perspectives_in_prefix:]
|
755 |
-
else:
|
756 |
-
fused_perspectives_content = overall_header + "Detailed perspectives are integrated within the main insight or not separately provided."
|
757 |
-
elif full_response_content:
|
758 |
-
synthesized_summary_content = full_response_content
|
759 |
-
fused_perspectives_content = "See main insight above for details."
|
760 |
-
else:
|
761 |
-
synthesized_summary_content = overall_header + "No insight generated."
|
762 |
-
fused_perspectives_content = overall_header + "No perspectives generated."
|
763 |
-
|
764 |
-
tier_features_for_query = TIER_FEATURES.get(current_tier_name, TIER_FEATURES["free"])
|
765 |
-
if tier_features_for_query["memory_enabled"]:
|
766 |
-
if ai_system and hasattr(ai_system, 'memory_bank') and ai_system.memory_bank:
|
767 |
-
memory_data_list = []
|
768 |
-
if hasattr(ai_system.memory_bank, 'retrieve_recent_memories_async'):
|
769 |
-
memory_data_list = await ai_system.memory_bank.retrieve_recent_memories_async(limit=10)
|
770 |
-
elif hasattr(ai_system.memory_bank, 'retrieve_recent_memories'):
|
771 |
-
possible_coro = ai_system.memory_bank.retrieve_recent_memories(limit=10)
|
772 |
-
if asyncio.iscoroutine(possible_coro): memory_data_list = await possible_coro
|
773 |
-
else: memory_data_list = possible_coro # type: ignore
|
774 |
-
rendered_memory_html = render_memory_entries_as_html(memory_data_list)
|
775 |
-
else: rendered_memory_html = "<div class='memory-entry'>Memory system error.</div>"
|
776 |
-
else: rendered_memory_html = "<div class='memory-entry'>Memory logging disabled for tier.</div>"
|
777 |
-
except Exception as e:
|
778 |
-
synthesized_summary_content = f"⚠️ Anomaly during processing: {html.escape(type(e).__name__)}. Please check logs."; fused_perspectives_content=""
|
779 |
-
rendered_memory_html = f"<div class='memory-entry'>Error during processing: {html.escape(str(e))}</div>"
|
780 |
-
logger.error(f"❌ Anomaly during query processing: {e}", exc_info=True)
|
781 |
-
|
782 |
-
active_models_md = "**Status:** `Ready`";
|
783 |
-
if ai_system and hasattr(ai_system, 'get_status_report') and callable(ai_system.get_status_report):
|
784 |
-
try:
|
785 |
-
status_report = await ai_system.get_status_report()
|
786 |
-
models_queried_for_this_request = status_report.get("models_last_queried_for_perspectives", [])
|
787 |
-
active_cores_str = ", ".join(models_queried_for_this_request) if models_queried_for_this_request else "N/A"
|
788 |
-
actual_models_used_count_display = len(models_queried_for_this_request)
|
789 |
-
active_models_md = f"**Active Cores (Used for Query):** `{active_cores_str}` ({actual_models_used_count_display} models) | **Status:** `Online`"
|
790 |
-
except Exception as e_status:
|
791 |
-
logger.warning(f"Status report error: {e_status}"); active_models_md = "**Status:** `Report Unavailable`"
|
792 |
-
|
793 |
-
yield (
|
794 |
-
gr.update(value="", visible=False),
|
795 |
-
gr.update(value="Process Inquiry", interactive=True),
|
796 |
-
gr.update(interactive=True),
|
797 |
-
gr.update(),
|
798 |
-
gr.update(value=fused_perspectives_content, visible=True),
|
799 |
-
gr.update(value=synthesized_summary_content, visible=True),
|
800 |
-
gr.update(value=active_models_md),
|
801 |
-
gr.update(value=rendered_memory_html)
|
802 |
-
)
|
803 |
-
|
804 |
-
clear_fn_outputs_ordered = [query_input_component, user_token_input, tier_status_display, fused_response_output_component, synthesized_summary_component, active_models_display_component, memory_display_panel_html, export_memory_button]
|
805 |
-
submit_fn_outputs_ordered = [status_indicator_component, submit_button_component, clear_button_component, query_input_component, fused_response_output_component, synthesized_summary_component, active_models_display_component, memory_display_panel_html]
|
806 |
-
|
807 |
-
submit_button_component.click(fn=process_query_wrapper_internal, inputs=[query_input_component, user_token_input], outputs=submit_fn_outputs_ordered, show_progress="hidden")
|
808 |
-
clear_button_component.click(fn=clear_all_fields, inputs=[], outputs=clear_fn_outputs_ordered, show_progress="hidden")
|
809 |
-
|
810 |
-
token_change_outputs = [tier_status_display, export_memory_button]
|
811 |
-
user_token_input.change(fn=update_tier_display_and_features_ui, inputs=[user_token_input], outputs=token_change_outputs, show_progress="hidden")
|
812 |
-
|
813 |
-
export_memory_button.click(fn=export_user_memory_data_for_download, inputs=[user_token_input], outputs=[])
|
814 |
-
|
815 |
-
return demo
|
816 |
-
|
817 |
-
# --- Main Execution Block (for direct run of this file) ---
|
818 |
-
if __name__ == "__main__":
|
819 |
-
logger.info("--- Initializing ZOTHEOS Gradio Interface (V11 - Direct Run) ---")
|
820 |
-
|
821 |
-
logger.info("Building ZOTHEOS Gradio UI...")
|
822 |
-
zotheos_interface = build_interface(logo_path_verified, favicon_path_verified)
|
823 |
-
logger.info("✅ ZOTHEOS Gradio UI built.")
|
824 |
-
|
825 |
-
logger.info("🚀 Launching ZOTHEOS Gradio application...")
|
826 |
-
print("\n" + "="*60 + f"\n ZOTHEOS Interface ({APP_TITLE} - V11)\n" + "="*60)
|
827 |
-
if initialization_error:
|
828 |
-
print(f"‼️ WARNING: ZOTHEOS AI System failed to initialize fully: {type(initialization_error).__name__} - {initialization_error}")
|
829 |
-
if 'get_user_tier' not in globals() or get_user_tier == get_user_tier_fallback:
|
830 |
-
print(" Additionally, user authentication module might have failed to load. Using fallback tier logic.")
|
831 |
-
elif not ai_system:
|
832 |
-
print(f"‼️ WARNING: ZOTHEOS AI System object (ai_system) is None. Backend will not function.")
|
833 |
-
else:
|
834 |
-
print("✅ ZOTHEOS AI System appears initialized.")
|
835 |
-
|
836 |
-
try:
|
837 |
-
import llama_cpp # type: ignore
|
838 |
-
print("✅ llama-cpp-python library available.")
|
839 |
-
except ImportError:
|
840 |
-
print("\n‼️ CRITICAL WARNING: 'llama-cpp-python' not found! ZOTHEOS backend will fail.\n Install via: pip install llama-cpp-python --force-reinstall --upgrade --no-cache-dir --verbose")
|
841 |
-
|
842 |
-
print("\nZOTHEOS Interface starting. Access via URL below.")
|
843 |
-
print("="*60 + "\n")
|
844 |
-
|
845 |
-
launch_kwargs = {
|
846 |
-
"server_name": "0.0.0.0",
|
847 |
-
"server_port": int(os.getenv("PORT", 7860)),
|
848 |
-
"share": os.getenv("GRADIO_SHARE", "False").lower() == "true",
|
849 |
-
"inbrowser": True,
|
850 |
-
"show_api": False,
|
851 |
-
"favicon_path": favicon_path_verified
|
852 |
-
}
|
853 |
-
|
854 |
-
zotheos_interface.queue().launch(**launch_kwargs)
|
855 |
-
logger.info("--- ZOTHEOS Gradio Interface Shutdown ---")
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import asyncio
|
3 |
+
import logging
|
4 |
+
import os
|
5 |
+
import sys
|
6 |
+
import time
|
7 |
+
import random
|
8 |
+
from typing import Optional, Dict, Any, List, Union, Tuple
|
9 |
+
import html
|
10 |
+
import json
|
11 |
+
import tempfile
|
12 |
+
|
13 |
+
# --- Constants ---
|
14 |
+
APP_TITLE = "ZOTHEOS - Ethical Fusion AI"
|
15 |
+
FAVICON_FILENAME = "favicon_blackBW.ico"
|
16 |
+
LOGO_FILENAME = "zotheos_logo.png"
|
17 |
+
|
18 |
+
# --- Logger (Your existing code is perfect) ---
|
19 |
+
def init_logger():
|
20 |
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
|
21 |
+
logger_instance = logging.getLogger("ZOTHEOS_Interface")
|
22 |
+
if not logger_instance.handlers:
|
23 |
+
handler = logging.StreamHandler(sys.stdout); handler.setFormatter(logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S")); logger_instance.addHandler(handler)
|
24 |
+
return logger_instance
|
25 |
+
logger = init_logger()
|
26 |
+
|
27 |
+
# --- Asset Path Helper (Your existing code is perfect) ---
|
28 |
+
def get_asset_path(filename: str) -> Optional[str]:
|
29 |
+
base_dir = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
|
30 |
+
for path in [os.path.join(base_dir, 'assets', filename), os.path.join(base_dir, filename)]:
|
31 |
+
if os.path.exists(path):
|
32 |
+
logger.info(f"Asset found: '{filename}' -> '{path}'")
|
33 |
+
return path
|
34 |
+
logger.warning(f"Asset NOT FOUND: '{filename}'.")
|
35 |
+
return None
|
36 |
+
|
37 |
+
logo_path_verified = get_asset_path(LOGO_FILENAME)
|
38 |
+
favicon_path_verified = get_asset_path(FAVICON_FILENAME)
|
39 |
+
|
40 |
+
# --- Core Logic Imports (Your existing code is perfect) ---
|
41 |
+
MainFusionPublic = None; initialization_error: Optional[Exception] = None; DEFAULT_INFERENCE_PRESET_INTERFACE = "balanced"; get_user_tier = lambda token: "error (auth module failed)"
|
42 |
+
try:
|
43 |
+
script_dir = os.path.dirname(os.path.abspath(__file__)); project_root = script_dir
|
44 |
+
if project_root not in sys.path: sys.path.insert(0, project_root)
|
45 |
+
from modules.main_fusion_public import MainFusionPublic
|
46 |
+
try: from modules.config_settings_public import DEFAULT_INFERENCE_PRESET as CONFIG_DEFAULT_PRESET; DEFAULT_INFERENCE_PRESET_INTERFACE = CONFIG_DEFAULT_PRESET
|
47 |
+
except ImportError: pass
|
48 |
+
from modules.user_auth import get_user_tier as imported_get_user_tier; get_user_tier = imported_get_user_tier
|
49 |
+
except Exception as e: initialization_error = e
|
50 |
+
|
51 |
+
# --- Initialize AI System (Your existing code is perfect) ---
|
52 |
+
ai_system: Optional[MainFusionPublic] = None
|
53 |
+
if 'MainFusionPublic' in globals() and MainFusionPublic is not None and initialization_error is None:
|
54 |
+
try: ai_system = MainFusionPublic()
|
55 |
+
except Exception as e_init: initialization_error = e_init
|
56 |
+
elif initialization_error is None: initialization_error = ModuleNotFoundError("MainFusionPublic module could not be imported.")
|
57 |
+
|
58 |
+
# --- TIER_FEATURES Dictionary (Your existing code is perfect) ---
|
59 |
+
TIER_FEATURES = {"free": {"display_name": "Free Tier", "memory_enabled": False, "export_enabled": False}, "starter": {"display_name": "Starter Tier", "memory_enabled": True, "export_enabled": False}, "pro": {"display_name": "Pro Tier", "memory_enabled": True, "export_enabled": True}, "error (auth module failed)": {"display_name": "Error", "memory_enabled": False, "export_enabled": False}}
|
60 |
+
|
61 |
+
#
|
62 |
+
# --- JESSICA WALSH INSPIRED REDESIGN ---
|
63 |
+
#
|
64 |
+
zotheos_base_css = """
|
65 |
+
@import url('https://fonts.googleapis.com/css2?family=Bebas+Neue&family=Inter:wght@400;600;700&display=swap');
|
66 |
+
|
67 |
+
:root {
|
68 |
+
--bg-main: #050505;
|
69 |
+
--bg-surface: #121212;
|
70 |
+
--bg-surface-raised: #1A1A1A;
|
71 |
+
--text-primary: #EAEAEA;
|
72 |
+
--text-secondary: #888888;
|
73 |
+
--border-subtle: #2A2A2A;
|
74 |
+
--accent-glow: #00BFFF; /* Electric "Truth" Blue */
|
75 |
+
--accent-glow-darker: #009ACD;
|
76 |
+
|
77 |
+
--font-display: 'Bebas Neue', 'Staatliches', cursive;
|
78 |
+
--font-body: 'Inter', sans-serif;
|
79 |
+
|
80 |
+
--radius-md: 12px;
|
81 |
+
--radius-sm: 8px;
|
82 |
+
--shadow-glow: 0 0 15px rgba(0, 191, 255, 0.2);
|
83 |
+
}
|
84 |
+
|
85 |
+
/* --- Base & Reset --- */
|
86 |
+
footer, canvas { display: none !important; }
|
87 |
+
html, body {
|
88 |
+
background-color: var(--bg-main) !important;
|
89 |
+
font-family: var(--font-body);
|
90 |
+
color: var(--text-primary);
|
91 |
+
line-height: 1.6;
|
92 |
+
scroll-behavior: smooth;
|
93 |
+
}
|
94 |
+
.gradio-container {
|
95 |
+
max-width: 800px !important;
|
96 |
+
margin: 0 auto !important;
|
97 |
+
padding: 2rem 1.5rem !important;
|
98 |
+
background-color: transparent !important;
|
99 |
+
}
|
100 |
+
|
101 |
+
/* --- Typographic Hierarchy --- */
|
102 |
+
h1, h2, h3, #header_subtitle, #welcome_message {
|
103 |
+
font-family: var(--font-display) !important;
|
104 |
+
text-transform: uppercase;
|
105 |
+
letter-spacing: 1.5px;
|
106 |
+
color: var(--text-primary);
|
107 |
+
text-align: center;
|
108 |
+
}
|
109 |
+
|
110 |
+
/* --- Header & Branding --- */
|
111 |
+
#header_column { margin-bottom: 2rem; }
|
112 |
+
#header_logo img {
|
113 |
+
max-height: 70px;
|
114 |
+
filter: brightness(0) invert(1); /* Force pure white logo */
|
115 |
+
}
|
116 |
+
#header_subtitle { font-size: 1.2rem !important; color: var(--text-secondary); }
|
117 |
+
#welcome_message {
|
118 |
+
font-size: 1.5rem !important;
|
119 |
+
color: var(--text-primary);
|
120 |
+
margin-bottom: 2.5rem;
|
121 |
+
}
|
122 |
+
|
123 |
+
/* --- Interactive Elements & Input Areas --- */
|
124 |
+
.gradio-group {
|
125 |
+
background: transparent !important;
|
126 |
+
border: none !important;
|
127 |
+
padding: 0 !important;
|
128 |
+
}
|
129 |
+
.gradio-textbox textarea, .gradio-textbox input {
|
130 |
+
background-color: var(--bg-surface) !important;
|
131 |
+
color: var(--text-primary) !important;
|
132 |
+
border: 1px solid var(--border-subtle) !important;
|
133 |
+
border-radius: var(--radius-md) !important;
|
134 |
+
padding: 1rem !important;
|
135 |
+
transition: border-color 0.3s, box-shadow 0.3s;
|
136 |
+
}
|
137 |
+
.gradio-textbox textarea:focus, .gradio-textbox input:focus {
|
138 |
+
border-color: var(--accent-glow) !important;
|
139 |
+
box-shadow: var(--shadow-glow) !important;
|
140 |
+
}
|
141 |
+
#query_input textarea { min-height: 180px !important; }
|
142 |
+
|
143 |
+
/* --- Buttons: The Core Interaction --- */
|
144 |
+
.gradio-button {
|
145 |
+
border-radius: var(--radius-md) !important;
|
146 |
+
transition: all 0.2s ease-in-out;
|
147 |
+
border: none !important;
|
148 |
+
}
|
149 |
+
#submit_button {
|
150 |
+
background: var(--accent-glow) !important;
|
151 |
+
color: var(--bg-main) !important;
|
152 |
+
font-weight: 700 !important;
|
153 |
+
text-transform: uppercase;
|
154 |
+
}
|
155 |
+
#submit_button:hover {
|
156 |
+
background: var(--text-primary) !important;
|
157 |
+
transform: translateY(-2px);
|
158 |
+
box-shadow: 0 4px 20px rgba(0,0,0,0.2);
|
159 |
+
}
|
160 |
+
#clear_button {
|
161 |
+
background: var(--bg-surface-raised) !important;
|
162 |
+
color: var(--text-secondary) !important;
|
163 |
+
}
|
164 |
+
#clear_button:hover {
|
165 |
+
background: var(--border-subtle) !important;
|
166 |
+
color: var(--text-primary) !important;
|
167 |
+
}
|
168 |
+
#export_memory_button {
|
169 |
+
background: transparent !important;
|
170 |
+
color: var(--text-secondary) !important;
|
171 |
+
border: 1px solid var(--border-subtle) !important;
|
172 |
+
}
|
173 |
+
#export_memory_button:hover {
|
174 |
+
color: var(--text-primary) !important;
|
175 |
+
border-color: var(--accent-glow) !important;
|
176 |
+
}
|
177 |
+
|
178 |
+
/* --- Output & Information Display --- */
|
179 |
+
#synthesized_summary_output, #fusion_output {
|
180 |
+
background-color: var(--bg-surface) !important;
|
181 |
+
border: 1px solid var(--border-subtle) !important;
|
182 |
+
border-radius: var(--radius-md) !important;
|
183 |
+
padding: 1.5rem !important;
|
184 |
+
margin-top: 1rem;
|
185 |
+
}
|
186 |
+
#synthesized_summary_output h2, #fusion_output h2 { font-size: 1.2rem; color: var(--accent-glow); }
|
187 |
+
|
188 |
+
/* --- Accordions & Tier Status --- */
|
189 |
+
.gradio-accordion { border: none !important; }
|
190 |
+
.gradio-accordion > .label-wrap {
|
191 |
+
background: var(--bg-surface-raised) !important;
|
192 |
+
border-radius: var(--radius-sm) !important;
|
193 |
+
padding: 0.75rem 1rem !important;
|
194 |
+
color: var(--text-secondary) !important;
|
195 |
+
}
|
196 |
+
#tier_status_display { text-align: right !important; }
|
197 |
+
#tier_status_display small { color: var(--text-secondary) !important; }
|
198 |
+
|
199 |
+
/* --- Loading Indicator --- */
|
200 |
+
.cosmic-loading { padding: 3rem; }
|
201 |
+
.orbital-spinner {
|
202 |
+
border-color: var(--border-subtle);
|
203 |
+
border-top-color: var(--accent-glow);
|
204 |
+
}
|
205 |
+
.thinking-text { font-family: var(--font-body); color: var(--text-secondary); }
|
206 |
+
|
207 |
+
/* --- Footer --- */
|
208 |
+
#footer_attribution {
|
209 |
+
font-size: 0.8rem;
|
210 |
+
color: #555;
|
211 |
+
text-align: center;
|
212 |
+
margin-top: 4rem;
|
213 |
+
padding-bottom: 1rem;
|
214 |
+
line-height: 1.5;
|
215 |
+
}
|
216 |
+
#footer_attribution strong { color: #777; font-weight: 600; }
|
217 |
+
"""
|
218 |
+
|
219 |
+
# --- Helper Functions (Your existing code is perfect) ---
|
220 |
+
def render_memory_entries_as_html(memory_entries: List[dict]) -> str:
|
221 |
+
# This function is fine as-is. The new CSS will style the output.
|
222 |
+
if not memory_entries: return "<div class='memory-entry'>No stored memory entries or memory disabled.</div>"
|
223 |
+
html_blocks = [];
|
224 |
+
for entry in reversed(memory_entries[-5:]):
|
225 |
+
query = html.escape(entry.get("query", "N/A")); ts_iso = entry.get("metadata", {}).get("timestamp_iso", "Unknown");
|
226 |
+
try: dt_obj = time.strptime(ts_iso, '%Y-%m-%dT%H:%M:%SZ'); formatted_ts = time.strftime('%Y-%m-%d %H:%M UTC', dt_obj)
|
227 |
+
except: formatted_ts = html.escape(ts_iso)
|
228 |
+
models_list = entry.get("metadata", {}).get("active_models_queried", []); models_str = html.escape(", ".join(models_list)) if models_list else "N/A"
|
229 |
+
summary_raw = entry.get("metadata", {}).get("synthesized_summary_text", "No summary."); summary = html.escape(summary_raw[:300]) + ("..." if len(summary_raw) > 300 else "")
|
230 |
+
html_blocks.append(f"<div class='memory-entry'><p><strong>🕒 TS:</strong> {formatted_ts}</p><p><strong>🧠 Models:</strong> {models_str}</p><div><strong>❓ Q:</strong><div class='query-text'>{query}</div></div><div><strong>📌 Sum:</strong><div class='summary-text'>{summary}</div></div></div>")
|
231 |
+
return "\n".join(html_blocks)
|
232 |
+
|
233 |
+
def format_tier_status_for_ui(tier_name: str) -> str:
|
234 |
+
features = TIER_FEATURES.get(tier_name, TIER_FEATURES["error (auth module failed)"])
|
235 |
+
return f"<small>Tier: **{features['display_name']}** | Memory: {'✅' if features['memory_enabled'] else '❌'} | Export: {'✅' if features['export_enabled'] else '❌'}</small>"
|
236 |
+
|
237 |
+
def update_tier_display_and_features_ui(user_token_str: Optional[str]) -> Tuple:
|
238 |
+
tier_name = get_user_tier(user_token_str or "")
|
239 |
+
tier_features = TIER_FEATURES.get(tier_name, TIER_FEATURES["free"])
|
240 |
+
return (gr.update(value=format_tier_status_for_ui(tier_name)), gr.update(interactive=tier_features["export_enabled"]))
|
241 |
+
|
242 |
+
# --- Build Gradio Interface (Main Structure is Yours, Components are Styled) ---
|
243 |
+
def build_interface(logo_path_param: Optional[str], favicon_path_param: Optional[str]) -> gr.Blocks:
|
244 |
+
theme = gr.themes.Base(font=[gr.themes.GoogleFont("Inter"), "sans-serif"])
|
245 |
+
initial_tier_name = get_user_tier("")
|
246 |
+
initial_tier_features = TIER_FEATURES.get(initial_tier_name, TIER_FEATURES["free"])
|
247 |
+
|
248 |
+
with gr.Blocks(theme=theme, css=zotheos_base_css, title=APP_TITLE) as demo:
|
249 |
+
with gr.Column(elem_classes="centered-container"):
|
250 |
+
# --- Header ---
|
251 |
+
with gr.Column(elem_id="header_column"):
|
252 |
+
if logo_path_param:
|
253 |
+
gr.Image(value=logo_path_param, elem_id="header_logo", show_label=False, container=False, interactive=False)
|
254 |
+
gr.Markdown("Ethical Fusion AI for Synthesized Intelligence", elem_id="header_subtitle")
|
255 |
+
|
256 |
+
gr.Markdown("Fusing perspectives for deeper truth.", elem_id="welcome_message")
|
257 |
+
|
258 |
+
# --- Authentication & Tier Status ---
|
259 |
+
with gr.Row(equal_height=False):
|
260 |
+
user_token_input = gr.Textbox(label="Access Token", placeholder="Enter token...", type="password", scale=3, container=False)
|
261 |
+
tier_status_display = gr.Markdown(value=format_tier_status_for_ui(initial_tier_name), elem_id="tier_status_display", scale=2)
|
262 |
+
|
263 |
+
# --- Core Input ---
|
264 |
+
with gr.Group():
|
265 |
+
query_input_component = gr.Textbox(label="Your Inquiry:", placeholder="e.g., Analyze the ethical implications of AI in art...", lines=6, elem_id="query_input")
|
266 |
+
status_indicator_component = gr.HTML(elem_id="status_indicator", visible=False)
|
267 |
+
with gr.Row():
|
268 |
+
clear_button_component = gr.Button("Clear", elem_id="clear_button", variant="secondary")
|
269 |
+
submit_button_component = gr.Button("Process Inquiry", elem_id="submit_button", variant="primary", scale=3)
|
270 |
+
|
271 |
+
# --- Main Outputs ---
|
272 |
+
with gr.Group():
|
273 |
+
synthesized_summary_component = gr.Markdown(elem_id="synthesized_summary_output", value="Synthesized insight will appear here...", visible=True)
|
274 |
+
fused_response_output_component = gr.Markdown(elem_id="fusion_output", value="Detailed perspectives will appear here...", visible=True)
|
275 |
+
|
276 |
+
# --- Tools & Info ---
|
277 |
+
export_memory_button = gr.Button("Export Memory Log", elem_id="export_memory_button", interactive=initial_tier_features["export_enabled"])
|
278 |
+
with gr.Accordion("System Status & Active Models", open=False):
|
279 |
+
active_models_display_component = gr.Markdown("**Status:** Initializing...")
|
280 |
+
with gr.Accordion("Recent Interaction Chronicle", open=False):
|
281 |
+
memory_display_panel_html = gr.HTML("<div class='memory-entry'>No memory entries yet.</div>")
|
282 |
+
|
283 |
+
# --- Footer ---
|
284 |
+
gr.Markdown("---", elem_id="footer_separator")
|
285 |
+
gr.Markdown("ZOTHEOS © 2025 ZOTHEOS LLC. System Architect: David A. Garcia. All Rights Reserved.", elem_id="footer_attribution")
|
286 |
+
|
287 |
+
# --- Backend Logic & Event Handlers (Your existing code is perfect) ---
|
288 |
+
async def process_query_wrapper_internal(query, token):
|
289 |
+
tier_name = get_user_tier(token or ""); display_name = TIER_FEATURES.get(tier_name, {})["display_name"]
|
290 |
+
loading_html = f"<div class='cosmic-loading'><div class='orbital-spinner'></div><div class='thinking-text'>Synthesizing (Tier: {display_name})...</div></div>"
|
291 |
+
yield (gr.update(value=loading_html, visible=True), gr.update(interactive=False), gr.update(interactive=False), "", "", "", "Loading...")
|
292 |
+
if not ai_system:
|
293 |
+
yield (gr.update(value="<p style='color:red;'>SYSTEM OFFLINE</p>", visible=True), gr.update(interactive=True), gr.update(interactive=True), "Error", "Error", "Offline", "Error")
|
294 |
+
return
|
295 |
+
response = await ai_system.process_query_with_fusion(query, user_token=token, fusion_mode_override=DEFAULT_INFERENCE_PRESET_INTERFACE)
|
296 |
+
# Simple response parsing for demo
|
297 |
+
summary = response[:response.find("###")] if "###" in response else response
|
298 |
+
perspectives = response[response.find("###"):] if "###" in response else "Details integrated in summary."
|
299 |
+
mem_data = await ai_system.memory_bank.retrieve_recent_memories_async(limit=5) if TIER_FEATURES[tier_name]['memory_enabled'] and hasattr(ai_system, 'memory_bank') else []
|
300 |
+
mem_html = render_memory_entries_as_html(mem_data)
|
301 |
+
status_report = await ai_system.get_status_report()
|
302 |
+
status_md = f"**Last Used:** {', '.join(status_report.get('models_last_queried_for_perspectives', ['N/A']))}"
|
303 |
+
yield (gr.update(visible=False), gr.update(interactive=True), gr.update(interactive=True), summary, perspectives, status_md, mem_html)
|
304 |
+
|
305 |
+
submit_button_component.click(fn=process_query_wrapper_internal, inputs=[query_input_component, user_token_input], outputs=[status_indicator_component, submit_button_component, clear_button_component, synthesized_summary_component, fused_response_output_component, active_models_display_component, memory_display_panel_html], show_progress="hidden")
|
306 |
+
clear_button_component.click(lambda: ("", "", "", "Synthesized insight...", "Detailed perspectives..."), outputs=[query_input_component, user_token_input, synthesized_summary_component, fused_response_output_component])
|
307 |
+
user_token_input.change(fn=update_tier_display_and_features_ui, inputs=[user_token_input], outputs=[tier_status_display, export_memory_button])
|
308 |
+
export_memory_button.click(fn=export_user_memory_data_for_download, inputs=[user_token_input], outputs=[])
|
309 |
+
|
310 |
+
return demo
|
311 |
+
|
312 |
+
# --- Main Execution Block (Your existing code is perfect) ---
|
313 |
+
if __name__ == "__main__":
|
314 |
+
logger.info("--- Initializing ZOTHEOS Gradio Interface (V12 - Walsh Redesign) ---")
|
315 |
+
zotheos_interface = build_interface(logo_path_verified, favicon_path_verified)
|
316 |
+
logger.info("✅ ZOTHEOS Gradio UI built.")
|
317 |
+
launch_kwargs = {"server_name": "0.0.0.0", "server_port": int(os.getenv("PORT", 7860)), "share": os.getenv("GRADIO_SHARE", "False").lower() == "true"}
|
318 |
+
zotheos_interface.queue().launch(**launch_kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|