What is the most lightweight chatbot model in huggingface? Could you write a code in…
- User
What is the most lightweight chatbot model in huggingface? Could you write a code in python to chat with it?
- Assistant
What is the most lightweight chatbot model in huggingface? Could you write a code in python to chat with it?
One of the most lightweight chatbot models available on Hugging Face is microsoft/DialoGPT-small, which has approximately 117 million parameters. This model is suitable for applications where computational resources are limited but still provides coherent and contextually relevant responses.
Below is a Python script that demonstrates how to interact with DialoGPT-small using the Hugging Face transformers library. This script sets up a simple command-line chatbot where you can type messages and receive responses from the model.
Python Installed: Ensure you have Python 3.7 or later installed.
Install Required Libraries: You'll need the transformers and torch libraries. You can install them using pip:
pip install transformers torch
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
def main():
# Load pre-trained model tokenizer (vocabulary)
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-small")
# Load pre-trained model weights
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-small")
# Set the model to evaluation mode to deactivate dropout modules
model.eval()
# Initialize the chat history
chat_history = []
print("Chatbot is ready! Type 'exit' or 'quit' to end the conversation.\n")
while True:
# Get user input
user_input = input("You: ")
if user_input.lower() in {"exit", "quit"}:
print("Chatbot: Goodbye!")
break
# Encode the user input and add it to the chat history
new_user_input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors='pt')
# Append the new user input tokens to the chat history
chat_history.append(new_user_input_ids)
# Concatenate all user inputs and bot responses into a single tensor
if len(chat_history) > 0:
bot_input_ids = torch.cat(chat_history, dim=-1)
else:
bot_input_ids = new_user_input_ids
# Generate a response from the model
with torch.no_grad():
output_ids = model.generate(
bot_input_ids,
max_length=1000,
pad_token_id=tokenizer.eos_token_id,
no_repeat_ngram_size=3,
do_sample=True,
top_k=50,
top_p=0.95,
temperature=0.75
)
# Extract the response tokens from the output
response_ids = output_ids[:, bot_input_ids.shape[-1]:]
# Decode the response tokens to a string
response = tokenizer.decode(response_ids[0], skip_special_tokens=True)
# Print the chatbot's response
print(f"Chatbot: {response}\n")
# Append the response tokens to the chat history
chat_history.append(response_ids)
# Optional: To limit the chat history size and save memory
if len(chat_history) > 10:
chat_history = chat_history[-10:]
if __name__ == "__main__":
main()
Loading the Model and Tokenizer:
AutoTokenizer and AutoModelForCausalLM from Hugging Face's transformers library to load the DialoGPT-small model and its corresponding tokenizer.Chat Loop:
'exit' or 'quit'.chat_history to maintain the context of the conversation.Generating Responses:
chat_history.max_length, no_repeat_ngram_size, top_k, top_p, and temperature control the diversity and coherence of the generated responses.Maintaining Chat History:
Save the Script: Save the above Python script to a file, e.g., chatbot.py.
Run the Script:
python chatbot.py
Interact: Start typing your messages and see the chatbot's responses. Type 'exit' or 'quit' to terminate the conversation.
Chatbot is ready! Type 'exit' or 'quit' to end the conversation.
You: Hi there!
Chatbot: Hello! How can I assist you today?
You: Can you tell me a joke?
Chatbot: Why did the scarecrow win an award? Because he was outstanding in his field!
You: exit
Chatbot: Goodbye!
Performance: DialoGPT-small is optimized for performance on machines with limited resources. However, running it on CPU might be slower compared to using a GPU.
Customization: You can fine-tune the response generation parameters (like top_k, top_p, temperature) to better suit your desired chatbot behavior.
Extensibility: For more advanced features, consider integrating this chatbot into a web application using frameworks like Flask or FastAPI.
Feel free to modify and extend this script to better fit your specific use case!
Not yet included in a published release.