Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,18 +5,34 @@ import gradio as gr
|
|
5 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
|
6 |
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")
|
7 |
|
8 |
-
def predict(
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
|
6 |
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")
|
7 |
|
8 |
+
def predict(user_input, history=[]):
|
9 |
+
# Encode the user input + end-of-text token
|
10 |
+
new_input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors='pt')
|
11 |
+
|
12 |
+
# Prepare chat history
|
13 |
+
if history:
|
14 |
+
bot_input_ids = torch.cat([torch.tensor(history), new_input_ids], dim=-1)
|
15 |
+
else:
|
16 |
+
bot_input_ids = new_input_ids
|
17 |
+
|
18 |
+
# Generate response
|
19 |
+
chat_history_ids = model.generate(
|
20 |
+
bot_input_ids,
|
21 |
+
max_length=1000,
|
22 |
+
pad_token_id=tokenizer.eos_token_id
|
23 |
+
)
|
24 |
+
|
25 |
+
# Decode the bot's reply
|
26 |
+
response = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
27 |
+
|
28 |
+
return response, chat_history_ids.tolist()
|
29 |
+
|
30 |
+
iface = gr.Interface(
|
31 |
+
fn=predict,
|
32 |
+
inputs=["text", "state"],
|
33 |
+
outputs=["text", "state"],
|
34 |
+
title="DialoGPT Chatbot",
|
35 |
+
description="Chat with DialoGPT-large. Your chat history is preserved.",
|
36 |
+
)
|
37 |
+
|
38 |
+
iface.launch(share=True, show_error=True, inline=True)
|