File size: 1,279 Bytes
e24ec36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re
from chatbotTrainer import ChatbotTrainer  # Import the ChatbotTrainer class


def main():
    # Initialize the chatbot
    chatbot_trainer = ChatbotTrainer()

    # Ensure the model and tokenizer are loaded
    if chatbot_trainer.model is None:
        chatbot_trainer.load_model_file()

    print("Chatbot is ready. Type 'exit' to end the conversation.")

    while True:
        try:
            user_input = input("You: ").strip()
            if not user_input:
                print("Chatbot: Please say something, I'm here to help!")
                continue

            if user_input.lower() == "exit":
                print("Chatbot: Goodbye! Have a great day!")
                break

            # Generate a response
            response = chatbot_trainer.generate_response(user_input)

            # Handle empty or invalid responses
            if not response or response.strip() == "":
                response = "I'm sorry, I don't have a response for that."

            print(f"Alan: {response}")
        except Exception as e:
            print(f"Chatbot: An error occurred while generating a response. ({str(e)})")


# Run the chatbot if the script is executed directly
if __name__ == "__main__":
    main()