Created
March 6, 2024 12:47
-
-
Save fsndzomga/88115e9f5962ab4e27108e8faa289cd6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import streamlit as st | |
from st_chat_message import message | |
from dotenv import load_dotenv | |
import fitz | |
load_dotenv() | |
from raptor.raptor import RetrievalAugmentation # noqa | |
RA = RetrievalAugmentation() | |
def upload_document(): | |
uploaded_file = st.file_uploader("Upload Document", type=['pdf', 'txt']) | |
if uploaded_file: | |
st.success("Document uploaded successfully", icon="😎") | |
file_contents = "" | |
if uploaded_file.type == 'text/plain': | |
file_contents = uploaded_file.read().decode('utf-8') | |
elif uploaded_file.type == 'application/pdf': | |
with fitz.open("pdf", uploaded_file.getvalue()) as doc: | |
texts = [page.get_text() for page in doc] | |
file_contents = " ".join(texts) | |
RA.add_documents(file_contents) | |
st.spinner("Creating your document index...") | |
chat_interface() | |
def chat_interface(): | |
message("I am your AI Assistant. Ask any question about the document you uploaded !") | |
user_input = st.chat_input("Enter your message here") | |
if user_input: | |
message(user_input, is_user=True) | |
answer = RA.answer_question(question=user_input) | |
message(answer) | |
def main(): | |
st.title("RAG Chat App Using Recursive Abstractive Processing for Tree-Organized Retrieval") | |
upload_document() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment