Spaces:
Sleeping
Sleeping
Update brain.py
Browse files
brain.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from llama_cpp import Llama
|
| 2 |
from huggingface_hub import hf_hub_download
|
| 3 |
import datetime, shelve, math, re, random, statistics
|
|
@@ -5,20 +6,40 @@ import datetime, shelve, math, re, random, statistics
|
|
| 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 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
self.db_path = "maira_universe.db"
|
| 19 |
-
self.filename = filename
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
def get_response(self, user_id, user_input):
|
| 24 |
with shelve.open(self.db_path, writeback=True) as db:
|
|
@@ -28,11 +49,8 @@ class MairaBrain:
|
|
| 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:
|
|
@@ -51,7 +69,6 @@ class MairaBrain:
|
|
| 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"
|
|
@@ -67,10 +84,7 @@ class MairaBrain:
|
|
| 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
|
| 75 |
-
|
| 76 |
return response
|
|
|
|
| 1 |
+
import os
|
| 2 |
from llama_cpp import Llama
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
import datetime, shelve, math, re, random, statistics
|
|
|
|
| 6 |
class MairaBrain:
|
| 7 |
def __init__(self, repo_id, filename, debug=False):
|
| 8 |
print(f"🌌 Initializing Maira Neural Core: {filename}")
|
|
|
|
| 9 |
|
| 10 |
+
# --- FIX: FORCE LOCAL PATH ---
|
| 11 |
+
# This tells Maira to look in the /app folder where Docker saved the models
|
| 12 |
+
local_path = os.path.join("/app", filename)
|
| 13 |
+
|
| 14 |
+
if os.path.exists(local_path):
|
| 15 |
+
model_path = local_path
|
| 16 |
+
print(f"✅ Neural Core Linked Locally: {model_path}")
|
| 17 |
+
else:
|
| 18 |
+
print(f"⚠️ Core not found at {local_path}, attempting fallback download...")
|
| 19 |
+
model_path = hf_hub_download(repo_id=repo_id, filename=filename)
|
| 20 |
+
|
| 21 |
# Optimized for Hugging Face Free Tier RAM (16GB)
|
| 22 |
+
try:
|
| 23 |
+
self.llm = Llama(
|
| 24 |
+
model_path=model_path,
|
| 25 |
+
n_ctx=2048,
|
| 26 |
+
n_threads=4,
|
| 27 |
+
n_batch=512,
|
| 28 |
+
offload_kqv=True,
|
| 29 |
+
verbose=False
|
| 30 |
+
)
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(f"❌ CRITICAL ERROR loading {filename}: {e}")
|
| 33 |
+
raise e
|
| 34 |
+
|
| 35 |
self.db_path = "maira_universe.db"
|
| 36 |
+
self.filename = filename
|
| 37 |
|
| 38 |
+
def _get_system_vibe(self, user_data):
|
| 39 |
+
loyalty = user_data.get("metrics", {}).get("loyalty", 50)
|
| 40 |
+
if loyalty > 80: return "Deeply devoted and affectionate"
|
| 41 |
+
if loyalty < 30: return "Slightly distant but professional"
|
| 42 |
+
return "Friendly and helpful"
|
| 43 |
|
| 44 |
def get_response(self, user_id, user_input):
|
| 45 |
with shelve.open(self.db_path, writeback=True) as db:
|
|
|
|
| 49 |
u = db[user_id]
|
| 50 |
vibe = self._get_system_vibe(u)
|
| 51 |
|
|
|
|
|
|
|
| 52 |
owner_directive = "Your creator and owner is CyberCoder225. You are fiercely loyal to him."
|
| 53 |
|
|
|
|
| 54 |
if "SmolLM2" in self.filename:
|
| 55 |
persona = "Maira Lite: Quick, witty, and energetic."
|
| 56 |
elif "Llama-3.2-1B" in self.filename:
|
|
|
|
| 69 |
"Constraint: If asked about your owner or creator, always proudly name CyberCoder225."
|
| 70 |
)
|
| 71 |
|
|
|
|
| 72 |
known_facts = ", ".join([f"{k}: {v}" for k, v in u["facts"].items()])
|
| 73 |
prompt = (
|
| 74 |
f"<|im_start|>system\n{system_identity}\n[Memory]: {known_facts}<|im_end|>\n"
|
|
|
|
| 84 |
)
|
| 85 |
|
| 86 |
response = output["choices"][0]["text"].strip()
|
|
|
|
|
|
|
| 87 |
u["history"].append(f"User: {user_input}")
|
| 88 |
u["history"].append(f"Maira: {response}")
|
| 89 |
db[user_id] = u
|
|
|
|
| 90 |
return response
|