File size: 2,321 Bytes
4111d9f
 
c99568b
6f88c92
e609dbf
 
 
6f88c92
e609dbf
4111d9f
41b16f3
6f88c92
 
 
 
 
 
 
 
 
 
 
 
 
 
e609dbf
6f88c92
e609dbf
6f88c92
4111d9f
6f88c92
4111d9f
6f88c92
c99568b
e609dbf
c99568b
 
4111d9f
6f88c92
4111d9f
 
 
c99568b
4111d9f
e609dbf
 
c99568b
4111d9f
 
e609dbf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import gradio as gr
from transformers import pipeline

# Carregando o modelo de QA
try:
    QA_modelo = pipeline('question-answering', model='deepset/roberta-base-squad2')
except Exception as e:
    print(f"Erro ao carregar o modelo: {e}")
    exit(1)

# Dicionário com perguntas e contextos
contextos = {
    "How do I create an account?": "You can create an account by clicking on the 'Sign Up' button on our homepage...",
    "Which payment methods do you accept?": "We accept a wide range of payment methods including Visa, MasterCard...",
    "How can I track my order?": "Once your order has shipped, you will receive an email with a tracking number...",
    "Do you offer international shipping?": "Yes, we offer international shipping to most countries...",
    "How long does delivery take?": "For standard shipping, deliveries typically take between 3 to 5 business days...",
    "What is your return policy?": "Our return policy allows you to return products within 30 days of receiving them...",
    "Can I change or cancel my order after it's been placed?": "You can change or cancel your order within 24 hours...",
    "What should I do if I receive a damaged item?": "If you receive a damaged item, please contact our customer service...",
    "How do I reset my password?": "If you've forgotten your password, go to the login page and click on 'Forgot Password'..."
}

# Função para obter resposta com o modelo
def obter_resposta(pergunta, contexto):
    try:
        return QA_modelo(question=pergunta, context=contexto)
    except Exception as e:
        return {"answer": f"Erro ao processar: {e}"}

# Função para responder perguntas da FAQ
def respondendo_faq(pergunta):
    contexto = contextos.get(pergunta)  # Verifica se a pergunta existe no dicionário
    if not contexto:
        return "Question not found. Please select a valid question."
    resultado = obter_resposta(pergunta, contexto)
    return resultado['answer']

# Interface Gradio
app = gr.Interface(
    fn=respondendo_faq,
    inputs=gr.Dropdown(choices=list(contextos.keys()), label="Select your question"),
    outputs=gr.Textbox(label="Answer"),
    title='E-commerce FAQ',
    description='Select a question to get an answer from our FAQ.',
    theme="huggingface"
)

if __name__ == "__main__":
    app.launch(share=True)