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9ab
9d4b1c5
 
 
 
 
1d3f9ab
9d4b1c5
1d3f9ab
 
 
 
 
9d4b1c5
1d3f9ab
9d4b1c5
1d3f9ab
 
 
 
9d4b1c5
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import json
import logging
import os
import shutil
import sys
import uuid
from json import JSONDecodeError
from pathlib import Path
from typing import List, Optional

import pandas as pd
import pinecone
import streamlit as st
from annotated_text import annotation
from haystack import BaseComponent, Document
from haystack.document_stores import PineconeDocumentStore
from haystack.nodes import (
    DocxToTextConverter,
    EmbeddingRetriever,
    FARMReader,
    FileTypeClassifier,
    PDFToTextConverter,
    PreProcessor,
    TextConverter,
)
from haystack.pipelines import ExtractiveQAPipeline, Pipeline
from markdown import markdown
from sentence_transformers import SentenceTransformer


class PineconeSearch(BaseComponent):
    outgoing_edges = 1

    def run(self, query: str, top_k: Optional[int]):
        # process the inputs
        vector_embedding = emb_model.encode(query).tolist()
        response = index.query([vector_embedding], top_k=top_k, include_metadata=True)
        docs = [
            Document(
                content=d["metadata"]["text"],
                meta={
                    "title": d["metadata"]["filename"],
                    "context": d["metadata"]["text"],
                    "_split_id": d["metadata"]["_split_id"],
                },
            )
            for d in response["matches"]
        ]
        output = {"documents": docs, "query": query}
        return output, "output_1"

    def run_batch(self, queries: List[str], top_k: Optional[int]):

        return {}, "output_1"


# connect to pinecone environment
pinecone.init(api_key=st.secrets["pinecone_apikey"], environment="us-west1-gcp")
index_name = "qa-demo-fast-384"
# retriever_model = "sentence-transformers/multi-qa-mpnet-base-dot-v1"
retriever_model = "sentence-transformers/multi-qa-MiniLM-L6-cos-v1"
emb_model = SentenceTransformer(retriever_model)

embedding_dim = 384
preprocessor = PreProcessor(
    clean_empty_lines=True,
    clean_whitespace=True,
    clean_header_footer=False,
    split_by="word",
    split_length=100,
    split_respect_sentence_boundary=True,
)
file_type_classifier = FileTypeClassifier()
text_converter = TextConverter()
pdf_converter = PDFToTextConverter()
docx_converter = DocxToTextConverter()

# check if the abstractive-question-answering index exists
if index_name not in pinecone.list_indexes():
    # delete the current index and create the new index if it does not exist
    for delete_index in pinecone.list_indexes():
        pinecone.delete_index(delete_index)
    pinecone.create_index(index_name, dimension=embedding_dim, metric="cosine")

# connect to abstractive-question-answering index we created
index = pinecone.Index(index_name)

FILE_UPLOAD_PATH = "./data/uploads/"
os.makedirs(FILE_UPLOAD_PATH, exist_ok=True)


def create_doc_store():
    document_store = PineconeDocumentStore(
        api_key=st.secrets["pinecone_apikey"],
        index=index_name,
        similarity="cosine",
        embedding_dim=embedding_dim,
    )
    return document_store


def query(pipe, question, top_k_reader, top_k_retriever):
    res = pipe.run(
        query=question,
        params={"Retriever": {"top_k": top_k_retriever}, "Reader": {"top_k": top_k_reader}},
    )
    return res


document_store = create_doc_store()
# pipe = create_pipe(document_store)

retriever = EmbeddingRetriever(
    document_store=document_store,
    embedding_model=retriever_model,
    model_format="sentence_transformers",
)
# load the retriever model from huggingface model hub
sentence_encoder = SentenceTransformer(retriever_model)

reader = FARMReader(model_name_or_path="deepset/roberta-base-squad2", use_gpu=False)
# pipe = ExtractiveQAPipeline(reader, retriever)
# Custom built extractive QA pipeline
pipe = Pipeline()
pipe.add_node(component=PineconeSearch(), name="Retriever", inputs=["Query"])
pipe.add_node(component=reader, name="Reader", inputs=["Retriever"])


indexing_pipeline_with_classification = Pipeline()
indexing_pipeline_with_classification.add_node(
    component=file_type_classifier, name="FileTypeClassifier", inputs=["File"]
)
indexing_pipeline_with_classification.add_node(
    component=text_converter, name="TextConverter", inputs=["FileTypeClassifier.output_1"]
)
indexing_pipeline_with_classification.add_node(
    component=pdf_converter, name="PdfConverter", inputs=["FileTypeClassifier.output_2"]
)
indexing_pipeline_with_classification.add_node(
    component=docx_converter, name="DocxConverter", inputs=["FileTypeClassifier.output_4"]
)
indexing_pipeline_with_classification.add_node(
    component=preprocessor,
    name="Preprocessor",
    inputs=["TextConverter", "PdfConverter", "DocxConverter"],
)


def set_state_if_absent(key, value):
    if key not in st.session_state:
        st.session_state[key] = value


# Adjust to a question that you would like users to see in the search bar when they load the UI:
DEFAULT_QUESTION_AT_STARTUP = os.getenv(
    "DEFAULT_QUESTION_AT_STARTUP", "My blog post discusses remote work. Give me statistics."
)
DEFAULT_ANSWER_AT_STARTUP = os.getenv(
    "DEFAULT_ANSWER_AT_STARTUP",
    "7% more remote workers have been at their current organization for 5 years or fewer",
)

# Sliders
DEFAULT_DOCS_FROM_RETRIEVER = int(os.getenv("DEFAULT_DOCS_FROM_RETRIEVER", "3"))
DEFAULT_NUMBER_OF_ANSWERS = int(os.getenv("DEFAULT_NUMBER_OF_ANSWERS", "3"))


st.set_page_config(
    page_title="Haystack Demo", page_icon="https://haystack.deepset.ai/img/HaystackIcon.png"
)

# Persistent state
set_state_if_absent("question", DEFAULT_QUESTION_AT_STARTUP)
set_state_if_absent("answer", DEFAULT_ANSWER_AT_STARTUP)
set_state_if_absent("results", None)


# Small callback to reset the interface in case the text of the question changes
def reset_results(*args):
    st.session_state.answer = None
    st.session_state.results = None
    st.session_state.raw_json = None


# Title
st.write("# Haystack Search Demo")
st.markdown(
    """
This demo takes its data from two sample data csv with statistics on various topics. \n
Ask any question on this topic and see if Haystack can find the correct answer to your query! \n
*Note: do not use keywords, but full-fledged questions.* The demo is not optimized to deal with keyword queries and might misunderstand you.
""",
    unsafe_allow_html=True,
)

# Sidebar
st.sidebar.header("Options")
st.sidebar.write("## File Upload:")
data_files = st.sidebar.file_uploader(
    "upload", type=["pdf", "txt", "docx"], accept_multiple_files=True, label_visibility="hidden"
)
ALL_FILES = []
META_DATA = []
for data_file in data_files:
    # Upload file
    if data_file:
        file_path = Path(FILE_UPLOAD_PATH) / f"{uuid.uuid4().hex}_{data_file.name}"
        with open(file_path, "wb") as f:
            f.write(data_file.getbuffer())
        ALL_FILES.append(file_path)
        st.sidebar.write(str(data_file.name) + "    βœ… ")
        META_DATA.append({"filename": data_file.name})

data_files = []


if len(ALL_FILES) > 0:
    # document_store.update_embeddings(retriever, update_existing_embeddings=False)
    docs = indexing_pipeline_with_classification.run(file_paths=ALL_FILES, meta=META_DATA)[
        "documents"
    ]
    index_name = "qa_demo"
    # we will use batches of 64
    batch_size = 128
    # docs  = docs['documents']
    # with st.spinner(
    #         "🧠    Performing indexing of uplaoded documents... \n "
    #     ):
    my_bar = st.progress(0)
    upload_count = 0
    for i in range(0, len(docs), batch_size):
        # find end of batch
        i_end = min(i + batch_size, len(docs))
        # extract batch
        batch = [doc.content for doc in docs[i:i_end]]
        # generate embeddings for batch
        emb = sentence_encoder.encode(batch).tolist()
        # get metadata
        # meta = [doc.meta for doc in docs[i:i_end]]
        meta = []
        for doc in docs[i:i_end]:
            meta_dict = doc.meta
            meta_dict["text"] = doc.content
            meta.append(meta_dict)
        # create unique IDs
        ids = [doc.id for doc in docs[i:i_end]]
        # add all to upsert list
        to_upsert = list(zip(ids, emb, meta))
        # upsert/insert these records to pinecone
        _ = index.upsert(vectors=to_upsert)
        upload_count += batch_size
        upload_percentage = min(int((upload_count / len(docs)) * 100), 100)
        my_bar.progress(upload_percentage)

top_k_reader = st.sidebar.slider(
    "Max. number of answers",
    min_value=1,
    max_value=10,
    value=DEFAULT_NUMBER_OF_ANSWERS,
    step=1,
    on_change=reset_results,
)
top_k_retriever = st.sidebar.slider(
    "Max. number of documents from retriever",
    min_value=1,
    max_value=10,
    value=DEFAULT_DOCS_FROM_RETRIEVER,
    step=1,
    on_change=reset_results,
)
# data_files = st.file_uploader(
#         "upload", type=["csv"], accept_multiple_files=True, label_visibility="hidden"
#     )
# for data_file in data_files:
#     # Upload file
#     if data_file:
#         raw_json = upload_doc(data_file)

question = st.text_input(
    value=st.session_state.question,
    max_chars=100,
    on_change=reset_results,
    label="question",
    label_visibility="hidden",
)
col1, col2 = st.columns(2)
col1.markdown("<style>.stButton button {width:100%;}</style>", unsafe_allow_html=True)
col2.markdown("<style>.stButton button {width:100%;}</style>", unsafe_allow_html=True)

# Run button
run_pressed = col1.button("Run")
if run_pressed:

    run_query = run_pressed or question != st.session_state.question
    # Get results for query
    if run_query and question:
        reset_results()
        st.session_state.question = question

        with st.spinner("🧠 &nbsp;&nbsp; Performing neural search on documents... \n "):
            try:
                st.session_state.results = query(
                    pipe, question, top_k_reader=top_k_reader, top_k_retriever=top_k_retriever
                )
            except JSONDecodeError as je:
                st.error(
                    "πŸ‘“ &nbsp;&nbsp; An error occurred reading the results. Is the document store working?"
                )
            except Exception as e:
                logging.exception(e)
                if "The server is busy processing requests" in str(e) or "503" in str(e):
                    st.error("πŸ§‘β€πŸŒΎ &nbsp;&nbsp; All our workers are busy! Try again later.")
                else:
                    st.error(f"🐞 &nbsp;&nbsp; An error occurred during the request. {str(e)}")


if st.session_state.results:

    st.write("## Results:")

    for count, result in enumerate(st.session_state.results["answers"]):
        answer, context = result.answer, result.context
        start_idx = context.find(answer)
        end_idx = start_idx + len(answer)
        # Hack due to this bug: https://github.com/streamlit/streamlit/issues/3190
        try:
            filename = result.meta["title"]
            st.write(
                markdown(
                    f'From file:  {filename} \n {context[:start_idx] } {str(annotation(answer, "ANSWER", "#8ef"))} {context[end_idx:]} \n '
                ),
                unsafe_allow_html=True,
            )
        except:
            filename = result.meta.get("filename", "")
            st.write(
                markdown(
                    f'From file: {filename} \n {context[:start_idx] } {str(annotation(answer, "ANSWER", "#8ef"))} {context[end_idx:]} \n '
                ),
                unsafe_allow_html=True,
            )