def predict(self): global ITEM_QUEUE, RETRY_TIME retry = 0 # retry times logger.info(f"Start predict thread") while retry <= RETRY_TIME: try: item = ITEM_QUEUE.get(timeout=3) text = item["clean_text"] tokens = model.prepross(text) item["predict"] = model.predict(tokens) if len(tokens) else (1, 0, 0, 0) # write data fhandler = FileWriter(self.filename, mode=self.mode) with fhandler as writer: writer.write(json.dumps(item, ensure_ascii=False) + "\n") logger.debug(f"Predict item id is {item['id']}") # if get item right, re_initial retry retry = 0 except queue.Empty: logger.debug(f"Queue is empty, wait 1 seconds.") retry += 1 time.sleep(1) # exhausted RETRY_TIME logger.info(f"Data item is exhausted")
def predict(): data = request.json image = preprocess.process_image(data['image']) with graph.as_default(): predictions = model.predict(image)[0] return jsonify({'predictions': predictions.tolist()})
def get_bot_response_intent(): userText = request.args.get('msg') results = model.predict([bag_of_words(userText, words)])[0] results_index = np.argmax(results) tag = labels[results_index] if results[results_index] > 0.7: for tg in data["intents"]: if tg['tag'] == tag: responses = tg['responses'] return str(random.choice(responses)) else: return "I didn't get that please try again"
def chat(): # Logic for POST request if request.method == 'POST': user_input = request.get_json(force=True) results = model.predict([bag_of_words(user_input["user_message"], words)])[0] results_index = np.argmax(results) tag = labels[results_index] print(user_input["user_message"]) if results[results_index] > 0.7: for tg in data["intents"]: if tg['tag'] == tag: responses = tg['responses'] print(random.choice(responses)) else: return "I didn't get that please try again" # print("I didn't get that. PLease type another question") return jsonify("See ya")
# if you need to create the data: # test_data = process_test_data() # if you already have some saved: test_data = np.load('test_data.npy') fig = plt.figure() # plot last 12 of test data and predicted class for num, data in enumerate(test_data[:12]): # cat: [1,0] # dog: [0,1] img_num = data[1] img_data = data[0] y = fig.add_subplot(3, 4, num + 1) orig = img_data data = img_data.reshape(IMG_SIZE, IMG_SIZE, 1) model_out = model.predict([data])[0] if np.argmax(model_out) == 1: str_label = 'Dog' else: str_label = 'Cat' y.imshow(orig, cmap='gray') plt.title(str_label) y.axes.get_xaxis().set_visible(False) y.axes.get_yaxis().set_visible(False) plt.show()