Spaces:
Running
Running
silvanocerza
commited on
Commit
•
c5936cd
0
Parent(s):
First commit
Browse files- .gitignore +197 -0
- README.md +1 -0
- main.py +133 -0
- requirements.txt +7 -0
.gitignore
ADDED
@@ -0,0 +1,197 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Created by https://www.toptal.com/developers/gitignore/api/visualstudiocode,python
|
2 |
+
# Edit at https://www.toptal.com/developers/gitignore?templates=visualstudiocode,python
|
3 |
+
|
4 |
+
### Python ###
|
5 |
+
# Byte-compiled / optimized / DLL files
|
6 |
+
__pycache__/
|
7 |
+
*.py[cod]
|
8 |
+
*$py.class
|
9 |
+
|
10 |
+
# C extensions
|
11 |
+
*.so
|
12 |
+
|
13 |
+
# Distribution / packaging
|
14 |
+
.Python
|
15 |
+
build/
|
16 |
+
develop-eggs/
|
17 |
+
dist/
|
18 |
+
downloads/
|
19 |
+
eggs/
|
20 |
+
.eggs/
|
21 |
+
lib/
|
22 |
+
lib64/
|
23 |
+
parts/
|
24 |
+
sdist/
|
25 |
+
var/
|
26 |
+
wheels/
|
27 |
+
share/python-wheels/
|
28 |
+
*.egg-info/
|
29 |
+
.installed.cfg
|
30 |
+
*.egg
|
31 |
+
MANIFEST
|
32 |
+
|
33 |
+
# PyInstaller
|
34 |
+
# Usually these files are written by a python script from a template
|
35 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
36 |
+
*.manifest
|
37 |
+
*.spec
|
38 |
+
|
39 |
+
# Installer logs
|
40 |
+
pip-log.txt
|
41 |
+
pip-delete-this-directory.txt
|
42 |
+
|
43 |
+
# Unit test / coverage reports
|
44 |
+
htmlcov/
|
45 |
+
.tox/
|
46 |
+
.nox/
|
47 |
+
.coverage
|
48 |
+
.coverage.*
|
49 |
+
.cache
|
50 |
+
nosetests.xml
|
51 |
+
coverage.xml
|
52 |
+
*.cover
|
53 |
+
*.py,cover
|
54 |
+
.hypothesis/
|
55 |
+
.pytest_cache/
|
56 |
+
cover/
|
57 |
+
|
58 |
+
# Translations
|
59 |
+
*.mo
|
60 |
+
*.pot
|
61 |
+
|
62 |
+
# Django stuff:
|
63 |
+
*.log
|
64 |
+
local_settings.py
|
65 |
+
db.sqlite3
|
66 |
+
db.sqlite3-journal
|
67 |
+
|
68 |
+
# Flask stuff:
|
69 |
+
instance/
|
70 |
+
.webassets-cache
|
71 |
+
|
72 |
+
# Scrapy stuff:
|
73 |
+
.scrapy
|
74 |
+
|
75 |
+
# Sphinx documentation
|
76 |
+
docs/_build/
|
77 |
+
|
78 |
+
# PyBuilder
|
79 |
+
.pybuilder/
|
80 |
+
target/
|
81 |
+
|
82 |
+
# Jupyter Notebook
|
83 |
+
.ipynb_checkpoints
|
84 |
+
|
85 |
+
# IPython
|
86 |
+
profile_default/
|
87 |
+
ipython_config.py
|
88 |
+
|
89 |
+
# pyenv
|
90 |
+
# For a library or package, you might want to ignore these files since the code is
|
91 |
+
# intended to run in multiple environments; otherwise, check them in:
|
92 |
+
# .python-version
|
93 |
+
|
94 |
+
# pipenv
|
95 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
96 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
97 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
98 |
+
# install all needed dependencies.
|
99 |
+
#Pipfile.lock
|
100 |
+
|
101 |
+
# poetry
|
102 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
103 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
104 |
+
# commonly ignored for libraries.
|
105 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
106 |
+
#poetry.lock
|
107 |
+
|
108 |
+
# pdm
|
109 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
110 |
+
#pdm.lock
|
111 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
112 |
+
# in version control.
|
113 |
+
# https://pdm.fming.dev/#use-with-ide
|
114 |
+
.pdm.toml
|
115 |
+
|
116 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
117 |
+
__pypackages__/
|
118 |
+
|
119 |
+
# Celery stuff
|
120 |
+
celerybeat-schedule
|
121 |
+
celerybeat.pid
|
122 |
+
|
123 |
+
# SageMath parsed files
|
124 |
+
*.sage.py
|
125 |
+
|
126 |
+
# Environments
|
127 |
+
.env
|
128 |
+
.venv
|
129 |
+
env/
|
130 |
+
venv/
|
131 |
+
ENV/
|
132 |
+
env.bak/
|
133 |
+
venv.bak/
|
134 |
+
|
135 |
+
# Spyder project settings
|
136 |
+
.spyderproject
|
137 |
+
.spyproject
|
138 |
+
|
139 |
+
# Rope project settings
|
140 |
+
.ropeproject
|
141 |
+
|
142 |
+
# mkdocs documentation
|
143 |
+
/site
|
144 |
+
|
145 |
+
# mypy
|
146 |
+
.mypy_cache/
|
147 |
+
.dmypy.json
|
148 |
+
dmypy.json
|
149 |
+
|
150 |
+
# Pyre type checker
|
151 |
+
.pyre/
|
152 |
+
|
153 |
+
# pytype static type analyzer
|
154 |
+
.pytype/
|
155 |
+
|
156 |
+
# Cython debug symbols
|
157 |
+
cython_debug/
|
158 |
+
|
159 |
+
# PyCharm
|
160 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
161 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
162 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
163 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
164 |
+
#.idea/
|
165 |
+
|
166 |
+
### Python Patch ###
|
167 |
+
# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
|
168 |
+
poetry.toml
|
169 |
+
|
170 |
+
# ruff
|
171 |
+
.ruff_cache/
|
172 |
+
|
173 |
+
# LSP config files
|
174 |
+
pyrightconfig.json
|
175 |
+
|
176 |
+
### VisualStudioCode ###
|
177 |
+
.vscode/*
|
178 |
+
!.vscode/settings.json
|
179 |
+
!.vscode/tasks.json
|
180 |
+
!.vscode/launch.json
|
181 |
+
!.vscode/extensions.json
|
182 |
+
!.vscode/*.code-snippets
|
183 |
+
|
184 |
+
# Local History for Visual Studio Code
|
185 |
+
.history/
|
186 |
+
|
187 |
+
# Built Visual Studio Code Extensions
|
188 |
+
*.vsix
|
189 |
+
|
190 |
+
### VisualStudioCode Patch ###
|
191 |
+
# Ignore all local history of files
|
192 |
+
.history
|
193 |
+
.ionide
|
194 |
+
|
195 |
+
downloaded_docs/*
|
196 |
+
|
197 |
+
# End of https://www.toptal.com/developers/gitignore/api/visualstudiocode,python
|
README.md
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
# Search all the docs demo
|
main.py
ADDED
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List, Tuple
|
2 |
+
from pathlib import Path
|
3 |
+
import subprocess
|
4 |
+
import os
|
5 |
+
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
from haystack.preview import Pipeline
|
8 |
+
from haystack.preview.components.retrievers import MemoryBM25Retriever
|
9 |
+
from haystack.preview.components.generators.openai.gpt import GPTGenerator
|
10 |
+
from haystack.preview.components.builders.prompt_builder import PromptBuilder
|
11 |
+
from haystack.preview.components.preprocessors import (
|
12 |
+
DocumentCleaner,
|
13 |
+
TextDocumentSplitter,
|
14 |
+
)
|
15 |
+
from haystack.preview.components.writers import DocumentWriter
|
16 |
+
from haystack.preview.components.file_converters import TextFileToDocument
|
17 |
+
from haystack.preview.document_stores.memory import MemoryDocumentStore
|
18 |
+
import streamlit as st
|
19 |
+
|
20 |
+
# Load the environment variables, we're going to need it for OpenAI
|
21 |
+
load_dotenv()
|
22 |
+
|
23 |
+
# This is the list of documentation that we're going to fetch
|
24 |
+
DOCUMENTATIONS = [
|
25 |
+
("docarray", "https://github.com/docarray/docarray.git", "./docs/**/*.md"),
|
26 |
+
("streamlit", "https://github.com/streamlit/docs.git", "./content/**/*.md"),
|
27 |
+
("jinja", "https://github.com/pallets/jinja.git", "./docs/**/*.rst"),
|
28 |
+
("pandas", "https://github.com/pandas-dev/pandas.git", "./docs/source/**/*.rst"),
|
29 |
+
(
|
30 |
+
"elasticsearch",
|
31 |
+
"https://github.com/elastic/elasticsearch.git",
|
32 |
+
"./docs/**/*.asciidoc",
|
33 |
+
),
|
34 |
+
]
|
35 |
+
|
36 |
+
|
37 |
+
@st.cache_data(show_spinner=False)
|
38 |
+
def fetch(documentations: List[Tuple[str, str, str]]):
|
39 |
+
paths = []
|
40 |
+
for name, url, pattern in documentations:
|
41 |
+
st.write(f"Fetching {name} repository")
|
42 |
+
repo = Path(__file__).parent / "downloaded_docs" / name
|
43 |
+
if not repo.exists():
|
44 |
+
subprocess.run(["git", "clone", "--depth", "1", url, str(repo)], check=True)
|
45 |
+
paths.extend(repo.glob(pattern))
|
46 |
+
|
47 |
+
return paths
|
48 |
+
|
49 |
+
|
50 |
+
@st.cache_resource
|
51 |
+
def document_store():
|
52 |
+
# We're going to store the processed documents in here
|
53 |
+
return MemoryDocumentStore()
|
54 |
+
|
55 |
+
|
56 |
+
@st.cache_resource
|
57 |
+
def index_files(files):
|
58 |
+
# We create some components
|
59 |
+
text_converter = TextFileToDocument(progress_bar=False)
|
60 |
+
document_cleaner = DocumentCleaner()
|
61 |
+
document_splitter = TextDocumentSplitter()
|
62 |
+
document_writer = DocumentWriter(
|
63 |
+
document_store=document_store(), policy="overwrite"
|
64 |
+
)
|
65 |
+
|
66 |
+
# And our pipeline
|
67 |
+
indexing_pipeline = Pipeline()
|
68 |
+
indexing_pipeline.add_component("converter", text_converter)
|
69 |
+
indexing_pipeline.add_component("cleaner", document_cleaner)
|
70 |
+
indexing_pipeline.add_component("splitter", document_splitter)
|
71 |
+
indexing_pipeline.add_component("writer", document_writer)
|
72 |
+
indexing_pipeline.connect("converter", "cleaner")
|
73 |
+
indexing_pipeline.connect("cleaner", "splitter")
|
74 |
+
indexing_pipeline.connect("splitter", "writer")
|
75 |
+
|
76 |
+
# And now we clone and save the documentation in our MemoryDocumentStore
|
77 |
+
indexing_pipeline.run({"converter": {"paths": files}})
|
78 |
+
|
79 |
+
|
80 |
+
def search(question: str) -> str:
|
81 |
+
retriever = MemoryBM25Retriever(document_store=document_store(), top_k=10)
|
82 |
+
|
83 |
+
template = """Take a deep breath and think then answer given the context
|
84 |
+
Context: {{ documents|map(attribute='text')|join('\n') }}
|
85 |
+
Question: {{ question }}
|
86 |
+
Answer:
|
87 |
+
"""
|
88 |
+
prompt_builder = PromptBuilder(template)
|
89 |
+
|
90 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "")
|
91 |
+
generator = GPTGenerator(api_key=OPENAI_API_KEY)
|
92 |
+
|
93 |
+
pipe = Pipeline()
|
94 |
+
|
95 |
+
pipe.add_component("docs_retriever", retriever)
|
96 |
+
pipe.add_component("builder", prompt_builder)
|
97 |
+
pipe.add_component("gpt35", generator)
|
98 |
+
|
99 |
+
pipe.connect("docs_retriever.documents", "builder.documents")
|
100 |
+
pipe.connect("builder.prompt", "gpt35.prompt")
|
101 |
+
res = pipe.run(
|
102 |
+
{
|
103 |
+
"docs_retriever": {"query": question},
|
104 |
+
"builder": {"question": question},
|
105 |
+
}
|
106 |
+
)
|
107 |
+
return res["gpt35"]["replies"][0]
|
108 |
+
|
109 |
+
|
110 |
+
with st.status(
|
111 |
+
"Downloading documentation files...",
|
112 |
+
expanded=st.session_state.get("expanded", True),
|
113 |
+
) as status:
|
114 |
+
files = fetch(DOCUMENTATIONS)
|
115 |
+
status.update(label="Indexing documentation...")
|
116 |
+
index_files(files)
|
117 |
+
status.update(
|
118 |
+
label="Download and indexing complete!", state="complete", expanded=False
|
119 |
+
)
|
120 |
+
st.session_state["expanded"] = False
|
121 |
+
|
122 |
+
|
123 |
+
st.header("Documentation finder", divider="rainbow")
|
124 |
+
|
125 |
+
|
126 |
+
if question := st.text_input(
|
127 |
+
label="What do you need to know?", placeholder="What is a DataFrame?"
|
128 |
+
):
|
129 |
+
with st.spinner("Waiting"):
|
130 |
+
answer = search(question)
|
131 |
+
|
132 |
+
st.balloons()
|
133 |
+
st.markdown(answer)
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
haystack-ai==0.79.0
|
2 |
+
ipython
|
3 |
+
more_itertools
|
4 |
+
langdetect
|
5 |
+
streamlit
|
6 |
+
python-dotenv
|
7 |
+
watchdog
|