Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
3 |
import torch
|
4 |
import pandas as pd
|
5 |
import gradio as gr
|
|
|
6 |
|
7 |
# Carregando o tokenizador e o modelo
|
8 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
@@ -16,64 +17,67 @@ dados_pedidos = {
|
|
16 |
df_status_pedidos = pd.DataFrame(dados_pedidos)
|
17 |
|
18 |
# Função para verificar o status do pedido
|
19 |
-
def verificar_status_pedido(numero_pedido):
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
except:
|
26 |
-
return 'Order number not found. Please check and try again'
|
27 |
|
28 |
# Lista de palavras-chave para o status
|
29 |
palavras_chave_status = ['order', 'order status', 'status of my order', 'check my order', 'track my order', 'order update']
|
30 |
|
31 |
-
def responder(input_usuario, ids_historico_chat):
|
32 |
-
|
33 |
-
|
34 |
-
else:
|
35 |
-
novo_usuario_input_ids = tokenizer.encode(input_usuario + tokenizer.eos_token, return_tensors='pt')
|
36 |
|
|
|
|
|
37 |
if ids_historico_chat is not None:
|
38 |
-
|
39 |
-
|
40 |
else:
|
41 |
-
|
42 |
|
43 |
ids_historico_chat = model.generate(
|
44 |
bot_input_ids,
|
45 |
max_length=1000,
|
46 |
pad_token_id=tokenizer.eos_token_id
|
47 |
)
|
48 |
-
resposta = tokenizer.decode(ids_historico_chat[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
49 |
|
50 |
-
|
|
|
51 |
|
52 |
# Criando interface
|
53 |
with gr.Blocks() as app:
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
[msg, chatbot, estado, aguardando_numero_pedido],
|
76 |
-
[chatbot, estado, aguardando_numero_pedido, msg]
|
77 |
-
)
|
78 |
-
if "__name"=="__main":
|
79 |
-
app.launch(share=True)
|
|
|
3 |
import torch
|
4 |
import pandas as pd
|
5 |
import gradio as gr
|
6 |
+
from typing import List, Dict, Tuple, Optional
|
7 |
|
8 |
# Carregando o tokenizador e o modelo
|
9 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
|
|
17 |
df_status_pedidos = pd.DataFrame(dados_pedidos)
|
18 |
|
19 |
# Função para verificar o status do pedido
|
20 |
+
def verificar_status_pedido(numero_pedido: str) -> str:
|
21 |
+
try:
|
22 |
+
status = df_status_pedidos[df_status_pedidos['numero_pedido'] == numero_pedido]['status'].iloc[0]
|
23 |
+
return f'The status of your order {numero_pedido} is: {status}'
|
24 |
+
except IndexError:
|
25 |
+
return 'Order number not found. Please check and try again'
|
|
|
|
|
26 |
|
27 |
# Lista de palavras-chave para o status
|
28 |
palavras_chave_status = ['order', 'order status', 'status of my order', 'check my order', 'track my order', 'order update']
|
29 |
|
30 |
+
def responder(input_usuario: str, ids_historico_chat: Optional[torch.Tensor]) -> Tuple[str, torch.Tensor]:
|
31 |
+
if any(keyword in input_usuario.lower() for keyword in palavras_chave_status):
|
32 |
+
return 'Could you please enter your order number?', ids_historico_chat
|
|
|
|
|
33 |
|
34 |
+
novo_usuario_input_ids = tokenizer.encode(input_usuario + tokenizer.eos_token, return_tensors='pt')
|
35 |
+
|
36 |
if ids_historico_chat is not None:
|
37 |
+
bot_input_ids = torch.cat([ids_historico_chat, novo_usuario_input_ids], dim=-1)
|
|
|
38 |
else:
|
39 |
+
bot_input_ids = novo_usuario_input_ids
|
40 |
|
41 |
ids_historico_chat = model.generate(
|
42 |
bot_input_ids,
|
43 |
max_length=1000,
|
44 |
pad_token_id=tokenizer.eos_token_id
|
45 |
)
|
|
|
46 |
|
47 |
+
resposta = tokenizer.decode(ids_historico_chat[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
48 |
+
return resposta, ids_historico_chat
|
49 |
|
50 |
# Criando interface
|
51 |
with gr.Blocks() as app:
|
52 |
+
chatbot = gr.Chatbot(type='messages')
|
53 |
+
msg = gr.Textbox(placeholder='Type your message here...')
|
54 |
+
estado = gr.State(None)
|
55 |
+
aguardando_numero_pedido = gr.State(False)
|
56 |
+
|
57 |
+
def processar_entrada(input_usuario: str, historico: List[Dict[str, str]],
|
58 |
+
ids_historico_chat: Optional[torch.Tensor],
|
59 |
+
aguardando_numero_pedido: bool) -> Tuple[List[Dict[str, str]], Optional[torch.Tensor], bool, str]:
|
60 |
+
if aguardando_numero_pedido:
|
61 |
+
resposta = verificar_status_pedido(input_usuario)
|
62 |
+
aguardando_numero_pedido = False
|
63 |
+
else:
|
64 |
+
resposta, ids_historico_chat = responder(input_usuario, ids_historico_chat)
|
65 |
+
|
66 |
+
if resposta == 'Could you please enter your order number?':
|
67 |
+
aguardando_numero_pedido = True
|
68 |
+
|
69 |
+
historico.extend([
|
70 |
+
{"role": "user", "content": input_usuario},
|
71 |
+
{"role": "assistant", "content": resposta}
|
72 |
+
])
|
73 |
+
|
74 |
+
return historico, ids_historico_chat, aguardando_numero_pedido, ""
|
75 |
+
|
76 |
+
msg.submit(
|
77 |
+
processar_entrada,
|
78 |
+
[msg, chatbot, estado, aguardando_numero_pedido],
|
79 |
+
[chatbot, estado, aguardando_numero_pedido, msg]
|
80 |
+
)
|
81 |
|
82 |
+
if __name__ == "__main__":
|
83 |
+
app.launch(share=True)
|
|
|
|
|
|
|
|
|
|