kdevoe commited on
Commit
0e4ab50
·
verified ·
1 Parent(s): c7566a7

Adding memory with langchain

Browse files
Files changed (1) hide show
  1. app.py +22 -6
app.py CHANGED
@@ -1,20 +1,35 @@
1
  import gradio as gr
2
  from transformers import T5Tokenizer, T5ForConditionalGeneration
 
 
3
 
4
  # Load the tokenizer and model for flan-t5
5
  tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-base")
6
  model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-base")
7
 
8
- # Define the chatbot function
 
 
 
9
  def chat_with_flan(input_text):
10
- # Prepare the input for the model
11
- input_ids = tokenizer.encode(input_text, return_tensors="pt")
 
 
 
 
 
 
12
 
13
  # Generate the response from the model
14
  outputs = model.generate(input_ids, max_length=200, num_return_sequences=1)
15
 
16
- # Decode and return the response
17
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
 
 
18
  return response
19
 
20
  # Set up the Gradio interface
@@ -22,9 +37,10 @@ interface = gr.Interface(
22
  fn=chat_with_flan,
23
  inputs=gr.Textbox(label="Chat with FLAN-T5"),
24
  outputs=gr.Textbox(label="FLAN-T5's Response"),
25
- title="FLAN-T5 Chatbot",
26
- description="This is a simple chatbot powered by the FLAN-T5 model.",
27
  )
28
 
29
  # Launch the Gradio app
30
  interface.launch()
 
 
1
  import gradio as gr
2
  from transformers import T5Tokenizer, T5ForConditionalGeneration
3
+ from langchain.memory import ConversationBufferMemory
4
+ from langchain.prompts import PromptTemplate
5
 
6
  # Load the tokenizer and model for flan-t5
7
  tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-base")
8
  model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-base")
9
 
10
+ # Set up conversational memory using LangChain's ConversationBufferMemory
11
+ memory = ConversationBufferMemory()
12
+
13
+ # Define the chatbot function with memory
14
  def chat_with_flan(input_text):
15
+ # Retrieve conversation history and append the current user input
16
+ conversation_history = memory.load_memory_variables({})['history']
17
+
18
+ # Combine the history with the current user input
19
+ full_input = f"{conversation_history}\nUser: {input_text}\nAssistant:"
20
+
21
+ # Tokenize the input for the model
22
+ input_ids = tokenizer.encode(full_input, return_tensors="pt")
23
 
24
  # Generate the response from the model
25
  outputs = model.generate(input_ids, max_length=200, num_return_sequences=1)
26
 
27
+ # Decode the model output
28
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
29
+
30
+ # Update the memory with the user input and model response
31
+ memory.save_context({"input": input_text}, {"output": response})
32
+
33
  return response
34
 
35
  # Set up the Gradio interface
 
37
  fn=chat_with_flan,
38
  inputs=gr.Textbox(label="Chat with FLAN-T5"),
39
  outputs=gr.Textbox(label="FLAN-T5's Response"),
40
+ title="FLAN-T5 Chatbot with Memory",
41
+ description="This is a simple chatbot powered by the FLAN-T5 model with conversational memory, using LangChain.",
42
  )
43
 
44
  # Launch the Gradio app
45
  interface.launch()
46
+