Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,65 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import os
|
| 3 |
-
from pathlib import Path
|
| 4 |
-
from sentence_transformers import SentenceTransformer
|
| 5 |
-
from streamlit_utils import local_css, remote_css, load_pdf_files
|
| 6 |
-
from config import load_app_config, set_global_api_key
|
| 7 |
-
from indexing_utils import ServiceContextLoader, create_multi_index
|
| 8 |
-
from query_executers import QueryExecuter
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from sentence_transformers import SentenceTransformer
|
| 5 |
+
from streamlit_utils import local_css, remote_css, load_pdf_files
|
| 6 |
+
from config import load_app_config, set_global_api_key
|
| 7 |
+
from indexing_utils import ServiceContextLoader, create_multi_index
|
| 8 |
+
from query_executers import QueryExecuter
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
#multi_index_demo_app.py (revoir ici )
|
| 12 |
+
#https://www.youtube.com/watch?v=xOOIVwH3g68
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def main():
|
| 16 |
+
"""
|
| 17 |
+
This is the main method of the streamlit application
|
| 18 |
+
"""
|
| 19 |
+
# Set the OpenAI Api key as an environment variable
|
| 20 |
+
set_global_api_key()
|
| 21 |
+
|
| 22 |
+
dirname = Path(os.path.dirname(__file__))
|
| 23 |
+
local_css((dirname /"style.css").as_posix())
|
| 24 |
+
remote_css('https://fonts.googleapis.com/icon?family=Material+Icons')
|
| 25 |
+
|
| 26 |
+
# Load a Configuration object for the application
|
| 27 |
+
app_config = load_app_config()
|
| 28 |
+
|
| 29 |
+
# Initialize a ServiceContext for the QueryEngine
|
| 30 |
+
service_context = ServiceContextLoader(app_config=app_config).load()
|
| 31 |
+
|
| 32 |
+
# Initialize a simple SentenceTransformer model for clustering the final responses
|
| 33 |
+
sbert_model = SentenceTransformer(app_config.ClusteringConfig.SentenceTransformerModel)
|
| 34 |
+
|
| 35 |
+
st.title("Parallel Multi-Document Question Answering")
|
| 36 |
+
|
| 37 |
+
# Provide a file_uploader with drag and drop functionality
|
| 38 |
+
multiple_files = st.file_uploader(
|
| 39 |
+
"Drop multiple files:", accept_multiple_files=True
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
if multiple_files is None:
|
| 43 |
+
st.text("No upload")
|
| 44 |
+
else:
|
| 45 |
+
files = [file for file in multiple_files if str(file.name).endswith(".pdf")]
|
| 46 |
+
|
| 47 |
+
# Load the pdf files based on the file objects
|
| 48 |
+
file_content_list = load_pdf_files(files=files)
|
| 49 |
+
|
| 50 |
+
if file_content_list:
|
| 51 |
+
top_k = app_config.QueryEngineConfig.similarity_top_k
|
| 52 |
+
|
| 53 |
+
# Create a multi-index query engine based on the pdf file content
|
| 54 |
+
multi_index_query_engine = create_multi_index(file_content_list=file_content_list,
|
| 55 |
+
_service_context=service_context,
|
| 56 |
+
top_k=top_k)
|
| 57 |
+
|
| 58 |
+
# Execute the query and display the results in the streamlit app
|
| 59 |
+
query_executer = QueryExecuter(query_engine=multi_index_query_engine,
|
| 60 |
+
sbert_model=sbert_model,
|
| 61 |
+
config=app_config)
|
| 62 |
+
query_executer.run()
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
main()
|