Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,40 @@
|
|
1 |
-
from transformers import pipeline
|
2 |
-
from transformers import Conversation
|
3 |
import gradio as gr
|
|
|
|
|
4 |
chatbot = pipeline(model="facebook/blenderbot-400M-distill")
|
|
|
|
|
5 |
message_list = []
|
6 |
response_list = []
|
7 |
|
|
|
8 |
def vanilla_chatbot(message, history):
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
demo_chatbot.launch(share
|
|
|
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(
|
32 |
+
fn=vanilla_chatbot,
|
33 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter your message here..."),
|
34 |
+
outputs=gr.Textbox(placeholder="Bot response will appear here...", readonly=True),
|
35 |
+
title="Mashdemy Chatbot",
|
36 |
+
description="Enter text to start chatting."
|
37 |
+
)
|
38 |
|
39 |
+
# Launch the Gradio interface
|
40 |
+
demo_chatbot.launch(share=True)
|