|
import gradio as gr |
|
from transformers import pipeline |
|
|
|
|
|
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) |
|
|
|
|
|
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'..." |
|
} |
|
|
|
|
|
def obter_resposta(pergunta, contexto): |
|
try: |
|
return QA_modelo(question=pergunta, context=contexto) |
|
except Exception as e: |
|
return {"answer": f"Erro ao processar: {e}"} |
|
|
|
|
|
def respondendo_faq(pergunta): |
|
contexto = contextos.get(pergunta) |
|
if not contexto: |
|
return "Question not found. Please select a valid question." |
|
resultado = obter_resposta(pergunta, contexto) |
|
return resultado['answer'] |
|
|
|
|
|
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) |