from transformers import pipeline, set_seed | |
# Initialize the conversation pipeline | |
set_seed(42) | |
roleplay_bot = pipeline('conversational', model='microsoft/DialoGPT-medium') | |
# Memory to store past interactions | |
memory = [] | |
def update_memory(user_input, bot_response): | |
memory.append({"user": user_input, "bot": bot_response}) | |
def get_memory_context(): | |
context = "" | |
for interaction in memory[-5:]: # limiting memory to last 5 interactions for simplicity | |
context += f"User: {interaction['user']}\nBot: {interaction['bot']}\n" | |
return context | |
def interact(user_input): | |
context = get_memory_context() | |
input_with_context = context + f"User: {user_input}\n" | |
bot_response = roleplay_bot(input_with_context)[0]['generated_text'].split('\n')[-1] | |
update_memory(user_input, bot_response) | |
return bot_response | |
# Example interaction | |
user_input = "Hi! How are you?" | |
print("User:", user_input) | |
bot_response = interact(user_input) | |
print("Bot:", bot_response) | |
# Continue with more interactions as needed |