git clone https://github.com/yangjianxin1/GPT2-chitchat.git
把項目拉下來
-
下載模型文件: 從[模型分享]中下載模型文件,例如
model_epoch40_50w
。 -
將模型文件放到正確的位置: 將下載的模型文件夾
model_epoch40_50w
放到項目的model
目錄下。 -
安裝依賴: 進入項目目錄,確保你的環境滿足
requirements.txt
中指定的依賴。你可以運行以下命令:pip install -r requirements.txt
-
運行交互式對話: 執行
interact.py
文件,指定模型路徑。如果你想使用 GPU 進行生成,可以添加--device gpu_id
參數,其中gpu_id
是你要使用的 GPU 的編號。python interact.py --model_path model/model_epoch40_50w --device 0
如果你想在 CPU 上運行,可以添加
--no_cuda
參數:python interact.py --model_path model/model_epoch40_50w --no_cuda
-
與模型進行對話: 運行上述命令后,你可以與訓練好的模型進行對話。在對話中輸入 Ctrl+Z 可以結束對話,并聊天記錄將保存到
sample/sample.txt
文件中。
確保你的環境中已經安裝了 Python 3.6、transformers==4.2.0
和 pytorch==1.7.0
。
安裝實例
# 安裝 transformers 4.2.0
pip install transformers==4.2.0# 安裝 PyTorch 1.7.0(CPU版本)
pip install torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
如果使用 GPU,可以使用以下命令:
# 安裝 PyTorch 1.7.0(GPU版本,適用于 CUDA 11.0)
pip install torch==1.7.0+cu110 torchvision==0.8.1+cu110 torchaudio==0.7.0+cu110 -f https://download.pytorch.org/whl/torch_stable.html