Siri23 commited on
Commit
3dfbb4a
·
verified ·
1 Parent(s): 2c0b1a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -16
app.py CHANGED
@@ -1,31 +1,27 @@
1
- from transformers import Conversation, pipeline
2
  import gradio as gr
3
 
4
  # Load the chatbot pipeline
5
- chatbot = pipeline(model="facebook/blenderbot-400M-distill")
6
 
7
- # Initialize message history lists
8
- message_list = []
9
- response_list = []
10
 
11
  # Function to interact with the chatbot
12
  def vanilla_chatbot(message, history):
13
- # Create a Conversation object
14
- conversation = Conversation(
15
- text=message,
16
- past_user_inputs=message_list,
17
- generated_responses=response_list
18
- )
19
 
20
  # Generate bot response
21
- bot_response = chatbot(conversation.messages[0]['content'])
22
 
23
- # Append message and response to history lists
24
- message_list.append(message)
25
- response_list.append(bot_response[0]['generated_text'])
26
 
27
  # Return the generated response
28
- return bot_response[0]['generated_text']
29
 
30
  # Create a Gradio chat interface
31
  demo_chatbot = gr.Interface(
 
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 = []
 
9
 
10
  # Function to interact with the chatbot
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(