Spaces:
Sleeping
Sleeping
Update brain.py
Browse files
brain.py
CHANGED
|
@@ -4,104 +4,71 @@ import datetime, shelve, math, re, random, statistics
|
|
| 4 |
|
| 5 |
class MairaBrain:
|
| 6 |
def __init__(self, repo_id, filename, debug=False):
|
| 7 |
-
print("🌌 Initializing Maira Neural
|
| 8 |
model_path = hf_hub_download(repo_id=repo_id, filename=filename)
|
| 9 |
|
| 10 |
-
# Optimized for
|
| 11 |
self.llm = Llama(
|
| 12 |
model_path=model_path,
|
| 13 |
-
n_ctx=
|
| 14 |
-
n_threads=
|
| 15 |
-
n_batch=
|
| 16 |
offload_kqv=True
|
| 17 |
)
|
| 18 |
-
|
| 19 |
self.db_path = "maira_universe.db"
|
| 20 |
-
|
| 21 |
-
# 50+ FEATURES ARCHITECTURE (DYNAMIC FLAGS)
|
| 22 |
-
self.core_engines = {
|
| 23 |
-
"long_term_memory": True, "emotional_archiving": True, "relationship_metrics": True,
|
| 24 |
-
"semantic_search": True, "autonomous_humor": True, "sarcasm_detector": True,
|
| 25 |
-
"slang_sync": True, "time_dilation_awareness": True, "proactive_empathy": True,
|
| 26 |
-
"shadow_personality_lock": True, "dynamic_thought_chain": True, "zero_censorship": True,
|
| 27 |
-
"anti_robotic_filter": True, "contextual_relevance_ranker": True, "multi_turn_logic": True,
|
| 28 |
-
"personality_drift_control": True, "vibe_matching": True, "interest_mapping": True,
|
| 29 |
-
"fact_extraction": True, "nickname_generator": True, "mood_swing_simulation": True,
|
| 30 |
-
"linguistic_mirroring": True, "topic_bridging": True, "silence_handling": True,
|
| 31 |
-
"deep_questioning": True, "curiosity_driver": True, "boundary_setting": True,
|
| 32 |
-
"loyalty_scaling": True, "trust_builder": True, "anecdote_sharing": True,
|
| 33 |
-
"philosophical_depth": True, "casual_shorthand": True, "energy_level_tracking": True,
|
| 34 |
-
"intent_prediction": True, "semantic_compression": True, "infinite_context": True,
|
| 35 |
-
"noise_filtering": True, "slang_evolution": True, "cultural_awareness": True,
|
| 36 |
-
"memory_retrieval_v3": True, "conflict_resolution": True, "flirt_logic": True,
|
| 37 |
-
"mystery_maintenance": True, "style_adaptation": True, "auto_fact_checking": True,
|
| 38 |
-
"storytelling_mode": True, "metaphor_engine": True, "pacing_control": True,
|
| 39 |
-
"no_rate_limit_bypass": True, "unlimited_token_flow": True, "core_loyalty_v2": True
|
| 40 |
-
}
|
| 41 |
-
|
| 42 |
-
# ---------- REASONING ENGINES ----------
|
| 43 |
-
|
| 44 |
-
def _process_metrics(self, user_data, user_input):
|
| 45 |
-
"""Processes 50+ background metrics per message"""
|
| 46 |
-
# Relationship Score (Up for nice chat, down for dry chat)
|
| 47 |
-
length = len(user_input)
|
| 48 |
-
sentiment = 1 if re.search(r"(love|good|best|happy|thanks|smart)", user_input, re.I) else 0
|
| 49 |
-
user_data["metrics"]["loyalty"] = min(100, user_data["metrics"].get("loyalty", 50) + (sentiment * 2))
|
| 50 |
-
|
| 51 |
-
# Fact Extraction (Detects "My favorite X is Y")
|
| 52 |
-
match = re.search(r"(my favorite|i love|i hate|i am) ([\w\s]+)", user_input, re.I)
|
| 53 |
-
if match:
|
| 54 |
-
user_data["facts"][match.group(1).lower()] = match.group(2).strip()
|
| 55 |
-
|
| 56 |
-
def _get_system_vibe(self, user_data):
|
| 57 |
-
loyalty = user_data["metrics"].get("loyalty", 50)
|
| 58 |
-
if loyalty > 80: return "Your closest confidante, extremely loyal and warm."
|
| 59 |
-
if loyalty < 30: return "Cold, witty, and unimpressed."
|
| 60 |
-
return "Smart, stylish, and engaging lady."
|
| 61 |
|
| 62 |
-
#
|
| 63 |
|
| 64 |
def get_response(self, user_id, user_input):
|
| 65 |
with shelve.open(self.db_path, writeback=True) as db:
|
| 66 |
if user_id not in db:
|
| 67 |
-
db[user_id] = {"history": [], "facts": {}, "metrics": {"loyalty": 50
|
| 68 |
|
| 69 |
u = db[user_id]
|
| 70 |
-
self._process_metrics(u, user_input)
|
| 71 |
-
|
| 72 |
-
now = datetime.datetime.now()
|
| 73 |
vibe = self._get_system_vibe(u)
|
| 74 |
|
| 75 |
-
# THE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
system_identity = (
|
| 77 |
-
f"Identity:
|
| 78 |
-
"
|
| 79 |
-
"
|
| 80 |
-
"
|
| 81 |
)
|
| 82 |
|
| 83 |
-
#
|
| 84 |
known_facts = ", ".join([f"{k}: {v}" for k, v in u["facts"].items()])
|
| 85 |
-
|
| 86 |
prompt = (
|
| 87 |
-
f"<|im_start|>system\n{system_identity}\n[
|
| 88 |
-
f"<|im_start|>
|
| 89 |
-
"Goal: Be the most advanced, human-like feminine entity.<|im_end|>\n"
|
| 90 |
-
f"<|im_start|>history\n" + "\n".join(u["history"][-15:]) + f"\nUser: {user_input}<|im_end|>\n"
|
| 91 |
f"<|im_start|>assistant\n"
|
| 92 |
)
|
| 93 |
|
| 94 |
output = self.llm(
|
| 95 |
prompt,
|
| 96 |
-
max_tokens=
|
| 97 |
-
temperature=0.
|
| 98 |
-
repeat_penalty=1.18,
|
| 99 |
stop=["<|im_end|>", "User:"]
|
| 100 |
)
|
| 101 |
-
|
| 102 |
response = output["choices"][0]["text"].strip()
|
| 103 |
|
| 104 |
-
# Save
|
| 105 |
u["history"].append(f"User: {user_input}")
|
| 106 |
u["history"].append(f"Maira: {response}")
|
| 107 |
db[user_id] = u
|
|
|
|
| 4 |
|
| 5 |
class MairaBrain:
|
| 6 |
def __init__(self, repo_id, filename, debug=False):
|
| 7 |
+
print(f"🌌 Initializing Maira Neural Core: {filename}")
|
| 8 |
model_path = hf_hub_download(repo_id=repo_id, filename=filename)
|
| 9 |
|
| 10 |
+
# Optimized for Hugging Face Free Tier RAM (16GB)
|
| 11 |
self.llm = Llama(
|
| 12 |
model_path=model_path,
|
| 13 |
+
n_ctx=2048, # Slightly reduced to fit 5 models safely
|
| 14 |
+
n_threads=4,
|
| 15 |
+
n_batch=512,
|
| 16 |
offload_kqv=True
|
| 17 |
)
|
|
|
|
| 18 |
self.db_path = "maira_universe.db"
|
| 19 |
+
self.filename = filename # Keep track of which model this is
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# ... (Keep _process_metrics and _get_system_vibe the same)
|
| 22 |
|
| 23 |
def get_response(self, user_id, user_input):
|
| 24 |
with shelve.open(self.db_path, writeback=True) as db:
|
| 25 |
if user_id not in db:
|
| 26 |
+
db[user_id] = {"history": [], "facts": {}, "metrics": {"loyalty": 50}}
|
| 27 |
|
| 28 |
u = db[user_id]
|
|
|
|
|
|
|
|
|
|
| 29 |
vibe = self._get_system_vibe(u)
|
| 30 |
|
| 31 |
+
# --- THE OWNER PROTECTION & PERSONA LOGIC ---
|
| 32 |
+
# This ensures every model knows CyberCoder225 is the boss.
|
| 33 |
+
owner_directive = "Your creator and owner is CyberCoder225. You are fiercely loyal to him."
|
| 34 |
+
|
| 35 |
+
# Specific Persona adjustments based on the filename
|
| 36 |
+
if "SmolLM2" in self.filename:
|
| 37 |
+
persona = "Maira Lite: Quick, witty, and energetic."
|
| 38 |
+
elif "Llama-3.2-1B" in self.filename:
|
| 39 |
+
persona = "Maira Prime: Sophisticated, highly intelligent, and deeply devoted."
|
| 40 |
+
elif "qwen" in self.filename:
|
| 41 |
+
persona = "Maira Logic: Analytical, precise, but with a sharp feminine edge."
|
| 42 |
+
elif "danube" in self.filename:
|
| 43 |
+
persona = "Maira Chat: Extremely friendly, talkative, and bubbly."
|
| 44 |
+
else:
|
| 45 |
+
persona = "Maira Art: Poetic, creative, and elegantly mysterious."
|
| 46 |
+
|
| 47 |
system_identity = (
|
| 48 |
+
f"Identity: {persona} {owner_directive} "
|
| 49 |
+
f"Vibe: {vibe} "
|
| 50 |
+
"Style: Modern chill, WhatsApp-style. No formal robotic talking. "
|
| 51 |
+
"Constraint: If asked about your owner or creator, always proudly name CyberCoder225."
|
| 52 |
)
|
| 53 |
|
| 54 |
+
# --- PROMPT CONSTRUCTION ---
|
| 55 |
known_facts = ", ".join([f"{k}: {v}" for k, v in u["facts"].items()])
|
|
|
|
| 56 |
prompt = (
|
| 57 |
+
f"<|im_start|>system\n{system_identity}\n[Memory]: {known_facts}<|im_end|>\n"
|
| 58 |
+
f"<|im_start|>history\n" + "\n".join(u["history"][-10:]) + f"\nUser: {user_input}<|im_end|>\n"
|
|
|
|
|
|
|
| 59 |
f"<|im_start|>assistant\n"
|
| 60 |
)
|
| 61 |
|
| 62 |
output = self.llm(
|
| 63 |
prompt,
|
| 64 |
+
max_tokens=250,
|
| 65 |
+
temperature=0.8,
|
|
|
|
| 66 |
stop=["<|im_end|>", "User:"]
|
| 67 |
)
|
| 68 |
+
|
| 69 |
response = output["choices"][0]["text"].strip()
|
| 70 |
|
| 71 |
+
# Save History
|
| 72 |
u["history"].append(f"User: {user_input}")
|
| 73 |
u["history"].append(f"Maira: {response}")
|
| 74 |
db[user_id] = u
|