Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,17 +6,27 @@ import numpy as np
|
|
6 |
from sklearn.metrics.pairwise import cosine_similarity
|
7 |
import json
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
class VietnameseChatbot:
|
10 |
def __init__(self, model_name='intfloat/multilingual-e5-small'):
|
11 |
"""
|
12 |
Initialize the Vietnamese chatbot with pre-loaded model and conversation data
|
13 |
"""
|
14 |
-
# Load pre-trained model and tokenizer
|
15 |
-
|
16 |
-
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
17 |
-
|
18 |
-
print("Loading model...")
|
19 |
-
self.model = AutoModel.from_pretrained(model_name, torch_dtype=torch.float16)
|
20 |
|
21 |
# Load comprehensive conversation dataset
|
22 |
self.conversation_data = self._load_conversation_data()
|
@@ -40,7 +50,7 @@ class VietnameseChatbot:
|
|
40 |
{"query": "Bạn từ đâu đến?", "response": "Tôi được phát triển bởi một nhóm kỹ sư AI, và tôn chỉ của tôi là hỗ trợ con người."},
|
41 |
|
42 |
# Small talk
|
43 |
-
{"query": "Bạn thích gì?", "response": "Tôi thích học hỏi và
|
44 |
{"query": "Bạn có thể làm gì?", "response": "Tôi có thể trò chuyện, trả lời câu hỏi, và hỗ trợ bạn trong nhiều tình huống khác nhau."},
|
45 |
|
46 |
# Weather and time
|
@@ -56,9 +66,11 @@ class VietnameseChatbot:
|
|
56 |
{"query": "Bye", "response": "Tạm biệt! Rất vui được trò chuyện với bạn."},
|
57 |
]
|
58 |
|
|
|
59 |
def _precompute_embeddings(self):
|
60 |
"""
|
61 |
Pre-compute embeddings for all conversation queries
|
|
|
62 |
"""
|
63 |
embeddings = []
|
64 |
for item in self.conversation_data:
|
@@ -119,6 +131,14 @@ class VietnameseChatbot:
|
|
119 |
print(f"Response generation error: {e}")
|
120 |
return "Đã xảy ra lỗi. Xin vui lòng thử lại."
|
121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
def main():
|
123 |
st.set_page_config(
|
124 |
page_title="Trợ Lý AI Tiếng Việt",
|
@@ -128,8 +148,8 @@ def main():
|
|
128 |
st.title("🤖 Trợ Lý AI Tiếng Việt")
|
129 |
st.caption("Trò chuyện với trợ lý AI được phát triển bằng mô hình đa ngôn ngữ")
|
130 |
|
131 |
-
# Initialize chatbot
|
132 |
-
chatbot =
|
133 |
|
134 |
# Chat history in session state
|
135 |
if 'messages' not in st.session_state:
|
|
|
6 |
from sklearn.metrics.pairwise import cosine_similarity
|
7 |
import json
|
8 |
|
9 |
+
@st.cache_resource
|
10 |
+
def load_model_and_tokenizer(model_name='intfloat/multilingual-e5-small'):
|
11 |
+
"""
|
12 |
+
Cached function to load model and tokenizer
|
13 |
+
This ensures the model is loaded only once and reused
|
14 |
+
"""
|
15 |
+
print("Loading tokenizer...")
|
16 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
17 |
+
|
18 |
+
print("Loading model...")
|
19 |
+
model = AutoModel.from_pretrained(model_name, torch_dtype=torch.float16)
|
20 |
+
|
21 |
+
return tokenizer, model
|
22 |
+
|
23 |
class VietnameseChatbot:
|
24 |
def __init__(self, model_name='intfloat/multilingual-e5-small'):
|
25 |
"""
|
26 |
Initialize the Vietnamese chatbot with pre-loaded model and conversation data
|
27 |
"""
|
28 |
+
# Load pre-trained model and tokenizer using cached function
|
29 |
+
self.tokenizer, self.model = load_model_and_tokenizer(model_name)
|
|
|
|
|
|
|
|
|
30 |
|
31 |
# Load comprehensive conversation dataset
|
32 |
self.conversation_data = self._load_conversation_data()
|
|
|
50 |
{"query": "Bạn từ đâu đến?", "response": "Tôi được phát triển bởi một nhóm kỹ sư AI, và tôn chỉ của tôi là hỗ trợ con người."},
|
51 |
|
52 |
# Small talk
|
53 |
+
{"query": "Bạn thích gì?", "response": "Tôi thích học hỏi và giú đỡ mọi người. Mỗi cuộc trò chuyện là một cơ hội để tôi phát triển."},
|
54 |
{"query": "Bạn có thể làm gì?", "response": "Tôi có thể trò chuyện, trả lời câu hỏi, và hỗ trợ bạn trong nhiều tình huống khác nhau."},
|
55 |
|
56 |
# Weather and time
|
|
|
66 |
{"query": "Bye", "response": "Tạm biệt! Rất vui được trò chuyện với bạn."},
|
67 |
]
|
68 |
|
69 |
+
@st.cache_data
|
70 |
def _precompute_embeddings(self):
|
71 |
"""
|
72 |
Pre-compute embeddings for all conversation queries
|
73 |
+
Cached to avoid recomputing on every run
|
74 |
"""
|
75 |
embeddings = []
|
76 |
for item in self.conversation_data:
|
|
|
131 |
print(f"Response generation error: {e}")
|
132 |
return "Đã xảy ra lỗi. Xin vui lòng thử lại."
|
133 |
|
134 |
+
@st.cache_resource
|
135 |
+
def initialize_chatbot():
|
136 |
+
"""
|
137 |
+
Cached function to initialize the chatbot
|
138 |
+
This ensures the chatbot is created only once
|
139 |
+
"""
|
140 |
+
return VietnameseChatbot()
|
141 |
+
|
142 |
def main():
|
143 |
st.set_page_config(
|
144 |
page_title="Trợ Lý AI Tiếng Việt",
|
|
|
148 |
st.title("🤖 Trợ Lý AI Tiếng Việt")
|
149 |
st.caption("Trò chuyện với trợ lý AI được phát triển bằng mô hình đa ngôn ngữ")
|
150 |
|
151 |
+
# Initialize chatbot using cached initialization
|
152 |
+
chatbot = initialize_chatbot()
|
153 |
|
154 |
# Chat history in session state
|
155 |
if 'messages' not in st.session_state:
|