import gradio as gr import pandas as pd from haystack.schema import Answer from haystack.document_stores import InMemoryDocumentStore from haystack.pipelines import FAQPipeline, ExtractiveQAPipeline from haystack.nodes import EmbeddingRetriever, TfidfRetriever, FARMReader, PDFToTextConverter from haystack.utils import print_answers from haystack.utils import convert_files_to_docs import logging # FAQ Haystack function calls def start_haystack(): document_store = InMemoryDocumentStore(index="document", embedding_field='embedding', embedding_dim=384, similarity='cosine') retriever = EmbeddingRetriever(document_store=document_store, embedding_model='sentence-transformers/all-MiniLM-L6-v2', use_gpu=True, top_k=1) load_data_to_store(document_store,retriever) pipeline = FAQPipeline(retriever=retriever) return pipeline, document_store def load_data_to_store(document_store, retriever): df = pd.read_csv('monopoly_qa-v1.csv') questions = list(df.Question) df['embedding'] = retriever.embed_queries(texts=questions) df = df.rename(columns={"Question":"content","Answer":"answer"}) df.drop('link to source (to prevent duplicate sources)',axis=1, inplace=True) dicts = df.to_dict(orient="records") document_store.write_documents(dicts) faq_pipeline, doc_store = start_haystack() def predict_faq(question): prediction = faq_pipeline.run(question) answer = prediction["answers"][0].meta faq_response = "FAQ Question: " + answer["query"] + "\n"+"Answer: " + answer["answer"] return faq_response # Extractive QA functions def_start_ex_haystack(): return true # Gradio App section input_question =gr.inputs.Textbox(label="enter your monopoly question here") response = "text" examples = ["how much cash do we get to start with?", "at what point can I buy houses?", "what happens when I land on free parking?"] mon_faq = gr.Interface( fn=predict_faq, inputs=input_question, outputs=response, examples=examples, title="Monopoly FAQ Semantic Search") # def return_feedback(input_question,feedback_answer):