Downloads · 30 days
0
chavudosoa/chatbot_model_h5
chatbot_model_h5 is a machine learning model from chavudosoa. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
import numpy as np from keras.models import loadmodel from keras.preprocessing.sequence import padsequences import pickle
Downloads · 30 days
0
Access
Public
Updated Jan 9, 2024
Repo size
37.8 MB
Likes
0
Public
Click a slice to open those files.
.h537.5 MB · 99%
From the Hugging Face model README
import numpy as np from keras.models import load_model from keras.preprocessing.sequence import pad_sequences import pickle
model = load_model('chatbot_model.h5')
with open('tokenizer.pkl', 'rb') as tokenizer_file: tokenizer = pickle.load(tokenizer_file)
def generate_response(seed_text, num_words, temperature=1.0): for _ in range(num_words): token_list = tokenizer.texts_to_sequences([seed_text])[0]
# 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_text
print("Chatbot: Hello! Type 'exit' to end the conversation.") while True: user_input = input("You: ").lower() # Convert to lowercase for consistency if user_input == 'exit': print("Chatbot: Goodbye!") break
response = generate_response(user_input, num_words=20, temperature=0.5)
print("Chatbot:", response)