gpt4 book ai didi

python - 使用 Streamlit file_uploader 后 "TypeError: expected str, bytes or os.PathLike object, not NoneType"

转载 作者:行者123 更新时间:2023-12-02 05:47:24 28 4
gpt4 key购买 nike

我正在尝试使用 streamlit 和 gpt 索引来创建某种虚拟测试网站。我不断收到同样的错误。

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)

为什么我不断收到“TypeError:预期的 str、bytes 或 os.PathLike 对象,而不是 NoneType”?

这是完整的错误:

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

最佳答案

根据 Streamlit 文档,st.file_uploader 要么返回 None,要么返回一个 UploadedFileObject(可以用作“类文件” "对象):

https://docs.streamlit.io/library/api-reference/widgets/st.file_uploader

它没有明确提及它,但据推测,如果没有文件上传,它会返回 None。我们无法告诉您为什么没有上传文件。

在您的情况下,这意味着 fileup 变量是 None,这是 SimpleDirectoryReader 的无效参数,如错误消息所示.

文档中的示例包含一个 if 语句,仅当返回值不是 None 时才继续执行脚本。您或许也应该这样做。

关于python - 使用 Streamlit file_uploader 后 "TypeError: expected str, bytes or os.PathLike object, not NoneType",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75440754/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com