Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -26,7 +26,6 @@ class ChatBot:
|
|
26 |
st.write(f"Knowledge base saved: {self.knowledge_base}")
|
27 |
|
28 |
def learn_and_response(self, user_input):
|
29 |
-
# Try to find a response
|
30 |
response = self.find_response(user_input)
|
31 |
if response is None:
|
32 |
st.write(f"I don't have a response for '{user_input}'. Please teach me:")
|
@@ -35,9 +34,8 @@ class ChatBot:
|
|
35 |
if st.button("Submit Response"):
|
36 |
if response:
|
37 |
self.teach_response(user_input, response)
|
38 |
-
|
39 |
-
|
40 |
-
return response # Return the learned response immediately
|
41 |
else:
|
42 |
st.warning("Please provide a response before submitting.")
|
43 |
else:
|
@@ -45,17 +43,18 @@ class ChatBot:
|
|
45 |
return response
|
46 |
|
47 |
def find_response(self, user_input):
|
48 |
-
|
49 |
for question in self.knowledge_base.get("questions", []):
|
50 |
if question['question'].lower() == user_input.lower():
|
51 |
return question['response']
|
52 |
return None
|
53 |
|
54 |
def teach_response(self, user_input, response):
|
55 |
-
|
56 |
new_question = {'question': user_input.lower(), 'response': response}
|
57 |
self.knowledge_base['questions'].append(new_question)
|
58 |
st.write(f"New question added: {new_question}")
|
|
|
59 |
|
60 |
if __name__ == "__main__":
|
61 |
# Initialize the chatbot with the JSON file
|
|
|
26 |
st.write(f"Knowledge base saved: {self.knowledge_base}")
|
27 |
|
28 |
def learn_and_response(self, user_input):
|
|
|
29 |
response = self.find_response(user_input)
|
30 |
if response is None:
|
31 |
st.write(f"I don't have a response for '{user_input}'. Please teach me:")
|
|
|
34 |
if st.button("Submit Response"):
|
35 |
if response:
|
36 |
self.teach_response(user_input, response)
|
37 |
+
st.success(f"Response saved for '{user_input}': {response}")
|
38 |
+
return response
|
|
|
39 |
else:
|
40 |
st.warning("Please provide a response before submitting.")
|
41 |
else:
|
|
|
43 |
return response
|
44 |
|
45 |
def find_response(self, user_input):
|
46 |
+
"""Find a response in the knowledge base."""
|
47 |
for question in self.knowledge_base.get("questions", []):
|
48 |
if question['question'].lower() == user_input.lower():
|
49 |
return question['response']
|
50 |
return None
|
51 |
|
52 |
def teach_response(self, user_input, response):
|
53 |
+
"""Teach the bot a new response."""
|
54 |
new_question = {'question': user_input.lower(), 'response': response}
|
55 |
self.knowledge_base['questions'].append(new_question)
|
56 |
st.write(f"New question added: {new_question}")
|
57 |
+
self.save_knowledge_base() # Save the updated knowledge base
|
58 |
|
59 |
if __name__ == "__main__":
|
60 |
# Initialize the chatbot with the JSON file
|