sourabhzanwar commited on
Commit
3cc9efa
β€’
1 Parent(s): 77a47ea

updateed app.py, added file to check if v1 is already changed

Browse files
Files changed (11) hide show
  1. .DS_Store +0 -0
  2. .gitignore +4 -0
  3. .streamlit/config.toml +6 -0
  4. README.md +105 -6
  5. app.py +249 -0
  6. change_log.txt +1 -0
  7. ml_logo.png +0 -0
  8. requirements.txt +7 -0
  9. utils/config.py +41 -0
  10. utils/haystack.py +120 -0
  11. utils/ui.py +16 -0
.DS_Store ADDED
Binary file (6.15 kB). View file
 
.gitignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ .env
2
+ .vscode
3
+ .idea
4
+ *.pyc
.streamlit/config.toml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ [theme]
2
+ primaryColor = "#E694FF"
3
+ backgroundColor = "#FFFFFF"
4
+ secondaryBackgroundColor = "#F0F0F0"
5
+ textColor = "#262730"
6
+ font = "sans-serif"
README.md CHANGED
@@ -1,12 +1,111 @@
1
  ---
2
- title: Rag Search
3
- emoji: πŸ‘€
4
- colorFrom: gray
5
- colorTo: red
6
  sdk: streamlit
7
- sdk_version: 1.28.2
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Haystack Search Pipeline with Streamlit
3
+ emoji: πŸ‘‘
4
+ colorFrom: indigo
5
+ colorTo: indigo
6
  sdk: streamlit
7
+ sdk_version: 1.23.0
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
+ # Template Streamlit App for Haystack Search Pipelines
13
+
14
+ This template [Streamlit](https://docs.streamlit.io/) app set up for simple [Haystack search applications](https://docs.haystack.deepset.ai/docs/semantic_search). The template is ready to do QA with **Retrievel Augmented Generation**, or **Ectractive QA**
15
+
16
+ See the ['How to use this template'](#how-to-use-this-template) instructions below to create a simple UI for your own Haystack search pipelines.
17
+
18
+ Below you will also find instructions on how you could [push this to Hugging Face Spaces πŸ€—](#pushing-to-hugging-face-spaces-).
19
+
20
+ ## Installation and Running
21
+ To run the bare application which does _nothing_:
22
+ 1. Install requirements: `pip install -r requirements.txt`
23
+ 2. Run the streamlit app: `streamlit run app.py`
24
+
25
+ This will start up the app on `localhost:8501` where you will find a simple search bar. Before you start editing, you'll notice that the app will only show you instructions on what to edit.
26
+
27
+ ### Optional Configurations
28
+
29
+ You can set optional cofigurations to set the:
30
+ - `--task` you want to start the app with: `rag` or `extractive` (default: rag)
31
+ - `--store` you want to use: `inmemory`, `opensearch`, `weaviate` or `milvus` (default: inmemory)
32
+ - `--name` you want to have for the app. (default: 'My Search App')
33
+
34
+ E.g.:
35
+
36
+ ```bash
37
+ streamlit run app.py -- --store opensearch --task extractive --name 'My Opensearch Documentation Search'
38
+ ```
39
+
40
+ In a `.env` file, include all the config settings that you would like to use based on:
41
+ - The DocumentStore of your choice
42
+ - The Extractive/Generative model of your choice
43
+
44
+ While the `/utils/config.py` will create default values for some configurations, others have to be set in the `.env` such as the `OPENAI_KEY`
45
+
46
+ Example `.env`
47
+
48
+ ```
49
+ OPENAI_KEY=YOUR_KEY
50
+ EMBEDDING_MODEL=sentence-transformers/all-MiniLM-L12-v2
51
+ GENERATIVE_MODEL=text-davinci-003
52
+ ```
53
+
54
+
55
+ ## How to use this template
56
+ 1. Create a new repository from this template or simply open it in a codespace to start playing around πŸ’™
57
+ 2. Make sure your `requirements.txt` file includes the Haystack and Streamlit versions you would like to use.
58
+ 3. Change the code in `utils/haystack.py` if you would like a different pipeline.
59
+ 4. Create a `.env`file with all of your configuration settings.
60
+ 5. Make any UI edits you'd like to and [share with the Haystack community](https://haystack.deepeset.ai/community)
61
+ 6. Run the app as show in [installation and running](#installation-and-running)
62
+
63
+ ### Repo structure
64
+ - `./utils`: This is where we have 3 files:
65
+ - `config.py`: This file extracts all of the configuration settings from a `.env` file. For some config settings, it uses default values. An example of this is in [this demo project](https://github.com/TuanaCelik/should-i-follow/blob/main/utils/config.py).
66
+ - `haystack.py`: Here you will find some functions already set up for you to start creating your Haystack search pipeline. It includes 2 main functions called `start_haystack()` which is what we use to create a pipeline and cache it, and `query()` which is the function called by `app.py` once a user query is received.
67
+ - `ui.py`: Use this file for any UI and initial value setups.
68
+ - `app.py`: This is the main Streamlit application file that we will run. In its current state it has a simple search bar, a 'Run' button, and a response that you can highlight answers with.
69
+
70
+ ### What to edit?
71
+ There are default pipelines both in `start_haystack_extractive()` and `start_haystack_rag()`
72
+
73
+ - Change the pipelines to use the embedding models, extractive or generative models as you need.
74
+ - If using the `rag` task, change the `default_prompt_template` to use one of our available ones on [PromptHub](https://prompthub.deepset.ai) or create your own `PromptTemplate`
75
+
76
+
77
+ ## Pushing to Hugging Face Spaces πŸ€—
78
+
79
+ Below is an example GitHub action that will let you push your Streamlit app straight to the Hugging Face Hub as a Space.
80
+
81
+ A few things to pay attention to:
82
+
83
+ 1. Create a New Space on Hugging Face with the Streamlit SDK.
84
+ 2. Create a Hugging Face token on your HF account.
85
+ 3. Create a secret on your GitHub repo called `HF_TOKEN` and put your Hugging Face token here.
86
+ 4. If you're using DocumentStores or APIs that require some keys/tokens, make sure these are provided as a secret for your HF Space too!
87
+ 5. This readme is set up to tell HF spaces that it's using streamlit and that the app is running on `app.py`, make any changes to the frontmatter of this readme to display the title, emoji etc you desire.
88
+ 6. Create a file in `.github/workflows/hf_sync.yml`. Here's an example that you can change with your own information, and an [example workflow](https://github.com/TuanaCelik/should-i-follow/blob/main/.github/workflows/hf_sync.yml) working for the [Should I Follow demo](https://huggingface.co/spaces/deepset/should-i-follow)
89
+
90
+ ```yaml
91
+ name: Sync to Hugging Face hub
92
+ on:
93
+ push:
94
+ branches: [main]
95
+
96
+ # to run this workflow manually from the Actions tab
97
+ workflow_dispatch:
98
+
99
+ jobs:
100
+ sync-to-hub:
101
+ runs-on: ubuntu-latest
102
+ steps:
103
+ - uses: actions/checkout@v2
104
+ with:
105
+ fetch-depth: 0
106
+ lfs: true
107
+ - name: Push to hub
108
+ env:
109
+ HF_TOKEN: ${{ secrets.HF_TOKEN }}
110
+ run: git push --force https://{YOUR_HF_USERNAME}:$HF_TOKEN@{YOUR_HF_SPACE_REPO} main
111
+ ```
app.py ADDED
@@ -0,0 +1,249 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pydantic
2
+ module_file_path = pydantic.__file__
3
+
4
+ module_file_path = module_file_path.split('pydantic')[0] + 'haystack'
5
+
6
+ import os
7
+ import fileinput
8
+
9
+
10
+ def replace_string_in_files(folder_path, old_str, new_str):
11
+ for subdir, dirs, files in os.walk(folder_path):
12
+ for file in files:
13
+ file_path = os.path.join(subdir, file)
14
+
15
+ # Check if the file is a text file (you can modify this condition based on your needs)
16
+ if file.endswith(".txt") or file.endswith(".py"):
17
+ # Open the file in place for editing
18
+ with fileinput.FileInput(file_path, inplace=True) as f:
19
+ for line in f:
20
+ # Replace the old string with the new string
21
+ print(line.replace(old_str, new_str), end='')
22
+
23
+ with open('change_log.txt','r') as f:
24
+ status = f.readlines()
25
+
26
+ if status[-1] != 'changed':
27
+ replace_string_in_files(module_file_path, 'from pydantic', 'from pydantic.v1')
28
+ with open('change_log.txt','w'):
29
+ f.write('changed')
30
+
31
+
32
+
33
+
34
+ from operator import index
35
+ import streamlit as st
36
+ import logging
37
+ import os
38
+
39
+ from annotated_text import annotation
40
+ from json import JSONDecodeError
41
+ from markdown import markdown
42
+ from utils.config import parser
43
+ from utils.haystack import start_document_store, query, initialize_pipeline, start_preprocessor_node, start_retriever, start_reader
44
+ from utils.ui import reset_results, set_initial_state
45
+ import pandas as pd
46
+ import haystack
47
+
48
+
49
+ # Whether the file upload should be enabled or not
50
+ DISABLE_FILE_UPLOAD = bool(os.getenv("DISABLE_FILE_UPLOAD"))
51
+ # Define a function to handle file uploads
52
+ def upload_files():
53
+ uploaded_files = st.sidebar.file_uploader(
54
+ "upload", type=["pdf", "txt", "docx"], accept_multiple_files=True, label_visibility="hidden"
55
+ )
56
+ return uploaded_files
57
+
58
+ # Define a function to process a single file
59
+
60
+ def process_file(data_file, preprocesor, document_store):
61
+ # read file and add content
62
+ file_contents = data_file.read().decode("utf-8")
63
+ docs = [{
64
+ 'content': str(file_contents),
65
+ 'meta': {'name': str(data_file.name)}
66
+ }]
67
+ try:
68
+ names = [item.meta.get('name') for item in document_store.get_all_documents()]
69
+ #if args.store == 'inmemory':
70
+ # doc = converter.convert(file_path=files, meta=None)
71
+ if data_file.name in names:
72
+ print(f"{data_file.name} already processed")
73
+ else:
74
+ print(f'preprocessing uploaded doc {data_file.name}.......')
75
+ #print(data_file.read().decode("utf-8"))
76
+ preprocessed_docs = preprocesor.process(docs)
77
+ print('writing to document store.......')
78
+ document_store.write_documents(preprocessed_docs)
79
+ print('updating emebdding.......')
80
+ document_store.update_embeddings(retriever)
81
+ except Exception as e:
82
+ print(e)
83
+
84
+ try:
85
+ args = parser.parse_args()
86
+ preprocesor = start_preprocessor_node()
87
+ document_store = start_document_store(type=args.store)
88
+ retriever = start_retriever(document_store)
89
+ reader = start_reader()
90
+ st.set_page_config(
91
+ page_title="MLReplySearch",
92
+ layout="centered",
93
+ page_icon=":shark:",
94
+ menu_items={
95
+ 'Get Help': 'https://www.extremelycoolapp.com/help',
96
+ 'Report a bug': "https://www.extremelycoolapp.com/bug",
97
+ 'About': "# This is a header. This is an *extremely* cool app!"
98
+ }
99
+ )
100
+ st.sidebar.image("ml_logo.png", use_column_width=True)
101
+
102
+ # Sidebar for Task Selection
103
+ st.sidebar.header('Options:')
104
+
105
+ # OpenAI Key Input
106
+ openai_key = st.sidebar.text_input("Enter OpenAI Key:", type="password")
107
+
108
+ if openai_key:
109
+ task_options = ['Extractive', 'Generative']
110
+ else:
111
+ task_options = ['Extractive']
112
+
113
+ task_selection = st.sidebar.radio('Select the task:', task_options)
114
+
115
+ # Check the task and initialize pipeline accordingly
116
+ if task_selection == 'Extractive':
117
+ pipeline_extractive = initialize_pipeline("extractive", document_store, retriever, reader)
118
+ elif task_selection == 'Generative' and openai_key: # Check for openai_key to ensure user has entered it
119
+ pipeline_rag = initialize_pipeline("rag", document_store, retriever, reader, openai_key=openai_key)
120
+
121
+
122
+ set_initial_state()
123
+
124
+ st.write('# ' + args.name)
125
+
126
+
127
+ # File upload block
128
+ if not DISABLE_FILE_UPLOAD:
129
+ st.sidebar.write("## File Upload:")
130
+ #data_files = st.sidebar.file_uploader(
131
+ # "upload", type=["pdf", "txt", "docx"], accept_multiple_files=True, label_visibility="hidden"
132
+ #)
133
+ data_files = upload_files()
134
+ if data_files is not None:
135
+ for data_file in data_files:
136
+ # Upload file
137
+ if data_file:
138
+ try:
139
+ #raw_json = upload_doc(data_file)
140
+ # Call the process_file function for each uploaded file
141
+ if args.store == 'inmemory':
142
+ processed_data = process_file(data_file, preprocesor, document_store)
143
+ st.sidebar.write(str(data_file.name) + "    βœ… ")
144
+ except Exception as e:
145
+ st.sidebar.write(str(data_file.name) + "    ❌ ")
146
+ st.sidebar.write("_This file could not be parsed, see the logs for more information._")
147
+
148
+ if "question" not in st.session_state:
149
+ st.session_state.question = ""
150
+ # Search bar
151
+ question = st.text_input("", value=st.session_state.question, max_chars=100, on_change=reset_results)
152
+
153
+ run_pressed = st.button("Run")
154
+
155
+ run_query = (
156
+ run_pressed or question != st.session_state.question #or task_selection != st.session_state.task
157
+ )
158
+
159
+ # Get results for query
160
+ if run_query and question:
161
+ if task_selection == 'Extractive':
162
+ reset_results()
163
+ st.session_state.question = question
164
+ with st.spinner("πŸ”Ž    Running your pipeline"):
165
+ try:
166
+ st.session_state.results_extractive = query(pipeline_extractive, question)
167
+ st.session_state.task = task_selection
168
+ except JSONDecodeError as je:
169
+ st.error(
170
+ "πŸ‘“    An error occurred reading the results. Is the document store working?"
171
+ )
172
+ except Exception as e:
173
+ logging.exception(e)
174
+ st.error("🐞    An error occurred during the request.")
175
+
176
+ elif task_selection == 'Generative':
177
+ reset_results()
178
+ st.session_state.question = question
179
+ with st.spinner("πŸ”Ž    Running your pipeline"):
180
+ try:
181
+ st.session_state.results_generative = query(pipeline_rag, question)
182
+ st.session_state.task = task_selection
183
+ except JSONDecodeError as je:
184
+ st.error(
185
+ "πŸ‘“    An error occurred reading the results. Is the document store working?"
186
+ )
187
+ except Exception as e:
188
+ if "API key is invalid" in str(e):
189
+ logging.exception(e)
190
+ st.error("🐞    incorrect API key provided. You can find your API key at https://platform.openai.com/account/api-keys.")
191
+ else:
192
+ logging.exception(e)
193
+ st.error("🐞    An error occurred during the request.")
194
+ # Display results
195
+ if (st.session_state.results_extractive or st.session_state.results_generative) and run_query:
196
+
197
+ # Handle Extractive Answers
198
+ if task_selection == 'Extractive':
199
+ results = st.session_state.results_extractive
200
+
201
+ st.subheader("Extracted Answers:")
202
+
203
+ if 'answers' in results:
204
+ answers = results['answers']
205
+ treshold = 0.2
206
+ higher_then_treshold = any(ans.score > treshold for ans in answers)
207
+ if not higher_then_treshold:
208
+ st.markdown(f"<span style='color:red'>Please note none of the answers achieved a score higher then {int(treshold) * 100}%. Which probably means that the desired answer is not in the searched documents.</span>", unsafe_allow_html=True)
209
+ for count, answer in enumerate(answers):
210
+ if answer.answer:
211
+ text, context = answer.answer, answer.context
212
+ start_idx = context.find(text)
213
+ end_idx = start_idx + len(text)
214
+ score = round(answer.score, 3)
215
+ st.markdown(f"**Answer {count + 1}:**")
216
+ st.markdown(
217
+ context[:start_idx] + str(annotation(body=text, label=f'SCORE {score}', background='#964448', color='#ffffff')) + context[end_idx:],
218
+ unsafe_allow_html=True,
219
+ )
220
+ else:
221
+ st.info(
222
+ "πŸ€” &nbsp;&nbsp; Haystack is unsure whether any of the documents contain an answer to your question. Try to reformulate it!"
223
+ )
224
+
225
+ # Handle Generative Answers
226
+ elif task_selection == 'Generative':
227
+ results = st.session_state.results_generative
228
+ st.subheader("Generated Answer:")
229
+ if 'results' in results:
230
+ st.markdown("**Answer:**")
231
+ st.write(results['results'][0])
232
+
233
+ # Handle Retrieved Documents
234
+ if 'documents' in results:
235
+ retrieved_documents = results['documents']
236
+ st.subheader("Retriever Results:")
237
+
238
+ data = []
239
+ for i, document in enumerate(retrieved_documents):
240
+ # Truncate the content
241
+ truncated_content = (document.content[:150] + '...') if len(document.content) > 150 else document.content
242
+ data.append([i + 1, document.meta['name'], truncated_content])
243
+
244
+ # Convert data to DataFrame and display using Streamlit
245
+ df = pd.DataFrame(data, columns=['Ranked Context', 'Document Name', 'Content'])
246
+ st.table(df)
247
+
248
+ except SystemExit as e:
249
+ os._exit(e.code)
change_log.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ unchanged
ml_logo.png ADDED
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ safetensors==0.3.3.post1
2
+ farm-haystack[inference,weaviate,opensearch]==1.20.0
3
+ milvus-haystack
4
+ streamlit==1.23.0
5
+ markdown
6
+ st-annotated-text
7
+ datasets
utils/config.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import os
3
+ import os
4
+ from dotenv import load_dotenv
5
+
6
+ load_dotenv()
7
+ parser = argparse.ArgumentParser(description='This app lists animals')
8
+
9
+ document_store_choices = ('inmemory', 'weaviate', 'milvus', 'opensearch')
10
+ parser.add_argument('--store', choices=document_store_choices, default='inmemory', help='DocumentStore selection (default: %(default)s)')
11
+ parser.add_argument('--name', default="My Search App")
12
+
13
+ model_configs = {
14
+ 'EMBEDDING_MODEL': os.getenv("EMBEDDING_MODEL", "sentence-transformers/all-MiniLM-L12-v2"),
15
+ 'GENERATIVE_MODEL': os.getenv("GENERATIVE_MODEL", "gpt-4"),
16
+ 'EXTRACTIVE_MODEL': os.getenv("EXTRACTIVE_MODEL", "deepset/roberta-base-squad2"),
17
+ 'OPENAI_KEY': os.getenv("OPENAI_KEY"),
18
+ 'COHERE_KEY': os.getenv("COHERE_KEY"),
19
+ }
20
+
21
+ document_store_configs = {
22
+ # Weaviate Config
23
+ 'WEAVIATE_HOST': os.getenv("WEAVIATE_HOST", "http://localhost"),
24
+ 'WEAVIATE_PORT': os.getenv("WEAVIATE_PORT", 8080),
25
+ 'WEAVIATE_INDEX': os.getenv("WEAVIATE_INDEX", "Document"),
26
+ 'WEAVIATE_EMBEDDING_DIM': os.getenv("WEAVIATE_EMBEDDING_DIM", 768),
27
+
28
+ # OpenSearch Config
29
+ 'OPENSEARCH_SCHEME': os.getenv("OPENSEARCH_SCHEME", "https"),
30
+ 'OPENSEARCH_USERNAME': os.getenv("OPENSEARCH_USERNAME", "admin"),
31
+ 'OPENSEARCH_PASSWORD': os.getenv("OPENSEARCH_PASSWORD", "admin"),
32
+ 'OPENSEARCH_HOST': os.getenv("OPENSEARCH_HOST", "localhost"),
33
+ 'OPENSEARCH_PORT': os.getenv("OPENSEARCH_PORT", 9200),
34
+ 'OPENSEARCH_INDEX': os.getenv("OPENSEARCH_INDEX", "document"),
35
+ 'OPENSEARCH_EMBEDDING_DIM': os.getenv("OPENSEARCH_EMBEDDING_DIM", 768),
36
+
37
+ # Milvus Config
38
+ 'MILVUS_URI': os.getenv("MILVUS_URI", "http://localhost:19530/default"),
39
+ 'MILVUS_INDEX': os.getenv("MILVUS_INDEX", "document"),
40
+ 'MILVUS_EMBEDDING_DIM': os.getenv("MILVUS_EMBEDDING_DIM", 768),
41
+ }
utils/haystack.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ from utils.config import document_store_configs, model_configs
4
+ from haystack import Pipeline
5
+ from haystack.schema import Answer
6
+ from haystack.document_stores import BaseDocumentStore
7
+ from haystack.document_stores import InMemoryDocumentStore, OpenSearchDocumentStore, WeaviateDocumentStore
8
+ from haystack.nodes import EmbeddingRetriever, FARMReader, PromptNode, PreProcessor
9
+ from milvus_haystack import MilvusDocumentStore
10
+ #Use this file to set up your Haystack pipeline and querying
11
+
12
+ @st.cache_resource(show_spinner=False)
13
+ def start_preprocessor_node():
14
+ print('initializing preprocessor node')
15
+ processor = PreProcessor(
16
+ clean_empty_lines= True,
17
+ clean_whitespace=True,
18
+ clean_header_footer=True,
19
+ #remove_substrings=None,
20
+ split_by="word",
21
+ split_length=100,
22
+ split_respect_sentence_boundary=True,
23
+ #split_overlap=0,
24
+ #max_chars_check= 10_000
25
+ )
26
+ return processor
27
+ #return docs
28
+
29
+ @st.cache_resource(show_spinner=False)
30
+ def start_document_store(type: str):
31
+ #This function starts the documents store of your choice based on your command line preference
32
+ print('initializing document store')
33
+ if type == 'inmemory':
34
+ document_store = InMemoryDocumentStore(use_bm25=True, embedding_dim=384)
35
+ '''
36
+ documents = [
37
+ {
38
+ 'content': "Pi is a super dog",
39
+ 'meta': {'name': "pi.txt"}
40
+ },
41
+ {
42
+ 'content': "The revenue of siemens is 5 milion Euro",
43
+ 'meta': {'name': "siemens.txt"}
44
+ },
45
+ ]
46
+ document_store.write_documents(documents)
47
+ '''
48
+ elif type == 'opensearch':
49
+ document_store = OpenSearchDocumentStore(scheme = document_store_configs['OPENSEARCH_SCHEME'],
50
+ username = document_store_configs['OPENSEARCH_USERNAME'],
51
+ password = document_store_configs['OPENSEARCH_PASSWORD'],
52
+ host = document_store_configs['OPENSEARCH_HOST'],
53
+ port = document_store_configs['OPENSEARCH_PORT'],
54
+ index = document_store_configs['OPENSEARCH_INDEX'],
55
+ embedding_dim = document_store_configs['OPENSEARCH_EMBEDDING_DIM'])
56
+ elif type == 'weaviate':
57
+ document_store = WeaviateDocumentStore(host = document_store_configs['WEAVIATE_HOST'],
58
+ port = document_store_configs['WEAVIATE_PORT'],
59
+ index = document_store_configs['WEAVIATE_INDEX'],
60
+ embedding_dim = document_store_configs['WEAVIATE_EMBEDDING_DIM'])
61
+ elif type == 'milvus':
62
+ document_store = MilvusDocumentStore(uri = document_store_configs['MILVUS_URI'],
63
+ index = document_store_configs['MILVUS_INDEX'],
64
+ embedding_dim = document_store_configs['MILVUS_EMBEDDING_DIM'],
65
+ return_embedding=True)
66
+ return document_store
67
+
68
+ # cached to make index and models load only at start
69
+ @st.cache_resource(show_spinner=False)
70
+ def start_retriever(_document_store: BaseDocumentStore):
71
+ print('initializing retriever')
72
+ retriever = EmbeddingRetriever(document_store=_document_store,
73
+ embedding_model=model_configs['EMBEDDING_MODEL'],
74
+ top_k=5)
75
+ #
76
+
77
+ #_document_store.update_embeddings(retriever)
78
+ return retriever
79
+
80
+
81
+ @st.cache_resource(show_spinner=False)
82
+ def start_reader():
83
+ print('initializing reader')
84
+ reader = FARMReader(model_name_or_path=model_configs['EXTRACTIVE_MODEL'])
85
+ return reader
86
+
87
+
88
+
89
+ # cached to make index and models load only at start
90
+ @st.cache_resource(show_spinner=False)
91
+ def start_haystack_extractive(_document_store: BaseDocumentStore, _retriever: EmbeddingRetriever, _reader: FARMReader):
92
+ print('initializing pipeline')
93
+ pipe = Pipeline()
94
+ pipe.add_node(component=_retriever, name="Retriever", inputs=["Query"])
95
+ pipe.add_node(component= _reader, name="Reader", inputs=["Retriever"])
96
+ return pipe
97
+
98
+ @st.cache_resource(show_spinner=False)
99
+ def start_haystack_rag(_document_store: BaseDocumentStore, _retriever: EmbeddingRetriever, openai_key):
100
+ prompt_node = PromptNode(default_prompt_template="deepset/question-answering",
101
+ model_name_or_path=model_configs['GENERATIVE_MODEL'],
102
+ api_key=openai_key)
103
+ pipe = Pipeline()
104
+
105
+ pipe.add_node(component=_retriever, name="Retriever", inputs=["Query"])
106
+ pipe.add_node(component=prompt_node, name="PromptNode", inputs=["Retriever"])
107
+
108
+ return pipe
109
+
110
+ #@st.cache_data(show_spinner=True)
111
+ def query(_pipeline, question):
112
+ params = {}
113
+ results = _pipeline.run(question, params=params)
114
+ return results
115
+
116
+ def initialize_pipeline(task, document_store, retriever, reader, openai_key = ""):
117
+ if task == 'extractive':
118
+ return start_haystack_extractive(document_store, retriever, reader)
119
+ elif task == 'rag':
120
+ return start_haystack_rag(document_store, retriever, openai_key)
utils/ui.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ def set_state_if_absent(key, value):
4
+ if key not in st.session_state:
5
+ st.session_state[key] = value
6
+
7
+ def set_initial_state():
8
+ set_state_if_absent("question", "Ask something here?")
9
+ set_state_if_absent("results_extractive", None)
10
+ set_state_if_absent("results_generative", None)
11
+ set_state_if_absent("task", None)
12
+
13
+ def reset_results(*args):
14
+ st.session_state.results_extractive = None
15
+ st.session_state.results_generative = None
16
+ st.session_state.task = None