Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,48 @@
|
|
|
|
1 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
2 |
import torch
|
3 |
|
|
|
4 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
5 |
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
# pretty print last ouput tokens from bot
|
19 |
-
print("DialoGPT: {}".format(tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)))
|
20 |
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
import torch
|
4 |
|
5 |
+
# Carregar o modelo e tokenizer
|
6 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
7 |
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
|
8 |
|
9 |
+
# Fun莽茫o para gerar resposta
|
10 |
+
def generate_response(user_input, chat_history=None):
|
11 |
+
if chat_history is None:
|
12 |
+
chat_history = []
|
13 |
+
|
14 |
+
# Codificar a entrada do usu谩rio
|
15 |
+
new_user_input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors='pt')
|
16 |
+
|
17 |
+
# Concatenar a entrada do usu谩rio com o hist贸rico da conversa
|
18 |
+
if chat_history:
|
19 |
+
bot_input_ids = torch.cat([chat_history, new_user_input_ids], dim=-1)
|
20 |
+
else:
|
21 |
+
bot_input_ids = new_user_input_ids
|
22 |
+
|
23 |
+
# Gerar resposta
|
24 |
+
response_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
|
25 |
+
|
26 |
+
# Decodificar a resposta
|
27 |
+
response = tokenizer.decode(response_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
28 |
+
|
29 |
+
# Atualizar o hist贸rico da conversa
|
30 |
+
chat_history = response_ids
|
31 |
+
|
32 |
+
return response, chat_history
|
33 |
|
34 |
+
# Interface Gradio
|
35 |
+
demo = gr.Interface(
|
36 |
+
fn=generate_response,
|
37 |
+
inputs=["text"],
|
38 |
+
outputs=["text"],
|
39 |
+
title="DialoGPT Conversa",
|
40 |
+
description="Converse com o modelo DialoGPT",
|
41 |
+
allow_flagging="never"
|
42 |
+
)
|
43 |
|
44 |
+
# Inicializar o hist贸rico da conversa
|
45 |
+
chat_history = None
|
|
|
|
|
|
|
46 |
|
47 |
+
# Lan莽ar a interface
|
48 |
+
demo.launch()
|