-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.py
59 lines (46 loc) · 1.23 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
47
48
49
50
51
52
53
54
55
56
57
58
59
from spacy.lang.en import English
import numpy
from flask import Flask, render_template, request
import json
import pickle
import os
import time
import tensorflow as tf
from tensorflow.keras import layers, models, regularizers
from voc import voc
import random
nlp = English()
tokenizer = nlp.Defaults.create_tokenizer(nlp)
PAD_Token=0
app = Flask(__name__)
model= models.load_model('mymodel.h5')
with open("mydata.pickle", "rb") as f:
data = pickle.load(f)
def predict(ques):
ques= data.getQuestionInNum(ques)
ques=numpy.array(ques)
# ques=ques/255
ques = numpy.expand_dims(ques, axis = 0)
y_pred = model.predict(ques)
res=numpy.argmax(y_pred, axis=1)
return res
def getresponse(results):
tag= data.index2tags[int(results)]
response= data.response[tag]
return response
def chat(inp):
while True:
inp_x=inp.lower()
results = predict(inp_x)
response= getresponse(results)
return random.choice(response)
@app.route("/")
def home():
return render_template("index.html")
@app.route("/get")
def get_bot_response():
userText = request.args.get('msg')
time.sleep(1)
return str(chat(userText))
if __name__ == "__main__":
app.run()