Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +15 -23
- chatbot_utils.py +14 -3
app.py
CHANGED
@@ -68,31 +68,23 @@ for msg in st.session_state.messages:
|
|
68 |
st.markdown('</div>', unsafe_allow_html=True) # Close chat-box
|
69 |
st.markdown('</div>', unsafe_allow_html=True) # Close chat-container
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
with st.form(key="chat_form"):
|
75 |
-
user_input = st.text_input("π¬ α₯α«ααα α«α΅αα‘:", value=st.session_state.input, key="input_form")
|
76 |
submit = st.form_submit_button("ααα΅ α αα£")
|
77 |
|
78 |
-
if submit:
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
# Append user input
|
85 |
-
st.session_state.messages.append({"sender": "user", "text": user_input})
|
86 |
-
|
87 |
-
# Generate response
|
88 |
-
response = bot.get_answer(user_input)
|
89 |
-
if response == "__OUT_OF_SCOPE__":
|
90 |
-
response = "αα
αα³α£ αα
αα α₯α«α αα΅α°αα α αα»αα©αα’ α₯α£αα α αα αααα΅ αααα©α’"
|
91 |
-
|
92 |
-
st.session_state.messages.append({"sender": "bot", "text": response})
|
93 |
-
|
94 |
-
# Reset input manually
|
95 |
-
st.session_state.input_form = ""
|
96 |
|
|
|
|
|
|
|
|
|
97 |
|
|
|
|
|
98 |
|
|
|
68 |
st.markdown('</div>', unsafe_allow_html=True) # Close chat-box
|
69 |
st.markdown('</div>', unsafe_allow_html=True) # Close chat-container
|
70 |
|
71 |
+
# Form with clear_on_submit and direct input capture
|
72 |
+
with st.form(key="chat_form", clear_on_submit=True):
|
73 |
+
user_input = st.text_input("π¬ α₯α«ααα α«α΅αα‘:")
|
|
|
|
|
74 |
submit = st.form_submit_button("ααα΅ α αα£")
|
75 |
|
76 |
+
if submit:
|
77 |
+
if user_input.strip() == "":
|
78 |
+
st.warning("α₯α£αα α₯α«α α«α΅αα‘α’")
|
79 |
+
else:
|
80 |
+
# Append user input
|
81 |
+
st.session_state.messages.append({"sender": "user", "text": user_input})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
+
# Generate response
|
84 |
+
response = bot.get_answer(user_input)
|
85 |
+
if response == "__OUT_OF_SCOPE__":
|
86 |
+
response = "αα
αα³α£ αα
αα α₯α«α αα΅α°αα α αα»αα©α α¨α₯α αα¨α ααͺ ααα’ α₯α£αα α αα αααα΅ αααα©α’"
|
87 |
|
88 |
+
# Append bot response
|
89 |
+
st.session_state.messages.append({"sender": "bot", "text": response})
|
90 |
|
chatbot_utils.py
CHANGED
@@ -6,7 +6,7 @@ import numpy as np
|
|
6 |
from sklearn.metrics.pairwise import cosine_similarity
|
7 |
|
8 |
class AmharicChatbot:
|
9 |
-
def __init__(self, csv_path, threshold=0.
|
10 |
self.df = pd.read_csv(csv_path)
|
11 |
self.model = SentenceTransformer("intfloat/multilingual-e5-small")
|
12 |
self.threshold = threshold
|
@@ -23,10 +23,21 @@ class AmharicChatbot:
|
|
23 |
def get_answer(self, user_question, k=1):
|
24 |
user_embedding = self.model.encode([f"query: {user_question}"])[0].astype("float32")
|
25 |
D, I = self.index.search(np.array([user_embedding]), k)
|
|
|
|
|
|
|
|
|
26 |
top_idx = I[0][0]
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
29 |
score = cosine_similarity([user_embedding], [top_embedding])[0][0]
|
|
|
30 |
if score < self.threshold:
|
31 |
return "__OUT_OF_SCOPE__"
|
|
|
32 |
return self.df.iloc[top_idx]["answer"]
|
|
|
|
6 |
from sklearn.metrics.pairwise import cosine_similarity
|
7 |
|
8 |
class AmharicChatbot:
|
9 |
+
def __init__(self, csv_path, threshold=0.80):
|
10 |
self.df = pd.read_csv(csv_path)
|
11 |
self.model = SentenceTransformer("intfloat/multilingual-e5-small")
|
12 |
self.threshold = threshold
|
|
|
23 |
def get_answer(self, user_question, k=1):
|
24 |
user_embedding = self.model.encode([f"query: {user_question}"])[0].astype("float32")
|
25 |
D, I = self.index.search(np.array([user_embedding]), k)
|
26 |
+
|
27 |
+
if len(I[0]) == 0:
|
28 |
+
return "__OUT_OF_SCOPE__"
|
29 |
+
|
30 |
top_idx = I[0][0]
|
31 |
+
top_embedding = self.embeddings[top_idx]
|
32 |
+
|
33 |
+
# Normalize embeddings before cosine similarity
|
34 |
+
user_embedding = user_embedding / np.linalg.norm(user_embedding)
|
35 |
+
top_embedding = top_embedding / np.linalg.norm(top_embedding)
|
36 |
+
|
37 |
score = cosine_similarity([user_embedding], [top_embedding])[0][0]
|
38 |
+
|
39 |
if score < self.threshold:
|
40 |
return "__OUT_OF_SCOPE__"
|
41 |
+
|
42 |
return self.df.iloc[top_idx]["answer"]
|
43 |
+
|