yvann-ba / Robby-chatbot

AI chatbot 🤖 for chat with CSV, PDF, TXT files 📄 and YTB videos 🎥 | using Langchain🦜 | OpenAI | Streamlit ⚡

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Any ideas how to add button to upload specified file?

aiakubovich opened this issue · comments

Hi. I was trying to add button to upload specified file from folder but for some reason it does not work. I want from app to have two ways to upload files. From folder ("Use Data from folder" button) and from the "drag and drop" box. The option with the "drag and drop" box works fine (it as alrady in), but for the option with the "Use Data from folder" button, chat appears once, but when I put a question in chat and click "send", chat disappears for some reason.

Here code for buttons from main app:

use_example_file = st.sidebar.button("Use Data from folder")
uploaded_file = utils.handle_upload(["pdf", "txt", "csv"], use_example_file)

if uploaded_file:
    [no changes here]

Where I changed handle_upload little bit:

@staticmethod
def handle_upload(file_types, use_example_file):
    """
    Handles and display uploaded_file
    :param file_types: List of accepted file types, e.g., ["csv", "pdf", "txt"]
    """
    
    if use_example_file is False:
        uploaded_file = st.sidebar.file_uploader("upload", type=file_types, label_visibility="collapsed")
    else: 
        # uploaded_file = use_example_file
        # use_example_file = st.sidebar.button(use_example_file)
        uploaded_file = open("example.csv", "rb")

    if uploaded_file is not None:

        def show_csv_file(uploaded_file):
            file_container = st.expander("Your CSV file :")
            uploaded_file.seek(0)
            shows = pd.read_csv(uploaded_file)
            file_container.write(shows)

        def show_pdf_file(uploaded_file):
            file_container = st.expander("Your PDF file :")
            with pdfplumber.open(uploaded_file) as pdf:
                pdf_text = ""
                for page in pdf.pages:
                    pdf_text += page.extract_text() + "\n\n"
            file_container.write(pdf_text)
        
        def show_txt_file(uploaded_file):
            file_container = st.expander("Your TXT file:")
            uploaded_file.seek(0)
            content = uploaded_file.read().decode("utf-8")
            file_container.write(content)
        
        def get_file_extension(uploaded_file):
            return os.path.splitext(uploaded_file)[1].lower()
        
        file_extension = get_file_extension(uploaded_file.name)

        # Show the contents of the file based on its extension
        #if file_extension == ".csv" :
        #    show_csv_file(uploaded_file)
        if file_extension== ".pdf" : 
            show_pdf_file(uploaded_file)
        elif file_extension== ".txt" : 
            show_txt_file(uploaded_file)

    else:
        st.session_state["reset_chat"] = True

    #print(uploaded_file)
    return uploaded_file