-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
46 lines (36 loc) · 1.57 KB
/
main.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
import numpy as np
import pickle, random, string
from keras.models import load_model
from flask import Flask, render_template, request
from tensorflow.keras.preprocessing.sequence import pad_sequences
app = Flask(__name__)
# Define your model and tokenizer here
chatbot_model = load_model('model/model.h5')
tokenizer = pickle.load(open('model/tokenizer.pkl','rb'))
max_sequence_length = pickle.load(open('model/max_sequence_length.pkl','rb'))
le = pickle.load(open('model/le.pkl','rb'))
responses = pickle.load(open('model/responses.pkl','rb'))
@app.route('/')
def home():
return render_template("index.html")
@app.route('/chat', methods=['POST'])
def chat():
texts_p = []
prediction_input = request.form['message']
# Remove punctuation and convert to lowercase
prediction_input = [letter.lower() for letter in prediction_input if letter not in string.punctuation]
prediction_input = ''.join(prediction_input)
texts_p.append(prediction_input)
# Tokenize and pad the input
prediction_input = tokenizer.texts_to_sequences(texts_p)
prediction_input = np.array(prediction_input).reshape(-1)
prediction_input = pad_sequences([prediction_input], max_sequence_length)
# Get the model's output prediction
output = chatbot_model.predict(prediction_input)
output = output.argmax()
# Find the corresponding response based on the predicted tag
response_tag = le.inverse_transform([output])[0]
response = random.choice(responses[response_tag])
return {'response': response}
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)