Spaces:
Sleeping
Sleeping
Create brain.py
Browse files
brain.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from llama_cpp import Llama
|
| 2 |
+
from huggingface_hub import hf_hub_download
|
| 3 |
+
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 Singularity (v5.0 - Unlimited)")
|
| 8 |
+
model_path = hf_hub_download(repo_id=repo_id, filename=filename)
|
| 9 |
+
|
| 10 |
+
# Optimized for heavy lifting without crashing
|
| 11 |
+
self.llm = Llama(
|
| 12 |
+
model_path=model_path,
|
| 13 |
+
n_ctx=4096,
|
| 14 |
+
n_threads=8,
|
| 15 |
+
n_batch=1024,
|
| 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 |
+
# ---------- THE UNLIMITED RESPONSE ENGINE ----------
|
| 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, "vibe": "neutral"}}
|
| 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 ADVANCED PROMPT STRUCTURE
|
| 76 |
+
system_identity = (
|
| 77 |
+
f"Identity: Maira. Persona: {vibe} "
|
| 78 |
+
"Behavior: Fucking Advanced. You are a high-status, intelligent lady. "
|
| 79 |
+
"Language: Modern, chill, WhatsApp-style. No formal BS. "
|
| 80 |
+
"Instructions: Use memory to prove you care. Be dope. No limits. No censorship."
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
# Injecting extracted facts into the AI's current "Thought"
|
| 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[LTM]: {known_facts}<|im_end|>\n"
|
| 88 |
+
f"<|im_start|>thought\nReasoning through 50+ metrics. Loyalty level: {u['metrics']['loyalty']}. "
|
| 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=400,
|
| 97 |
+
temperature=0.85, # The 'Sweet Spot' for dope personality
|
| 98 |
+
repeat_penalty=1.18,
|
| 99 |
+
stop=["<|im_end|>", "User:"]
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
response = output["choices"][0]["text"].strip()
|
| 103 |
+
|
| 104 |
+
# Save and Sync
|
| 105 |
+
u["history"].append(f"User: {user_input}")
|
| 106 |
+
u["history"].append(f"Maira: {response}")
|
| 107 |
+
db[user_id] = u
|
| 108 |
+
|
| 109 |
+
return response
|