YAML Metadata Warning:empty or missing yaml metadata in repo card
Check out the documentation for more information.
chatbot.py
from transformers import AutoModelForCausalLM, AutoTokenizer import torch
Load pre-trained model and tokenizer
model_name = "microsoft/DialoGPT-medium" # You can choose other models from Hugging Face tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForCausalLM.from_pretrained(model_name)
def chat_with_ai(user_input): # Encode the user input and generate a response new_user_input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors='pt')
# Append the new user input to the previous history
bot_input_ids = torch.cat([chat_history_ids, new_user_input_ids], dim=-1) if 'chat_history_ids' in locals() else new_user_input_ids
# Generate a response from the model
chat_history_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
# Get the predicted response and decode it
bot_response = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
return bot_response
Main loop for chatting
print("AI Chatbot is ready! Type 'exit' to stop.") while True: user_input = input("You: ") if user_input.lower() == 'exit': break response = chat_with_ai(user_input) print("AI: " + response)