Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,26 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
|
| 4 |
-
model_name = "microsoft/DialoGPT-medium"
|
| 5 |
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 8 |
|
| 9 |
chat_history_ids = None
|
| 10 |
|
| 11 |
-
def
|
| 12 |
global chat_history_ids
|
| 13 |
-
|
| 14 |
|
| 15 |
if chat_history_ids is not None:
|
| 16 |
-
input_ids = torch.cat([chat_history_ids,
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
chat_history_ids = output
|
| 20 |
|
| 21 |
-
|
| 22 |
-
return
|
| 23 |
|
| 24 |
-
|
| 25 |
-
iface.launch()
|
|
|
|
| 1 |
+
import torch
|
| 2 |
import gradio as gr
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
|
| 5 |
+
model_name = "microsoft/DialoGPT-medium"
|
| 6 |
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 9 |
|
| 10 |
chat_history_ids = None
|
| 11 |
|
| 12 |
+
def respond(message, chat_history=[]):
|
| 13 |
global chat_history_ids
|
| 14 |
+
new_input_ids = tokenizer.encode(message + tokenizer.eos_token, return_tensors='pt')
|
| 15 |
|
| 16 |
if chat_history_ids is not None:
|
| 17 |
+
input_ids = torch.cat([chat_history_ids, new_input_ids], dim=-1)
|
| 18 |
+
else:
|
| 19 |
+
input_ids = new_input_ids
|
| 20 |
|
| 21 |
+
chat_history_ids = model.generate(input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
|
|
|
|
| 22 |
|
| 23 |
+
reply = tokenizer.decode(chat_history_ids[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
|
| 24 |
+
return reply
|
| 25 |
|
| 26 |
+
gr.Interface(fn=respond, inputs="text", outputs="text", title="DialoGPT Chatbot").launch()
|
|
|