-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
37 lines (32 loc) · 1.06 KB
/
Copy pathtest.py
File metadata and controls
37 lines (32 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
'''
Creating the chating api
'''
import pickle
import random
# from textblob import TextBlob
# Load the trained pipeline and supporting files
with open("pickles/chatbot_pipeline.pkl", "rb") as file:
pipeline = pickle.load(file)
with open("pickles/label_encoder.pkl", "rb") as file:
label_encoder = pickle.load(file)
with open("pickles/responses.pkl", "rb") as file:
responses = pickle.load(file)
# Chatbot response function
def chatbot_response(user_input):
# Predict the intent
prediction = pipeline.predict([user_input])[0]
tag = label_encoder.inverse_transform([prediction])[0]
# Retrieve a random response for the predicted intent
for response in responses:
if tag in response:
return random.choice(response[tag])
return "I'm sorry, I didn't understand that."
# Chat loop
print("Bot: Chatbot is ready! Type 'exit' to quit.")
while True:
user_input = input("You: ")
if user_input.lower() == "exit":
print("Goodbye!")
break
response = chatbot_response(user_input)
print(f"Bot: {response}")