-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbot.py
57 lines (47 loc) · 1.78 KB
/
chatbot.py
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
48
49
50
51
52
53
54
55
56
57
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
from chatterbot.comparisons import LevenshteinDistance
from chatterbot.response_selection import get_first_response
from flask import Flask, render_template, request
# Create a new ChatBot instance
chatbot = ChatBot('MyChatBot', logic_adapters=[
{
"import_path": "chatterbot.logic.BestMatch",
"statement_comparison_function": LevenshteinDistance, # Use the imported class
"response_selection_method": get_first_response, # Use the imported function
"default_response": "I'm sorry, I don't have a specific answer for that.",
"maximum_similarity_threshold": 0.50
}
]
)
# Create a new trainer for the chatbot
trainer = ChatterBotCorpusTrainer(chatbot)
# Train the chatbot on the English language corpus data
trainer.train('chatterbot.corpus.english','chatterbot.corpus.english.conversations')
# Function to interact with the chatbot
'''def chat_with_bot():
print("Type 'exit' to end the conversation.")
while True:
user_input = input("You: ")
if user_input.lower() == 'exit':
print("Goodbye!")
break
response = chatbot.get_response(user_input)
print(f"ChatBot: {response}")
# Start the conversation
if __name__ == "__main__":
chat_with_bot()'''
# Create the Flask app
app = Flask(__name__)
# Define the route for the home page
@app.route('/')
def index():
return render_template('index.html')
# Define the route to handle user input and chatbot response
@app.route('/get_response', methods=['POST'])
def get_response():
user_input = request.form['user_input']
response = chatbot.get_response(user_input).text
return {'response': response}
if __name__ == '__main__':
app.run(debug=True)