Spaces:
Runtime error
Runtime error
intoxication
commited on
Commit
β’
209df17
1
Parent(s):
5738cf2
Upload 8 files
Browse files- .gitignore +129 -0
- .streamlit/config.toml +3 -0
- README.md +91 -7
- app.py +58 -0
- requirements.txt +5 -0
- utils/config.py +0 -0
- utils/haystack.py +22 -0
- utils/ui.py +12 -0
.gitignore
ADDED
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Byte-compiled / optimized / DLL files
|
2 |
+
__pycache__/
|
3 |
+
*.py[cod]
|
4 |
+
*$py.class
|
5 |
+
|
6 |
+
# C extensions
|
7 |
+
*.so
|
8 |
+
|
9 |
+
# Distribution / packaging
|
10 |
+
.Python
|
11 |
+
build/
|
12 |
+
develop-eggs/
|
13 |
+
dist/
|
14 |
+
downloads/
|
15 |
+
eggs/
|
16 |
+
.eggs/
|
17 |
+
lib/
|
18 |
+
lib64/
|
19 |
+
parts/
|
20 |
+
sdist/
|
21 |
+
var/
|
22 |
+
wheels/
|
23 |
+
pip-wheel-metadata/
|
24 |
+
share/python-wheels/
|
25 |
+
*.egg-info/
|
26 |
+
.installed.cfg
|
27 |
+
*.egg
|
28 |
+
MANIFEST
|
29 |
+
|
30 |
+
# PyInstaller
|
31 |
+
# Usually these files are written by a python script from a template
|
32 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
33 |
+
*.manifest
|
34 |
+
*.spec
|
35 |
+
|
36 |
+
# Installer logs
|
37 |
+
pip-log.txt
|
38 |
+
pip-delete-this-directory.txt
|
39 |
+
|
40 |
+
# Unit test / coverage reports
|
41 |
+
htmlcov/
|
42 |
+
.tox/
|
43 |
+
.nox/
|
44 |
+
.coverage
|
45 |
+
.coverage.*
|
46 |
+
.cache
|
47 |
+
nosetests.xml
|
48 |
+
coverage.xml
|
49 |
+
*.cover
|
50 |
+
*.py,cover
|
51 |
+
.hypothesis/
|
52 |
+
.pytest_cache/
|
53 |
+
|
54 |
+
# Translations
|
55 |
+
*.mo
|
56 |
+
*.pot
|
57 |
+
|
58 |
+
# Django stuff:
|
59 |
+
*.log
|
60 |
+
local_settings.py
|
61 |
+
db.sqlite3
|
62 |
+
db.sqlite3-journal
|
63 |
+
|
64 |
+
# Flask stuff:
|
65 |
+
instance/
|
66 |
+
.webassets-cache
|
67 |
+
|
68 |
+
# Scrapy stuff:
|
69 |
+
.scrapy
|
70 |
+
|
71 |
+
# Sphinx documentation
|
72 |
+
docs/_build/
|
73 |
+
|
74 |
+
# PyBuilder
|
75 |
+
target/
|
76 |
+
|
77 |
+
# Jupyter Notebook
|
78 |
+
.ipynb_checkpoints
|
79 |
+
|
80 |
+
# IPython
|
81 |
+
profile_default/
|
82 |
+
ipython_config.py
|
83 |
+
|
84 |
+
# pyenv
|
85 |
+
.python-version
|
86 |
+
|
87 |
+
# pipenv
|
88 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
89 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
90 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
91 |
+
# install all needed dependencies.
|
92 |
+
#Pipfile.lock
|
93 |
+
|
94 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
95 |
+
__pypackages__/
|
96 |
+
|
97 |
+
# Celery stuff
|
98 |
+
celerybeat-schedule
|
99 |
+
celerybeat.pid
|
100 |
+
|
101 |
+
# SageMath parsed files
|
102 |
+
*.sage.py
|
103 |
+
|
104 |
+
# Environments
|
105 |
+
.env
|
106 |
+
.venv
|
107 |
+
env/
|
108 |
+
venv/
|
109 |
+
ENV/
|
110 |
+
env.bak/
|
111 |
+
venv.bak/
|
112 |
+
|
113 |
+
# Spyder project settings
|
114 |
+
.spyderproject
|
115 |
+
.spyproject
|
116 |
+
|
117 |
+
# Rope project settings
|
118 |
+
.ropeproject
|
119 |
+
|
120 |
+
# mkdocs documentation
|
121 |
+
/site
|
122 |
+
|
123 |
+
# mypy
|
124 |
+
.mypy_cache/
|
125 |
+
.dmypy.json
|
126 |
+
dmypy.json
|
127 |
+
|
128 |
+
# Pyre type checker
|
129 |
+
.pyre/
|
.streamlit/config.toml
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
[server]
|
2 |
+
enableCORS = false
|
3 |
+
enableXsrfProtection = false
|
README.md
CHANGED
@@ -1,13 +1,97 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: streamlit
|
7 |
-
sdk_version: 1.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
-
license: artistic-2.0
|
11 |
---
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) which does _nothing_ in this state.
|
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 |
+
<img width="768" alt="image" src="https://github.com/deepset-ai/haystack-search-pipeline-streamlit/assets/15802862/f38bc0ef-3828-459b-9415-d7d84c6f7ce1">
|
28 |
+
|
29 |
+
## How to use this template
|
30 |
+
1. Create a new repository from this template or simply open it in a codespace to start playing around π
|
31 |
+
2. Make sure your `requirements.txt` file includes the Haystack and Streamlit versions you would like to use.
|
32 |
+
3. Complete the code to include your Haystack search pipeline and return the results.
|
33 |
+
4. Make any UI edits you'd like to and [share with the Haystack community](https://haystack.deepeset.ai/community) π₯³
|
34 |
+
|
35 |
+
### Repo structure
|
36 |
+
- `./utils`: This is where we have 3 files:
|
37 |
+
- `config.py`: This is empty in the current state. You may use this file if you'd like to make use of any secrets such as an OpenAI key, a token for an API and so on. An example of this is in [this demo project](https://github.com/TuanaCelik/should-i-follow/blob/main/utils/config.py).
|
38 |
+
- `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.
|
39 |
+
- `ui.py`: Use this file for any UI and initial value setups.
|
40 |
+
- `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.
|
41 |
+
|
42 |
+
### What to edit?
|
43 |
+
1. Create your Haystack search pipeline in the `start_haystack()` function. For example and Extractive QA pipeline:
|
44 |
+
|
45 |
+
```python
|
46 |
+
#choose a document store and write documents to it
|
47 |
+
document_store = InMemoryDocumentStore(use_bm25=True)
|
48 |
+
|
49 |
+
retriever = BM25Retriever(document_store=document_store)
|
50 |
+
reader = FARMReader(model_name_or_path="deepset/roberta-base-squad2", use_gpu=True)
|
51 |
+
|
52 |
+
pipe = Pipeline()
|
53 |
+
pipe.add_node(component=retriever, name="Retriever", inputs=['Query'])
|
54 |
+
pipe.add_node(component=reader, name="Reader", inputs=["Reader])
|
55 |
+
```
|
56 |
+
2. Run your Haystack search pipeline in the `query()` function and return the `results`. E.g.
|
57 |
+
```python
|
58 |
+
params = {"Retriever": {"top_k": 5}}
|
59 |
+
results = pipe.run(question, params=params)
|
60 |
+
return results["answers"]
|
61 |
+
```
|
62 |
+
|
63 |
+
## Pushing to Hugging Face Spaces π€
|
64 |
+
|
65 |
+
Below is an example GitHub action that will let you push your Streamlit app straight to the Hugging Face Hub as a Space.
|
66 |
+
|
67 |
+
A few things to pay attention to:
|
68 |
+
|
69 |
+
1. Create a New Space on Hugging Face with the Streamlit SDK.
|
70 |
+
2. Create a Hugging Face token on your HF account.
|
71 |
+
3. Create a secret on your GitHub repo called `HF_TOKEN` and put your Hugging Face token here.
|
72 |
+
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!
|
73 |
+
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.
|
74 |
+
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)
|
75 |
+
|
76 |
+
```yaml
|
77 |
+
name: Sync to Hugging Face hub
|
78 |
+
on:
|
79 |
+
push:
|
80 |
+
branches: [main]
|
81 |
+
|
82 |
+
# to run this workflow manually from the Actions tab
|
83 |
+
workflow_dispatch:
|
84 |
+
|
85 |
+
jobs:
|
86 |
+
sync-to-hub:
|
87 |
+
runs-on: ubuntu-latest
|
88 |
+
steps:
|
89 |
+
- uses: actions/checkout@v2
|
90 |
+
with:
|
91 |
+
fetch-depth: 0
|
92 |
+
lfs: true
|
93 |
+
- name: Push to hub
|
94 |
+
env:
|
95 |
+
HF_TOKEN: ${{ secrets.HF_TOKEN }}
|
96 |
+
run: git push --force https://{YOUR_HF_USERNAME}:$HF_TOKEN@{YOUR_HF_SPACE_REPO} main
|
97 |
+
```
|
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from annotated_text import annotation
|
3 |
+
from json import JSONDecodeError
|
4 |
+
import logging
|
5 |
+
from markdown import markdown
|
6 |
+
|
7 |
+
import streamlit as st
|
8 |
+
|
9 |
+
from utils.haystack import query
|
10 |
+
from utils.ui import reset_results, set_initial_state
|
11 |
+
|
12 |
+
set_initial_state()
|
13 |
+
|
14 |
+
st.write("# Start building out the content of your application here")
|
15 |
+
|
16 |
+
# Search bar
|
17 |
+
question = st.text_input("Ask a question", value=st.session_state.question, max_chars=100, on_change=reset_results)
|
18 |
+
|
19 |
+
run_pressed = st.button("Run")
|
20 |
+
|
21 |
+
run_query = (
|
22 |
+
run_pressed or question != st.session_state.question
|
23 |
+
)
|
24 |
+
|
25 |
+
# Get results for query
|
26 |
+
if run_query and question:
|
27 |
+
reset_results()
|
28 |
+
st.session_state.question = question
|
29 |
+
with st.spinner("π Running your pipeline"):
|
30 |
+
try:
|
31 |
+
st.session_state.results = query(question)
|
32 |
+
except JSONDecodeError as je:
|
33 |
+
st.error(
|
34 |
+
"π An error occurred reading the results. Is the document store working?"
|
35 |
+
)
|
36 |
+
except Exception as e:
|
37 |
+
logging.exception(e)
|
38 |
+
st.error("π An error occurred during the request.")
|
39 |
+
|
40 |
+
|
41 |
+
|
42 |
+
if st.session_state.results:
|
43 |
+
st.write('## Do something with your results')
|
44 |
+
answers = st.session_state.results
|
45 |
+
|
46 |
+
for count, answer in enumerate(answers):
|
47 |
+
if answer.answer:
|
48 |
+
text, context = answer.answer, answer.context
|
49 |
+
start_idx = context.find(text)
|
50 |
+
end_idx = start_idx + len(text)
|
51 |
+
st.write(
|
52 |
+
markdown(context[:start_idx] + str(annotation(body=text, label="ANSWER", background="#964448", color='#ffffff')) + context[end_idx:]),
|
53 |
+
unsafe_allow_html=True,
|
54 |
+
)
|
55 |
+
else:
|
56 |
+
st.info(
|
57 |
+
"π€ Haystack is unsure whether any of the documents contain an answer to your question. Try to reformulate it!"
|
58 |
+
)
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
farm-haystack==1.17.1
|
2 |
+
streamlit==1.23.0
|
3 |
+
markdown
|
4 |
+
st-annotated-text
|
5 |
+
datasets
|
utils/config.py
ADDED
File without changes
|
utils/haystack.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
|
3 |
+
from haystack import Pipeline
|
4 |
+
from haystack.schema import Answer
|
5 |
+
#Use this file to set up your Haystack pipeline and querying
|
6 |
+
|
7 |
+
|
8 |
+
# cached to make index and models load only at start
|
9 |
+
@st.cache_resource(show_spinner=False)
|
10 |
+
def start_haystack():
|
11 |
+
#Use this function to contruct a pipeline
|
12 |
+
pipe = Pipeline()
|
13 |
+
return pipe
|
14 |
+
|
15 |
+
pipe = start_haystack()
|
16 |
+
|
17 |
+
@st.cache_data(show_spinner=True)
|
18 |
+
def query(question):
|
19 |
+
print("Received question")
|
20 |
+
params = {}
|
21 |
+
# results = pipe.run(question, params=params)
|
22 |
+
return [Answer(answer="results", context="Call pipe.run(question, params=params) and return results in /utils/haystack.py query()")]
|
utils/ui.py
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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", None)
|
10 |
+
|
11 |
+
def reset_results(*args):
|
12 |
+
st.session_state.results = None
|