-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbot.py
34 lines (26 loc) · 921 Bytes
/
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
from flask import Flask, request, render_template
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
app = Flask(__name__)
# Create a new chatbot
chatbot = ChatBot('MyChatBot')
# Create a new trainer for the chatbot
trainer = ChatterBotCorpusTrainer(chatbot)
# Train the chatbot on the English corpus
trainer.train('chatterbot.corpus.english')
# Function to generate response based on user input
def generate_response(user_input):
response = chatbot.get_response(user_input)
return str(response)
# Route for home page
@app.route('/')
def home():
return render_template('index.html')
# Route for handling user input and generating response
@app.route('/get-response', methods=['POST'])
def get_response():
user_input = request.form['user_input']
response = generate_response(user_input)
return response
if __name__ == '__main__':
app.run(debug=True)