-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot.py
More file actions
29 lines (22 loc) · 790 Bytes
/
chatbot.py
File metadata and controls
29 lines (22 loc) · 790 Bytes
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
from flask import Flask, request, jsonify
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
from flask_cors import CORS
# Create a new Flask app
app = Flask(__name__)
CORS(app)
# Create a new chatbot instance
chatbot = ChatBot('MyBot')
# Set up the trainer
trainer = ChatterBotCorpusTrainer(chatbot)
# Train the chatbot with the English corpus
trainer.train("chatterbot.corpus.english")
@app.route('/chat', methods=['POST'])
def chat():
user_input = request.json.get('message')
if user_input.lower() == 'exit':
return jsonify({'response': 'Goodbye!'})
response = chatbot.get_response(user_input)
return jsonify({'response': str(response)})
if __name__ == "__main__":
app.run(debug=True)