Update app.py
Browse files
app.py
CHANGED
|
@@ -1456,18 +1456,11 @@ class Hive:
|
|
| 1456 |
def chat(self, message:str, effective_role:str, caller_id: Optional[str],
|
| 1457 |
k:int=None, max_new_tokens:int=256, temperature:float=None, prompt_override: Optional[str] = None) -> str: # type: ignore
|
| 1458 |
temp = temperature if temperature is not None else (self.decoding_temperature if not self.lite_mode else 0.7)
|
| 1459 |
-
|
| 1460 |
-
user_obj, _ = _find_user(_load_users(), caller_id)
|
| 1461 |
-
user_prefs = (user_obj.get("prefs", {}) or {}) if user_obj else {}
|
| 1462 |
-
user_lang = user_prefs.get("language", "en")
|
| 1463 |
-
phonics_on = user_prefs.get("phonics_on", False)
|
| 1464 |
-
|
| 1465 |
-
final_message, intent = self._prepare_chat_input(message, user_lang, phonics_on, prompt_override) # type: ignore
|
| 1466 |
|
| 1467 |
if self.lite_mode:
|
| 1468 |
-
prompt = f"
|
| 1469 |
full_reply = "".join(list(self.chat_stream(prompt, max_new_tokens, temp)))
|
| 1470 |
-
return full_reply
|
| 1471 |
|
| 1472 |
kk = k if k is not None else (self.retrieval_k if hasattr(self, 'retrieval_k') else 6)
|
| 1473 |
snippets = self._get_retrieval_context(message, effective_role, caller_id, kk) # type: ignore
|
|
@@ -1520,6 +1513,7 @@ def launch_ui(bootstrap_instance: "Bootstrap"):
|
|
| 1520 |
chatbot = gr.Chatbot(height=600, type="messages", label="Chat", placeholder="Initializing...")
|
| 1521 |
msg = gr.Textbox(placeholder="Please wait for the model to load...", interactive=False, show_label=False, container=False, scale=4)
|
| 1522 |
|
|
|
|
| 1523 |
with gr.Column(scale=1, min_width=300):
|
| 1524 |
with gr.Sidebar():
|
| 1525 |
uid_state=gr.State(None); role_state=gr.State("guest"); mode_state=gr.State("user"); phonics_state=gr.State(False) # type: ignore
|
|
@@ -1734,12 +1728,19 @@ def launch_ui(bootstrap_instance: "Bootstrap"):
|
|
| 1734 |
gr.Button(interactive=True), # apply_btn
|
| 1735 |
)
|
| 1736 |
demo.load(wait_for_memory_features, None, [core_status, summary_output, msg, summary_btn, vocab_output, vocab_btn, progress_output, online_now, ingest_now_btn, mem_compress_btn, hotpatch_apply, propose_btn, test_btn, apply_btn, network_status_md])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1737 |
vocab_btn.click(do_get_vocab_word, [uid_state], [vocab_output]) # type: ignore
|
| 1738 |
|
| 1739 |
|
| 1740 |
def wait_for_voice_features(request: gr.Request):
|
| 1741 |
"""Waits for ASR/TTS models and enables voice-related UI elements."""
|
| 1742 |
bootstrap_instance.voice_ready.wait()
|
|
|
|
| 1743 |
hive_instance = get_hive_instance() # type: ignore
|
| 1744 |
|
| 1745 |
voice_ready = not hive_instance.lite_mode and hasattr(hive_instance, 'asr_service') and hasattr(hive_instance, 'tts_service')
|
|
@@ -2038,6 +2039,7 @@ class Bootstrap:
|
|
| 2038 |
self.hive_instance: Optional[Hive] = None
|
| 2039 |
self.hive_lite_instance: Optional[Hive] = None
|
| 2040 |
self.hive_ready = threading.Event()
|
|
|
|
| 2041 |
self.voice_ready = threading.Event()
|
| 2042 |
self.env: Optional[Dict] = None
|
| 2043 |
self.app: Optional[gr.Blocks] = None # type: ignore
|
|
@@ -2077,27 +2079,34 @@ class Bootstrap:
|
|
| 2077 |
print("[Bootstrap] Low memory detected, enabling ultra-constrained mode.")
|
| 2078 |
self.config["CTX_TOKENS"] = min(self.config.get("CTX_TOKENS", 2048), 1024)
|
| 2079 |
|
| 2080 |
-
# --- Lite Core Initialization (Fast Path) ---
|
| 2081 |
self._run_task("lite_core_init", self._init_lite_core)
|
| 2082 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2083 |
def full_init_task():
|
| 2084 |
"""Initializes the full Hive instance."""
|
| 2085 |
-
|
| 2086 |
-
self.hive_instance =
|
| 2087 |
self.hive_ready.set()
|
| 2088 |
|
| 2089 |
def self_optimize_task():
|
| 2090 |
"""Kicks off the self-optimization process after full initialization."""
|
| 2091 |
self.hive_ready.wait() # Wait for the full hive to be ready
|
| 2092 |
if self.hive_instance and hasattr(self.hive_instance, 'selfopt'):
|
| 2093 |
-
print("[Bootstrap] Triggering initial self-optimization cycle.")
|
| 2094 |
self.hive_instance.selfopt.trigger_once()
|
| 2095 |
|
| 2096 |
def voice_init_task():
|
| 2097 |
"""Initializes voice models in a separate thread."""
|
| 2098 |
-
|
| 2099 |
-
|
| 2100 |
-
executor.submit(get_tts, CFG["TTS_LANG"])
|
| 2101 |
self.voice_ready.set()
|
| 2102 |
|
| 2103 |
# --- Launch Background Initialization Tasks ---
|
|
@@ -2107,7 +2116,7 @@ class Bootstrap:
|
|
| 2107 |
"full_core_init": (self._run_task, "full_core_init", full_init_task),
|
| 2108 |
"self_optimize": (self._run_task, "self_optimize", self_optimize_task),
|
| 2109 |
}
|
| 2110 |
-
for name, (runner, task_name, target) in tasks.items():
|
| 2111 |
threading.Thread(target=runner, args=(task_name, target), daemon=True).start()
|
| 2112 |
|
| 2113 |
import signal
|
|
@@ -2122,6 +2131,7 @@ class Bootstrap:
|
|
| 2122 |
Hive.bootstrap_instance = self
|
| 2123 |
self.hive_lite_instance = Hive(lite=True, caps=self.caps)
|
| 2124 |
self.video_service = VideoService() # type: ignore
|
|
|
|
| 2125 |
self.video_service.start()
|
| 2126 |
|
| 2127 |
def soft_restart(self):
|
|
|
|
| 1456 |
def chat(self, message:str, effective_role:str, caller_id: Optional[str],
|
| 1457 |
k:int=None, max_new_tokens:int=256, temperature:float=None, prompt_override: Optional[str] = None) -> str: # type: ignore
|
| 1458 |
temp = temperature if temperature is not None else (self.decoding_temperature if not self.lite_mode else 0.7)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1459 |
|
| 1460 |
if self.lite_mode:
|
| 1461 |
+
prompt = f"<|user|>\n{message}</s>\n<|assistant|>\n"
|
| 1462 |
full_reply = "".join(list(self.chat_stream(prompt, max_new_tokens, temp)))
|
| 1463 |
+
return full_reply
|
| 1464 |
|
| 1465 |
kk = k if k is not None else (self.retrieval_k if hasattr(self, 'retrieval_k') else 6)
|
| 1466 |
snippets = self._get_retrieval_context(message, effective_role, caller_id, kk) # type: ignore
|
|
|
|
| 1513 |
chatbot = gr.Chatbot(height=600, type="messages", label="Chat", placeholder="Initializing...")
|
| 1514 |
msg = gr.Textbox(placeholder="Please wait for the model to load...", interactive=False, show_label=False, container=False, scale=4)
|
| 1515 |
|
| 1516 |
+
|
| 1517 |
with gr.Column(scale=1, min_width=300):
|
| 1518 |
with gr.Sidebar():
|
| 1519 |
uid_state=gr.State(None); role_state=gr.State("guest"); mode_state=gr.State("user"); phonics_state=gr.State(False) # type: ignore
|
|
|
|
| 1728 |
gr.Button(interactive=True), # apply_btn
|
| 1729 |
)
|
| 1730 |
demo.load(wait_for_memory_features, None, [core_status, summary_output, msg, summary_btn, vocab_output, vocab_btn, progress_output, online_now, ingest_now_btn, mem_compress_btn, hotpatch_apply, propose_btn, test_btn, apply_btn, network_status_md])
|
| 1731 |
+
def wait_for_lite_core():
|
| 1732 |
+
"""Waits for the lite Hive core and enables basic chat."""
|
| 1733 |
+
bootstrap_instance.lite_core_ready.wait()
|
| 1734 |
+
return gr.Textbox(placeholder=f"Talk to {CFG['AGENT_NAME']} (Lite Mode)", interactive=True)
|
| 1735 |
+
|
| 1736 |
+
demo.load(wait_for_lite_core, None, [msg])
|
| 1737 |
vocab_btn.click(do_get_vocab_word, [uid_state], [vocab_output]) # type: ignore
|
| 1738 |
|
| 1739 |
|
| 1740 |
def wait_for_voice_features(request: gr.Request):
|
| 1741 |
"""Waits for ASR/TTS models and enables voice-related UI elements."""
|
| 1742 |
bootstrap_instance.voice_ready.wait()
|
| 1743 |
+
bootstrap_instance.hive_ready.wait() # Also wait for full core for voice features
|
| 1744 |
hive_instance = get_hive_instance() # type: ignore
|
| 1745 |
|
| 1746 |
voice_ready = not hive_instance.lite_mode and hasattr(hive_instance, 'asr_service') and hasattr(hive_instance, 'tts_service')
|
|
|
|
| 2039 |
self.hive_instance: Optional[Hive] = None
|
| 2040 |
self.hive_lite_instance: Optional[Hive] = None
|
| 2041 |
self.hive_ready = threading.Event()
|
| 2042 |
+
self.lite_core_ready = threading.Event()
|
| 2043 |
self.voice_ready = threading.Event()
|
| 2044 |
self.env: Optional[Dict] = None
|
| 2045 |
self.app: Optional[gr.Blocks] = None # type: ignore
|
|
|
|
| 2079 |
print("[Bootstrap] Low memory detected, enabling ultra-constrained mode.")
|
| 2080 |
self.config["CTX_TOKENS"] = min(self.config.get("CTX_TOKENS", 2048), 1024)
|
| 2081 |
|
|
|
|
| 2082 |
self._run_task("lite_core_init", self._init_lite_core)
|
| 2083 |
|
| 2084 |
+
# --- Pre-launch heavy model loading in parallel ---
|
| 2085 |
+
llm_thread = threading.Thread(target=lambda: get_hive_instance(lite=False, caps=self.caps), daemon=True)
|
| 2086 |
+
asr_thread = threading.Thread(target=get_asr, daemon=True)
|
| 2087 |
+
tts_thread = threading.Thread(target=lambda: get_tts(CFG["TTS_LANG"]), daemon=True)
|
| 2088 |
+
|
| 2089 |
+
llm_thread.start()
|
| 2090 |
+
asr_thread.start()
|
| 2091 |
+
tts_thread.start()
|
| 2092 |
+
|
| 2093 |
def full_init_task():
|
| 2094 |
"""Initializes the full Hive instance."""
|
| 2095 |
+
llm_thread.join() # Wait for the LLM to be loaded
|
| 2096 |
+
self.hive_instance = get_hive_instance(lite=False) # Retrieve the pre-loaded instance
|
| 2097 |
self.hive_ready.set()
|
| 2098 |
|
| 2099 |
def self_optimize_task():
|
| 2100 |
"""Kicks off the self-optimization process after full initialization."""
|
| 2101 |
self.hive_ready.wait() # Wait for the full hive to be ready
|
| 2102 |
if self.hive_instance and hasattr(self.hive_instance, 'selfopt'):
|
| 2103 |
+
print("[Bootstrap] Triggering initial self-optimization cycle.")
|
| 2104 |
self.hive_instance.selfopt.trigger_once()
|
| 2105 |
|
| 2106 |
def voice_init_task():
|
| 2107 |
"""Initializes voice models in a separate thread."""
|
| 2108 |
+
asr_thread.join()
|
| 2109 |
+
tts_thread.join()
|
|
|
|
| 2110 |
self.voice_ready.set()
|
| 2111 |
|
| 2112 |
# --- Launch Background Initialization Tasks ---
|
|
|
|
| 2116 |
"full_core_init": (self._run_task, "full_core_init", full_init_task),
|
| 2117 |
"self_optimize": (self._run_task, "self_optimize", self_optimize_task),
|
| 2118 |
}
|
| 2119 |
+
for name, (runner, task_name, target) in tasks.items():
|
| 2120 |
threading.Thread(target=runner, args=(task_name, target), daemon=True).start()
|
| 2121 |
|
| 2122 |
import signal
|
|
|
|
| 2131 |
Hive.bootstrap_instance = self
|
| 2132 |
self.hive_lite_instance = Hive(lite=True, caps=self.caps)
|
| 2133 |
self.video_service = VideoService() # type: ignore
|
| 2134 |
+
self.lite_core_ready.set()
|
| 2135 |
self.video_service.start()
|
| 2136 |
|
| 2137 |
def soft_restart(self):
|