Spaces:
Sleeping
Sleeping
Henryk Borzymowski
commited on
Commit
Β·
90ba1bf
1
Parent(s):
10f015e
openai key as input
Browse files- .env +0 -3
- .gitignore +3 -0
- .vscode/launch.json +0 -29
- .vscode/settings.json +0 -3
- app.py +30 -13
- utils/__pycache__/config.cpython-310.pyc +0 -0
- utils/__pycache__/haystack.cpython-310.pyc +0 -0
- utils/__pycache__/ui.cpython-310.pyc +0 -0
- utils/config.py +0 -2
- utils/haystack.py +4 -4
.env
DELETED
@@ -1,3 +0,0 @@
|
|
1 |
-
OPENAI_KEY=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
|
2 |
-
EMBEDDING_MODEL=sentence-transformers/all-MiniLM-L12-v2
|
3 |
-
GENERATIVE_MODEL=text-davinci-003
|
|
|
|
|
|
|
|
.gitignore
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
.env
|
2 |
+
.vscode
|
3 |
+
*.pyc
|
.vscode/launch.json
DELETED
@@ -1,29 +0,0 @@
|
|
1 |
-
{
|
2 |
-
"version": "0.2.0",
|
3 |
-
"configurations": [
|
4 |
-
// {
|
5 |
-
// "name": "Python: Streamlit",
|
6 |
-
// "type": "python",
|
7 |
-
// "request": "launch",
|
8 |
-
// "program": "${workspaceFolder}/app.py",
|
9 |
-
// "args": ["--name", "My Opensearch Documentation Search"],
|
10 |
-
// "cwd": "${workspaceFolder}",
|
11 |
-
// "env": {
|
12 |
-
// "STREAMLIT_SERVER_ON": "1"
|
13 |
-
// },
|
14 |
-
// }
|
15 |
-
{
|
16 |
-
"name": "Python:Streamlit",
|
17 |
-
"type": "python",
|
18 |
-
"request": "launch",
|
19 |
-
"module": "streamlit",
|
20 |
-
"args": [
|
21 |
-
"run",
|
22 |
-
"${workspaceFolder}/app.py",
|
23 |
-
"--",
|
24 |
-
"--name",
|
25 |
-
"Document Insights: Extractive & Generative Methods",
|
26 |
-
]
|
27 |
-
}
|
28 |
-
]
|
29 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.vscode/settings.json
DELETED
@@ -1,3 +0,0 @@
|
|
1 |
-
{
|
2 |
-
"python.pythonPath": "/Users/h.borzymowski/opt/anaconda3/envs/haystag_rag/bin/python3"
|
3 |
-
}
|
|
|
|
|
|
|
|
app.py
CHANGED
@@ -10,27 +10,41 @@ from utils.config import parser
|
|
10 |
from utils.haystack import start_document_store, query, initialize_pipeline
|
11 |
from utils.ui import reset_results, set_initial_state
|
12 |
import pandas as pd
|
|
|
13 |
|
14 |
try:
|
15 |
args = parser.parse_args()
|
16 |
document_store = start_document_store(type=args.store)
|
17 |
st.set_page_config(
|
18 |
-
page_title="
|
19 |
layout="centered",
|
20 |
-
page_icon
|
21 |
menu_items={
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
26 |
st.sidebar.image("ml_logo.png", use_column_width=True)
|
27 |
|
28 |
# Sidebar for Task Selection
|
29 |
st.sidebar.header('Options:')
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
set_initial_state()
|
36 |
|
@@ -76,9 +90,12 @@ try:
|
|
76 |
"π An error occurred reading the results. Is the document store working?"
|
77 |
)
|
78 |
except Exception as e:
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
82 |
# Display results
|
83 |
if (st.session_state.results_extractive or st.session_state.results_generative) and run_query:
|
84 |
|
|
|
10 |
from utils.haystack import start_document_store, query, initialize_pipeline
|
11 |
from utils.ui import reset_results, set_initial_state
|
12 |
import pandas as pd
|
13 |
+
import haystack
|
14 |
|
15 |
try:
|
16 |
args = parser.parse_args()
|
17 |
document_store = start_document_store(type=args.store)
|
18 |
st.set_page_config(
|
19 |
+
page_title="MLReplySearch",
|
20 |
layout="centered",
|
21 |
+
page_icon=":shark:",
|
22 |
menu_items={
|
23 |
+
'Get Help': 'https://www.extremelycoolapp.com/help',
|
24 |
+
'Report a bug': "https://www.extremelycoolapp.com/bug",
|
25 |
+
'About': "# This is a header. This is an *extremely* cool app!"
|
26 |
+
}
|
27 |
+
)
|
28 |
st.sidebar.image("ml_logo.png", use_column_width=True)
|
29 |
|
30 |
# Sidebar for Task Selection
|
31 |
st.sidebar.header('Options:')
|
32 |
+
|
33 |
+
# OpenAI Key Input
|
34 |
+
openai_key = st.sidebar.text_input("Enter OpenAI Key:", type="password")
|
35 |
+
|
36 |
+
if openai_key:
|
37 |
+
task_options = ['Extractive', 'Generative']
|
38 |
+
else:
|
39 |
+
task_options = ['Extractive']
|
40 |
+
|
41 |
+
task_selection = st.sidebar.radio('Select the task:', task_options)
|
42 |
+
|
43 |
+
# Check the task and initialize pipeline accordingly
|
44 |
+
if task_selection == 'Extractive':
|
45 |
+
pipeline_extractive = initialize_pipeline("extractive", document_store)
|
46 |
+
elif task_selection == 'Generative' and openai_key: # Check for openai_key to ensure user has entered it
|
47 |
+
pipeline_rag = initialize_pipeline("rag", document_store, openai_key=openai_key)
|
48 |
|
49 |
set_initial_state()
|
50 |
|
|
|
90 |
"π An error occurred reading the results. Is the document store working?"
|
91 |
)
|
92 |
except Exception as e:
|
93 |
+
if "API key is invalid" in str(e):
|
94 |
+
logging.exception(e)
|
95 |
+
st.error("π incorrect API key provided. You can find your API key at https://platform.openai.com/account/api-keys.")
|
96 |
+
else:
|
97 |
+
logging.exception(e)
|
98 |
+
st.error("π An error occurred during the request.")
|
99 |
# Display results
|
100 |
if (st.session_state.results_extractive or st.session_state.results_generative) and run_query:
|
101 |
|
utils/__pycache__/config.cpython-310.pyc
DELETED
Binary file (1.52 kB)
|
|
utils/__pycache__/haystack.cpython-310.pyc
DELETED
Binary file (3.02 kB)
|
|
utils/__pycache__/ui.cpython-310.pyc
DELETED
Binary file (749 Bytes)
|
|
utils/config.py
CHANGED
@@ -7,9 +7,7 @@ load_dotenv()
|
|
7 |
parser = argparse.ArgumentParser(description='This app lists animals')
|
8 |
|
9 |
document_store_choices = ('inmemory', 'weaviate', 'milvus', 'opensearch')
|
10 |
-
task_choices = ('extractive', 'rag')
|
11 |
parser.add_argument('--store', choices=document_store_choices, default='inmemory', help='DocumentStore selection (default: %(default)s)')
|
12 |
-
#parser.add_argument('--task', choices=task_choices, default='rag', help='Task selection (default: %(default)s)')
|
13 |
parser.add_argument('--name', default="My Search App")
|
14 |
|
15 |
model_configs = {
|
|
|
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 = {
|
utils/haystack.py
CHANGED
@@ -62,14 +62,14 @@ def start_haystack_extractive(_document_store: BaseDocumentStore):
|
|
62 |
return pipe
|
63 |
|
64 |
@st.cache_resource(show_spinner=False)
|
65 |
-
def start_haystack_rag(_document_store: BaseDocumentStore):
|
66 |
retriever = EmbeddingRetriever(document_store=_document_store,
|
67 |
embedding_model=model_configs['EMBEDDING_MODEL'],
|
68 |
top_k=5)
|
69 |
_document_store.update_embeddings(retriever)
|
70 |
prompt_node = PromptNode(default_prompt_template="deepset/question-answering",
|
71 |
model_name_or_path=model_configs['GENERATIVE_MODEL'],
|
72 |
-
api_key=
|
73 |
pipe = Pipeline()
|
74 |
|
75 |
pipe.add_node(component=retriever, name="Retriever", inputs=["Query"])
|
@@ -83,8 +83,8 @@ def query(_pipeline, question):
|
|
83 |
results = _pipeline.run(question, params=params)
|
84 |
return results
|
85 |
|
86 |
-
def initialize_pipeline(task, document_store):
|
87 |
if task == 'extractive':
|
88 |
return start_haystack_extractive(document_store)
|
89 |
elif task == 'rag':
|
90 |
-
return start_haystack_rag(document_store)
|
|
|
62 |
return pipe
|
63 |
|
64 |
@st.cache_resource(show_spinner=False)
|
65 |
+
def start_haystack_rag(_document_store: BaseDocumentStore, openai_key):
|
66 |
retriever = EmbeddingRetriever(document_store=_document_store,
|
67 |
embedding_model=model_configs['EMBEDDING_MODEL'],
|
68 |
top_k=5)
|
69 |
_document_store.update_embeddings(retriever)
|
70 |
prompt_node = PromptNode(default_prompt_template="deepset/question-answering",
|
71 |
model_name_or_path=model_configs['GENERATIVE_MODEL'],
|
72 |
+
api_key=openai_key)
|
73 |
pipe = Pipeline()
|
74 |
|
75 |
pipe.add_node(component=retriever, name="Retriever", inputs=["Query"])
|
|
|
83 |
results = _pipeline.run(question, params=params)
|
84 |
return results
|
85 |
|
86 |
+
def initialize_pipeline(task, document_store, openai_key = ""):
|
87 |
if task == 'extractive':
|
88 |
return start_haystack_extractive(document_store)
|
89 |
elif task == 'rag':
|
90 |
+
return start_haystack_rag(document_store, openai_key)
|