Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,27 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
2 |
+
import torch
|
3 |
import gradio as gr
|
4 |
|
5 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
|
6 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")
|
7 |
+
|
8 |
+
# Initialize chat history
|
9 |
+
chat_history_ids = None
|
10 |
+
|
11 |
+
def chat_cpu(user_input):
|
12 |
+
global chat_history_ids
|
13 |
+
# Encode the new user input, add the eos_token, and return a tensor in PyTorch
|
14 |
+
new_user_input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors='pt')
|
15 |
+
|
16 |
+
# Append the new user input tokens to the chat history
|
17 |
+
bot_input_ids = torch.cat([chat_history_ids, new_user_input_ids], dim=-1) if chat_history_ids is not None else new_user_input_ids
|
18 |
+
|
19 |
+
# Generate a response while limiting the total chat history to 1000 tokens
|
20 |
+
chat_history_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
|
21 |
+
|
22 |
+
# Pretty print last output tokens from bot
|
23 |
+
response = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
24 |
+
return "DialoGPT: {}".format(response)
|
25 |
+
|
26 |
+
iface = gr.Interface(fn=chat_cpu, inputs="text", outputs="text")
|
27 |
+
iface.launch(share=True)
|