Spaces:
Runtime error
Runtime error
File size: 3,543 Bytes
1e40d63 0667117 1e40d63 9cc056e 1e40d63 711c996 1e40d63 711c996 1e40d63 0667117 8ee5801 0667117 e306148 1e40d63 8ee5801 e306148 8ee5801 e306148 1e40d63 e306148 1e40d63 e306148 1e40d63 c518990 1e40d63 0667117 1e40d63 0667117 1e40d63 0667117 1e40d63 0667117 9a76c37 1e40d63 |
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
import gradio as gr
import os
import time
from haystack.document_stores import InMemoryDocumentStore
from haystack.nodes import EmbeddingRetriever
import pandas as pd
from deep_translator import GoogleTranslator
def load_qa_model():
document_store = InMemoryDocumentStore()
retriever = EmbeddingRetriever(
document_store=document_store,
embedding_model="sentence-transformers/all-MiniLM-L6-v2",
use_gpu=False,
scale_score=False,
)
# Get dataframe with columns "question", "answer" and some custom metadata
df = pd.read_csv('social-faq.csv', on_bad_lines='skip', delimiter=';')
# Minimal cleaning
df.fillna(value="", inplace=True)
df["question"] = df["question"].apply(lambda x: x.strip())
questions = list(df["question"].values)
df["embedding"] = retriever.embed_queries(queries=questions).tolist()
df = df.rename(columns={"question": "content"})
# Convert Dataframe to list of dicts and index them in our DocumentStore
docs_to_index = df.to_dict(orient="records")
document_store.write_documents(docs_to_index)
return retriever
def add_text(chat_history, text):
chat_history = chat_history + [(text, None)]
return chat_history, gr.Textbox(value="", interactive=False)
def add_file(chat_history, file):
chat_history = chat_history + [((file.name,), None)]
return chat_history
def chatbot_response(chat_history, language):
chat_history[-1][1] = ""
global retriever
global last_answer
if language == 'pt-br':
response = get_answers(retriever, GoogleTranslator(source='pt', target='en').translate(chat_history[-1][0]))
response = GoogleTranslator(source='en', target='pt').translate(response)
else:
response = get_answers(retriever, history[-1][0])
last_answer = response
for character in response:
chat_history[-1][1] += character
time.sleep(0.01)
yield chat_history
def get_answers(retriever, query):
from haystack.pipelines import FAQPipeline
pipe = FAQPipeline(retriever=retriever)
from haystack.utils import print_answers
# Run any question and change top_k to see more or less answers
prediction = pipe.run(query=query, params={"Retriever": {"top_k": 1}})
answers = prediction['answers']
if answers:
return answers[0].answer
else:
return "I don't have an answer to that question"
retriever = load_qa_model()
last_answer = ""
with gr.Blocks() as demo:
with gr.Accordion("Settings", open=False):
language = gr.Radio(["en-us", "pt-br"], label="Language", info="Choose the language to display the classification result and audio", value='en-us', interactive=True)
with gr.Tab("Help"):
chatbot = gr.Chatbot(
[],
elem_id="chatbot",
bubble_full_width=False,
# avatar_images=(None, "content/avatar-socialear.png"),
)
with gr.Row():
txt = gr.Textbox(
scale=4,
show_label=False,
placeholder="Enter text and press enter",
container=False,
)
inputRecord = gr.Audio(label="Record a question", source="microphone", type="filepath")
with gr.Column():
btn = gr.Button(value="Listen the answer")
audioOutput = gr.Audio(interactive=False)
txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
chatbot_response, [chatbot, language], chatbot
)
txt_msg.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
demo.queue()
demo.launch()
|