尝试在 Ubuntu 22.04 WSL 下运行 GPT 检测器时出错

Not*_*1ds 11 python cuda windows-subsystem-for-linux

虽然OpenAI 检测器在识别由 ChatGPT 和其他基于 OpenAI 的模型创建的内容方面非常有用,但随着使用量的增加(尤其是 Stack Exchange 网站上的用户),它的下降频率越来越高。

根据项目README在本地安装后,尝试使用以下命令从 repo 目录运行它时收到以下错误python -m detector.server ../gpt-2-models/detector-base.pt

Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/home/ntd/src/gpt-2-output-dataset/detector/server.py", line 120, in <module>
    fire.Fire(main)
  File "/home/ntd/src/venv/openai-detector/lib/python3.10/site-packages/fire/core.py", line 141, in Fire
    component_trace = _Fire(component, args, parsed_flag_args, context, name)
  File "/home/ntd/src/venv/openai-detector/lib/python3.10/site-packages/fire/core.py", line 475, in _Fire
    component, remaining_args = _CallAndUpdateTrace(
  File "/home/ntd/src/venv/openai-detector/lib/python3.10/site-packages/fire/core.py", line 691, in _CallAndUpdateTrace
    component = fn(*varargs, **kwargs)
  File "/home/ntd/src/gpt-2-output-dataset/detector/server.py", line 89, in main
    model.load_state_dict(data['model_state_dict'])
  File "/home/ntd/src/venv/openai-detector/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1671, in load_state_dict
    raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
RuntimeError: Error(s) in loading state_dict for RobertaForSequenceClassification:
        Missing key(s) in state_dict: "roberta.embeddings.position_ids".
        Unexpected key(s) in state_dict: "roberta.pooler.dense.weight", "roberta.pooler.dense.bias".
Run Code Online (Sandbox Code Playgroud)

我尝试根据本期的transformers==2.9.1评论进行更改,但也失败了。pip install -r requirements.txt

Not*_*1ds 13

保留这个答案,因为它仍然包含有用的信息,但我在这个答案中添加了一个更简单的方法。如果你有 Docker Desktop,你也可以使用这个答案。这两个较新的答案都具有严格遵循原始 HuggingSpace 实现的优点,而这个答案缺乏这一点。


这里的主要问题似乎可以通过使用transformers==2.5.1(而不是 2.9.1)来解决,但我还需要 Rust 编译器(和build-essential)来构建它。其中大部分(至少从步骤 11 开始)也可能适用于非 WSL Ubuntu。然而,CUDA 还存在一些额外的依赖项(我不能完全确定是哪些依赖项,因为我没有用于测试的纯 Ubuntu GPU 系统)。

以下是我在 WSL 上的 Ubuntu 22.04 上安装的完整步骤。请注意,您可以通过不为检测器设置特殊发行版、不设置 Pythonvenv或什至跳过两者来大大简化它。老实说,就“隔离”而言,两者都做是多余的,但步骤都在这里,具体取决于您想要如何处理它:

  1. 从 PowerShell注册了新的 Ubuntu 22.04 WSL 发行版ubuntu2204.exe。以前不存在,原因您将在下面看到。

  2. 根据要求添加用户名和密码。

  3. 运行正常,初始sudo apt update && sudo apt upgrade -y

  4. /etc/wsl.conf使用我的答案设置默认用户名。

  5. 退出Ubuntu

  6. wsl --shutdown

  7. 为我的“openai- detector”实例创建了一个目录:

    mkdir D:\WSL\instances\openai-detector
    
    Run Code Online (Sandbox Code Playgroud)
  8. 将刚刚创建的 Ubuntu 22.04 实例复制到名为 的新发行版openai-detector

    wsl --import --vhd openai-detector D:\wsl\instances\openai-detector\ $env:localappdata\Packages\CanonicalGroupLimited.Ubuntu22.04LTS_79rhkp1fndgsc\LocalState\ext4.vhdx --version 2
    
    Run Code Online (Sandbox Code Playgroud)
  9. 删除了该ubuntu-22.04发行版,因为我总是可以在需要时按需创建另一个发行版(如上所述)。但是,只有当您确定这是您刚刚创建的文件并且其中没有您需要的文件时,才请执行此操作。这是一个不可逆转、可破坏的操作。老实说,每次我这样做时我都有点紧张,因为我有可能不小心使用了错误的发行版名称。只是......小心

    wsl --unregister ubuntu-22.04
    
    Run Code Online (Sandbox Code Playgroud)
  10. 启动上面创建的新openai-detector发行版:

    wsl ~ -d openai-detector
    
    Run Code Online (Sandbox Code Playgroud)
  11. 安装rustupbuild-essential

    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
    source "$HOME/.cargo/env"
    sudo apt install build-essential
    
    Run Code Online (Sandbox Code Playgroud)
  12. 设置虚拟环境:

    sudo apt install python3-venv
    python3 -m venv ~/src/venv/openai-detector
    source ~/src/venv/openai-detector/bin/activate
    
    Run Code Online (Sandbox Code Playgroud)
  13. 克隆检测器并下载模型文件:

    cd ~/src
    git clone https://github.com/openai/gpt-2-output-dataset.git
    mkdir gpt-2-models
    cd gpt-2-models
    wget https://openaipublic.azureedge.net/gpt-2/detector-models/v1/detector-base.pt
    # and/or
    wget https://openaipublic.azureedge.net/gpt-2/detector-models/v1/detector-large.pt
    
    Run Code Online (Sandbox Code Playgroud)
  14. 修改使用 Transformers 2.5.1 的要求:

    editor ~/src/gpt-2-output-dataset/requirements.txt
    
    Run Code Online (Sandbox Code Playgroud)

    将行更改transformers为:

    transformers==2.5.1
    
    Run Code Online (Sandbox Code Playgroud)
  15. 安装要求:

    pip install wheel
    cd ~/src/gpt-2-output-dataset
    pip install -r requirements.txt
    
    Run Code Online (Sandbox Code Playgroud)
  16. 跑步:

    python -m detector.server ../gpt-2-models/detector-base.pt
    
    Run Code Online (Sandbox Code Playgroud)

初始安装后,将来启动时需要执行以下操作:

wsl ~ -d openai-detector
cd ~/src/gpt-2-output-dataset
source ~/src/venv/openai-detector/bin/activate
python -m detector.server ../gpt-2-models/detector-base.pt
Run Code Online (Sandbox Code Playgroud)

OpenAI Detector 的本地副本应在localhost:8080.


Not*_*1ds 4

还有一个解决方案,以防您由于某种原因无法使用我的基于 Docker 的答案。我相信这应该取代我原来的解决方案作为首选方法。

使用HuggingFace 空间的Dockerfile作为指导,我已经能够在新的 Ubuntu 22.04 上重现这一点。我正在 WSL 上运行它,但感谢 @cocomac 确认这也适用于普通 Debian。它应该可以在任何最新的基于 Debian 的发行版上运行:

sudo apt update && sudo apt upgrade -y

sudo apt install -y \
    git \
    make build-essential libssl-dev zlib1g-dev \
    libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
    libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev git-lfs  \
    ffmpeg libsm6 libxext6 cmake libgl1-mesa-glx

git lfs install
cd ~
git clone https://huggingface.co/spaces/openai/openai-detector
cd openai-detector

PATH=/home/user/.local/bin:$PATH
curl https://pyenv.run | bash
source ~/.bashrc
PATH=$HOME/.pyenv/shims:$HOME/.pyenv/bin:$PATH

pyenv install 3.7.5
# ^ Takes a while

pyenv global 3.7.5
pyenv rehash

python --version
# Confirm 3.7.5

pip install --no-cache-dir --upgrade pip setuptools wheel
pip install --no-cache-dir \
    datasets \
    huggingface-hub "protobuf<4" "click<8.1"

pip install --no-cache-dir -r requirements.txt
# Ignore dependency errors, as it still appears to work

python -m detector.server detector-base.pt --port 7860
Run Code Online (Sandbox Code Playgroud)

对于将来的启动,请将以下内容添加到webui.sh脚本之类的内容中:

PATH=$HOME/.pyenv/shims:$HOME/.pyenv/bin:$PATH
pyenv global 3.7.5
pyenv rehash
cd ~/openai-detector
python -m detector.server detector-base.pt --port 7860
Run Code Online (Sandbox Code Playgroud)