Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,7 @@ class ChatBot:
|
|
8 |
def __init__(self, knowledge_base_file='knowledge_base.json'):
|
9 |
self.knowledge_base_file = knowledge_base_file
|
10 |
self.knowledge_base = self.load_knowledge_base()
|
|
|
11 |
|
12 |
def load_knowledge_base(self):
|
13 |
"""Load knowledge base from the JSON file."""
|
@@ -15,12 +16,14 @@ class ChatBot:
|
|
15 |
with open(self.knowledge_base_file, "r") as f:
|
16 |
return json.load(f)
|
17 |
except FileNotFoundError:
|
|
|
18 |
return {"questions": []} # Return an empty structure if file does not exist
|
19 |
|
20 |
def save_knowledge_base(self):
|
21 |
"""Save knowledge base to the JSON file."""
|
22 |
with open(self.knowledge_base_file, 'w') as f:
|
23 |
json.dump(self.knowledge_base, f, indent=4)
|
|
|
24 |
|
25 |
def learn_and_response(self, user_input):
|
26 |
# Try to find a response
|
@@ -29,7 +32,6 @@ class ChatBot:
|
|
29 |
st.write(f"I don't have a response for '{user_input}'. Please teach me:")
|
30 |
# Display a text input to teach the bot
|
31 |
response = st.text_input(f"Teach me a response for '{user_input}':", key="teach_input")
|
32 |
-
# When the user provides a response and submits it, teach the bot
|
33 |
if st.button("Submit Response"):
|
34 |
if response:
|
35 |
self.teach_response(user_input, response)
|
@@ -38,6 +40,8 @@ class ChatBot:
|
|
38 |
return response # Return the learned response immediately
|
39 |
else:
|
40 |
st.warning("Please provide a response before submitting.")
|
|
|
|
|
41 |
return response
|
42 |
|
43 |
def find_response(self, user_input):
|
@@ -51,6 +55,7 @@ class ChatBot:
|
|
51 |
# Add the new question-response pair to the knowledge base
|
52 |
new_question = {'question': user_input.lower(), 'response': response}
|
53 |
self.knowledge_base['questions'].append(new_question)
|
|
|
54 |
|
55 |
if __name__ == "__main__":
|
56 |
# Initialize the chatbot with the JSON file
|
|
|
8 |
def __init__(self, knowledge_base_file='knowledge_base.json'):
|
9 |
self.knowledge_base_file = knowledge_base_file
|
10 |
self.knowledge_base = self.load_knowledge_base()
|
11 |
+
st.write(f"Loaded knowledge base: {self.knowledge_base}")
|
12 |
|
13 |
def load_knowledge_base(self):
|
14 |
"""Load knowledge base from the JSON file."""
|
|
|
16 |
with open(self.knowledge_base_file, "r") as f:
|
17 |
return json.load(f)
|
18 |
except FileNotFoundError:
|
19 |
+
st.write("Knowledge base file not found, starting with an empty knowledge base.")
|
20 |
return {"questions": []} # Return an empty structure if file does not exist
|
21 |
|
22 |
def save_knowledge_base(self):
|
23 |
"""Save knowledge base to the JSON file."""
|
24 |
with open(self.knowledge_base_file, 'w') as f:
|
25 |
json.dump(self.knowledge_base, f, indent=4)
|
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
|
|
|
32 |
st.write(f"I don't have a response for '{user_input}'. Please teach me:")
|
33 |
# Display a text input to teach the bot
|
34 |
response = st.text_input(f"Teach me a response for '{user_input}':", key="teach_input")
|
|
|
35 |
if st.button("Submit Response"):
|
36 |
if response:
|
37 |
self.teach_response(user_input, response)
|
|
|
40 |
return response # Return the learned response immediately
|
41 |
else:
|
42 |
st.warning("Please provide a response before submitting.")
|
43 |
+
else:
|
44 |
+
st.write(f"Found response for '{user_input}': {response}")
|
45 |
return response
|
46 |
|
47 |
def find_response(self, user_input):
|
|
|
55 |
# Add the new question-response pair to the knowledge base
|
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
|