From 6e23253013c1f2049255d3fa3dea477f1b407533 Mon Sep 17 00:00:00 2001 From: imClumsyPanda Date: Sat, 13 May 2023 09:50:30 +0800 Subject: [PATCH] update pdf_loader.py and image_loader.py --- .gitignore | 1 + .../README.md} | 41 +++++++++++++++--- {img => content/samples}/test.jpg | Bin {docs => content/samples}/test.pdf | Bin content/{ => samples}/test.txt | 0 loader/image_loader.py | 15 +++++-- loader/pdf_loader.py | 16 +++++-- test_image.py | 12 ----- test_pdf.py | 12 ----- 9 files changed, 60 insertions(+), 37 deletions(-) rename content/{langchain-ChatGLM_README.md => samples/README.md} (85%) rename {img => content/samples}/test.jpg (100%) rename {docs => content/samples}/test.pdf (100%) rename content/{ => samples}/test.txt (100%) delete mode 100644 test_image.py delete mode 100644 test_pdf.py diff --git a/.gitignore b/.gitignore index 0f0791c..027639e 100644 --- a/.gitignore +++ b/.gitignore @@ -172,3 +172,4 @@ llm/* embedding/* pyrightconfig.json +loader/tmp_files \ No newline at end of file diff --git a/content/langchain-ChatGLM_README.md b/content/samples/README.md similarity index 85% rename from content/langchain-ChatGLM_README.md rename to content/samples/README.md index cbf40e6..9a7afb4 100644 --- a/content/langchain-ChatGLM_README.md +++ b/content/samples/README.md @@ -31,6 +31,10 @@ ## 硬件需求 - ChatGLM-6B 模型硬件需求 + + 注:如未将模型下载至本地,请执行前检查`$HOME/.cache/huggingface/`文件夹剩余空间,模型文件下载至本地需要 15 GB 存储空间。 + + 模型下载方法可参考 [常见问题](docs/FAQ.md) 中 Q8。 | **量化等级** | **最低 GPU 显存**(推理) | **最低 GPU 显存**(高效参数微调) | | -------------- | ------------------------- | --------------------------------- | @@ -38,6 +42,17 @@ | INT8 | 8 GB | 9 GB | | INT4 | 6 GB | 7 GB | +- MOSS 模型硬件需求 + + 注:如未将模型下载至本地,请执行前检查`$HOME/.cache/huggingface/`文件夹剩余空间,模型文件下载至本地需要 70 GB 存储空间 + + 模型下载方法可参考 [常见问题](docs/FAQ.md) 中 Q8。 + + | **量化等级** | **最低 GPU 显存**(推理) | **最低 GPU 显存**(高效参数微调) | + |-------------------|-----------------------| --------------------------------- | + | FP16(无量化) | 68 GB | - | + | INT8 | 20 GB | - | + - Embedding 模型硬件需求 本项目中默认选用的 Embedding 模型 [GanymedeNil/text2vec-large-chinese](https://huggingface.co/GanymedeNil/text2vec-large-chinese/tree/main) 约占用显存 3GB,也可修改为在 CPU 中运行。 @@ -66,6 +81,7 @@ docker run --gpus all -d --name chatglm -p 7860:7860 -v ~/github/langchain-ChatG 本项目已在 Python 3.8 - 3.10,CUDA 11.7 环境下完成测试。已在 Windows、ARM 架构的 macOS、Linux 系统中完成测试。 +vue前端需要node18环境 ### 从本地加载模型 请参考 [THUDM/ChatGLM-6B#从本地加载模型](https://github.com/THUDM/ChatGLM-6B#从本地加载模型) @@ -97,19 +113,31 @@ $ python webui.py ```shell $ python api.py ``` +或成功部署 API 后,执行以下脚本体验基于 VUE 的前端页面 +```shell +$ cd views +$ pnpm i -注:如未将模型下载至本地,请执行前检查`$HOME/.cache/huggingface/`文件夹剩余空间,至少15G。 +$ npm run dev +``` 执行后效果如下图所示: -![webui](img/webui_0419.png) +1. `对话` Tab 界面 +![](img/webui_0510_0.png) +2. `知识库测试 Beta` Tab 界面 +![](img/webui_0510_1.png) +3. `模型配置` Tab 界面 +![](img/webui_0510_2.png) + Web UI 可以实现如下功能: -1. 运行前自动读取`configs/model_config.py`中`LLM`及`Embedding`模型枚举及默认模型设置运行模型,如需重新加载模型,可在 `模型配置` 标签页重新选择后点击 `重新加载模型` 进行模型加载; +1. 运行前自动读取`configs/model_config.py`中`LLM`及`Embedding`模型枚举及默认模型设置运行模型,如需重新加载模型,可在 `模型配置` Tab 重新选择后点击 `重新加载模型` 进行模型加载; 2. 可手动调节保留对话历史长度、匹配知识库文段数量,可根据显存大小自行调节; -3. 具备模式选择功能,可选择 `LLM对话` 与 `知识库问答` 模式进行对话,支持流式对话; +3. `对话` Tab 具备模式选择功能,可选择 `LLM对话` 与 `知识库问答` 模式进行对话,支持流式对话; 4. 添加 `配置知识库` 功能,支持选择已有知识库或新建知识库,并可向知识库中**新增**上传文件/文件夹,使用文件上传组件选择好文件后点击 `上传文件并加载知识库`,会将所选上传文档数据加载至知识库中,并基于更新后知识库进行问答; -5. 后续版本中将会增加对知识库的修改或删除,及知识库中已导入文件的查看。 +5. 新增 `知识库测试 Beta` Tab,可用于测试不同文本切分方法与检索相关度阈值设置,暂不支持将测试参数作为 `对话` Tab 设置参数。 +6. 后续版本中将会增加对知识库的修改或删除,及知识库中已导入文件的查看。 ### 常见问题 @@ -159,6 +187,7 @@ Web UI 可以实现如下功能: - [x] [THUDM/chatglm-6b-int4](https://huggingface.co/THUDM/chatglm-6b-int4) - [x] [THUDM/chatglm-6b-int4-qe](https://huggingface.co/THUDM/chatglm-6b-int4-qe) - [x] [ClueAI/ChatYuan-large-v2](https://huggingface.co/ClueAI/ChatYuan-large-v2) + - [x] [fnlp/moss-moon-003-sft](https://huggingface.co/fnlp/moss-moon-003-sft) - [ ] 增加更多 Embedding 模型支持 - [x] [nghuyong/ernie-3.0-nano-zh](https://huggingface.co/nghuyong/ernie-3.0-nano-zh) - [x] [nghuyong/ernie-3.0-base-zh](https://huggingface.co/nghuyong/ernie-3.0-base-zh) @@ -178,6 +207,6 @@ Web UI 可以实现如下功能: - [ ] 实现调用 API 的 Web UI Demo ## 项目交流群 -![二维码](img/qr_code_14.jpg) +![二维码](img/qr_code_17.jpg) 🎉 langchain-ChatGLM 项目交流群,如果你也对本项目感兴趣,欢迎加入群聊参与讨论交流。 diff --git a/img/test.jpg b/content/samples/test.jpg similarity index 100% rename from img/test.jpg rename to content/samples/test.jpg diff --git a/docs/test.pdf b/content/samples/test.pdf similarity index 100% rename from docs/test.pdf rename to content/samples/test.pdf diff --git a/content/test.txt b/content/samples/test.txt similarity index 100% rename from content/test.txt rename to content/samples/test.txt diff --git a/loader/image_loader.py b/loader/image_loader.py index 5a1552a..9c215d1 100644 --- a/loader/image_loader.py +++ b/loader/image_loader.py @@ -11,14 +11,15 @@ class UnstructuredPaddleImageLoader(UnstructuredFileLoader): def _get_elements(self) -> List: def image_ocr_txt(filepath, dir_path="tmp_files"): - if not os.path.exists(dir_path): - os.makedirs(dir_path) + full_dir_path = os.path.join(filepath, dir_path) + if not os.path.exists(full_dir_path): + os.makedirs(full_dir_path) filename = os.path.split(filepath)[-1] ocr = PaddleOCR(lang="ch", use_gpu=False, show_log=False) result = ocr.ocr(img=filepath) ocr_result = [i[1][0] for line in result for i in line] - txt_file_path = os.path.join(dir_path, "%s.txt" % (filename)) + txt_file_path = os.path.join(full_dir_path, "%s.txt" % (filename)) with open(txt_file_path, 'w', encoding='utf-8') as fout: fout.write("\n".join(ocr_result)) return txt_file_path @@ -26,3 +27,11 @@ class UnstructuredPaddleImageLoader(UnstructuredFileLoader): txt_file_path = image_ocr_txt(self.file_path) from unstructured.partition.text import partition_text return partition_text(filename=txt_file_path, **self.unstructured_kwargs) + + +if __name__ == "__main__": + filepath = "../content/samples/test.jpg" + loader = UnstructuredPaddleImageLoader(filepath, mode="elements") + docs = loader.load() + for doc in docs: + print(doc) diff --git a/loader/pdf_loader.py b/loader/pdf_loader.py index cc623d9..03666a7 100644 --- a/loader/pdf_loader.py +++ b/loader/pdf_loader.py @@ -12,13 +12,14 @@ class UnstructuredPaddlePDFLoader(UnstructuredFileLoader): def _get_elements(self) -> List: def pdf_ocr_txt(filepath, dir_path="tmp_files"): - if not os.path.exists(dir_path): - os.makedirs(dir_path) + full_dir_path = os.path.join(filepath, dir_path) + if not os.path.exists(full_dir_path): + os.makedirs(full_dir_path) filename = os.path.split(filepath)[-1] ocr = PaddleOCR(lang="ch", use_gpu=False, show_log=False) doc = fitz.open(filepath) - txt_file_path = os.path.join(dir_path, "%s.txt" % (filename)) - img_name = './img/.tmp.png' + txt_file_path = os.path.join(full_dir_path, "%s.txt" % (filename)) + img_name = os.path.join(full_dir_path, 'tmp.png') with open(txt_file_path, 'w', encoding='utf-8') as fout: for i in range(doc.page_count): @@ -42,3 +43,10 @@ class UnstructuredPaddlePDFLoader(UnstructuredFileLoader): txt_file_path = pdf_ocr_txt(self.file_path) from unstructured.partition.text import partition_text return partition_text(filename=txt_file_path, **self.unstructured_kwargs) + +if __name__ == "__main__": + filepath = "../content/samples/test.pdf" + loader = UnstructuredPaddlePDFLoader(filepath, mode="elements") + docs = loader.load() + for doc in docs: + print(doc) \ No newline at end of file diff --git a/test_image.py b/test_image.py deleted file mode 100644 index ed60890..0000000 --- a/test_image.py +++ /dev/null @@ -1,12 +0,0 @@ -from configs.model_config import * -import nltk - -nltk.data.path = [NLTK_DATA_PATH] + nltk.data.path - -filepath = "./img/test.jpg" -from loader import UnstructuredPaddleImageLoader - -loader = UnstructuredPaddleImageLoader(filepath, mode="elements") -docs = loader.load() -for doc in docs: - print(doc) diff --git a/test_pdf.py b/test_pdf.py deleted file mode 100644 index 32dcb34..0000000 --- a/test_pdf.py +++ /dev/null @@ -1,12 +0,0 @@ -from configs.model_config import * -import nltk - -nltk.data.path = [NLTK_DATA_PATH] + nltk.data.path - -filepath = "docs/test.pdf" -from loader import UnstructuredPaddlePDFLoader - -loader = UnstructuredPaddlePDFLoader(filepath, mode="elements") -docs = loader.load() -for doc in docs: - print(doc)