Spaces:
Running
Running
Create brain_lazy.py
Browse files- brain_lazy.py +57 -0
brain_lazy.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
brain_lazy.py
|
3 |
+
Lazy loader for multimodular_modul_v7
|
4 |
+
- Avoids Hugging Face runtime startup timeout
|
5 |
+
- Loads brain in background while API/CLI starts instantly
|
6 |
+
"""
|
7 |
+
|
8 |
+
import os
|
9 |
+
import importlib
|
10 |
+
import threading
|
11 |
+
import time
|
12 |
+
|
13 |
+
# ------------------------------
|
14 |
+
# Persistent storage cache setup
|
15 |
+
# ------------------------------
|
16 |
+
os.environ["TRANSFORMERS_CACHE"] = "/home/user/app/cache"
|
17 |
+
os.environ["HF_HOME"] = "/home/user/app/cache"
|
18 |
+
os.makedirs("/home/user/app/cache", exist_ok=True)
|
19 |
+
|
20 |
+
# ------------------------------
|
21 |
+
# Brain loader
|
22 |
+
# ------------------------------
|
23 |
+
_brain = None
|
24 |
+
_is_loading = False
|
25 |
+
_is_ready = False
|
26 |
+
_lock = threading.Lock()
|
27 |
+
|
28 |
+
def _load_brain():
|
29 |
+
global _brain, _is_ready, _is_loading
|
30 |
+
with _lock:
|
31 |
+
if _brain is None:
|
32 |
+
_is_loading = True
|
33 |
+
print("⏳ Loading multimodular brain (lazy mode)...")
|
34 |
+
start_time = time.time()
|
35 |
+
_brain = importlib.import_module("multimodular_modul_v7")
|
36 |
+
_is_ready = True
|
37 |
+
_is_loading = False
|
38 |
+
print(f"✅ Brain loaded in {time.time() - start_time:.2f} seconds.")
|
39 |
+
return _brain
|
40 |
+
|
41 |
+
def preload_in_background():
|
42 |
+
threading.Thread(target=_load_brain, daemon=True).start()
|
43 |
+
|
44 |
+
# Start background preload at import
|
45 |
+
preload_in_background()
|
46 |
+
|
47 |
+
# ------------------------------
|
48 |
+
# Proxy functions
|
49 |
+
# ------------------------------
|
50 |
+
def process_input(text): return _load_brain().process_input(text)
|
51 |
+
def search_kb(query): return _load_brain().search_kb(query)
|
52 |
+
def upload_media(path): return _load_brain().upload_media(path)
|
53 |
+
def backup_brain(): return _load_brain().backup_brain()
|
54 |
+
def restore_brain(): return _load_brain().restore_brain()
|
55 |
+
def show_creative_skills(): return _load_brain().show_creative_skills()
|
56 |
+
def sync_status(): return _load_brain().sync_status()
|
57 |
+
def is_ready(): return _is_ready
|