raiyan22
raiyan22

Reputation: 1179

ValueError: Expected str or list, got <class 'streamlit.runtime.uploaded_file_manager.UploadedFile'>

I was trying to create a simple chat with csv app with OpenAI and streamlit. Following is my code:

from langchain.agents import create_csv_agent
from langchain.llms import OpenAI
from dotenv import load_dotenv
import os
import streamlit as st

def main():
    
    load_dotenv()

    # Load the OpenAI API key from the environment variable
    if os.getenv("OPENAI_API_KEY") is None or os.getenv("OPENAI_API_KEY") == "":
        print("OPENAI_API_KEY is not set")
        exit(1)
    else:
        print("OPENAI_API_KEY is set")

    st.set_page_config(page_title="Ask your CSV")
    st.header("Ask your CSV 📈")

    csv_file = st.file_uploader("Upload a CSV file", type="csv")
    if csv_file is not None:

        agent = create_csv_agent(
            OpenAI(temperature=0), csv_file, verbose=True)

        user_question = st.text_input("Ask a question about your CSV: ")

        if user_question is not None and user_question != "":
            with st.spinner(text="In progress..."):
                st.write(agent.run(user_question))


if __name__ == "__main__":
    main()

The code shows me this error:

2023-08-10 22:40:36.259 Uncaught app exception
Traceback (most recent call last):
  File "C:\Users\raiya\OneDrive\Desktop\chatCSV\venv\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 552, in _run_script
    exec(code, module.__dict__)
  File "C:\Users\raiya\OneDrive\Desktop\chatCSV\main.py", line 33, in <module>
    main()
  File "C:\Users\raiya\OneDrive\Desktop\chatCSV\main.py", line 24, in main
    agent = create_csv_agent(
  File "C:\Users\raiya\OneDrive\Desktop\chatCSV\venv\lib\site-packages\langchain\agents\agent_toolkits\csv\base.py", line 32, in create_csv_agent
    raise ValueError(f"Expected str or list, got {type(path)}")
ValueError: Expected str or list, got <class 'streamlit.runtime.uploaded_file_manager.UploadedFile'>

I suppose it has something to do with reading the file. Additionally to solve the issue I tried this solution but it does not seem to work. Can anyone help me fix this error?

Upvotes: 0

Views: 396

Answers (1)

ZKS
ZKS

Reputation: 2856

This is how it worked for me on Ubuntu 20.4.6. and VS Code

 pip install streamlit # in you virtual env

Working code, I removed all environment related code , as it is already set in my .environment file.

     from langchain.agents import create_csv_agent
            from langchain.llms import OpenAI
            import os
            import streamlit as st

            def main():

                st.set_page_config(page_title="Ask your CSV")
                st.header("Ask your CSV 📈")

                csv_file = st.file_uploader("Upload a CSV file", type="csv")
                if csv_file is not None:

                    agent = create_csv_agent(
                        OpenAI(temperature=0), csv_file, verbose=True)

                    user_question = st.text_input("Ask a question about your CSV: ")

                    if user_question is not None and user_question != "":
                        with st.spinner(text="In progress..."):
                            st.write(agent.run(user_question))



            if __name__ == "__main__":
                main()

In Terminal execute below command

       streamlit run "you path"/main.py

In browser I was able to run the app

App

Upvotes: 0

Related Questions