Spaces:
Runtime error
Runtime error
darpan-jain
commited on
Commit
•
788ec8d
1
Parent(s):
60152e3
Chatbot interface using `DialoGPT-medium`
Browse files
app.py
CHANGED
@@ -1,3 +1,41 @@
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
2 |
import gradio as gr
|
3 |
+
import torch
|
4 |
|
5 |
+
|
6 |
+
# tokenizer = AutoTokenizer.from_pretrained("chavinlo/gpt4-x-alpaca")
|
7 |
+
# model = AutoModelForCausalLM.from_pretrained("chavinlo/gpt4-x-alpaca")
|
8 |
+
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
10 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
|
11 |
+
|
12 |
+
def user(message, history):
|
13 |
+
return "", history + [[message, None]]
|
14 |
+
|
15 |
+
def bot(history):
|
16 |
+
user_message = history[-1][0]
|
17 |
+
new_user_input_ids = tokenizer.encode(user_message + tokenizer.eos_token, return_tensors='pt')
|
18 |
+
|
19 |
+
# append the new user input tokens to the chat history
|
20 |
+
bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
|
21 |
+
|
22 |
+
# generate a response
|
23 |
+
history = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id).tolist()
|
24 |
+
|
25 |
+
# convert the tokens to text, and then split the responses into lines
|
26 |
+
response = tokenizer.decode(history[0]).split("<|endoftext|>")
|
27 |
+
response = [(response[i], response[i+1]) for i in range(0, len(response)-1, 2)] # convert to tuples of list
|
28 |
+
return history
|
29 |
+
|
30 |
+
## Use the above methods to create the Chatbot interface
|
31 |
+
with gr.Blocks() as demo:
|
32 |
+
chatbot = gr.Chatbot()
|
33 |
+
msg = gr.Textbox()
|
34 |
+
clear = gr.Button("Clear")
|
35 |
+
|
36 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
37 |
+
bot, chatbot, chatbot
|
38 |
+
)
|
39 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
40 |
+
|
41 |
+
demo.launch()
|