Siri23 commited on
Commit
0eaa6b6
·
verified ·
1 Parent(s): 3dfbb4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -8
app.py CHANGED
@@ -1,8 +1,10 @@
1
- from transformers import pipeline
2
  import gradio as gr
3
 
4
- # Load the chatbot pipeline
5
- chatbot = pipeline("conversational", model="facebook/blenderbot-400M-distill")
 
 
6
 
7
  # Initialize message history
8
  conversation_history = []
@@ -11,17 +13,21 @@ conversation_history = []
11
  def vanilla_chatbot(message, history):
12
  global conversation_history
13
 
14
- # Create a conversation turn
15
- conversation_history.append({"role": "user", "content": message})
 
 
 
16
 
17
  # Generate bot response
18
- bot_response = chatbot(conversation_history)
 
19
 
20
  # Append bot response to history
21
- conversation_history.append({"role": "bot", "content": bot_response.generated_responses[-1]})
22
 
23
  # Return the generated response
24
- return bot_response.generated_responses[-1]
25
 
26
  # Create a Gradio chat interface
27
  demo_chatbot = gr.Interface(
@@ -34,3 +40,4 @@ demo_chatbot = gr.Interface(
34
 
35
  # Launch the Gradio interface
36
  demo_chatbot.launch(share=True)
 
 
1
+ from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
2
  import gradio as gr
3
 
4
+ # Load the model and tokenizer
5
+ model_name = "facebook/blenderbot-400M-distill"
6
+ tokenizer = BlenderbotTokenizer.from_pretrained(model_name)
7
+ model = BlenderbotForConditionalGeneration.from_pretrained(model_name)
8
 
9
  # Initialize message history
10
  conversation_history = []
 
13
  def vanilla_chatbot(message, history):
14
  global conversation_history
15
 
16
+ # Append user message to history
17
+ conversation_history.append(message)
18
+
19
+ # Encode the new user input, add the eos_token and return a tensor in Pytorch
20
+ inputs = tokenizer([message], return_tensors='pt')
21
 
22
  # Generate bot response
23
+ reply_ids = model.generate(**inputs)
24
+ bot_response = tokenizer.batch_decode(reply_ids, skip_special_tokens=True)[0]
25
 
26
  # Append bot response to history
27
+ conversation_history.append(bot_response)
28
 
29
  # Return the generated response
30
+ return bot_response
31
 
32
  # Create a Gradio chat interface
33
  demo_chatbot = gr.Interface(
 
40
 
41
  # Launch the Gradio interface
42
  demo_chatbot.launch(share=True)
43
+