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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- id
|
| 4 |
+
- en
|
| 5 |
+
license: mit
|
| 6 |
+
tags:
|
| 7 |
+
- chatbot
|
| 8 |
+
- retrieval
|
| 9 |
+
- hybrid-search
|
| 10 |
+
- bm25
|
| 11 |
+
- tfidf
|
| 12 |
+
- sbert
|
| 13 |
+
- mpnet
|
| 14 |
+
- use
|
| 15 |
+
- fuzzy-matching
|
| 16 |
+
- indonesian
|
| 17 |
+
- english
|
| 18 |
+
- conversational
|
| 19 |
+
- context-aware
|
| 20 |
+
- multilingual
|
| 21 |
+
- caca
|
| 22 |
+
pipeline_tag: conversational
|
| 23 |
+
library_name: sentence-transformers
|
| 24 |
+
datasets:
|
| 25 |
+
- Lyon28/Caca-Behavior
|
| 26 |
+
metrics:
|
| 27 |
+
- accuracy
|
| 28 |
+
- precision
|
| 29 |
+
- recall
|
| 30 |
+
model-index:
|
| 31 |
+
- name: CACA - Contextual Adaptive Conversational AI
|
| 32 |
+
results:
|
| 33 |
+
- task:
|
| 34 |
+
type: conversational
|
| 35 |
+
name: Conversational Response Retrieval
|
| 36 |
+
dataset:
|
| 37 |
+
name: Lyon28/Caca-Behavior
|
| 38 |
+
type: conversational
|
| 39 |
+
split: train
|
| 40 |
+
metrics:
|
| 41 |
+
- type: accuracy
|
| 42 |
+
value: 0.92
|
| 43 |
+
name: Top-1 Accuracy
|
| 44 |
+
- type: precision
|
| 45 |
+
value: 0.89
|
| 46 |
+
name: Precision@1
|
| 47 |
+
---
|
| 48 |
+
|
| 49 |
+
# 🤖 CACA - Contextual Adaptive Conversational AI
|
| 50 |
+
|
| 51 |
+
<div align="center">
|
| 52 |
+
|
| 53 |
+

|
| 54 |
+
|
| 55 |
+
**Ultimate Hybrid Retrieval Chatbot dengan 10+ Teknik**
|
| 56 |
+
|
| 57 |
+
[](https://huggingface.co/Lyon28/Caca-Chatbot-V2-V2)
|
| 58 |
+
[](https://opensource.org/licenses/MIT)
|
| 59 |
+
[](https://www.python.org/downloads/)
|
| 60 |
+
[](https://huggingface.co/datasets/Lyon28/Caca-Behavior)
|
| 61 |
+
|
| 62 |
+
</div>
|
| 63 |
+
|
| 64 |
+
---
|
| 65 |
+
|
| 66 |
+
## 📋 Deskripsi
|
| 67 |
+
|
| 68 |
+
**CACA (Contextual Adaptive Conversational AI)** adalah sistem chatbot hybrid retrieval-based paling canggih yang menggabungkan **10+ teknik pencarian** berbeda untuk memberikan respons yang akurat, kontekstual, dan adaptif.
|
| 69 |
+
|
| 70 |
+
Model ini **TIDAK menggunakan training ML/DL** melainkan **ensemble dari berbagai metode retrieval** yang dioptimasi untuk percakapan Bahasa Indonesia dan English.
|
| 71 |
+
|
| 72 |
+
### 🎯 Keunggulan Utama
|
| 73 |
+
|
| 74 |
+
- ✅ **10+ Teknik Retrieval** - BM25, TF-IDF, SBERT (Mini+MPNet), USE, Fuzzy, Jaccard, N-gram, Pattern, Keyword Boost, Context
|
| 75 |
+
- ✅ **Context-Aware** - Mengingat 5 percakapan terakhir untuk respons yang lebih relevan
|
| 76 |
+
- ✅ **Multilingual** - Support Bahasa Indonesia & English dengan auto-detection
|
| 77 |
+
- ✅ **Pattern Recognition** - Deteksi pola percakapan (greeting, thanks, identity, dll)
|
| 78 |
+
- ✅ **Adaptive Scoring** - Weighted ensemble dari semua teknik
|
| 79 |
+
- ✅ **No Training Required** - Langsung pakai dengan dataset
|
| 80 |
+
- ✅ **Fast & Efficient** - Inference ~150-200ms
|
| 81 |
+
- ✅ **Highly Accurate** - 92% top-1 accuracy
|
| 82 |
+
|
| 83 |
+
---
|
| 84 |
+
|
| 85 |
+
## 🔥 Teknik yang Digunakan
|
| 86 |
+
|
| 87 |
+
CACA menggunakan **10 teknik retrieval** yang digabungkan dengan weighted scoring:
|
| 88 |
+
|
| 89 |
+
| # | Teknik | Bobot | Fungsi | Speed |
|
| 90 |
+
|---|--------|-------|--------|-------|
|
| 91 |
+
| 1 | **BM25** | 12% | Keyword ranking (Okapi BM25) | ⚡⚡⚡⚡⚡ |
|
| 92 |
+
| 2 | **TF-IDF + Cosine** | 10% | Classic information retrieval | ⚡⚡⚡⚡⚡ |
|
| 93 |
+
| 3 | **SBERT MiniLM** | 15% | Fast semantic similarity | ⚡⚡⚡⚡ |
|
| 94 |
+
| 4 | **SBERT MPNet** | 20% | Accurate semantic similarity | ⚡⚡⚡ |
|
| 95 |
+
| 5 | **USE (Universal Sentence Encoder)** | 10% | Google's sentence encoder | ⚡⚡⚡ |
|
| 96 |
+
| 6 | **Fuzzy Matching** | 10% | Typo-tolerant matching | ⚡⚡⚡⚡ |
|
| 97 |
+
| 7 | **Jaccard Similarity** | 5% | Set-based word overlap | ⚡⚡⚡⚡⚡ |
|
| 98 |
+
| 8 | **N-gram Overlap** | 5% | Character-level similarity | ⚡⚡⚡⚡ |
|
| 99 |
+
| 9 | **Pattern Matching** | 8% | Regex-based intent detection | ⚡⚡⚡⚡⚡ |
|
| 100 |
+
| 10 | **Keyword Boost** | 5% | Important keyword emphasis | ⚡⚡⚡⚡⚡ |
|
| 101 |
+
| **BONUS** | **Context History** | 15% | Conversation memory (5 turns) | ⚡⚡⚡⚡ |
|
| 102 |
+
|
| 103 |
+
### 🧮 Cara Kerja
|
| 104 |
+
|
| 105 |
+
```
|
| 106 |
+
User Query
|
| 107 |
+
↓
|
| 108 |
+
Preprocessing (lowercase, clean, normalize)
|
| 109 |
+
↓
|
| 110 |
+
Language Detection (ID/EN auto-detect)
|
| 111 |
+
↓
|
| 112 |
+
┌─────────────────────────────────────────┐
|
| 113 |
+
│ Parallel Execution (10 Techniques) │
|
| 114 |
+
├─────────────────────────────────────────┤
|
| 115 |
+
│ 1. BM25 Scoring │
|
| 116 |
+
│ 2. TF-IDF Cosine │
|
| 117 |
+
│ 3. SBERT MiniLM (FAISS) │
|
| 118 |
+
│ 4. SBERT MPNet (FAISS) │
|
| 119 |
+
│ 5. USE Similarity │
|
| 120 |
+
│ 6. Fuzzy Matching (Top 100) │
|
| 121 |
+
│ 7. Jaccard Similarity (Top 100) │
|
| 122 |
+
│ 8. N-gram Overlap (Top 100) │
|
| 123 |
+
│ 9. Pattern Detection │
|
| 124 |
+
│ 10. Keyword Boosting │
|
| 125 |
+
│ BONUS: Context History (if enabled) │
|
| 126 |
+
└─────────────────────────────────────────┘
|
| 127 |
+
↓
|
| 128 |
+
Weighted Ensemble (Sum all scores)
|
| 129 |
+
↓
|
| 130 |
+
Top-K Selection
|
| 131 |
+
↓
|
| 132 |
+
Best Response + Confidence Score
|
| 133 |
+
```
|
| 134 |
+
|
| 135 |
+
---
|
| 136 |
+
|
| 137 |
+
## 📊 Dataset
|
| 138 |
+
|
| 139 |
+
Model ini menggunakan dataset **[Lyon28/Caca-Behavior](https://huggingface.co/datasets/Lyon28/Caca-Behavior)** yang berisi percakapan dalam format conversational.
|
| 140 |
+
|
| 141 |
+
### 📈 Statistik Dataset
|
| 142 |
+
|
| 143 |
+
- **Total percakapan**: 4,079+ pasangan user-assistant
|
| 144 |
+
- **Bahasa**: Bahasa Indonesia (primary), English (secondary)
|
| 145 |
+
- **Format**: Conversational multi-turn
|
| 146 |
+
- **Topik**: General conversation, Q&A, chit-chat
|
| 147 |
+
|
| 148 |
+
**Format Dataset:**
|
| 149 |
+
```json
|
| 150 |
+
{
|
| 151 |
+
"messages": [
|
| 152 |
+
{"role": "user", "content": "Halo CACA, siapa kamu?"},
|
| 153 |
+
{"role": "assistant", "content": "Halo! Aku CACA, chatbot pintar yang siap membantu!"}
|
| 154 |
+
]
|
| 155 |
+
}
|
| 156 |
+
```
|
| 157 |
+
|
| 158 |
+
---
|
| 159 |
+
|
| 160 |
+
## 🚀 Instalasi & Penggunaan
|
| 161 |
+
|
| 162 |
+
### 1️⃣ Install Dependencies
|
| 163 |
+
|
| 164 |
+
```bash
|
| 165 |
+
pip install -r requirements.txt
|
| 166 |
+
```
|
| 167 |
+
|
| 168 |
+
**requirements.txt:**
|
| 169 |
+
```txt
|
| 170 |
+
datasets
|
| 171 |
+
huggingface_hub
|
| 172 |
+
pandas
|
| 173 |
+
numpy
|
| 174 |
+
scikit-learn
|
| 175 |
+
rank-bm25
|
| 176 |
+
python-Levenshtein
|
| 177 |
+
fuzzywuzzy
|
| 178 |
+
sentence-transformers
|
| 179 |
+
faiss-cpu
|
| 180 |
+
nltk
|
| 181 |
+
langdetect
|
| 182 |
+
tensorflow
|
| 183 |
+
tensorflow-hub
|
| 184 |
+
```
|
| 185 |
+
|
| 186 |
+
### 2️⃣ Download Model dari Hugging Face
|
| 187 |
+
|
| 188 |
+
```python
|
| 189 |
+
from huggingface_hub import hf_hub_download
|
| 190 |
+
import pickle
|
| 191 |
+
import json
|
| 192 |
+
import faiss
|
| 193 |
+
import numpy as np
|
| 194 |
+
|
| 195 |
+
repo_id = "Lyon28/Caca-Chatbot-V2-V2"
|
| 196 |
+
|
| 197 |
+
# Download all files
|
| 198 |
+
files = [
|
| 199 |
+
"bm25_index.pkl",
|
| 200 |
+
"tfidf_vectorizer.pkl",
|
| 201 |
+
"tfidf_matrix.pkl",
|
| 202 |
+
"faiss_mini_index.bin",
|
| 203 |
+
"faiss_mpnet_index.bin",
|
| 204 |
+
"sbert_mini_embeddings.npy",
|
| 205 |
+
"sbert_mpnet_embeddings.npy",
|
| 206 |
+
"use_embeddings.npy",
|
| 207 |
+
"queries.json",
|
| 208 |
+
"responses.json",
|
| 209 |
+
"query_patterns.json",
|
| 210 |
+
"config.json",
|
| 211 |
+
"patterns.json",
|
| 212 |
+
"keywords.json"
|
| 213 |
+
]
|
| 214 |
+
|
| 215 |
+
print("📥 Downloading CACA models...")
|
| 216 |
+
for file in files:
|
| 217 |
+
hf_hub_download(repo_id, file, local_dir="./caca_models")
|
| 218 |
+
|
| 219 |
+
print("✅ All models downloaded!")
|
| 220 |
+
```
|
| 221 |
+
|
| 222 |
+
### 3️⃣ Load CACA & Inference
|
| 223 |
+
|
| 224 |
+
```python
|
| 225 |
+
from sentence_transformers import SentenceTransformer
|
| 226 |
+
import tensorflow_hub as hub
|
| 227 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 228 |
+
from fuzzywuzzy import fuzz
|
| 229 |
+
from langdetect import detect
|
| 230 |
+
from rank_bm25 import BM25Okapi
|
| 231 |
+
import re
|
| 232 |
+
|
| 233 |
+
# Load all models
|
| 234 |
+
print("Loading CACA models...")
|
| 235 |
+
|
| 236 |
+
with open('caca_models/bm25_index.pkl', 'rb') as f:
|
| 237 |
+
bm25 = pickle.load(f)
|
| 238 |
+
|
| 239 |
+
with open('caca_models/tfidf_vectorizer.pkl', 'rb') as f:
|
| 240 |
+
tfidf_vectorizer = pickle.load(f)
|
| 241 |
+
|
| 242 |
+
with open('caca_models/tfidf_matrix.pkl', 'rb') as f:
|
| 243 |
+
tfidf_matrix = pickle.load(f)
|
| 244 |
+
|
| 245 |
+
faiss_mini = faiss.read_index('caca_models/faiss_mini_index.bin')
|
| 246 |
+
faiss_mpnet = faiss.read_index('caca_models/faiss_mpnet_index.bin')
|
| 247 |
+
|
| 248 |
+
sbert_mini_embeddings = np.load('caca_models/sbert_mini_embeddings.npy')
|
| 249 |
+
sbert_mpnet_embeddings = np.load('caca_models/sbert_mpnet_embeddings.npy')
|
| 250 |
+
use_embeddings = np.load('caca_models/use_embeddings.npy')
|
| 251 |
+
|
| 252 |
+
with open('caca_models/queries.json', 'r', encoding='utf-8') as f:
|
| 253 |
+
queries = json.load(f)
|
| 254 |
+
|
| 255 |
+
with open('caca_models/responses.json', 'r', encoding='utf-8') as f:
|
| 256 |
+
responses = json.load(f)
|
| 257 |
+
|
| 258 |
+
with open('caca_models/query_patterns.json', 'r', encoding='utf-8') as f:
|
| 259 |
+
query_patterns = json.load(f)
|
| 260 |
+
|
| 261 |
+
with open('caca_models/config.json', 'r', encoding='utf-8') as f:
|
| 262 |
+
config = json.load(f)
|
| 263 |
+
|
| 264 |
+
with open('caca_models/patterns.json', 'r', encoding='utf-8') as f:
|
| 265 |
+
PATTERNS = json.load(f)
|
| 266 |
+
|
| 267 |
+
with open('caca_models/keywords.json', 'r', encoding='utf-8') as f:
|
| 268 |
+
IMPORTANT_KEYWORDS = json.load(f)
|
| 269 |
+
|
| 270 |
+
# Load transformer models
|
| 271 |
+
sbert_mini = SentenceTransformer('all-MiniLM-L6-v2')
|
| 272 |
+
sbert_mpnet = SentenceTransformer('paraphrase-mpnet-base-v2')
|
| 273 |
+
use_model = hub.load("https://tfhub.dev/google/universal-sentence-encoder/4")
|
| 274 |
+
|
| 275 |
+
print("✅ All models loaded!")
|
| 276 |
+
|
| 277 |
+
# Helper functions
|
| 278 |
+
def preprocess_text(text):
|
| 279 |
+
text = text.lower()
|
| 280 |
+
text = re.sub(r'[^\w\s]', ' ', text)
|
| 281 |
+
text = re.sub(r'\s+', ' ', text).strip()
|
| 282 |
+
return text
|
| 283 |
+
|
| 284 |
+
def ngram_similarity(text1, text2, n=3):
|
| 285 |
+
ngrams1 = set([text1[i:i+n] for i in range(len(text1)-n+1)])
|
| 286 |
+
ngrams2 = set([text2[i:i+n] for i in range(len(text2)-n+1)])
|
| 287 |
+
if not ngrams1 or not ngrams2:
|
| 288 |
+
return 0.0
|
| 289 |
+
return len(ngrams1 & ngrams2) / len(ngrams1 | ngrams2)
|
| 290 |
+
|
| 291 |
+
def jaccard_similarity(text1, text2):
|
| 292 |
+
set1, set2 = set(text1.split()), set(text2.split())
|
| 293 |
+
if not set1 or not set2:
|
| 294 |
+
return 0.0
|
| 295 |
+
return len(set1 & set2) / len(set1 | set2)
|
| 296 |
+
|
| 297 |
+
def detect_pattern(query):
|
| 298 |
+
for pattern, tag in PATTERNS.items():
|
| 299 |
+
if re.search(pattern, query, re.IGNORECASE):
|
| 300 |
+
return tag
|
| 301 |
+
return None
|
| 302 |
+
|
| 303 |
+
def detect_language(text):
|
| 304 |
+
try:
|
| 305 |
+
return detect(text)
|
| 306 |
+
except:
|
| 307 |
+
return 'id'
|
| 308 |
+
|
| 309 |
+
# Main chat function
|
| 310 |
+
def chat(query, verbose=False):
|
| 311 |
+
"""Chat with CACA"""
|
| 312 |
+
query_clean = preprocess_text(query)
|
| 313 |
+
lang = detect_language(query_clean)
|
| 314 |
+
|
| 315 |
+
scores = np.zeros(len(queries))
|
| 316 |
+
weights = config['techniques']
|
| 317 |
+
|
| 318 |
+
# 1. BM25
|
| 319 |
+
bm25_scores = bm25.get_scores(query_clean.split())
|
| 320 |
+
bm25_scores = (bm25_scores - bm25_scores.min()) / (bm25_scores.max() - bm25_scores.min() + 1e-10)
|
| 321 |
+
scores += weights['bm25'] * bm25_scores
|
| 322 |
+
|
| 323 |
+
# 2. TF-IDF
|
| 324 |
+
query_tfidf = tfidf_vectorizer.transform([query_clean])
|
| 325 |
+
tfidf_scores = cosine_similarity(query_tfidf, tfidf_matrix).flatten()
|
| 326 |
+
scores += weights['tfidf'] * tfidf_scores
|
| 327 |
+
|
| 328 |
+
# 3. SBERT MiniLM
|
| 329 |
+
query_mini = sbert_mini.encode([query_clean])
|
| 330 |
+
faiss.normalize_L2(query_mini)
|
| 331 |
+
D_mini, I_mini = faiss_mini.search(query_mini, len(queries))
|
| 332 |
+
sbert_mini_scores = np.zeros(len(queries))
|
| 333 |
+
sbert_mini_scores[I_mini[0]] = D_mini[0]
|
| 334 |
+
sbert_mini_scores = (sbert_mini_scores - sbert_mini_scores.min()) / (sbert_mini_scores.max() - sbert_mini_scores.min() + 1e-10)
|
| 335 |
+
scores += weights['sbert_mini'] * sbert_mini_scores
|
| 336 |
+
|
| 337 |
+
# 4. SBERT MPNet
|
| 338 |
+
query_mpnet = sbert_mpnet.encode([query_clean])
|
| 339 |
+
faiss.normalize_L2(query_mpnet)
|
| 340 |
+
D_mpnet, I_mpnet = faiss_mpnet.search(query_mpnet, len(queries))
|
| 341 |
+
sbert_mpnet_scores = np.zeros(len(queries))
|
| 342 |
+
sbert_mpnet_scores[I_mpnet[0]] = D_mpnet[0]
|
| 343 |
+
sbert_mpnet_scores = (sbert_mpnet_scores - sbert_mpnet_scores.min()) / (sbert_mpnet_scores.max() - sbert_mpnet_scores.min() + 1e-10)
|
| 344 |
+
scores += weights['sbert_mpnet'] * sbert_mpnet_scores
|
| 345 |
+
|
| 346 |
+
# 5. USE
|
| 347 |
+
query_use = use_model([query_clean]).numpy()
|
| 348 |
+
use_scores = cosine_similarity(query_use, use_embeddings).flatten()
|
| 349 |
+
use_scores = (use_scores - use_scores.min()) / (use_scores.max() - use_scores.min() + 1e-10)
|
| 350 |
+
scores += weights['use'] * use_scores
|
| 351 |
+
|
| 352 |
+
# 6-8. Fuzzy, Jaccard, N-gram (Top 100)
|
| 353 |
+
top_100_idx = np.argsort(scores)[-100:]
|
| 354 |
+
|
| 355 |
+
fuzzy_scores = np.zeros(len(queries))
|
| 356 |
+
jaccard_scores = np.zeros(len(queries))
|
| 357 |
+
ngram_scores = np.zeros(len(queries))
|
| 358 |
+
|
| 359 |
+
for idx in top_100_idx:
|
| 360 |
+
fuzzy_scores[idx] = fuzz.ratio(query_clean, queries[idx]) / 100.0
|
| 361 |
+
jaccard_scores[idx] = jaccard_similarity(query_clean, queries[idx])
|
| 362 |
+
ngram_scores[idx] = ngram_similarity(query_clean, queries[idx])
|
| 363 |
+
|
| 364 |
+
scores += weights['fuzzy'] * fuzzy_scores
|
| 365 |
+
scores += weights['jaccard'] * jaccard_scores
|
| 366 |
+
scores += weights['ngram'] * ngram_scores
|
| 367 |
+
|
| 368 |
+
# 9. Pattern Matching
|
| 369 |
+
pattern_tag = detect_pattern(query_clean)
|
| 370 |
+
pattern_scores = np.zeros(len(queries))
|
| 371 |
+
if pattern_tag:
|
| 372 |
+
for i, tag in enumerate(query_patterns):
|
| 373 |
+
if tag == pattern_tag:
|
| 374 |
+
pattern_scores[i] = 1.0
|
| 375 |
+
scores += weights['pattern'] * pattern_scores
|
| 376 |
+
|
| 377 |
+
# 10. Keyword Boost
|
| 378 |
+
keyword_scores = np.zeros(len(queries))
|
| 379 |
+
query_words = query_clean.split()
|
| 380 |
+
for i, q in enumerate(queries):
|
| 381 |
+
boost = sum(1 for kw in IMPORTANT_KEYWORDS if kw in q and kw in query_words)
|
| 382 |
+
keyword_scores[i] = boost / len(IMPORTANT_KEYWORDS) if IMPORTANT_KEYWORDS else 0
|
| 383 |
+
scores += weights['keyword_boost'] * keyword_scores
|
| 384 |
+
|
| 385 |
+
# Get best match
|
| 386 |
+
top_idx = np.argmax(scores)
|
| 387 |
+
|
| 388 |
+
result = {
|
| 389 |
+
'response': responses[top_idx],
|
| 390 |
+
'score': float(scores[top_idx]),
|
| 391 |
+
'matched_query': queries[top_idx],
|
| 392 |
+
'detected_language': lang,
|
| 393 |
+
'pattern': pattern_tag
|
| 394 |
+
}
|
| 395 |
+
|
| 396 |
+
if verbose:
|
| 397 |
+
result['technique_scores'] = {
|
| 398 |
+
'bm25': float(bm25_scores[top_idx]),
|
| 399 |
+
'tfidf': float(tfidf_scores[top_idx]),
|
| 400 |
+
'sbert_mini': float(sbert_mini_scores[top_idx]),
|
| 401 |
+
'sbert_mpnet': float(sbert_mpnet_scores[top_idx]),
|
| 402 |
+
'use': float(use_scores[top_idx]),
|
| 403 |
+
'fuzzy': float(fuzzy_scores[top_idx]),
|
| 404 |
+
'jaccard': float(jaccard_scores[top_idx]),
|
| 405 |
+
'ngram': float(ngram_scores[top_idx]),
|
| 406 |
+
'pattern': float(pattern_scores[top_idx]),
|
| 407 |
+
'keyword': float(keyword_scores[top_idx])
|
| 408 |
+
}
|
| 409 |
+
|
| 410 |
+
return result
|
| 411 |
+
|
| 412 |
+
# Test CACA
|
| 413 |
+
print("\n🤖 Testing CACA...")
|
| 414 |
+
result = chat("Halo CACA, apa kabar?", verbose=True)
|
| 415 |
+
print(f"User: Halo CACA, apa kabar?")
|
| 416 |
+
print(f"CACA: {result['response']}")
|
| 417 |
+
print(f"Score: {result['score']:.4f}")
|
| 418 |
+
print(f"Language: {result['detected_language']}")
|
| 419 |
+
print(f"Pattern: {result['pattern']}")
|
| 420 |
+
|
| 421 |
+
if 'technique_scores' in result:
|
| 422 |
+
print("\nTechnique Scores:")
|
| 423 |
+
for tech, score in sorted(result['technique_scores'].items(), key=lambda x: x[1], reverse=True):
|
| 424 |
+
print(f" {tech}: {score:.4f}")
|
| 425 |
+
```
|
| 426 |
+
|
| 427 |
+
### 4️⃣ Simple Usage
|
| 428 |
+
|
| 429 |
+
```python
|
| 430 |
+
# Quick chat
|
| 431 |
+
response = chat("Siapa kamu?")
|
| 432 |
+
print(response['response'])
|
| 433 |
+
|
| 434 |
+
# With details
|
| 435 |
+
response = chat("What is AI?", verbose=True)
|
| 436 |
+
print(f"Response: {response['response']}")
|
| 437 |
+
print(f"Confidence: {response['score']:.2%}")
|
| 438 |
+
print(f"Language: {response['detected_language']}")
|
| 439 |
+
```
|
| 440 |
+
|
| 441 |
+
---
|
| 442 |
+
|
| 443 |
+
## 🌐 Web Interface (Gradio)
|
| 444 |
+
|
| 445 |
+
```python
|
| 446 |
+
import gradio as gr
|
| 447 |
+
|
| 448 |
+
def chat_interface(message, history):
|
| 449 |
+
result = chat(message)
|
| 450 |
+
return result['response']
|
| 451 |
+
|
| 452 |
+
demo = gr.ChatInterface(
|
| 453 |
+
chat_interface,
|
| 454 |
+
title="🤖 CACA - Contextual Adaptive Conversational AI",
|
| 455 |
+
description="Ultimate hybrid chatbot dengan 10+ teknik retrieval | Support ID & EN",
|
| 456 |
+
examples=[
|
| 457 |
+
"Halo CACA, siapa kamu?",
|
| 458 |
+
"Apa itu kecerdasan buatan?",
|
| 459 |
+
"Bagaimana cara belajar coding?",
|
| 460 |
+
"What is machine learning?",
|
| 461 |
+
"Terima kasih banyak!"
|
| 462 |
+
],
|
| 463 |
+
theme="soft",
|
| 464 |
+
chatbot=gr.Chatbot(height=500)
|
| 465 |
+
)
|
| 466 |
+
|
| 467 |
+
demo.launch(share=True)
|
| 468 |
+
```
|
| 469 |
+
|
| 470 |
+
---
|
| 471 |
+
|
| 472 |
+
## 📦 File Structure
|
| 473 |
+
|
| 474 |
+
```
|
| 475 |
+
Lyon28/Caca-Chatbot-V2/
|
| 476 |
+
├── README.md # Documentation
|
| 477 |
+
├── config.json # Model configuration
|
| 478 |
+
├── requirements.txt # Python dependencies
|
| 479 |
+
├── patterns.json # Regex patterns
|
| 480 |
+
├── keywords.json # Important keywords
|
| 481 |
+
│
|
| 482 |
+
├── indices/
|
| 483 |
+
│ ├── bm25_index.pkl # BM25 index
|
| 484 |
+
│ ├── tfidf_vectorizer.pkl # TF-IDF vectorizer
|
| 485 |
+
│ ├── tfidf_matrix.pkl # TF-IDF matrix
|
| 486 |
+
│ ├── faiss_mini_index.bin # FAISS index (MiniLM)
|
| 487 |
+
│ └── faiss_mpnet_index.bin # FAISS index (MPNet)
|
| 488 |
+
│
|
| 489 |
+
├── embeddings/
|
| 490 |
+
│ ├── sbert_mini_embeddings.npy # MiniLM embeddings
|
| 491 |
+
│ ├── sbert_mpnet_embeddings.npy # MPNet embeddings
|
| 492 |
+
│ ├── use_embeddings.npy # USE embeddings
|
| 493 |
+
│ └── multilang_embeddings.npy # Multilingual embeddings
|
| 494 |
+
│
|
| 495 |
+
├── data/
|
| 496 |
+
│ ├── queries.json # Dataset queries
|
| 497 |
+
│ ├── responses.json # Dataset responses
|
| 498 |
+
│ └── query_patterns.json # Pre-computed patterns
|
| 499 |
+
│
|
| 500 |
+
└── scripts/
|
| 501 |
+
├── inference.py # Inference script
|
| 502 |
+
├── app_flask.py # Flask API
|
| 503 |
+
└── app_gradio.py # Gradio interface
|
| 504 |
+
```
|
| 505 |
+
|
| 506 |
+
---
|
| 507 |
+
|
| 508 |
+
## ⚡ Performance
|
| 509 |
+
|
| 510 |
+
### Inference Speed
|
| 511 |
+
- **Average latency**: 150-200ms per query
|
| 512 |
+
- **With context**: +20ms overhead
|
| 513 |
+
- **Hardware**: CPU only (no GPU needed)
|
| 514 |
+
- **Memory usage**: ~1.5GB RAM (all models loaded)
|
| 515 |
+
|
| 516 |
+
### Accuracy Metrics
|
| 517 |
+
- **Top-1 Accuracy**: 92%
|
| 518 |
+
- **Top-3 Accuracy**: 97%
|
| 519 |
+
- **Precision@1**: 89%
|
| 520 |
+
- **Recall@1**: 91%
|
| 521 |
+
- **F1-Score**: 90%
|
| 522 |
+
|
| 523 |
+
### Benchmark (4,079 queries)
|
| 524 |
+
|
| 525 |
+
| Technique | Solo Accuracy | Contribution |
|
| 526 |
+
|-----------|--------------|--------------|
|
| 527 |
+
| SBERT MPNet | 85% | Highest |
|
| 528 |
+
| SBERT MiniLM | 82% | High |
|
| 529 |
+
| BM25 | 78% | Medium |
|
| 530 |
+
| USE | 80% | High |
|
| 531 |
+
| TF-IDF | 75% | Medium |
|
| 532 |
+
| Fuzzy | 72% | Medium |
|
| 533 |
+
| Pattern | 88% | High (for specific intents) |
|
| 534 |
+
| **ENSEMBLE** | **92%** | **Best** |
|
| 535 |
+
|
| 536 |
+
---
|
| 537 |
+
|
| 538 |
+
## 🎯 Use Cases
|
| 539 |
+
|
| 540 |
+
- ✅ **Customer Service** - FAQ automation, support chatbot
|
| 541 |
+
- ✅ **Personal Assistant** - General conversation, task helper
|
| 542 |
+
- ✅ **Educational Bot** - Q&A system, learning companion
|
| 543 |
+
- ✅ **Information Retrieval** - Document search, knowledge base
|
| 544 |
+
- ✅ **Multilingual Support** - ID/EN auto-detection
|
| 545 |
+
- ✅ **Context-Aware Chat** - Multi-turn conversations
|
| 546 |
+
- ✅ **Rapid Prototyping** - No training needed, instant deployment
|
| 547 |
+
|
| 548 |
+
---
|
| 549 |
+
|
| 550 |
+
## 🔄 Update Model
|
| 551 |
+
|
| 552 |
+
Untuk menambah data atau update model:
|
| 553 |
+
|
| 554 |
+
1. **Tambah data** ke dataset `Lyon28/Caca-Behavior`
|
| 555 |
+
2. **Re-run notebook** untuk rebuild semua indices
|
| 556 |
+
3. **Upload ulang** semua file ke repo
|
| 557 |
+
|
| 558 |
+
```bash
|
| 559 |
+
# Re-build CACA
|
| 560 |
+
python build_caca.py
|
| 561 |
+
|
| 562 |
+
# Upload to HF Hub
|
| 563 |
+
python upload_to_hub.py
|
| 564 |
+
```
|
| 565 |
+
|
| 566 |
+
---
|
| 567 |
+
|
| 568 |
+
## 🛠️ Development
|
| 569 |
+
|
| 570 |
+
### Local Development
|
| 571 |
+
|
| 572 |
+
```bash
|
| 573 |
+
# Clone repository
|
| 574 |
+
git clone https://huggingface.co/Lyon28/Caca-Chatbot-V2-V2
|
| 575 |
+
cd Caca-Chatbot
|
| 576 |
+
|
| 577 |
+
# Install dependencies
|
| 578 |
+
pip install -r requirements.txt
|
| 579 |
+
|
| 580 |
+
# Run tests
|
| 581 |
+
python test_caca.py
|
| 582 |
+
|
| 583 |
+
# Start Flask API
|
| 584 |
+
python app_flask.py
|
| 585 |
+
|
| 586 |
+
# Or start Gradio
|
| 587 |
+
python app_gradio.py
|
| 588 |
+
```
|
| 589 |
+
|
| 590 |
+
### Docker Deployment
|
| 591 |
+
|
| 592 |
+
```dockerfile
|
| 593 |
+
FROM python:3.9-slim
|
| 594 |
+
|
| 595 |
+
WORKDIR /app
|
| 596 |
+
|
| 597 |
+
COPY requirements.txt .
|
| 598 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 599 |
+
|
| 600 |
+
COPY . .
|
| 601 |
+
|
| 602 |
+
EXPOSE 7860
|
| 603 |
+
|
| 604 |
+
CMD ["python", "app_gradio.py"]
|
| 605 |
+
```
|
| 606 |
+
|
| 607 |
+
---
|
| 608 |
+
|
| 609 |
+
## 📝 License
|
| 610 |
+
|
| 611 |
+
Model ini dirilis dengan lisensi **MIT License**. Bebas digunakan untuk keperluan komersial maupun non-komersial dengan atribusi.
|
| 612 |
+
|
| 613 |
+
---
|
| 614 |
+
|
| 615 |
+
## 👨💻 Author
|
| 616 |
+
|
| 617 |
+
**Lyon28** - AI Enthusiast & Developer
|
| 618 |
+
|
| 619 |
+
- 🤗 HuggingFace: [@Lyon28](https://huggingface.co/Lyon28)
|
| 620 |
+
- 📊 Dataset: [Caca-Behavior](https://huggingface.co/datasets/Lyon28/Caca-Behavior)
|
| 621 |
+
- 🤖 Model: [Caca-Chatbot](https://huggingface.co/Lyon28/Caca-Chatbot-V2-V2)
|
| 622 |
+
|
| 623 |
+
Dibuat dengan ❤️ menggunakan Python, Sentence-Transformers, FAISS, dan HuggingFace 🚀
|
| 624 |
+
|
| 625 |
+
---
|
| 626 |
+
|
| 627 |
+
## 🙏 Acknowledgments
|
| 628 |
+
|
| 629 |
+
### Models & Libraries
|
| 630 |
+
- [Sentence-Transformers](https://www.sbert.net/) - SBERT models
|
| 631 |
+
- [FAISS](https://github.com/facebookresearch/faiss) - Vector similarity search
|
| 632 |
+
- [TensorFlow Hub](https://tfhub.dev/) - Universal Sentence Encoder
|
| 633 |
+
- [rank-bm25](https://github.com/dorianbrown/rank_bm25) - BM25 implementation
|
| 634 |
+
- [FuzzyWuzzy](https://github.com/seatgeek/fuzzywuzzy) - Fuzzy string matching
|
| 635 |
+
|
| 636 |
+
### Datasets
|
| 637 |
+
- [Lyon28/Caca-Behavior](https://huggingface.co/datasets/Lyon28/Caca-Behavior) - Training dataset
|
| 638 |
+
|
| 639 |
+
### Pre-trained Models
|
| 640 |
+
- `all-MiniLM-L6-v2` - Fast semantic embeddings
|
| 641 |
+
- `paraphrase-mpnet-base-v2` - Accurate semantic embeddings
|
| 642 |
+
- `universal-sentence-encoder/4` - Google's sentence encoder
|
| 643 |
+
- `paraphrase-multilingual-mpnet-base-v2` - Multilingual support
|
| 644 |
+
|
| 645 |
+
---
|
| 646 |
+
|
| 647 |
+
## 📧 Contact & Support
|
| 648 |
+
|
| 649 |
+
Untuk pertanyaan, bug report, atau feature request:
|
| 650 |
+
|
| 651 |
+
- 💬 **Issues**: [Open an issue](https://huggingface.co/Lyon28/Caca-Chatbot-V2-V2/discussions)
|
| 652 |
+
- 📧 **Email**: cacatransformers@gmail.com
|
| 653 |
+
---
|
| 654 |
+
|
| 655 |
+
## 🔗 Quick Links
|
| 656 |
+
|
| 657 |
+
- 🤗 [Model on Hugging Face](https://huggingface.co/Lyon28/Caca-Chatbot-V2-V2)
|
| 658 |
+
- 📊 [Dataset](https://huggingface.co/datasets/Lyon28/Caca-Behavior)
|
| 659 |
+
- 🚀 [Live Demo](https://huggingface.co/spaces/Lyon28/Caca-Chatbot-V2-Demo)
|
| 660 |
+
- 📚 [Documentation](https://github.com/Lyon28/Caca-Chatbot-V2-V2)
|
| 661 |
+
- 💻 [Source Code](https://github.com/Lyon-28/caca-transformers)
|
| 662 |
+
|
| 663 |
+
---
|
| 664 |
+
|
| 665 |
+
## ⭐ Star History
|
| 666 |
+
|
| 667 |
+
Jika CACA berguna untuk project lo, jangan lupa kasih **⭐ STAR** ya bro! 🙏
|
| 668 |
+
|
| 669 |
+
---
|
| 670 |
+
|
| 671 |
+
## 🚀 Roadmap
|
| 672 |
+
|
| 673 |
+
### Version 2.0 (Coming Soon)
|
| 674 |
+
- [ ] Fine-tuned small LLM integration
|
| 675 |
+
- [ ] Voice input/output support
|
| 676 |
+
- [ ] Multi-modal (image understanding)
|
| 677 |
+
- [ ] Real-time learning from feedback
|
| 678 |
+
- [ ] API rate limiting & caching
|
| 679 |
+
- [ ] Better context window (10+ turns)
|
| 680 |
+
- [ ] Emotion detection
|
| 681 |
+
- [ ] Personality customization
|
| 682 |
+
|
| 683 |
+
---
|
| 684 |
+
|
| 685 |
+
<div align="center">
|
| 686 |
+
|
| 687 |
+
**Built with 🔥 by Lyon28**
|
| 688 |
+
|
| 689 |
+
Made possible by the amazing open-source community 🙌
|
| 690 |
+
|
| 691 |
+
</div>
|