Views
No views yet
# Ensure the sequence length does not exceed the model's input shape
token_list = pad_sequences([token_list], maxlen=model.input_shape[1], padding='pre')
# Predict the next word probabilities
predicted_probs = model.predict(token_list, verbose=0)[0]
# Adjust probabilities with temperature
scaled_probs = np.log(predicted_probs) / temperature
exp_probs = np.exp(scaled_probs)
predicted_probs = exp_probs / np.sum(exp_probs)
# Sample the next word index based on adjusted probabilities
predicted_id = np.random.choice(len(predicted_probs), size=1, p=predicted_probs)[0]
# Map the index to the corresponding word
output_word = tokenizer.index_word.get(predicted_id, 'unknown')
seed_text += " " + output_word
return seed_textresponse = generate_response(user_input, num_words=20, temperature=0.5)
print("Chatbot:", response)