|
Download README.md from chavudosoa/chatbot_model_h5: direct link, hf CLI and curl.
- Browser
- Download file 1.77 kB
-
https://huggingface.co/chavudosoa/chatbot_model_h5/resolve/main/README.md
- Command line
-
hf download hf://chavudosoa/chatbot_model_h5/README.md
-
curl -L -o README.md https://huggingface.co/chavudosoa/chatbot_model_h5/resolve/main/README.md
1.77 kB
| import numpy as np | |
| from keras.models import load_model | |
| from keras.preprocessing.sequence import pad_sequences | |
| import pickle | |
| # Load the model and tokenizer | |
| model = load_model('chatbot_model.h5') | |
| with open('tokenizer.pkl', 'rb') as tokenizer_file: | |
| tokenizer = pickle.load(tokenizer_file) | |
| # Function to generate a response with adjustable temperature | |
| 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 | |
| # Interactive chat | |
| 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) | |