Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 = []
|
| 11 |
+
|
| 12 |
+
# Function to interact with the chatbot
|
| 13 |
+
def vanilla_chatbot(message, history):
|
| 14 |
+
global conversation_history
|
| 15 |
+
# Append user message to history
|
| 16 |
+
conversation_history.append(message)
|
| 17 |
+
# Encode the new user input, add the eos_token and return a tensor in Pytorch
|
| 18 |
+
inputs = tokenizer([message], return_tensors='pt')
|
| 19 |
+
# Generate bot response
|
| 20 |
+
reply_ids = model.generate(**inputs)
|
| 21 |
+
bot_response = tokenizer.batch_decode(reply_ids, skip_special_tokens=True)[0]
|
| 22 |
+
# Append bot response to history
|
| 23 |
+
conversation_history.append(bot_response)
|
| 24 |
+
# Return the generated response
|
| 25 |
+
return bot_response
|
| 26 |
+
# Create a Gradio chat interface
|
| 27 |
+
demo_chatbot = gr.Interface(
|
| 28 |
+
fn=vanilla_chatbot,
|
| 29 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter your message here..."),
|
| 30 |
+
outputs=gr.Textbox(placeholder="Bot response will appear here..."),
|
| 31 |
+
title="Mashdemy Chatbot",
|
| 32 |
+
description="Enter text to start chatting."
|
| 33 |
+
)
|
| 34 |
+
# Launch the Gradio interface
|
| 35 |
+
demo_chatbot.launch(share=True)
|