File size: 1,885 Bytes
0efd337
07b00c0
 
0e4ab50
2ad20f5
07b00c0
 
 
 
 
 
 
2ad20f5
0e4ab50
 
 
 
07b00c0
0e4ab50
 
 
 
 
 
07b00c0
 
2ad20f5
07b00c0
6b31fe2
2ad20f5
0e4ab50
2ad20f5
0e4ab50
 
 
 
2ad20f5
 
 
 
07b00c0
 
 
 
 
0efd337
 
2ad20f5
 
0e4ab50
07b00c0
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import gradio as gr
from transformers import GPT2Tokenizer, GPT2LMHeadModel
import torch
from langchain.memory import ConversationBufferMemory

# Load the tokenizer and model for DistilGPT-2
tokenizer = GPT2Tokenizer.from_pretrained("distilgpt2")
model = GPT2LMHeadModel.from_pretrained("distilgpt2")

# Move model to device (GPU if available)
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
model.to(device)

# Set up conversational memory using LangChain's ConversationBufferMemory
memory = ConversationBufferMemory()

# Define the chatbot function with memory
def chat_with_distilgpt2(input_text):
    # Retrieve conversation history and append the current user input
    conversation_history = memory.load_memory_variables({})['history']
    
    # Combine the history with the current user input
    full_input = f"{conversation_history}\nUser: {input_text}\nAssistant:"
    
    # Tokenize the input and convert to tensor
    input_ids = tokenizer.encode(full_input, return_tensors="pt").to(device)
    
    # Generate the response using the model
    outputs = model.generate(input_ids, max_length=400, num_return_sequences=1, pad_token_id=tokenizer.eos_token_id)
    
    # Decode the model output
    response = tokenizer.decode(outputs[0], skip_special_tokens=True)
    
    # Update the memory with the user input and model response
    memory.save_context({"input": input_text}, {"output": response})
    
    return response

# Set up the Gradio interface
interface = gr.Interface(
    fn=chat_with_distilgpt2,
    inputs=gr.Textbox(label="Chat with DistilGPT-2"),
    outputs=gr.Textbox(label="DistilGPT-2's Response"),
    title="DistilGPT-2 Chatbot with Memory",
    description="This is a simple chatbot powered by the DistilGPT-2 model with conversational memory, using LangChain.",
)

# Launch the Gradio app
interface.launch()