File size: 1,033 Bytes
b229ee8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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