Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import BlenderbotSmallTokenizer, BlenderbotSmallForConditionalGeneration
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
tokenizer = BlenderbotSmallTokenizer.from_pretrained("facebook/blenderbot-90M")
|
| 5 |
+
model = BlenderbotSmallForConditionalGeneration.from_pretrained("facebook/blenderbot-90M")
|
| 6 |
+
|
| 7 |
+
history = []
|
| 8 |
+
|
| 9 |
+
def chat_fn(message):
|
| 10 |
+
global history
|
| 11 |
+
last_history = history[-3:]
|
| 12 |
+
conversation = ""
|
| 13 |
+
for user_msg, bot_msg in last_history:
|
| 14 |
+
conversation += f"User: {user_msg}\nBot: {bot_msg}\n"
|
| 15 |
+
conversation += f"User: {message}\nBot:"
|
| 16 |
+
|
| 17 |
+
inputs = tokenizer(conversation, return_tensors="pt")
|
| 18 |
+
reply_ids = model.generate(
|
| 19 |
+
**inputs,
|
| 20 |
+
max_length=100,
|
| 21 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 22 |
+
do_sample=True,
|
| 23 |
+
top_p=0.9,
|
| 24 |
+
temperature=0.7
|
| 25 |
+
)
|
| 26 |
+
bot_message = tokenizer.decode(reply_ids[0], skip_special_tokens=True)
|
| 27 |
+
history.append([message, bot_message])
|
| 28 |
+
return bot_message
|
| 29 |
+
|
| 30 |
+
demo = gr.Interface(fn=chat_fn, inputs="text", outputs="text", title="BlenderBot Chatbot")
|
| 31 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|