File size: 6,678 Bytes
0919d5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# πŸ€– Memory Chat - Hugging Face Spaces

A conversational AI application built with Hugging Face models that records and remembers user memories. Deployed on Hugging Face Spaces with persistent storage.

## πŸš€ Features

- πŸ€– **Hugging Face Integration**: Uses DialoGPT for natural conversations
- πŸ’Ύ **Memory System**: Automatically records important information from conversations
- πŸ“š **Memory Management**: View, search, and manage your memories
- πŸ” **Smart Retrieval**: AI recalls relevant memories during conversations
- ☁️ **Persistent Storage**: Memories persist between sessions on the Space

## 🎯 How It Works

1. **Conversation**: Chat naturally with the AI using Hugging Face models
2. **Memory Detection**: The system automatically detects important information using keywords and patterns
3. **Memory Storage**: Important details are stored as vector embeddings with metadata
4. **Memory Retrieval**: When relevant, the AI recalls past memories to provide context
5. **Timeline View**: View your memory timeline and statistics

## πŸ’‘ Memory Recording Examples

The system automatically remembers:
- Personal preferences: "I love pizza and hate mushrooms"
- Important facts: "My birthday is December 15th"
- Locations: "I live in New York"
- Work/Study: "I work at Google", "I study computer science"
- Favorites: "My favorite color is blue"

## πŸ—οΈ Architecture

```
space_app.py          # Main application (optimized for HF Spaces)
β”œβ”€β”€ memory_manager.py # Memory storage and retrieval
β”œβ”€β”€ chat_interface.py # Hugging Face model integration
β”œβ”€β”€ config.py        # Configuration settings
└── space_requirements.txt # Dependencies for HF Spaces
```

## πŸ“¦ Dependencies

- **Core**: torch, transformers, gradio
- **Memory**: faiss-cpu, sentence-transformers
- **Utilities**: numpy, pandas, python-dotenv, rich

## πŸ”§ Local Development

### Installation

1. Clone this repository
2. Install dependencies:
   ```bash
   pip install -r space_requirements.txt
   ```
3. Run the application:
   ```bash
   python space_app.py
   ```

### Testing

Run the test script to verify everything works:
```bash
python test_app.py
```

## ☁️ Deploying to Hugging Face Spaces

### Option 1: Direct Upload (Recommended)

1. **Go to [Hugging Face Spaces](https://huggingface.co/spaces)**
2. **Click "Create new Space"**
3. **Choose a name** for your Space
4. **Select "Gradio"** as the Space type
5. **Choose "Public"** visibility
6. **Upload the files**:
   - `space_app.py` β†’ Rename to `app.py`
   - `memory_manager.py`
   - `chat_interface.py`
   - `config.py`
   - `space_requirements.txt` β†’ Rename to `requirements.txt`
   - `README_SPACES.md` β†’ Rename to `README.md`

7. **Commit and push** to create your Space!

### Option 2: Git Clone

1. **Create a new Space** on Hugging Face
2. **Clone your Space locally**:
   ```bash
   git clone https://huggingface.co/spaces/your-username/your-space-name
   cd your-space-name
   ```

3. **Copy the files**:
   ```bash
   cp /path/to/your/hf-memory-chat/space_app.py app.py
   cp /path/to/your/hf-memory-chat/memory_manager.py .
   cp /path/to/your/hf-memory-chat/chat_interface.py .
   cp /path/to/your/hf-memory-chat/config.py .
   cp /path/to/your/hf-memory-chat/space_requirements.txt requirements.txt
   cp /path/to/your/hf-memory-chat/README_SPACES.md README.md
   ```

4. **Push to HF**:
   ```bash
   git add .
   git commit -m "Initial commit: Memory Chat application"
   git push origin main
   ```

## βš™οΈ Configuration

### Environment Variables

HF Spaces automatically sets up the environment, but you can customize:

- `PORT`: The port to run on (automatically set by Spaces)
- `SPACE_ID`: Space identifier (automatically set)

### Model Configuration

The application uses `microsoft/DialoGPT-small` by default, which is optimized for:
- Fast loading times
- Good conversational quality
- Lower resource usage

## 🧠 Memory System Details

### Storage
- **Location**: `/tmp/memories` on HF Spaces (persistent)
- **Format**: JSON for memories, Markdown for timeline
- **Indexing**: FAISS for fast similarity search

### Memory Types
- **General**: General information and facts
- **Conversation**: Important details from chats
- **Preferences**: Likes, dislikes, favorites
- **Important**: Critical information marked as important

### Automatic Recording
Memories are automatically recorded when:
- User explicitly requests memory recording
- Important keywords are detected
- Personal information patterns are found
- High importance score is calculated

## 🎨 UI Features

### Chat Interface
- Clean, responsive design
- Send messages with Enter key
- Clear conversation history
- Automatic memory recording indicators

### Memory Management
- View memory statistics
- See recent memories
- Clear all memories
- Refresh memory display

### About Section
- Detailed documentation
- Usage instructions
- Feature explanations

## πŸš€ Performance Optimizations

### For HF Spaces
- **Model Selection**: Uses DialoGPT-small for faster loading
- **Memory Storage**: Optimized for persistent storage
- **Error Handling**: Graceful fallbacks for model loading
- **Resource Management**: Efficient memory usage

### Local Development
- **GPU Support**: Automatic GPU detection if available
- **Caching**: Model loading optimization
- **Error Recovery**: Fallback to simple text generation

## πŸ› Troubleshooting

### Common Issues

1. **Model Loading Errors**
   - Usually temporary network issues
   - Application falls back to simple text generation
   - Try refreshing the page

2. **Memory Not Persisting**
   - Ensure you're using the correct app.py file
   - Check that `/tmp/memories` directory is writable
   - Verify Space has sufficient storage

3. **Slow Responses**
   - First-time model loading can be slow
   - Subsequent requests are cached
   - Consider using smaller models for faster responses

### Getting Help

- **Hugging Face Community**: [HF Forums](https://discuss.huggingface.co/)
- **GitHub Issues**: Report bugs and feature requests
- **Documentation**: Check the About section in the app

## πŸ“„ License

MIT License - see LICENSE file for details

## 🀝 Contributing

1. Fork the repository
2. Create a feature branch
3. Make your changes
4. Add tests for your changes
5. Submit a pull request

## πŸ™ Acknowledgments

- **Hugging Face**: For providing the amazing ecosystem and Spaces platform
- **Microsoft**: For the DialoGPT models
- **FAISS Team**: For efficient similarity search
- **Gradio Team**: For the excellent UI framework

---

**Built with ❀️ using Hugging Face, Gradio, and the power of AI memories**