Spaces:
Runtime error
Runtime error
Download Shelly/memory/memory_encoding.py from SkyWorlds01/Shelly_AI: direct link, hf CLI and curl.
- Browser
- Download file 3.8 kB
-
https://huggingface.co/spaces/SkyWorlds01/Shelly_AI/resolve/main/Shelly/memory/memory_encoding.py
- Command line
-
hf download hf://spaces/SkyWorlds01/Shelly_AI/Shelly/memory/memory_encoding.py
-
curl -L -o memory_encoding.py https://huggingface.co/spaces/SkyWorlds01/Shelly_AI/resolve/main/Shelly/memory/memory_encoding.py
3.8 kB
| """ | |
| Shelly AI - Memory Encoding | |
| Converts events and experiences into structured memory records. | |
| """ | |
| from __future__ import annotations | |
| import re | |
| import time | |
| import uuid | |
| from typing import Any, Dict, Optional | |
| class MemoryEncoder: | |
| def __init__(self): | |
| self.encoded_count = 0 | |
| def extract_entities(text: str) -> list: | |
| """ | |
| Extract simple named entities and important terms. | |
| This is a lightweight heuristic, not a trained NER model. | |
| """ | |
| if not isinstance(text, str): | |
| return [] | |
| words = re.findall( | |
| r"\b[A-Z][a-zA-Z0-9_-]{2,}\b", | |
| text, | |
| ) | |
| unique = [] | |
| for word in words: | |
| if word not in unique: | |
| unique.append(word) | |
| return unique[:100] | |
| def extract_keywords(text: str) -> list: | |
| if not isinstance(text, str): | |
| return [] | |
| words = re.findall( | |
| r"[A-Za-z0-9_'-]+", | |
| text.lower(), | |
| ) | |
| stop_words = { | |
| "the", "and", "for", "that", "this", "with", | |
| "from", "have", "has", "are", "was", "were", | |
| "you", "your", "she", "her", "they", "them", | |
| "yang", "dan", "atau", "untuk", "dari", "ini", | |
| "itu", "aku", "saya", "kamu", "dia", "adalah", | |
| } | |
| counts = {} | |
| for word in words: | |
| if len(word) > 2 and word not in stop_words: | |
| counts[word] = counts.get(word, 0) + 1 | |
| ranked = sorted( | |
| counts.items(), | |
| key=lambda item: item[1], | |
| reverse=True, | |
| ) | |
| return [word for word, _ in ranked[:30]] | |
| def encode( | |
| self, | |
| event: Any, | |
| context: Optional[Dict[str, Any]] = None, | |
| ) -> Dict[str, Any]: | |
| context = dict(context or {}) | |
| if isinstance(event, dict): | |
| content = event.get( | |
| "content", | |
| event.get("event", str(event)), | |
| ) | |
| event_type = event.get( | |
| "type", | |
| event.get("event_type", "experience"), | |
| ) | |
| source = event.get("source", "conversation") | |
| user_id = event.get("user_id") | |
| emotion_state = event.get("emotions", {}) | |
| else: | |
| content = str(event) | |
| event_type = "experience" | |
| source = "conversation" | |
| user_id = None | |
| emotion_state = {} | |
| content = str(content) | |
| record = { | |
| "id": str(uuid.uuid4()), | |
| "event": content, | |
| "event_type": event_type, | |
| "timestamp": time.time(), | |
| "context": context, | |
| "entities": self.extract_entities(content), | |
| "keywords": self.extract_keywords(content), | |
| "topic": context.get("topic"), | |
| "emotion_state": dict(emotion_state or {}), | |
| "novelty": float(context.get("novelty", 0.5)), | |
| "attention": float(context.get("attention", 0.5)), | |
| "repetitions": 1, | |
| "confidence": float(context.get("confidence", 0.5)), | |
| "source": source, | |
| "user_id": user_id, | |
| "retrieval_count": 0, | |
| "importance": float(context.get("importance", 0.5)), | |
| "original_event": content, | |
| "interpretations": [], | |
| "archived": False, | |
| } | |
| record["novelty"] = max( | |
| 0.0, | |
| min(1.0, record["novelty"]), | |
| ) | |
| record["attention"] = max( | |
| 0.0, | |
| min(1.0, record["attention"]), | |
| ) | |
| record["confidence"] = max( | |
| 0.0, | |
| min(1.0, record["confidence"]), | |
| ) | |
| self.encoded_count += 1 | |
| return record | |