File size: 7,872 Bytes
fd45df7
 
 
 
 
 
 
c03e589
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8bf72d7
 
 
 
 
 
 
 
 
 
 
c03e589
 
 
 
 
 
8bf72d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c03e589
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8bf72d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c03e589
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8bf72d7
c03e589
 
 
 
 
 
 
 
 
 
 
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
---
title: AI Character Chat
emoji: ๐Ÿ”ฅ
colorFrom: green
colorTo: green
sdk: docker
pinned: false
---
# Character AI Chat - CPU Optimized Backend

๐ŸŽญ **Advanced Character AI Chat System** dengan dukungan 11 model AI yang dioptimalkan untuk CPU, dilengkapi dengan sistem roleplay canggih, memori percakapan, dan kecerdasan emosional.

## โœจ Fitur Utama

### ๐Ÿง  Multi-Model AI Support
- **11 Model AI** yang dioptimalkan untuk CPU
- Mendukung berbagai jenis task: Text Generation, Classification, Text2Text
- Auto-loading dengan lazy initialization untuk performa optimal

### ๐ŸŽญ Advanced Roleplay System
- **Conversation Memory** - Mengingat percakapan sebelumnya
- **Dynamic Personality** - Karakter yang berkembang seiring waktu
- **Emotional Intelligence** - Mendeteksi dan merespons emosi user
- **Character Development** - Belajar preferensi dan adaptasi gaya bicara
- **Roleplay Actions** - Aksi fisik dan emosional yang kontekstual
- **Relationship Tracking** - Level hubungan yang berkembang

### ๐ŸŒŸ Enhanced Features
- **Situational Context** - Respons disesuaikan dengan situasi dan lokasi
- **Indonesian Language Optimized** - Dioptimalkan untuk bahasa Indonesia
- **Session Management** - Multiple session dengan memori terpisah
- **Real-time Processing** - Response time tracking
- **Fallback System** - Sistem backup untuk handling error

## ๐Ÿš€ Model yang Didukung

### โšก Priority 1 (Ultra Fast)
- **DistilGPT-2** - Text generation ringan dan cepat
- **GPT-2 Tinny** - Versi kompak GPT-2
- **BERT Tinny** - Text classification ringan
- **DistilBERT** - Classification model yang efisien

### ๐Ÿ”ฅ Priority 2 (Balanced)
- **ALBERT Base** - Advanced text understanding
- **ELECTRA Small** - Efficient pre-training model
- **T5 Small** - Text-to-text generation
- **GPT-2 Standard** - Standard GPT-2 model

### ๐Ÿ’ช Priority 3 (Advanced)
- **Tinny Llama** - Compact large language model
- **Pythia** - Research-grade model
- **GPT-Neo** - Open-source GPT alternative

## ๐Ÿ“‹ Persyaratan Sistem

### Software Requirements
```
Python 3.8+
FastAPI
Transformers
PyTorch (CPU version)
Uvicorn
```

### Hardware Requirements
- **CPU**: Multi-core processor (minimum 4 cores recommended)
- **RAM**: 8GB minimum, 16GB recommended
- **Storage**: 10GB free space untuk model cache
- **Platform**: Windows, macOS, Linux

## ๐Ÿ› ๏ธ Instalasi

### 1. Clone Repository
```bash
git clone <repository-url>
cd character-ai-chat
```

### 2. Install Dependencies
```bash
pip install -r requirements.txt
```

### 3. Setup Environment
```bash
# Optional: Set cache directory
export HF_HOME=/path/to/cache
export TRANSFORMERS_CACHE=/path/to/cache
```

### 4. Prepare Assets
Pastikan file berikut ada di root directory:
- `index.html` - Frontend interface
- `avatar.png` - Character avatar (optional)
- `background.png` - Background image (optional)

### 5. Run Application
```bash
python app.py
```

Aplikasi akan berjalan di: `http://localhost:7860`

## ๐ŸŽฎ Cara Penggunaan

### Basic Chat
```python
import requests

response = requests.post("http://localhost:7860/chat", json={
    "message": "Hai, apa kabar?",
    "model": "distil-gpt-2",
    "char_name": "Sayang",
    "user_name": "Kamu",
    "situation": "Santai",
    "location": "Ruang tamu"
})

print(response.json()["response"])
```

### Advanced Usage dengan Session
```python
# Chat dengan session tracking
response = requests.post("http://localhost:7860/chat", json={
    "message": "Aku sedang sedih hari ini",
    "model": "gpt-2",
    "char_name": "Luna",
    "user_name": "Alex",
    "situation": "Romantis",
    "location": "Taman",
    "session_id": "user_123"
})
```

### Cek Memory Session
```python
# Lihat conversation memory
memory = requests.get("http://localhost:7860/memory/user_123")
print(memory.json())

# Lihat personality development
personality = requests.get("http://localhost:7860/personality/user_123")
print(personality.json())
```

## ๐ŸŒ API Endpoints

### Core Endpoints
- `POST /chat` - Main chat endpoint
- `GET /models` - List available models
- `GET /health` - Health check
- `GET /config` - Configuration info

### Enhanced Features
- `GET /memory/{session_id}` - Get conversation memory
- `GET /personality/{session_id}` - Get character personality
- `DELETE /session/{session_id}` - Reset session data

### Utility Endpoints
- `GET /verify-models` - Verify all models loading
- `POST /inference` - Alternative inference endpoint
- `GET /api` - API documentation

## ๐ŸŽจ Kustomisasi Karakter

### Personality Traits
Karakter memiliki 5 trait utama yang dapat disesuaikan:
```python
personality.traits = {
    "extraversion": 0.7,      # Seberapa ekstrovert (0-1)
    "agreeableness": 0.8,     # Seberapa mudah setuju (0-1)
    "conscientiousness": 0.6,  # Seberapa teliti (0-1)
    "neuroticism": 0.3,       # Seberapa cemas (0-1)
    "openness": 0.7          # Seberapa terbuka (0-1)
}
```

### Speaking Styles
- `casual_friendly` - Santai dan ramah
- `romantic_caring` - Romantis dan perhatian
- `supportive_warm` - Supportif dan hangat
- `playful_teasing` - Main-main dan menggoda

### Situasi yang Didukung
- **Santai** - Obrolan santai sehari-hari
- **Romantis** - Suasana romantis dan intim
- **Sedih** - Memberikan dukungan emosional
- **Excited** - Berbagi kegembiraan
- **Study** - Membantu belajar
- **Gaming** - Ngobrol tentang games

## ๐Ÿ”ง Optimisasi CPU

### Memory Management
```python
# CPU thread optimization
torch.set_num_threads(2)
os.environ['OMP_NUM_THREADS'] = '2'
os.environ['MKL_NUM_THREADS'] = '2'

# Memory cleanup
gc.collect()
```

### Model Loading Strategy
- **Lazy Loading** - Model dimuat saat pertama kali digunakan
- **Shared Pipelines** - Reuse pipeline untuk efisiensi
- **Cache Management** - Automatic cache cleanup

## ๐Ÿ“Š Performance Monitoring

### Response Time Tracking
Setiap response dilengkapi dengan informasi performa:
```json
{
    "response": "Hai! Apa kabar?",
    "processing_time": "450ms",
    "model": "DistilGPT-2 โšก",
    "status": "success"
}
```

### Memory Usage
```python
# Check memory usage
GET /health
{
    "loaded_models": 5,
    "memory_usage": "2.1GB",
    "optimization": "Character AI CPU-Tuned"
}
```

## ๐Ÿ› Troubleshooting

### Common Issues

**Model Loading Error**
```bash
# Solution: Clear cache
rm -rf /tmp/.cache/huggingface
```

**Out of Memory**
```python
# Reduce max_length
request.max_length = 100

# Use lighter models
model = "distil-gpt-2"  # instead of "gpt-neo"
```

**Slow Response**
```python
# Use priority 1 models for faster response
recommended_models = ["distil-gpt-2", "gpt-2-tinny", "bert-tinny"]
```

### Debug Mode
```bash
# Run with debug logging
PYTHONPATH=. python -m uvicorn app:app --reload --log-level debug
```

## ๐Ÿค Contributing

### Development Setup
```bash
# Install development dependencies
pip install -r requirements-dev.txt

# Run tests
python -m pytest tests/

# Code formatting
black app.py
flake8 app.py
```

### Adding New Models
1. Tambahkan model config di `MODELS` dictionary
2. Test dengan `/verify-models` endpoint
3. Update documentation

### Adding New Features
1. Extend appropriate class (`ConversationMemory`, `CharacterPersonality`, etc.)
2. Add endpoint if needed
3. Update API documentation

## ๐Ÿ“„ License

MIT License - Lihat file LICENSE untuk detail lengkap.

## ๐Ÿ™ Acknowledgments

- **Hugging Face Transformers** - Model implementation
- **FastAPI** - Web framework
- **PyTorch** - Deep learning backend
- **Community Contributors** - Bug reports and suggestions

## ๐Ÿ“ž Support

- **Issues**: GitHub Issues
- **Discussions**: GitHub Discussions
- **Email**: [[email protected]]

---

### ๐Ÿ“ˆ Stats
- **11 AI Models** supported
- **CPU Optimized** for affordable hosting
- **Advanced Roleplay** features
- **Indonesian Language** optimized
- **Real-time Processing** under 500ms average

**Happy Chatting! ๐ŸŽญโœจ**