Spaces:
Runtime error
Runtime error
| """Frameworks for running multiple Streamlit applications as a single app. | |
| """ | |
| import streamlit as st | |
| from PIL import Image | |
| import tempfile | |
| import logging | |
| from sklearn.feature_extraction import _stop_words | |
| from haystack.document_stores import InMemoryDocumentStore | |
| from haystack.pipelines import ExtractiveQAPipeline | |
| from haystack.nodes import FARMReader, TfidfRetriever | |
| import scripts.process as pre | |
| import scripts.clean as clean | |
| logger = logging.getLogger(__name__) | |
| # Initialization | |
| if 'file' not in st.session_state: | |
| st.session_state['pipeline'] = None | |
| class MultiApp: | |
| """ | |
| Framework for combining multiple streamlit applications. | |
| """ | |
| def __init__(self): | |
| self.apps = [] | |
| def add_app(self, title, func): | |
| """Adds a new application. | |
| Parameters | |
| ---------- | |
| func: | |
| the python function to render this app. | |
| title: | |
| title of the app. Appears in the dropdown in the sidebar. | |
| """ | |
| self.apps.append({ | |
| "title": title, | |
| # "icon": icon, | |
| "function": func | |
| }) | |
| def run(self): | |
| if 'file' not in st.session_state: | |
| st.session_state['pipeline'] = None | |
| st.sidebar.write(format_func=lambda app: app['title']) | |
| image = Image.open('appStore/img/sdsn.png') | |
| st.sidebar.image(image) | |
| app = st.sidebar.radio( | |
| 'Pages', | |
| self.apps, | |
| format_func=lambda app: app['title']) | |
| app['function']() | |
| st.sidebar.markdown('') | |
| st.sidebar.markdown("## π Upload document ") | |
| file = st.sidebar.file_uploader('', type=['pdf', 'docx', 'txt']) #Upload PDF File | |
| if file is not None: | |
| with tempfile.NamedTemporaryFile(mode="wb") as temp: | |
| bytes_data = file.getvalue() | |
| temp.write(bytes_data) | |
| file_name = file.name | |
| file_path = temp.name | |
| st.write("Filename: ", file.name) | |
| # load document | |
| documents = pre.load_document(temp.name,file_name) | |
| documents_processed = pre.preprocessing(documents) | |
| pipeline = start_haystack(documents_processed) | |
| st.session_state['pipeline'] = pipeline |