List of Topics:
Location Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to Create a User Chatbot using the Transformers Library (DialoGPT)?

Create a Chatbot with GPT-2

Condition for Creating a User Chatbot using the Transformers Library (DialoGPT)

  • Description:
    A chatbot is an AI system designed to simulate human conversation, enabling interaction with users through text-based communication. Modern chatbots often use deep learning techniques, such as transformers, to generate human-like responses by understanding and processing natural language. In this approach, we leverage Hugging Faces Transformers library, which provides pre-trained modelslike DialoGPT for generating conversational responses.
Step-by-Step Process
  • Imports and Setup:
    transformers: The library that provides access to pre-trained models and tokenizers for natural language processing. In this case, we AutoModelForCausalLM to load a conversational model and AutoTokenizer to encode and decode input and output text.
    torch: A deep learning library that helps in tensor operations and model inference.It’s used for handling model inputs and outputs.
    DialoGPT: A version of GPT-2 fine-tuned specifically for conversational tasks.Its used here for generating contextually relevant replies.
  • Loading Pre-trained Models:
    DialoGPT Model: The chatbot uses a pre-trained version of DialoGPT-medium,which is a medium-sized variant of the DialoGPT model. This model has been trained on conversational data, allowing it to generate meaningful dialogue responses.
    Tokenizer: A tokenizer from Hugging Face is used to convert text input (users message) into numerical format, which the model can process. It also converts the model’s output (numeric tokens) back into readable text.
  • Conversation History:
    Maintaining Context: Unlike simple rule-based bots, this chatbot uses conversation history to generate contextually aware responses. The input tokens from the user and the bot’s previous responses are stored and passed to the model during each interaction. This enables the model to maintain context and make the conversation flow naturally.
    Input and Output: Each new user input is tokenized and appended to the conversation history. The model then generates a response based on the entire conversation.
  • User Input and Response Generation:
    User Interaction: The chatbot waits for the user to input text. After each message, the chatbot generates a response based on the conversation history.
    Response Generation: Using model.generate(), the bot generates a response based on the input provided. The model considers both the current input and prior conversation history to maintain continuity in the dialogue.
  • User Input and Response Generation:
    User Interaction: The chatbot waits for the user to input text. After each message, the chatbot generates a response based on the conversation history.
    Response Generation: Using model.generate(), the bot generates a response based on the input provided. The model considers both the current input and prior conversation history to maintain continuity in the dialogue.
  • User Input and Response Generation:
    User Interaction: The chatbot waits for the user to input text. After each message, the chatbot generates a response based on the conversation history.
    Response Generation: Using model.generate(), the bot generates a response based on the input provided. The model considers both the current input and prior conversation history to maintain continuity in the dialogue.
  • Managing Attention Masks:
    Avoiding Warnings: To prevent warnings related to the attention mask, an explicit attention mask is passed when generating the model’s output. The attention mask is a binary tensor indicating which tokens should be attended to (i.e., non-padding tokens). This ensures that the model correctly processes the input.
Sample Code
  • import torch
    from transformers import AutoModelForCausalLM, AutoTokenizer
    # Load pre-trained DialoGPT model and tokenizer
    model_name = "microsoft/DialoGPT-medium"
    model = AutoModelForCausalLM.from_pretrained(model_name)
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    # Initialize a chat history (a list of tokens) to maintain conversation context
    chat_history_ids = None
    # Function to interact with the chatbot
    def chat():
     global chat_history_ids
     print("Chatbot: Hello! Type 'exit' to end the conversation.")
     while True:
      # Take user input
      user_input = input("You: ")
      # Exit condition
      if user_input.lower() == "exit":
       print("Chatbot: Goodbye!")
       break
      # Encode the new user input
      new_user_input_ids = tokenizer.encode(user_input + tokenizer.eos_token,
       return_tensors='pt')
      # If there is prior conversation, append the new input to the chat
    history
      if chat_history_ids is not None:
       input_ids = torch.cat([chat_history_ids, new_user_input_ids], dim=-1)
      else:
       input_ids = new_user_input_ids
      # Generate attention mask
      attention_mask = torch.ones(input_ids.shape, device=input_ids.device) # Create attention mask of 1s
      # Get the model's response (output) given the input history
      chat_history_ids = model.generate(
       input_ids,
       attention_mask=attention_mask,
       max_length=1000,
       pad_token_id=tokenizer.eos_token_id,
       temperature=0.7,
       top_p=0.9,
       top_k=50,
       do_sample=True,
       no_repeat_ngram_size=3
      )
      # Decode the model's response and print it
      bot_response = tokenizer.decode(
       chat_history_ids[:, input_ids.shape[-1]:][0],
       skip_special_tokens=True
      )
      print("Chatbot:", bot_response)
    # Start the chatbot
    if __name__ == "__main__":
     chat()
Screenshots
  • chatbot_using_transformaers