Team Ai
Apppublic

andreped/ReferenceBot

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
1from typing import List2from typing import NoReturn3 4import streamlit as st5from langchain.docstore.document import Document6from streamlit.logger import get_logger7 8from knowledge_gpt.core.parsing import File9 10logger = get_logger(__name__)11 12 13def wrap_doc_in_html(docs: List[Document]) -> str:14    """Wraps each page in document separated by newlines in <p> tags"""15    text = [doc.page_content for doc in docs]16    if isinstance(text, list):17        # Add horizontal rules between pages18        text = "\n<hr/>\n".join(text)19    return "".join([f"<p>{line}</p>" for line in text.split("\n")])20 21 22def is_query_valid(query: str) -> bool:23    if not query:24        st.error("Please enter a question!")25        return False26    return True27 28 29def is_file_valid(file: File) -> bool:30    if len(file.docs) == 0 or "".join([doc.page_content for doc in file.docs]).strip() == "":31        st.error("Cannot read document! Make sure the document has selectable text")32        logger.error("Cannot read document")33        return False34    return True35 36 37def display_file_read_error(e: Exception, file_name: str) -> NoReturn:38    st.error("Error reading file. Make sure the file is not corrupted or encrypted")39    logger.error(f"{e.__class__.__name__}: {e}. Extension: {file_name.split('.')[-1]}")40    st.stop()41