Spaces:
Running
Running
""" | |
brain_fast_lazy.py | |
Combines: | |
- Lazy loading (bypass Hugging Face timeout) | |
- Speed optimizations (fast replies after load) | |
""" | |
import os | |
import time | |
import threading | |
import functools | |
from collections import OrderedDict | |
import importlib | |
# ------------------------------ | |
# Persistent cache setup | |
# ------------------------------ | |
os.environ["TRANSFORMERS_CACHE"] = "/home/user/app/cache" | |
os.environ["HF_HOME"] = "/home/user/app/cache" | |
os.makedirs("/home/user/app/cache", exist_ok=True) | |
# ------------------------------ | |
# Cache for replies | |
# ------------------------------ | |
CACHE_LIMIT = 50 | |
_response_cache = OrderedDict() | |
def _cache_result(func): | |
def wrapper(*args, **kwargs): | |
key = str((args, tuple(sorted(kwargs.items())))) | |
if key in _response_cache: | |
_response_cache.move_to_end(key) | |
return _response_cache[key] | |
result = func(*args, **kwargs) | |
_response_cache[key] = result | |
if len(_response_cache) > CACHE_LIMIT: | |
_response_cache.popitem(last=False) | |
return result | |
return wrapper | |
# ------------------------------ | |
# Brain loader + warm-up | |
# ------------------------------ | |
_brain = None | |
_is_loading = False | |
_is_ready = False | |
_lock = threading.Lock() | |
def _load_brain(): | |
global _brain, _is_ready, _is_loading | |
with _lock: | |
if _brain is None: | |
_is_loading = True | |
print("β³ Loading multimodular brain (fast-lazy mode)...") | |
start_time = time.time() | |
_brain = importlib.import_module("multimodular_modul_v7") | |
_is_ready = True | |
_is_loading = False | |
print(f"β Brain loaded in {time.time() - start_time:.2f}s") | |
_warm_up() | |
return _brain | |
def _warm_up(): | |
try: | |
print("π₯ Warming up models...") | |
_brain.process_input("Hello") | |
print("β Warm-up complete") | |
except Exception as e: | |
print(f"β Warm-up failed: {e}") | |
def preload_in_background(): | |
threading.Thread(target=_load_brain, daemon=True).start() | |
# Start background preload at import | |
preload_in_background() | |
# ------------------------------ | |
# Proxies with caching | |
# ------------------------------ | |
def process_input(text): return _load_brain().process_input(text) | |
def search_kb(query): return _load_brain().search_kb(query) | |
def upload_media(path): return _load_brain().upload_media(path) | |
def backup_brain(): return _load_brain().backup_brain() | |
def restore_brain(): return _load_brain().restore_brain() | |
def show_creative_skills(): return _load_brain().show_creative_skills() | |
def sync_status(): return _load_brain().sync_status() | |
def is_ready(): return _is_ready | |