-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
41 lines (31 loc) · 1018 Bytes
/
app.py
File metadata and controls
41 lines (31 loc) · 1018 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
30
31
32
33
34
35
36
37
38
39
40
41
from flask import Flask, render_template, request
import pickle
import json
import random
app = Flask(__name__)
# Load the trained model and vectorizer
with open('model/chatbot_model.pkl', 'rb') as f:
best_model = pickle.load(f)
with open('model/vectorizer.pkl', 'rb') as f:
vectorizer = pickle.load(f)
# Load the intents data
with open('dataset/intents1.json', 'r') as f:
intents = json.load(f)
def chatbot_response(user_input):
input_text = vectorizer.transform([user_input])
predicted_intent = best_model.predict(input_text)[0]
for intent in intents['intents']:
if intent['tag'] == predicted_intent:
response = random.choice(intent['responses'])
break
return response
@app.route('/')
def home():
return render_template('index.html')
@app.route('/chat', methods=['POST'])
def chat():
user_input = request.form['user_input']
response = chatbot_response(user_input)
return response
if __name__ == '__main__':
app.run(debug=True)