Spaces:
Sleeping
Sleeping
feat: Streamlit UI with memory sidebar, chat interface, and reset button
Browse files- .streamlit/config.toml +2 -0
- README.md +121 -0
- app.py +111 -0
.streamlit/config.toml
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[server]
|
| 2 |
+
fileWatcherType = "none"
|
README.md
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# MemoryMind — The Chatbot That Actually Remembers You
|
| 2 |
+
|
| 3 |
+
Most chatbots forget everything the moment you close the tab. MemoryMind doesn't.
|
| 4 |
+
|
| 5 |
+
It extracts facts from your conversation, stores them permanently, and uses them in future sessions — even weeks later. A live memory panel shows exactly what the bot knows about you, which memories it used in the last response, and lets you wipe everything with a single button.
|
| 6 |
+
|
| 7 |
+
---
|
| 8 |
+
|
| 9 |
+
## Features
|
| 10 |
+
|
| 11 |
+
- **Persistent memory** — facts survive browser refresh, app restarts, and new sessions
|
| 12 |
+
- **Semantic retrieval** — finds relevant memories by meaning, not just keywords
|
| 13 |
+
- **Memory correction** — say "actually I moved to Berlin" and it updates the old entry
|
| 14 |
+
- **Deduplication** — never stores the same fact twice
|
| 15 |
+
- **Memory panel** — live sidebar showing all stored facts, timestamps, and usage counts
|
| 16 |
+
- **Highlighted memories** — see exactly which memories informed each response
|
| 17 |
+
- **Reset button** — wipe all memory and start fresh (great for demos)
|
| 18 |
+
|
| 19 |
+
---
|
| 20 |
+
|
| 21 |
+
## Tech Stack
|
| 22 |
+
|
| 23 |
+
| Layer | Tool |
|
| 24 |
+
|---|---|
|
| 25 |
+
| LLM | Groq API — Llama 3.3 70B |
|
| 26 |
+
| Memory Store | ChromaDB (local, persistent) |
|
| 27 |
+
| Embeddings | sentence-transformers `all-MiniLM-L6-v2` |
|
| 28 |
+
| Frontend | Streamlit |
|
| 29 |
+
| Hosting | Hugging Face Spaces |
|
| 30 |
+
| Language | Python 3.11+ |
|
| 31 |
+
|
| 32 |
+
---
|
| 33 |
+
|
| 34 |
+
## Project Structure
|
| 35 |
+
|
| 36 |
+
```
|
| 37 |
+
├── app.py # Streamlit UI + memory sidebar
|
| 38 |
+
├── chatbot.py # LLM calls, prompt building, contradiction detection
|
| 39 |
+
├── memory.py # ChromaDB store, retrieve, deduplicate, correct
|
| 40 |
+
├── extractor.py # Fact extraction via second LLM call
|
| 41 |
+
├── requirements.txt
|
| 42 |
+
└── .env # GROQ_API_KEY (never committed)
|
| 43 |
+
```
|
| 44 |
+
|
| 45 |
+
---
|
| 46 |
+
|
| 47 |
+
## How Memory Works
|
| 48 |
+
|
| 49 |
+
```
|
| 50 |
+
User message
|
| 51 |
+
↓
|
| 52 |
+
Retrieve top-5 relevant memories (semantic search on ChromaDB)
|
| 53 |
+
↓
|
| 54 |
+
Build prompt: system + memories + chat history + user message
|
| 55 |
+
↓
|
| 56 |
+
Groq LLM → response
|
| 57 |
+
↓
|
| 58 |
+
Extract new facts from the exchange
|
| 59 |
+
↓
|
| 60 |
+
Deduplicate → check for contradictions → store or correct
|
| 61 |
+
↓
|
| 62 |
+
Render response + updated memory panel
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
---
|
| 66 |
+
|
| 67 |
+
## Setup
|
| 68 |
+
|
| 69 |
+
**1. Clone the repo**
|
| 70 |
+
```bash
|
| 71 |
+
git clone https://github.com/satomustar/Memorymind-Chatbot.git
|
| 72 |
+
cd Memorymind-Chatbot
|
| 73 |
+
```
|
| 74 |
+
|
| 75 |
+
**2. Create a virtual environment**
|
| 76 |
+
```bash
|
| 77 |
+
python3 -m venv .venv
|
| 78 |
+
source .venv/bin/activate
|
| 79 |
+
```
|
| 80 |
+
|
| 81 |
+
**3. Install dependencies**
|
| 82 |
+
```bash
|
| 83 |
+
pip install -r requirements.txt
|
| 84 |
+
```
|
| 85 |
+
|
| 86 |
+
**4. Add your Groq API key**
|
| 87 |
+
|
| 88 |
+
Create a `.env` file:
|
| 89 |
+
```
|
| 90 |
+
GROQ_API_KEY=your_key_here
|
| 91 |
+
```
|
| 92 |
+
|
| 93 |
+
Get a free key at [console.groq.com](https://console.groq.com).
|
| 94 |
+
|
| 95 |
+
**5. Run the app**
|
| 96 |
+
```bash
|
| 97 |
+
streamlit run app.py
|
| 98 |
+
```
|
| 99 |
+
|
| 100 |
+
---
|
| 101 |
+
|
| 102 |
+
## Testing
|
| 103 |
+
|
| 104 |
+
Test the memory core and chat logic without the UI:
|
| 105 |
+
|
| 106 |
+
```bash
|
| 107 |
+
# Test memory storage, retrieval, and deduplication
|
| 108 |
+
python test_extractor.py
|
| 109 |
+
|
| 110 |
+
# Interactive terminal chat
|
| 111 |
+
python chatbot.py
|
| 112 |
+
```
|
| 113 |
+
|
| 114 |
+
---
|
| 115 |
+
|
| 116 |
+
## Roadmap
|
| 117 |
+
|
| 118 |
+
- [ ] FastAPI backend (decouple UI from logic)
|
| 119 |
+
- [ ] Multi-user support with per-user memory namespaces
|
| 120 |
+
- [ ] Memory decay scoring (stale facts fade out)
|
| 121 |
+
- [ ] Deploy to Hugging Face Spaces
|
app.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime, timezone
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
|
| 5 |
+
from chatbot import chat
|
| 6 |
+
from memory import delete_all_memories, get_all_memories
|
| 7 |
+
|
| 8 |
+
st.set_page_config(page_title="MemoryMind", page_icon="🧠", layout="wide")
|
| 9 |
+
|
| 10 |
+
# --- session state ---
|
| 11 |
+
if "chat_history" not in st.session_state:
|
| 12 |
+
st.session_state.chat_history = []
|
| 13 |
+
if "last_used_ids" not in st.session_state:
|
| 14 |
+
st.session_state.last_used_ids = []
|
| 15 |
+
if "confirm_reset" not in st.session_state:
|
| 16 |
+
st.session_state.confirm_reset = False
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def _relative_time(iso_str: str) -> str:
|
| 20 |
+
try:
|
| 21 |
+
dt = datetime.fromisoformat(iso_str)
|
| 22 |
+
seconds = int((datetime.now(timezone.utc) - dt).total_seconds())
|
| 23 |
+
if seconds < 60:
|
| 24 |
+
return "just now"
|
| 25 |
+
if seconds < 3600:
|
| 26 |
+
return f"{seconds // 60}m ago"
|
| 27 |
+
if seconds < 86400:
|
| 28 |
+
return f"{seconds // 3600}h ago"
|
| 29 |
+
return f"{seconds // 86400}d ago"
|
| 30 |
+
except Exception:
|
| 31 |
+
return ""
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# --- sidebar ---
|
| 35 |
+
with st.sidebar:
|
| 36 |
+
st.title("🧠 Memory")
|
| 37 |
+
memories = get_all_memories()
|
| 38 |
+
st.caption(f"{len(memories)} fact{'s' if len(memories) != 1 else ''} stored")
|
| 39 |
+
st.divider()
|
| 40 |
+
|
| 41 |
+
if not memories:
|
| 42 |
+
st.caption("No memories yet. Start chatting!")
|
| 43 |
+
else:
|
| 44 |
+
for m in memories:
|
| 45 |
+
is_used = m["id"] in st.session_state.last_used_ids
|
| 46 |
+
meta = m["metadata"]
|
| 47 |
+
border_color = "#4caf50" if is_used else "#444"
|
| 48 |
+
bg_color = "#1a2e1a" if is_used else "#1a1a1a"
|
| 49 |
+
checkmark = " ✓" if is_used else ""
|
| 50 |
+
|
| 51 |
+
st.markdown(
|
| 52 |
+
f"""
|
| 53 |
+
<div style="background:{bg_color};border-left:3px solid {border_color};
|
| 54 |
+
padding:8px 12px;border-radius:4px;margin-bottom:8px;line-height:1.4;">
|
| 55 |
+
<div style="font-size:0.85rem;color:#e0e0e0;">{m['fact']}{checkmark}</div>
|
| 56 |
+
<div style="font-size:0.7rem;color:#888;margin-top:5px;">
|
| 57 |
+
<span style="background:#2a2a2a;padding:1px 7px;border-radius:10px;color:#aaa;">
|
| 58 |
+
{meta.get('type', '—')}
|
| 59 |
+
</span>
|
| 60 |
+
· {_relative_time(meta.get('timestamp', ''))}
|
| 61 |
+
· used {meta.get('usage_count', 0)}×
|
| 62 |
+
</div>
|
| 63 |
+
</div>""",
|
| 64 |
+
unsafe_allow_html=True,
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
st.divider()
|
| 68 |
+
|
| 69 |
+
if st.session_state.confirm_reset:
|
| 70 |
+
st.warning("This will wipe all memories. Are you sure?")
|
| 71 |
+
col1, col2 = st.columns(2)
|
| 72 |
+
with col1:
|
| 73 |
+
if st.button("Yes, reset", type="primary", use_container_width=True):
|
| 74 |
+
delete_all_memories()
|
| 75 |
+
st.session_state.chat_history = []
|
| 76 |
+
st.session_state.last_used_ids = []
|
| 77 |
+
st.session_state.confirm_reset = False
|
| 78 |
+
st.rerun()
|
| 79 |
+
with col2:
|
| 80 |
+
if st.button("Cancel", use_container_width=True):
|
| 81 |
+
st.session_state.confirm_reset = False
|
| 82 |
+
st.rerun()
|
| 83 |
+
else:
|
| 84 |
+
if st.button("🗑️ Reset Memory", use_container_width=True):
|
| 85 |
+
st.session_state.confirm_reset = True
|
| 86 |
+
st.rerun()
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
# --- main chat area ---
|
| 90 |
+
st.title("MemoryMind")
|
| 91 |
+
st.caption("The chatbot that actually remembers you.")
|
| 92 |
+
|
| 93 |
+
for message in st.session_state.chat_history:
|
| 94 |
+
with st.chat_message(message["role"]):
|
| 95 |
+
st.write(message["content"])
|
| 96 |
+
|
| 97 |
+
if prompt := st.chat_input("Say something..."):
|
| 98 |
+
with st.chat_message("user"):
|
| 99 |
+
st.write(prompt)
|
| 100 |
+
|
| 101 |
+
with st.chat_message("assistant"):
|
| 102 |
+
with st.spinner("Thinking..."):
|
| 103 |
+
reply, used_ids = chat(prompt, st.session_state.chat_history)
|
| 104 |
+
st.write(reply)
|
| 105 |
+
if used_ids:
|
| 106 |
+
st.caption(f"💡 {len(used_ids)} memor{'ies' if len(used_ids) != 1 else 'y'} used")
|
| 107 |
+
|
| 108 |
+
st.session_state.chat_history.append({"role": "user", "content": prompt})
|
| 109 |
+
st.session_state.chat_history.append({"role": "assistant", "content": reply})
|
| 110 |
+
st.session_state.last_used_ids = used_ids
|
| 111 |
+
st.rerun()
|