Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,10 @@
|
|
1 |
-
from transformers import
|
2 |
import gradio as gr
|
3 |
|
4 |
-
# Load the
|
5 |
-
|
|
|
|
|
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 |
-
#
|
15 |
-
conversation_history.append(
|
|
|
|
|
|
|
16 |
|
17 |
# Generate bot response
|
18 |
-
|
|
|
19 |
|
20 |
# Append bot response to history
|
21 |
-
conversation_history.append(
|
22 |
|
23 |
# Return the generated response
|
24 |
-
return bot_response
|
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 |
+
|