adrianoL commited on
Commit
6f88c92
·
verified ·
1 Parent(s): 55e7f37

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -17
app.py CHANGED
@@ -1,43 +1,48 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
- import time
4
 
 
5
  try:
6
  QA_modelo = pipeline('question-answering', model='deepset/roberta-base-squad2')
7
  except Exception as e:
8
- print(f"Error loading model: {e}")
9
  exit(1)
10
 
11
- def obter_resposta(pergunta, contexto, max_context_length=512):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  try:
13
- if len(contexto) > max_context_length:
14
- contexto = contexto[:max_context_length]
15
-
16
- start_time = time.time()
17
- resultado = QA_modelo(question=pergunta, context=contexto)
18
-
19
- if time.time() - start_time > 30:
20
- return {"answer": "Response timeout. Please try again."}
21
-
22
- return resultado
23
  except Exception as e:
24
- return {"answer": f"Error processing question: {e}"}
25
 
 
26
  def respondendo_faq(pergunta):
27
- contexto = contextos.get(pergunta)
28
  if not contexto:
29
  return "Question not found. Please select a valid question."
30
-
31
  resultado = obter_resposta(pergunta, contexto)
32
  return resultado['answer']
33
 
 
34
  app = gr.Interface(
35
  fn=respondendo_faq,
36
  inputs=gr.Dropdown(choices=list(contextos.keys()), label="Select your question"),
37
  outputs=gr.Textbox(label="Answer"),
38
  title='E-commerce FAQ',
39
  description='Select a question to get an answer from our FAQ.',
40
- examples=list(contextos.keys())[:3],
41
  theme="huggingface"
42
  )
43
 
 
1
  import gradio as gr
2
  from transformers import pipeline
 
3
 
4
+ # Carregando o modelo de QA
5
  try:
6
  QA_modelo = pipeline('question-answering', model='deepset/roberta-base-squad2')
7
  except Exception as e:
8
+ print(f"Erro ao carregar o modelo: {e}")
9
  exit(1)
10
 
11
+ # Dicionário com perguntas e contextos - MOVIDO PARA ANTES DA CRIAÇÃO DA INTERFACE
12
+ contextos = {
13
+ "How do I create an account?": "You can create an account by clicking on the 'Sign Up' button on our homepage...",
14
+ "Which payment methods do you accept?": "We accept a wide range of payment methods including Visa, MasterCard...",
15
+ "How can I track my order?": "Once your order has shipped, you will receive an email with a tracking number...",
16
+ "Do you offer international shipping?": "Yes, we offer international shipping to most countries...",
17
+ "How long does delivery take?": "For standard shipping, deliveries typically take between 3 to 5 business days...",
18
+ "What is your return policy?": "Our return policy allows you to return products within 30 days of receiving them...",
19
+ "Can I change or cancel my order after it's been placed?": "You can change or cancel your order within 24 hours...",
20
+ "What should I do if I receive a damaged item?": "If you receive a damaged item, please contact our customer service...",
21
+ "How do I reset my password?": "If you've forgotten your password, go to the login page and click on 'Forgot Password'..."
22
+ }
23
+
24
+ # Função para obter resposta com o modelo
25
+ def obter_resposta(pergunta, contexto):
26
  try:
27
+ return QA_modelo(question=pergunta, context=contexto)
 
 
 
 
 
 
 
 
 
28
  except Exception as e:
29
+ return {"answer": f"Erro ao processar: {e}"}
30
 
31
+ # Função para responder perguntas da FAQ
32
  def respondendo_faq(pergunta):
33
+ contexto = contextos.get(pergunta) # Verifica se a pergunta existe no dicionário
34
  if not contexto:
35
  return "Question not found. Please select a valid question."
 
36
  resultado = obter_resposta(pergunta, contexto)
37
  return resultado['answer']
38
 
39
+ # Interface Gradio
40
  app = gr.Interface(
41
  fn=respondendo_faq,
42
  inputs=gr.Dropdown(choices=list(contextos.keys()), label="Select your question"),
43
  outputs=gr.Textbox(label="Answer"),
44
  title='E-commerce FAQ',
45
  description='Select a question to get an answer from our FAQ.',
 
46
  theme="huggingface"
47
  )
48