Reputation: 51
I am trying to use streamlit and gpt index to create some kind of a dummy test website. I keep getting the same error.
from gpt_index import GPTListIndex, readers, GPTSimpleVectorIndex, LLMPredictor, PromptHelper, download_loader
from langchain import OpenAI
import logging
import sys
import os
import streamlit as st
os.environ['OPENAI_API_KEY'] = "I am deleting my api key from this post"
fileup = st.file_uploader(label=" ")
SimpleDirectoryReader = download_loader("SimpleDirectoryReader")
loader = SimpleDirectoryReader(fileup)
documents = loader.load_data()
index = GPTSimpleVectorIndex(documents)
index.save_to_disk('index.json')
question = st.text_input("What do you want me to do with the file uploaded?")
response = index.query(question)
st.write(response)
Why do I keep getting "TypeError: expected str, bytes or os.PathLike object, not NoneType"?
Here is the full error:
Traceback (most recent call last):
File "/home/appuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script
exec(code, module.__dict__)
File "/app/indextest/streamlit_app.py", line 16, in <module>
loader = SimpleDirectoryReader(fileup)
File ".modules/file.py", line 67, in __init__
self.input_dir = Path(input_dir)
File "/usr/local/lib/python3.9/pathlib.py", line 1082, in __new__
self = cls._from_parts(args, init=False)
File "/usr/local/lib/python3.9/pathlib.py", line 707, in _from_parts
drv, root, parts = self._parse_args(args)
File "/usr/local/lib/python3.9/pathlib.py", line 691, in _parse_args
a = os.fspath(a)
TypeError: expected str, bytes or os.PathLike object, not NoneType
Upvotes: 0
Views: 2604
Reputation: 1
fileuploader returns the following class
class UploadedFile(io.BytesIO):
"""A mutable uploaded file.
This class extends BytesIO, which has copy-on-write semantics when
initialized with `bytes`.
"""
def __init__(self, record: UploadedFileRec, file_urls: FileURLsProto):
# BytesIO's copy-on-write semantics doesn't seem to be mentioned in
# the Python docs - possibly because it's a CPython-only optimization
# and not guaranteed to be in other Python runtimes. But it's detailed
# here: https://hg.python.org/cpython/rev/79a5fbe2c78f
super().__init__(record.data)
self.file_id = record.file_id
self.name = record.name
self.type = record.type
self.size = len(record.data)
self._file_urls = file_urls
def __eq__(self, other: object) -> bool:
if not isinstance(other, UploadedFile):
return NotImplemented
return self.file_id == other.file_id
def __repr__(self) -> str:
return util.repr_(self)
Assuming you are trying to upload this file, you will first have to save the file and then upload it since it is asking for a path and not (io.BytesIO).
save_path = Path(save_folder, uploaded_file.name)
with open(save_path, mode='wb') as w:
w.write(uploaded_file.getvalue())
loader = SimpleDirectoryReader(save_path)
This will create a local copy on your system as well as provide a path as the input parameter to the SimpleDirectoryReader function.
Upvotes: 0
Reputation: 23250
According to the Streamlit documentation, st.file_uploader
either returns None
or an UploadedFileObject
(which can be used as a "file-like" object):
https://docs.streamlit.io/library/api-reference/widgets/st.file_uploader
It doesn't explicitly mention it, but presumably it returns None
if no file was uploaded. We can't tell you why no file was uploaded.
In your case this means that the fileup
variable is None
, which is an invalid argument for SimpleDirectoryReader
, as seen from the error message.
The example in the documentation includes an if
statement to only continue in the script if the returned value is not None
. You should probably do the same.
Upvotes: 0