Spaces:
Running
Running
Create brain_speed.py
Browse files- brain_speed.py +77 -0
brain_speed.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
brain_speed.py
|
3 |
+
Speed-optimized interface for multimodular_modul_v7
|
4 |
+
- Makes user replies faster after runtime is up
|
5 |
+
- Uses caching + warm-up
|
6 |
+
"""
|
7 |
+
|
8 |
+
import os
|
9 |
+
import time
|
10 |
+
import threading
|
11 |
+
import functools
|
12 |
+
from collections import OrderedDict
|
13 |
+
import importlib
|
14 |
+
|
15 |
+
# ------------------------------
|
16 |
+
# Cache Setup
|
17 |
+
# ------------------------------
|
18 |
+
CACHE_LIMIT = 50
|
19 |
+
_response_cache = OrderedDict()
|
20 |
+
|
21 |
+
def _cache_result(func):
|
22 |
+
@functools.wraps(func)
|
23 |
+
def wrapper(*args, **kwargs):
|
24 |
+
key = str((args, tuple(sorted(kwargs.items()))))
|
25 |
+
if key in _response_cache:
|
26 |
+
_response_cache.move_to_end(key)
|
27 |
+
return _response_cache[key]
|
28 |
+
result = func(*args, **kwargs)
|
29 |
+
_response_cache[key] = result
|
30 |
+
if len(_response_cache) > CACHE_LIMIT:
|
31 |
+
_response_cache.popitem(last=False)
|
32 |
+
return result
|
33 |
+
return wrapper
|
34 |
+
|
35 |
+
# ------------------------------
|
36 |
+
# Load + Warm-up
|
37 |
+
# ------------------------------
|
38 |
+
_brain = None
|
39 |
+
_is_ready = False
|
40 |
+
|
41 |
+
def _load_brain():
|
42 |
+
global _brain, _is_ready
|
43 |
+
if _brain is None:
|
44 |
+
print("⚡ Loading brain (speed mode)...")
|
45 |
+
start = time.time()
|
46 |
+
_brain = importlib.import_module("multimodular_modul_v7")
|
47 |
+
_is_ready = True
|
48 |
+
print(f"✅ Brain ready in {time.time() - start:.2f}s")
|
49 |
+
_warm_up()
|
50 |
+
return _brain
|
51 |
+
|
52 |
+
def _warm_up():
|
53 |
+
try:
|
54 |
+
print("🔥 Warming up models...")
|
55 |
+
_brain.process_input("Hello")
|
56 |
+
print("✅ Warm-up complete")
|
57 |
+
except Exception as e:
|
58 |
+
print(f"⚠ Warm-up failed: {e}")
|
59 |
+
|
60 |
+
def preload_in_background():
|
61 |
+
threading.Thread(target=_load_brain, daemon=True).start()
|
62 |
+
|
63 |
+
preload_in_background()
|
64 |
+
|
65 |
+
# ------------------------------
|
66 |
+
# Speed-optimized Proxies
|
67 |
+
# ------------------------------
|
68 |
+
@_cache_result
|
69 |
+
def process_input(text): return _load_brain().process_input(text)
|
70 |
+
@_cache_result
|
71 |
+
def search_kb(query): return _load_brain().search_kb(query)
|
72 |
+
def upload_media(path): return _load_brain().upload_media(path)
|
73 |
+
def backup_brain(): return _load_brain().backup_brain()
|
74 |
+
def restore_brain(): return _load_brain().restore_brain()
|
75 |
+
def show_creative_skills(): return _load_brain().show_creative_skills()
|
76 |
+
def sync_status(): return _load_brain().sync_status()
|
77 |
+
def is_ready(): return _is_ready
|