File size: 1,388 Bytes
b90ce49
 
232d7de
b90ce49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
38
39
40
41
42
43
44
45
46
47
import spacy
import string
import joblib
# Load the saved model
model_filename = 'chatbot_model.joblib'
loaded_model = joblib.load(model_filename)

# Load spaCy's English model
nlp = spacy.load('en_core_web_sm', disable=['parser', 'ner'])

def preprocess_text(text):
    text = text.lower()
    text = text.translate(str.maketrans('', '', string.punctuation))
    doc = nlp(text)
    text = " ".join([token.lemma_ for token in doc])
    return text

def get_model_response(question):
    processed_question = preprocess_text(question)
    response = loaded_model.predict([processed_question])[0]
    return response

print("Your chatbot is ready. You can start a conversation or type 'quit' to exit.")

while True:
    try:
        user_input = input("You: ").strip()

        if len(user_input) == 0:
            print("Chatbot: Please provide a valid input.")
            continue

        if any(keyword in user_input.lower() for keyword in ["bye", "goodbye", "quit"]):
            print("Chatbot: Goodbye! It was a pleasure assisting you.")
            break

        response = get_model_response(user_input)

        if len(response) == 0:
            print("Chatbot: Empty response received. Please try again.")
            continue

        print(f"Chatbot: {response}\n")

    except KeyboardInterrupt:
        print("Chatbot: You ended the conversation. Goodbye!")
        break