File size: 1,000 Bytes
e7fc8a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

from transformers import pipeline, Conversation

def chat_with_bot(message, conversation_history=None):
    """
    Has a conversation with a chatbot using a pretrained model.
    """
    chatbot_pipeline = pipeline("conversational")
    if conversation_history:
        conversation = Conversation(text=message, past_user_inputs=conversation_history['past_user_inputs'], generated_responses=conversation_history['generated_responses'])
    else:
        conversation = Conversation(text=message)
    
    result = chatbot_pipeline(conversation)
    return result.generated_responses[-1], {'past_user_inputs': result.past_user_inputs, 'generated_responses': result.generated_responses}

if __name__ == '__main__':
    user_message = "Hello, how are you?"
    response, history = chat_with_bot(user_message)
    print("Bot:", response)

    user_message = "What is the weather like today?"
    response, history = chat_with_bot(user_message, conversation_history=history)
    print("Bot:", response)