AthelaPerk commited on
Commit
488ab30
ยท
verified ยท
1 Parent(s): 5edf15f

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +49 -18
app.py CHANGED
@@ -1,25 +1,45 @@
1
  """
2
- Mnemo v2 - Interactive Demo
3
  Enhanced memory system with real embeddings, HNSW index, and temporal decay.
4
  """
5
 
6
  import gradio as gr
 
7
  import time
8
  from datetime import datetime
9
- from mnemo_core import get_mnemo
10
 
11
  def format_time(timestamp: float) -> str:
12
  return datetime.fromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S")
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  def add_memory(content: str, importance: float, tags: str, user_id: str):
 
15
  if not content.strip():
16
  return "โŒ Please enter content", get_stats_text(user_id)
17
 
18
  mnemo = get_mnemo()
19
  tags_list = [t.strip() for t in tags.split(",") if t.strip()] if tags else []
20
 
21
- result = mnemo.add(
 
 
 
22
  content=content,
 
23
  user_id=user_id or "default",
24
  importance=importance,
25
  tags=tags_list
@@ -30,15 +50,21 @@ def add_memory(content: str, importance: float, tags: str, user_id: str):
30
 
31
  return f"{icon} {result['message']} (ID: {result['id']})", get_stats_text(user_id)
32
 
 
 
33
  def search_memories(query: str, k: int, min_score: float, user_id: str):
 
34
  if not query.strip():
35
  return "โŒ Please enter a search query"
36
 
37
  mnemo = get_mnemo()
38
  start = time.time()
39
 
40
- results = mnemo.search(
41
- query=query,
 
 
 
42
  user_id=user_id or "default",
43
  k=k,
44
  min_score=min_score
@@ -66,7 +92,9 @@ def search_memories(query: str, k: int, min_score: float, user_id: str):
66
 
67
  return output
68
 
 
69
  def list_memories_ui(user_id: str, limit: int):
 
70
  mnemo = get_mnemo()
71
  memories = mnemo.list_memories(user_id=user_id or "default", limit=limit)
72
 
@@ -85,7 +113,9 @@ def list_memories_ui(user_id: str, limit: int):
85
 
86
  return output
87
 
 
88
  def delete_memory_ui(memory_id: str, user_id: str):
 
89
  if not memory_id.strip():
90
  return "โŒ Please enter a memory ID", get_stats_text(user_id)
91
 
@@ -96,24 +126,17 @@ def delete_memory_ui(memory_id: str, user_id: str):
96
  return f"โœ… Deleted memory: {memory_id}", get_stats_text(user_id)
97
  return f"โŒ Memory not found: {memory_id}", get_stats_text(user_id)
98
 
 
99
  def clear_memories_ui(user_id: str):
 
100
  mnemo = get_mnemo()
101
  count = mnemo.clear(user_id=user_id or "default")
102
  return f"๐Ÿ—‘๏ธ Cleared {count} memories", get_stats_text(user_id)
103
 
104
- def get_stats_text(user_id: str = "default") -> str:
105
- mnemo = get_mnemo()
106
- stats = mnemo.get_stats(user_id=user_id or "default")
107
-
108
- return f"""**System Stats**
109
- - Total memories: {stats['total_memories']}
110
- - User memories: {stats['user_memory_count']}
111
- - Total users: {stats['total_users']}
112
- - Adds: {stats['total_adds']} | Updates: {stats['total_updates']} | Deletes: {stats['total_deletes']}
113
- - Searches: {stats['total_searches']}
114
- - Decay half-life: {stats['decay_half_life_days']:.1f} days"""
115
 
 
116
  def load_examples(user_id: str):
 
117
  mnemo = get_mnemo()
118
 
119
  examples = [
@@ -127,10 +150,15 @@ def load_examples(user_id: str):
127
  ("Lives in San Francisco, timezone is PST", 0.6, ["personal", "location"]),
128
  ]
129
 
 
 
 
 
130
  added = 0
131
- for content, importance, tags in examples:
132
- result = mnemo.add(
133
  content=content,
 
134
  user_id=user_id or "default",
135
  importance=importance,
136
  tags=tags
@@ -140,6 +168,7 @@ def load_examples(user_id: str):
140
 
141
  return f"โœ… Loaded {added} example memories", get_stats_text(user_id)
142
 
 
143
  # Build the Gradio interface
144
  with gr.Blocks(title="Mnemo v2", theme=gr.themes.Soft()) as demo:
145
  gr.Markdown("""
@@ -152,6 +181,7 @@ with gr.Blocks(title="Mnemo v2", theme=gr.themes.Soft()) as demo:
152
  - ๐Ÿ“Š Composite relevance scoring (similarity + recency + frequency)
153
  - ๐Ÿ”„ Intelligent ADD/UPDATE detection
154
  - ๐Ÿ‘ฅ Multi-user support
 
155
  """)
156
 
157
  with gr.Row():
@@ -253,6 +283,7 @@ with gr.Blocks(title="Mnemo v2", theme=gr.themes.Soft()) as demo:
253
  | Vector Index | `faiss-cpu` HNSW (M=32, efSearch=50) |
254
  | Decay | Ebbinghaus curve, 7-day half-life |
255
  | Scoring | 40% similarity + 30% recency + 30% frequency |
 
256
 
257
  **Links:** [GitHub](https://huggingface.co/AthelaPerk/mnemo-memory) | [API](https://huggingface.co/spaces/AthelaPerk/mnemo-mcp)
258
  """)
 
1
  """
2
+ Mnemo v2 - Interactive Demo (ZeroGPU Compatible)
3
  Enhanced memory system with real embeddings, HNSW index, and temporal decay.
4
  """
5
 
6
  import gradio as gr
7
+ import spaces
8
  import time
9
  from datetime import datetime
10
+ from mnemo_core import get_mnemo, compute_embedding, compute_embeddings_batch
11
 
12
  def format_time(timestamp: float) -> str:
13
  return datetime.fromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S")
14
 
15
+ def get_stats_text(user_id: str = "default") -> str:
16
+ mnemo = get_mnemo()
17
+ stats = mnemo.get_stats(user_id=user_id or "default")
18
+
19
+ return f"""**System Stats**
20
+ - Total memories: {stats['total_memories']}
21
+ - User memories: {stats['user_memory_count']}
22
+ - Total users: {stats['total_users']}
23
+ - Adds: {stats['total_adds']} | Updates: {stats['total_updates']} | Deletes: {stats['total_deletes']}
24
+ - Searches: {stats['total_searches']}
25
+ - Decay half-life: {stats['decay_half_life_days']:.1f} days"""
26
+
27
+
28
+ @spaces.GPU(duration=30)
29
  def add_memory(content: str, importance: float, tags: str, user_id: str):
30
+ """Add a memory - requires GPU for embedding computation."""
31
  if not content.strip():
32
  return "โŒ Please enter content", get_stats_text(user_id)
33
 
34
  mnemo = get_mnemo()
35
  tags_list = [t.strip() for t in tags.split(",") if t.strip()] if tags else []
36
 
37
+ # Compute embedding (GPU operation)
38
+ embedding = compute_embedding(content)
39
+
40
+ result = mnemo.add_with_embedding(
41
  content=content,
42
+ embedding=embedding,
43
  user_id=user_id or "default",
44
  importance=importance,
45
  tags=tags_list
 
50
 
51
  return f"{icon} {result['message']} (ID: {result['id']})", get_stats_text(user_id)
52
 
53
+
54
+ @spaces.GPU(duration=30)
55
  def search_memories(query: str, k: int, min_score: float, user_id: str):
56
+ """Search memories - requires GPU for embedding computation."""
57
  if not query.strip():
58
  return "โŒ Please enter a search query"
59
 
60
  mnemo = get_mnemo()
61
  start = time.time()
62
 
63
+ # Compute query embedding (GPU operation)
64
+ query_embedding = compute_embedding(query)
65
+
66
+ results = mnemo.search_with_embedding(
67
+ query_embedding=query_embedding,
68
  user_id=user_id or "default",
69
  k=k,
70
  min_score=min_score
 
92
 
93
  return output
94
 
95
+
96
  def list_memories_ui(user_id: str, limit: int):
97
+ """List memories - no GPU needed."""
98
  mnemo = get_mnemo()
99
  memories = mnemo.list_memories(user_id=user_id or "default", limit=limit)
100
 
 
113
 
114
  return output
115
 
116
+
117
  def delete_memory_ui(memory_id: str, user_id: str):
118
+ """Delete a memory - no GPU needed."""
119
  if not memory_id.strip():
120
  return "โŒ Please enter a memory ID", get_stats_text(user_id)
121
 
 
126
  return f"โœ… Deleted memory: {memory_id}", get_stats_text(user_id)
127
  return f"โŒ Memory not found: {memory_id}", get_stats_text(user_id)
128
 
129
+
130
  def clear_memories_ui(user_id: str):
131
+ """Clear all memories - no GPU needed."""
132
  mnemo = get_mnemo()
133
  count = mnemo.clear(user_id=user_id or "default")
134
  return f"๐Ÿ—‘๏ธ Cleared {count} memories", get_stats_text(user_id)
135
 
 
 
 
 
 
 
 
 
 
 
 
136
 
137
+ @spaces.GPU(duration=60)
138
  def load_examples(user_id: str):
139
+ """Load example memories - requires GPU for batch embedding computation."""
140
  mnemo = get_mnemo()
141
 
142
  examples = [
 
150
  ("Lives in San Francisco, timezone is PST", 0.6, ["personal", "location"]),
151
  ]
152
 
153
+ # Batch compute embeddings for efficiency
154
+ texts = [ex[0] for ex in examples]
155
+ embeddings = compute_embeddings_batch(texts)
156
+
157
  added = 0
158
+ for i, (content, importance, tags) in enumerate(examples):
159
+ result = mnemo.add_with_embedding(
160
  content=content,
161
+ embedding=embeddings[i],
162
  user_id=user_id or "default",
163
  importance=importance,
164
  tags=tags
 
168
 
169
  return f"โœ… Loaded {added} example memories", get_stats_text(user_id)
170
 
171
+
172
  # Build the Gradio interface
173
  with gr.Blocks(title="Mnemo v2", theme=gr.themes.Soft()) as demo:
174
  gr.Markdown("""
 
181
  - ๐Ÿ“Š Composite relevance scoring (similarity + recency + frequency)
182
  - ๐Ÿ”„ Intelligent ADD/UPDATE detection
183
  - ๐Ÿ‘ฅ Multi-user support
184
+ - ๐ŸŽฎ ZeroGPU powered (H200)
185
  """)
186
 
187
  with gr.Row():
 
283
  | Vector Index | `faiss-cpu` HNSW (M=32, efSearch=50) |
284
  | Decay | Ebbinghaus curve, 7-day half-life |
285
  | Scoring | 40% similarity + 30% recency + 30% frequency |
286
+ | GPU | ZeroGPU (H200, dynamic allocation) |
287
 
288
  **Links:** [GitHub](https://huggingface.co/AthelaPerk/mnemo-memory) | [API](https://huggingface.co/spaces/AthelaPerk/mnemo-mcp)
289
  """)