Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,47 +1,45 @@
|
|
1 |
-
|
2 |
import gradio as gr
|
3 |
-
import torch
|
4 |
-
|
5 |
|
6 |
-
title = "
|
7 |
-
description = "
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
|
|
|
|
10 |
|
11 |
-
tokenizer =
|
12 |
-
model =
|
13 |
-
|
14 |
|
15 |
def predict(input, history=[]):
|
16 |
# tokenize the new input sentence
|
17 |
-
new_user_input_ids = tokenizer.encode(
|
18 |
-
input + tokenizer.eos_token, return_tensors="pt"
|
19 |
-
)
|
20 |
|
21 |
# append the new user input tokens to the chat history
|
22 |
bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
|
23 |
|
24 |
-
# generate a response
|
25 |
-
history = model.generate(
|
26 |
-
bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id
|
27 |
-
).tolist()
|
28 |
-
|
29 |
-
# convert the tokens to text, and then split the responses into lines
|
30 |
-
response = tokenizer.decode(history[0]).split("<|endoftext|>")
|
31 |
-
# print('decoded_response-->>'+str(response))
|
32 |
-
response = [
|
33 |
-
(response[i], response[i + 1]) for i in range(0, len(response) - 1, 2)
|
34 |
-
] # convert to tuples of list
|
35 |
-
# print('response-->>'+str(response))
|
36 |
-
return response, history
|
37 |
|
|
|
|
|
|
|
|
|
38 |
|
39 |
gr.Interface(
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
).launch()
|
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
|
|
|
|
3 |
|
4 |
+
title = "Have Fun With ChubbyBot"
|
5 |
+
description = """
|
6 |
+
<p>
|
7 |
+
<center>
|
8 |
+
The bot is trained on blended_skill_talk dataset using facebook/blenderbot-400M-distill.
|
9 |
+
<img src="https://huggingface.co/spaces/EXFINITE/BlenderBot-UI/resolve/main/img/cover.png" alt="rick" width="250"/>
|
10 |
+
</center>
|
11 |
+
</p>
|
12 |
+
"""
|
13 |
+
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/1907.06616' target='_blank'>Recipes for building an open-domain chatbot</a></p><p style='text-align: center'><a href='https://parl.ai/projects/recipes/' target='_blank'>Original PARLAI Code</a></p></center></p>"
|
14 |
|
15 |
+
import torch
|
16 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, BlenderbotForConditionalGeneration, BlenderbotForCausalLM, BlenderbotTokenizer
|
17 |
|
18 |
+
tokenizer = BlenderbotTokenizer.from_pretrained("facebook/blenderbot-400M-distill")
|
19 |
+
model = BlenderbotForConditionalGeneration.from_pretrained("facebook/blenderbot-400M-distill",add_cross_attention=False)
|
|
|
20 |
|
21 |
def predict(input, history=[]):
|
22 |
# tokenize the new input sentence
|
23 |
+
new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors='pt')
|
|
|
|
|
24 |
|
25 |
# append the new user input tokens to the chat history
|
26 |
bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
|
27 |
|
28 |
+
# generate a response
|
29 |
+
history = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id).tolist()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
# convert the tokens to text, and then split the responses into the right format
|
32 |
+
response = tokenizer.decode(history[0]).replace("<s>","").split("</s>")
|
33 |
+
response = [(response[i], response[i+1]) for i in range(0, len(response), 2)] # convert to tuples of list
|
34 |
+
return response, history
|
35 |
|
36 |
gr.Interface(
|
37 |
+
fn = predict,
|
38 |
+
inputs = ["textbox","state"],
|
39 |
+
outputs = ["chatbot","state"],
|
40 |
+
theme ="seafoam",
|
41 |
+
title = title,
|
42 |
+
description = description,
|
43 |
+
article = article
|
44 |
+
).launch(enable_queue=True)
|
45 |
+
|