Anderson432 commited on
Commit
df297aa
verified
1 Parent(s): bfbf6f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -11
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
- # Let's chat for 5 lines
8
- for step in range(5):
9
- # encode the new user input, add the eos_token and return a tensor in Pytorch
10
- new_user_input_ids = tokenizer.encode(input(">> User:") + tokenizer.eos_token, return_tensors='pt')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- # append the new user input tokens to the chat history
13
- bot_input_ids = torch.cat([chat_history_ids, new_user_input_ids], dim=-1) if step > 0 else new_user_input_ids
 
 
 
 
 
 
 
14
 
15
- # generated a response while limiting the total chat history to 1000 tokens,
16
- chat_history_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
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()