File size: 9,092 Bytes
6aadba1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | """
Mnemo Demo - Gradio Interface
Demonstrates smart memory injection and semantic search
"""
import gradio as gr
from mnemo import Mnemo, should_inject_memory
# Initialize global Mnemo instance
memory = Mnemo(use_real_embeddings=True)
# Pre-load some example memories
EXAMPLE_MEMORIES = [
"User prefers Python programming and dark mode interfaces",
"Project deadline is March 15th for the API redesign",
"Previous analysis identified gender bias in Victorian psychiatry diagnoses",
"Framework has 5 checkpoints: gender, class, age, occupation, marital status",
"User's favorite coffee is cappuccino with oat milk, no sugar",
"Team standup meeting every Tuesday at 2pm in conference room 401",
"Working on machine learning model for customer churn prediction"
]
def initialize_demo():
"""Initialize with example memories"""
memory.clear()
for mem in EXAMPLE_MEMORIES:
memory.add(mem)
return f"β
Initialized with {len(EXAMPLE_MEMORIES)} example memories"
def add_memory(content: str):
"""Add a new memory"""
if not content.strip():
return "β Please enter some content", get_all_memories()
mem_id = memory.add(content.strip())
return f"β
Added memory: {mem_id}", get_all_memories()
def search_memories(query: str, top_k: int = 5):
"""Search memories"""
if not query.strip():
return "β Please enter a search query"
results = memory.search(query.strip(), top_k=int(top_k))
if not results:
return "No memories found"
output = []
for r in results:
output.append(f"**[{r.id}]** (score: {r.score:.3f})")
output.append(f"{r.content}")
output.append(f"_Strategies: semantic={r.strategy_scores.get('semantic', 0):.2f}, bm25={r.strategy_scores.get('bm25', 0):.2f}, graph={r.strategy_scores.get('graph', 0):.2f}_")
output.append("---")
return "\n".join(output)
def check_injection(query: str, context: str = ""):
"""Check if memory should be injected"""
should, reason = should_inject_memory(query, context)
status = "β
**INJECT MEMORY**" if should else "βοΈ **SKIP MEMORY**"
return f"{status}\n\nReason: `{reason}`"
def get_context_for_injection(query: str, top_k: int = 3):
"""Get formatted context for prompt injection"""
if not query.strip():
return "β Please enter a query"
context = memory.get_context(query.strip(), top_k=int(top_k))
if not context:
return "_No relevant context found_"
return f"```\n{context}\n```"
def get_all_memories():
"""Get all stored memories"""
if len(memory) == 0:
return "_No memories stored_"
output = []
for mem in memory.list_all():
output.append(f"β’ **{mem.id}**: {mem.content[:100]}...")
return "\n".join(output)
def get_stats():
"""Get system statistics"""
stats = memory.get_stats()
output = []
for k, v in stats.items():
output.append(f"β’ **{k}**: {v}")
return "\n".join(output)
def clear_memories():
"""Clear all memories"""
memory.clear()
return "β
All memories cleared", "_No memories stored_"
# Gradio Interface
with gr.Blocks(title="Mnemo - AI Memory System", theme=gr.themes.Soft()) as demo:
gr.Markdown("""
# π§ Mnemo - AI Memory System
**Smart memory injection for LLMs, chatbots, and AI agents**
> 21x faster than mem0 β’ Context-check algorithm β’ Real embeddings
""")
with gr.Tab("πΎ Memory Store"):
gr.Markdown("### Add and view memories")
with gr.Row():
with gr.Column(scale=2):
memory_input = gr.Textbox(
label="New Memory",
placeholder="Enter a memory to store...",
lines=2
)
add_btn = gr.Button("β Add Memory", variant="primary")
add_status = gr.Markdown()
with gr.Column(scale=3):
memories_display = gr.Markdown(label="Stored Memories")
with gr.Row():
init_btn = gr.Button("π Load Example Memories")
clear_btn = gr.Button("ποΈ Clear All")
add_btn.click(add_memory, inputs=[memory_input], outputs=[add_status, memories_display])
init_btn.click(initialize_demo, outputs=[add_status])
init_btn.click(get_all_memories, outputs=[memories_display])
clear_btn.click(clear_memories, outputs=[add_status, memories_display])
with gr.Tab("π Search"):
gr.Markdown("### Search stored memories")
with gr.Row():
search_input = gr.Textbox(
label="Search Query",
placeholder="Enter search query...",
lines=1
)
top_k_slider = gr.Slider(
minimum=1, maximum=10, value=5, step=1,
label="Number of Results"
)
search_btn = gr.Button("π Search", variant="primary")
search_results = gr.Markdown(label="Results")
search_btn.click(
search_memories,
inputs=[search_input, top_k_slider],
outputs=[search_results]
)
with gr.Tab("π― Smart Injection"):
gr.Markdown("""
### Should memory be injected?
The context-check algorithm detects if a query references prior context and needs memory injection.
**Inject when:** "Based on your previous analysis...", "Compare to earlier...", "Using your framework..."
**Skip when:** "What is Python?", "This is a NEW topic...", standalone questions
""")
with gr.Row():
with gr.Column():
injection_query = gr.Textbox(
label="Query to Check",
placeholder="Enter a query to check...",
lines=2
)
injection_context = gr.Textbox(
label="Additional Context (optional)",
placeholder="Any additional context...",
lines=1
)
check_btn = gr.Button("π― Check Injection", variant="primary")
with gr.Column():
injection_result = gr.Markdown(label="Result")
check_btn.click(
check_injection,
inputs=[injection_query, injection_context],
outputs=[injection_result]
)
gr.Markdown("### Get Context for Injection")
with gr.Row():
context_query = gr.Textbox(
label="Query",
placeholder="Enter query to get context for...",
lines=1
)
context_top_k = gr.Slider(
minimum=1, maximum=5, value=3, step=1,
label="Number of Memories"
)
context_btn = gr.Button("π Get Context")
context_output = gr.Markdown(label="Formatted Context")
context_btn.click(
get_context_for_injection,
inputs=[context_query, context_top_k],
outputs=[context_output]
)
with gr.Tab("π Stats"):
gr.Markdown("### System Statistics")
stats_btn = gr.Button("π Refresh Stats")
stats_display = gr.Markdown()
stats_btn.click(get_stats, outputs=[stats_display])
with gr.Tab("βΉοΈ About"):
gr.Markdown("""
## About Mnemo
Mnemo (named after Mnemosyne, Greek goddess of memory) is an open-source memory system for AI applications.
### Features
- **π― Smart Injection** - Context-check algorithm with 90% accuracy
- **𧬠Real Embeddings** - sentence-transformers (with hash fallback)
- **π Multi-Strategy Search** - Semantic + BM25 + Knowledge Graph
- **π Feedback Learning** - Improves over time
- **β‘ Fast** - 21x faster than mem0
- **π Private** - Fully local, no API keys needed
### Installation
```bash
pip install mnemo-memory
```
### Quick Start
```python
from mnemo import Mnemo
m = Mnemo()
m.add("User prefers dark mode")
if m.should_inject("Based on user preferences..."):
context = m.get_context("preferences")
# Use context in prompt
```
### Links
- [HuggingFace Model](https://huggingface.co/AthelaPerk/mnemo-memory)
- [MCP Server](https://huggingface.co/spaces/AthelaPerk/mnemo-mcp)
---
MIT License β’ Built with β€οΈ for the AI community
""")
# Initialize with examples on load
demo.load(initialize_demo)
demo.load(get_all_memories, outputs=[])
if __name__ == "__main__":
demo.launch()
|