Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -13,41 +13,58 @@ examples = [["How are you?"]]
|
|
13 |
tokenizer = AutoTokenizer.from_pretrained("rinna/vicuna-13b-delta-finetuned-langchain-MRKL")
|
14 |
model = AutoModelForCausalLM.from_pretrained("rinna/vicuna-13b-delta-finetuned-langchain-MRKL")
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
new_user_input_ids = tokenizer.encode(input_text + tokenizer.eos_token, return_tensors="pt")
|
19 |
|
20 |
-
# Append the new user input tokens to the chat history
|
21 |
-
bot_input_ids = torch.cat([torch.tensor(chat_history), new_user_input_ids], dim=-1)
|
22 |
|
23 |
-
|
24 |
-
|
25 |
|
26 |
-
# Decode the response tokens into text
|
27 |
-
response = tokenizer.decode(chat_output[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
28 |
|
29 |
-
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
response = generate_response(input_text, chat_history)
|
34 |
|
35 |
-
#
|
36 |
-
|
|
|
|
|
37 |
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
chat_history = [] # Initialize chat history
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
)
|
52 |
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
tokenizer = AutoTokenizer.from_pretrained("rinna/vicuna-13b-delta-finetuned-langchain-MRKL")
|
14 |
model = AutoModelForCausalLM.from_pretrained("rinna/vicuna-13b-delta-finetuned-langchain-MRKL")
|
15 |
|
16 |
+
# tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
17 |
+
# model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
|
|
|
18 |
|
|
|
|
|
19 |
|
20 |
+
def user(message, history):
|
21 |
+
return "", history + [[message, None]]
|
22 |
|
|
|
|
|
23 |
|
24 |
+
def bot(history):
|
25 |
+
user_message = history[-1][0]
|
26 |
+
new_user_input_ids = tokenizer.encode(
|
27 |
+
user_message + tokenizer.eos_token, return_tensors="pt"
|
28 |
+
)
|
29 |
|
30 |
+
# append the new user input tokens to the chat history
|
31 |
+
bot_input_ids = torch.cat([torch.LongTensor([]), new_user_input_ids], dim=-1)
|
|
|
32 |
|
33 |
+
# generate a response
|
34 |
+
response = model.generate(
|
35 |
+
bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id
|
36 |
+
).tolist()
|
37 |
|
38 |
+
# convert the tokens to text, and then split the responses into lines
|
39 |
+
response = tokenizer.decode(response[0]).split("<|endoftext|>")
|
40 |
+
response = [
|
41 |
+
(response[i], response[i + 1]) for i in range(0, len(response) - 1, 2)
|
42 |
+
] # convert to tuples of list
|
43 |
+
history[-1] = response[0]
|
44 |
+
return history
|
45 |
|
|
|
46 |
|
47 |
+
with gr.Blocks() as demo:
|
48 |
+
chatbot = gr.Chatbot()
|
49 |
+
msg = gr.Textbox()
|
50 |
+
clear = gr.Button("Clear")
|
51 |
+
|
52 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
53 |
+
bot, chatbot, chatbot
|
54 |
+
)
|
55 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
|
|
56 |
|
57 |
+
demo.launch()
|
58 |
+
|
59 |
+
# iface = gr.Interface(
|
60 |
+
# fn=chatbot_interface,
|
61 |
+
# inputs=gr.inputs.Textbox(lines=2, label="Chat"),
|
62 |
+
# outputs=gr.outputs.Textbox(label="Response"),
|
63 |
+
# layout="vertical",
|
64 |
+
# title=title,
|
65 |
+
# description=description,
|
66 |
+
# examples=examples,
|
67 |
+
# theme="london"
|
68 |
+
# )
|
69 |
+
|
70 |
+
# iface.launch()
|