Spaces:
Running
Running
Update chat.py
Browse files
chat.py
CHANGED
|
@@ -1,26 +1,43 @@
|
|
| 1 |
-
import
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
class KiswahiliChatbot:
|
| 5 |
-
def __init__(self,
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
def chat(self, user_input):
|
| 15 |
-
# Clean input
|
| 16 |
-
user_input_clean = re.sub(r'[^\w\s?]', '', user_input).lower()
|
| 17 |
if not user_input_clean:
|
| 18 |
return "Tafadhali andika ujumbe."
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
import os
|
| 3 |
+
import logging
|
| 4 |
+
import re
|
| 5 |
+
|
| 6 |
+
logging.basicConfig(level=logging.INFO)
|
| 7 |
+
logger = logging.getLogger(__name__)
|
| 8 |
|
| 9 |
class KiswahiliChatbot:
|
| 10 |
+
def __init__(self, model_path="./trained_bert_model"):
|
| 11 |
+
# Load responses
|
| 12 |
+
response_file = os.path.join(model_path, "responses.json")
|
| 13 |
+
if not os.path.exists(response_file):
|
| 14 |
+
raise FileNotFoundError(f"{response_file} not found. Please create responses.json")
|
| 15 |
+
|
| 16 |
+
with open(response_file, "r", encoding="utf-8") as f:
|
| 17 |
+
data = json.load(f)
|
| 18 |
+
self.responses = data.get("responses", [])
|
| 19 |
|
| 20 |
+
logger.info(f"✅ Loaded {len(self.responses)} canned responses")
|
| 21 |
+
|
| 22 |
+
def _clean_text(self, text: str) -> str:
|
| 23 |
+
text = re.sub(r'[^\w\s?]', '', text)
|
| 24 |
+
return ' '.join(text.split()).lower()
|
| 25 |
+
|
| 26 |
+
def chat(self, user_input: str) -> str:
|
| 27 |
+
user_input_clean = self._clean_text(user_input)
|
| 28 |
|
|
|
|
|
|
|
|
|
|
| 29 |
if not user_input_clean:
|
| 30 |
return "Tafadhali andika ujumbe."
|
| 31 |
|
| 32 |
+
# Very simple matching
|
| 33 |
+
if "habari" in user_input_clean:
|
| 34 |
+
return "Nzuri sana! Habari yako pia?"
|
| 35 |
+
elif "jina" in user_input_clean:
|
| 36 |
+
return "Mimi ni KiswahiliChetu."
|
| 37 |
+
elif "tanzania" in user_input_clean:
|
| 38 |
+
return "Tanzania ni nchi ya Afrika Mashariki yenye miji mikuu Dodoma na Dar es Salaam."
|
| 39 |
+
elif "hakuna matata" in user_input_clean:
|
| 40 |
+
return "Ni msemo wa Kiswahili unaomaanisha 'Hakuna shida'."
|
| 41 |
+
else:
|
| 42 |
+
# fallback
|
| 43 |
+
return self.responses[0] if self.responses else "Samahani, sielewi. Unaweza kuuliza kwa njia nyingine?"
|