jemsXDXD commited on
Commit
9b671d5
·
verified ·
1 Parent(s): 17adfd0

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +66 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer
2
+ import torch
3
+ import pandas as pd
4
+ import gradio as gr
5
+
6
+ # Carrega o tokenizador e o modelo
7
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
8
+ model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
9
+
10
+ # Criando um dataframe com status de pedidos
11
+ dados_pedidos = {
12
+ "numero_pedido": ["12345", "67890", "11121", "22232"],
13
+ "status": ["Shipped","Processing","Delivered","Cancelled"]
14
+ }
15
+ df_status_pedidos = pd.DataFrame(dados_pedidos)
16
+
17
+ # Função para verificar o status do pedido
18
+ def verificar_status_pedido(numero_pedido):
19
+ try:
20
+ status = df_status_pedidos[df_status_pedidos['numero_pedido'] == numero_pedido]['status'].iloc[0]
21
+ return f'The status of your order {numero_pedido} is: {status}'
22
+ except:
23
+ return 'Order number not found. Please check and try again'
24
+
25
+ palavras_chave_status = ['order','order status', 'status of my order','check my order', 'track my order', 'order update']
26
+
27
+ def responder(input_usuario, ids_historico_chat):
28
+ if any(keyword in input_usuario.lower() for keyword in palavras_chave_status):
29
+ return 'Could you please enter you order number?', ids_historico_chat
30
+ else:
31
+ novo_usuario_input_ids = tokenizer.encode(input_usuario + tokenizer.eos_token, return_tensors='pt')
32
+ if ids_historico_chat is not None:
33
+ bot_input_ids = torch.cat([ids_historico_chat, novo_usuario_input_ids ], dim=-1)
34
+ else:
35
+ bot_input_ids = novo_usuario_input_ids
36
+
37
+ ids_historico_chat = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
38
+
39
+ resposta = tokenizer.decode(ids_historico_chat[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
40
+ return resposta, ids_historico_chat
41
+
42
+ # Criando interface
43
+ with gr.Blocks() as app: # This is the Gradio Blocks context
44
+ chatbot = gr.Chatbot()
45
+ msg = gr.Textbox(placeholder='Type your message here...')
46
+ estado = gr.State(None)
47
+ aguardando_numero_pedido = gr.State(False)
48
+
49
+ def processar_entrada(input_usuario, historico, ids_historico_chat,
50
+ aguardando_numero_pedido):
51
+ if aguardando_numero_pedido:
52
+ resposta = verificar_status_pedido(input_usuario)
53
+ aguardando_numero_pedido = False
54
+ else:
55
+ resposta, ids_historico_chat = responder(input_usuario, ids_historico_chat)
56
+
57
+ if resposta == 'Could you please enter you order number?':
58
+ aguardando_numero_pedido = True
59
+ historico.append((input_usuario, resposta))
60
+ return historico, ids_historico_chat, aguardando_numero_pedido, ""
61
+
62
+ # The submit call needs to be inside the 'with gr.Blocks()' block
63
+ msg.submit(
64
+ processar_entrada,[msg, chatbot, estado, aguardando_numero_pedido], [chatbot, estado, aguardando_numero_pedido, msg])
65
+
66
+ app.launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ Gradio
2
+ Transformers
3
+ Pillow
4
+ Torchvision
5
+ scikit-image