Repository: soulteary/docker-llama2-chat Branch: main Commit: 4bc43122cfe4 Files: 28 Total size: 62.6 KB Directory structure: gitextract_5db6cfvo/ ├── .gitignore ├── LICENSE ├── README.md ├── README_EN.md ├── docker/ │ ├── Dockerfile.13b │ ├── Dockerfile.7b │ ├── Dockerfile.7b-cn │ ├── Dockerfile.7b-cn-4bit │ └── Dockerfile.base ├── llama.cpp/ │ ├── Dockerfile.converter │ └── Dockerfile.runtime ├── llama2-13b/ │ ├── app.py │ └── model.py ├── llama2-7b/ │ ├── app.py │ └── model.py ├── llama2-7b-cn/ │ ├── app.py │ └── model.py ├── llama2-7b-cn-4bit/ │ ├── app.py │ ├── model.py │ └── quantization_4bit.py └── scripts/ ├── make-13b.sh ├── make-7b-cn-4bit.sh ├── make-7b-cn.sh ├── make-7b.sh ├── run-13b.sh ├── run-7b-cn-4bit.sh ├── run-7b-cn.sh └── run-7b.sh ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ .DS_Store ================================================ FILE: LICENSE ================================================ Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ================================================ FILE: README.md ================================================ # Docker LLaMA2 Chat / 羊驼二代

中文文档 | ENGLISH

[![](https://img.shields.io/badge/LLaMA2-Official_7B_/_13B-blue)](https://huggingface.co/meta-llama) [![](https://img.shields.io/badge/LLaMA2-Chinese_7B-blue)](https://huggingface.co/soulteary/Chinese-Llama-2-7b-4bit) [![](https://img.shields.io/badge/LLaMA2-Chinese_GGMLQ4-blue)](https://huggingface.co/soulteary/Chinese-Llama-2-7b-ggml-q4) [![](https://img.shields.io/badge/License-Apache_v2-blue)](https://github.com/soulteary/docker-llama2-chat/blob/main/LICENSE) 三步上手 LLaMA2,一起玩!相关博客教程已更新,**同样欢迎“一键三连”** 🌟🌟🌟。 > 使用 Docker 快速上手,本地部署 7B 或 13B 官方模型,或者 7B 中文模型。 ### 博客教程 | 类型 | 显存需求 | 特点 | 教程地址 | 教程时间 | | --- | --- | --- | --- | --- | | 官方版(英文) | 8~14GB | 原汁原味 | [使用 Docker 快速上手官方版 LLaMA2 开源大模型](https://soulteary.com/2023/07/21/use-docker-to-quickly-get-started-with-the-official-version-of-llama2-open-source-large-model.html) | 2023.07.21 | | LinkSoul 中文版(双语)| 8~14GB | 支持中文 | [使用 Docker 快速上手中文版 LLaMA2 开源大模型](https://soulteary.com/2023/07/21/use-docker-to-quickly-get-started-with-the-chinese-version-of-llama2-open-source-large-model.html) | 2023.07.21 | | Transformers 量化(中文/官方) | 5GB | 加速推理、节约显存 | [使用 Transformers 量化 Meta AI LLaMA2 中文版大模型](https://soulteary.com/2023/07/22/quantizing-meta-ai-llama2-chinese-version-large-models-using-transformers.html) | 2023.07.22 | | GGML (Llama.cpp) 量化 (中文/官方)| 可以不需要显存 | CPU 推理 | [构建能够使用 CPU 运行的 MetaAI LLaMA2 中文大模型](https://soulteary.com/2023/07/23/build-llama2-chinese-large-model-that-can-run-on-cpu.html) | 2023.07.23 | 你可以参考项目代码,举一反三,把模型跑起来,接入到你想玩的地方,包括并不局限于支持 LLaMA 1代的各种开源软件中。 ## 预览图 ![](.github/preview.png) ![](.github/llama2-cn-4bit.jpg) ![](.github/clip.gif) ## 使用方法 1. 一条命令,从项目中构建官方版(7B或13B)模型镜像,或中文版镜像(7B或INT4量化版): ```bash # 7B bash scripts/make-7b.sh # 或 13B bash scripts/make-13b.sh # 或 7B Chinese bash scripts/make-7b-cn.sh # 或 7B Chinese 4bit bash scripts/make-7b-cn-4bit.sh ``` 2. 选择适合你的命令,从 HuggingFace 下载 LLaMA2 或中文模型: ```bash # MetaAI LLaMA2 Models (10~14GB vRAM) git clone https://huggingface.co/meta-llama/Llama-2-7b-chat-hf git clone https://huggingface.co/meta-llama/Llama-2-13b-chat-hf mkdir meta-llama mv Llama-2-7b-chat-hf meta-llama/ mv Llama-2-13b-chat-hf meta-llama/ # 或 Chinese LLaMA2 (10~14GB vRAM) git clone https://huggingface.co/LinkSoul/Chinese-Llama-2-7b mkdir LinkSoul mv Chinese-Llama-2-7b LinkSoul/ # 或 Chinese LLaMA2 4BIT (5GB vRAM) git clone https://huggingface.co/soulteary/Chinese-Llama-2-7b-4bit mkdir soulteary mv Chinese-Llama-2-7b-4bit soulteary/ ``` 将下载好的模型,保持在一个正确的目录结构中。 ```bash tree -L 2 meta-llama soulteary └── ... LinkSoul └── ... meta-llama ├── Llama-2-13b-chat-hf │   ├── added_tokens.json │   ├── config.json │   ├── generation_config.json │   ├── LICENSE.txt │   ├── model-00001-of-00003.safetensors │   ├── model-00002-of-00003.safetensors │   ├── model-00003-of-00003.safetensors │   ├── model.safetensors.index.json │   ├── pytorch_model-00001-of-00003.bin │   ├── pytorch_model-00002-of-00003.bin │   ├── pytorch_model-00003-of-00003.bin │   ├── pytorch_model.bin.index.json │   ├── README.md │   ├── Responsible-Use-Guide.pdf │   ├── special_tokens_map.json │   ├── tokenizer_config.json │   ├── tokenizer.model │   └── USE_POLICY.md └── Llama-2-7b-chat-hf ├── added_tokens.json ├── config.json ├── generation_config.json ├── LICENSE.txt ├── model-00001-of-00002.safetensors ├── model-00002-of-00002.safetensors ├── model.safetensors.index.json ├── models--meta-llama--Llama-2-7b-chat-hf ├── pytorch_model-00001-of-00003.bin ├── pytorch_model-00002-of-00003.bin ├── pytorch_model-00003-of-00003.bin ├── pytorch_model.bin.index.json ├── README.md ├── special_tokens_map.json ├── tokenizer_config.json ├── tokenizer.json ├── tokenizer.model └── USE_POLICY.md ``` 3. 选择使用下面的适合你的命令,一键运行 LLaMA2 模型应用: ```bash # 7B bash scripts/run-7b.sh # 或 13B bash scripts/run-13b.sh # 或 Chinese 7B bash scripts/run-7b-cn.sh # 或 Chinese 7B 4BIT bash scripts/run-7b-cn-4bit.sh ``` 模型运行之后,在浏览器中访问 `http://localhost7860` 或者 `http://你的IP地址:7860` 就可以开始玩了。 ## 相关项目 - MetaAI LLaMA2: https://ai.meta.com/llama/ ❤️ - Meta LLaMA2 7B Chat: https://huggingface.co/meta-llama/Llama-2-7b-chat - Meta LLaMA2 13B Chat: https://huggingface.co/meta-llama/Llama-2-13b-chat - Chinese LLaMA2 7B: https://huggingface.co/LinkSoul/Chinese-Llama-2-7b ❤️ - Chinese LLaMA2 7B GGML q4: https://huggingface.co/soulteary/Chinese-Llama-2-7b-ggml-q4 - LLaMA2 GGML Converter: https://hub.docker.com/r/soulteary/llama2 ================================================ FILE: README_EN.md ================================================ # Docker LLaMA2 Chat / 羊驼二代

ENGLISH | 中文文档

[![](https://img.shields.io/badge/LLaMA2-Official_7B_/_13B-blue)](https://huggingface.co/meta-llama) [![](https://img.shields.io/badge/LLaMA2-Chinese_7B-blue)](https://huggingface.co/soulteary/Chinese-Llama-2-7b-4bit) [![](https://img.shields.io/badge/LLaMA2-Chinese_GGMLQ4-blue)](https://huggingface.co/soulteary/Chinese-Llama-2-7b-ggml-q4) [![](https://img.shields.io/badge/License-Apache_v2-blue)](https://github.com/soulteary/docker-llama2-chat/blob/main/LICENSE) Play! Together! **ONLY 3 STEPS!** Get started quickly, locally using the 7B or 13B models, using Docker. - Meta Llama2, tested by 4090, and costs 8~14GB vRAM. - Chinese Llama2 quantified, tested by 4090, and costs 5GB vRAM. - Use GGML(LLaMA.cpp), just use CPU play it. ## Preview ![](.github/preview.png) ![](.github/llama2-cn-4bit.jpg) ![](.github/clip.gif) ## Blogs - [Use Docker to quickly get started with the official version of Llama2 Open-source Large Model](https://soulteary.com/2023/07/21/use-docker-to-quickly-get-started-with-the-official-version-of-llama2-open-source-large-model.html) - [Use Docker to quickly get started with the chinese version of Llama2 Open-source Large Model](https://soulteary.com/2023/07/21/use-docker-to-quickly-get-started-with-the-chinese-version-of-llama2-open-source-large-model.html) - [Quantizing MetaAI Llama2 chinese version large models using Transformers](https://soulteary.com/2023/07/22/quantizing-meta-ai-llama2-chinese-version-large-models-using-transformers.html) - [Build Llama2 chinese large model that can run on CPU](https://soulteary.com/2023/07/23/build-llama2-chinese-large-model-that-can-run-on-cpu.html) ## Usage 1. Build LLaMA2 Docker image for 7B / 13B (official), 7B or 7B INT4 (chinese): ```bash # 7B bash scripts/make-7b.sh # OR 13B bash scripts/make-13b.sh # OR 7B Chinese bash scripts/make-7b-cn.sh # OR 7B Chinese 4bit bash scripts/make-7b-cn-4bit.sh ``` 2. Download LLaMA2 Models from HuggingFace, or chinese models. ```bash # MetaAI LLaMA2 Models (10~14GB vRAM) git clone https://huggingface.co/meta-llama/Llama-2-7b-chat-hf git clone https://huggingface.co/meta-llama/Llama-2-13b-chat-hf mkdir meta-llama mv Llama-2-7b-chat-hf meta-llama/ mv Llama-2-13b-chat-hf meta-llama/ # OR Chinese LLaMA2 (10~14GB vRAM) git clone https://huggingface.co/LinkSoul/Chinese-Llama-2-7b mkdir LinkSoul mv Chinese-Llama-2-7b LinkSoul/ # OR Chinese LLaMA2 4BIT (5GB vRAM) git clone https://huggingface.co/soulteary/Chinese-Llama-2-7b-4bit mkdir soulteary mv Chinese-Llama-2-7b-4bit soulteary/ ``` keep the correct directory structure. ```bash tree -L 2 meta-llama soulteary └── ... LinkSoul └── ... meta-llama ├── Llama-2-13b-chat-hf │   ├── added_tokens.json │   ├── config.json │   ├── generation_config.json │   ├── LICENSE.txt │   ├── model-00001-of-00003.safetensors │   ├── model-00002-of-00003.safetensors │   ├── model-00003-of-00003.safetensors │   ├── model.safetensors.index.json │   ├── pytorch_model-00001-of-00003.bin │   ├── pytorch_model-00002-of-00003.bin │   ├── pytorch_model-00003-of-00003.bin │   ├── pytorch_model.bin.index.json │   ├── README.md │   ├── Responsible-Use-Guide.pdf │   ├── special_tokens_map.json │   ├── tokenizer_config.json │   ├── tokenizer.model │   └── USE_POLICY.md └── Llama-2-7b-chat-hf ├── added_tokens.json ├── config.json ├── generation_config.json ├── LICENSE.txt ├── model-00001-of-00002.safetensors ├── model-00002-of-00002.safetensors ├── model.safetensors.index.json ├── models--meta-llama--Llama-2-7b-chat-hf ├── pytorch_model-00001-of-00003.bin ├── pytorch_model-00002-of-00003.bin ├── pytorch_model-00003-of-00003.bin ├── pytorch_model.bin.index.json ├── README.md ├── special_tokens_map.json ├── tokenizer_config.json ├── tokenizer.json ├── tokenizer.model └── USE_POLICY.md ``` 3. Run Llama2 model in docker command: ```bash # 7B bash scripts/run-7b.sh # OR 13B bash scripts/run-13b.sh # OR Chinese 7B bash scripts/run-7b-cn.sh # OR Chinese 7B 4BIT bash scripts/run-7b-cn-4bit.sh ``` enjoy, open `http://localhost7860` or `http://ip:7860` and play with the LLaMA2! ## Credit - MetaAI LLaMA2: https://ai.meta.com/llama/ ❤️ - Meta LLaMA2 7B Chat: https://huggingface.co/meta-llama/Llama-2-7b-chat - Meta LLaMA2 13B Chat: https://huggingface.co/meta-llama/Llama-2-13b-chat - Chinese LLaMA2 7B: https://huggingface.co/LinkSoul/Chinese-Llama-2-7b ❤️ - Chinese LLaMA2 7B GGML q4: https://huggingface.co/soulteary/Chinese-Llama-2-7b-ggml-q4 - LLaMA2 GGML Converter: https://hub.docker.com/r/soulteary/llama2 ================================================ FILE: docker/Dockerfile.13b ================================================ FROM soulteary/llama2:base COPY llama2-13b/* ./ CMD ["python", "app.py"] ================================================ FILE: docker/Dockerfile.7b ================================================ FROM soulteary/llama2:base COPY llama2-7b/* ./ CMD ["python", "app.py"] ================================================ FILE: docker/Dockerfile.7b-cn ================================================ FROM soulteary/llama2:base COPY llama2-7b-cn/* ./ CMD ["python", "app.py"] ================================================ FILE: docker/Dockerfile.7b-cn-4bit ================================================ FROM soulteary/llama2:base COPY llama2-7b-cn-4bit/* ./ CMD ["python", "app.py"] ================================================ FILE: docker/Dockerfile.base ================================================ FROM nvcr.io/nvidia/pytorch:23.06-py3 RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple && \ pip install accelerate==0.21.0 bitsandbytes==0.40.2 gradio==3.37.0 protobuf==3.20.3 scipy==1.11.1 sentencepiece==0.1.99 transformers==4.31.0 WORKDIR /app ================================================ FILE: llama.cpp/Dockerfile.converter ================================================ FROM alpine:3.18 as code RUN apk add --no-cache wget WORKDIR /app ARG CODE_BASE=eb542d3 ENV ENV_CODE_BASE=${CODE_BASE} RUN wget https://github.com/ggerganov/llama.cpp/archive/refs/tags/master-${ENV_CODE_BASE}.tar.gz && \ tar zxvf master-${ENV_CODE_BASE}.tar.gz && \ rm -rf master-${ENV_CODE_BASE}.tar.gz RUN mv llama.cpp-master-${ENV_CODE_BASE} llama.cpp FROM python:3.11.4-slim-bullseye as base COPY --from=code /app/llama.cpp /app/llama.cpp WORKDIR /app/llama.cpp ENV DEBIAN_FRONTEND="noninteractive" RUN apt-get update && apt-get install -y --no-install-recommends build-essential && rm -rf /var/lib/apt/lists/* RUN make -j$(nproc) FROM python:3.11.4-slim-bullseye as runtime RUN pip3 install numpy==1.24 sentencepiece==0.1.98 COPY --from=base /app/llama.cpp/ /app/llama.cpp/ WORKDIR /app/llama.cpp/ ================================================ FILE: llama.cpp/Dockerfile.runtime ================================================ FROM alpine:3.18 as code RUN apk add --no-cache wget WORKDIR /app ARG CODE_BASE=d2a4366 ENV ENV_CODE_BASE=${CODE_BASE} RUN wget https://github.com/ggerganov/llama.cpp/archive/refs/tags/master-${ENV_CODE_BASE}.tar.gz && \ tar zxvf master-${ENV_CODE_BASE}.tar.gz && \ rm -rf master-${ENV_CODE_BASE}.tar.gz RUN mv llama.cpp-master-${ENV_CODE_BASE} llama.cpp FROM python:3.11.4-slim-bullseye as base COPY --from=code /app/llama.cpp /app/llama.cpp WORKDIR /app/llama.cpp ENV DEBIAN_FRONTEND="noninteractive" RUN apt-get update && apt-get install -y --no-install-recommends build-essential && rm -rf /var/lib/apt/lists/* RUN make -j$(nproc) FROM python:3.11.4-slim-bullseye as runtime COPY --from=base /app/llama.cpp/LICENSE /app/llama.cpp/LICENSE COPY --from=base /app/llama.cpp/main /app/llama.cpp/main COPY --from=base /app/llama.cpp/prompts /app/llama.cpp/prompts WORKDIR /app/llama.cpp/ ================================================ FILE: llama2-13b/app.py ================================================ from typing import Iterator import gradio as gr import torch from model import run DEFAULT_SYSTEM_PROMPT = """\ You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.\ """ MAX_MAX_NEW_TOKENS = 2048 DEFAULT_MAX_NEW_TOKENS = 1024 DESCRIPTION = """ # Llama-2 13B Chat This Space demonstrates model [Llama-2-13b-chat](https://huggingface.co/meta-llama/Llama-2-13b-chat) by Meta, a Llama 2 model with 13B parameters fine-tuned for chat instructions. Feel free to play with it, or duplicate to run generations without a queue! If you want to run your own service, you can also [deploy the model on Inference Endpoints](https://huggingface.co/inference-endpoints). 🔎 For more details about the Llama 2 family of models and how to use them with `transformers`, take a look [at our blog post](https://huggingface.co/blog/llama2). 🔨 Looking for an even more powerful model? Check out the large [**70B** model demo](https://huggingface.co/spaces/ysharma/Explore_llamav2_with_TGI). 🐇 For a smaller model that you can run on many GPUs, check our [7B model demo](https://huggingface.co/spaces/huggingface-projects/llama-2-7b-chat). """ LICENSE = """

--- As a derivate work of [Llama-2-13b-chat](https://huggingface.co/meta-llama/Llama-2-13b-chat) by Meta, this demo is governed by the original [license](https://huggingface.co/spaces/huggingface-projects/llama-2-13b-chat/blob/main/LICENSE.txt) and [acceptable use policy](https://huggingface.co/spaces/huggingface-projects/llama-2-13b-chat/blob/main/USE_POLICY.md). """ if not torch.cuda.is_available(): DESCRIPTION += '\n

Running on CPU 🥶 This demo does not work on CPU.

' def clear_and_save_textbox(message: str) -> tuple[str, str]: return '', message def display_input(message: str, history: list[tuple[str, str]]) -> list[tuple[str, str]]: history.append((message, '')) return history def delete_prev_fn( history: list[tuple[str, str]]) -> tuple[list[tuple[str, str]], str]: try: message, _ = history.pop() except IndexError: message = '' return history, message or '' def generate( message: str, history_with_input: list[tuple[str, str]], system_prompt: str, max_new_tokens: int, top_p: float, temperature: float, top_k: int, ) -> Iterator[list[tuple[str, str]]]: if max_new_tokens > MAX_MAX_NEW_TOKENS: raise ValueError history = history_with_input[:-1] generator = run(message, history, system_prompt, max_new_tokens, temperature, top_p, top_k) try: first_response = next(generator) yield history + [(message, first_response)] except StopIteration: yield history + [(message, '')] for response in generator: yield history + [(message, response)] def process_example(message: str) -> tuple[str, list[tuple[str, str]]]: generator = generate(message, [], DEFAULT_SYSTEM_PROMPT, 1024, 0.95, 1, 1000) for x in generator: pass return '', x with gr.Blocks(css='style.css') as demo: gr.Markdown(DESCRIPTION) gr.DuplicateButton(value='Duplicate Space for private use', elem_id='duplicate-button') with gr.Group(): chatbot = gr.Chatbot(label='Chatbot') with gr.Row(): textbox = gr.Textbox( container=False, show_label=False, placeholder='Type a message...', scale=10, ) submit_button = gr.Button('Submit', variant='primary', scale=1, min_width=0) with gr.Row(): retry_button = gr.Button('🔄 Retry', variant='secondary') undo_button = gr.Button('↩️ Undo', variant='secondary') clear_button = gr.Button('🗑️ Clear', variant='secondary') saved_input = gr.State() with gr.Accordion(label='Advanced options', open=False): system_prompt = gr.Textbox(label='System prompt', value=DEFAULT_SYSTEM_PROMPT, lines=6) max_new_tokens = gr.Slider( label='Max new tokens', minimum=1, maximum=MAX_MAX_NEW_TOKENS, step=1, value=DEFAULT_MAX_NEW_TOKENS, ) temperature = gr.Slider( label='Temperature', minimum=0.1, maximum=4.0, step=0.1, value=1.0, ) top_p = gr.Slider( label='Top-p (nucleus sampling)', minimum=0.05, maximum=1.0, step=0.05, value=0.95, ) top_k = gr.Slider( label='Top-k', minimum=1, maximum=1000, step=1, value=50, ) gr.Examples( examples=[ 'Hello there! How are you doing?', 'Can you explain briefly to me what is the Python programming language?', 'Explain the plot of Cinderella in a sentence.', 'How many hours does it take a man to eat a Helicopter?', "Write a 100-word article on 'Benefits of Open-Source in AI research'", ], inputs=textbox, outputs=[textbox, chatbot], fn=process_example, cache_examples=True, ) gr.Markdown(LICENSE) textbox.submit( fn=clear_and_save_textbox, inputs=textbox, outputs=[textbox, saved_input], api_name=False, queue=False, ).then( fn=display_input, inputs=[saved_input, chatbot], outputs=chatbot, api_name=False, queue=False, ).then( fn=generate, inputs=[ saved_input, chatbot, system_prompt, max_new_tokens, temperature, top_p, top_k, ], outputs=chatbot, api_name=False, ) button_event_preprocess = submit_button.click( fn=clear_and_save_textbox, inputs=textbox, outputs=[textbox, saved_input], api_name=False, queue=False, ).then( fn=display_input, inputs=[saved_input, chatbot], outputs=chatbot, api_name=False, queue=False, ).then( fn=generate, inputs=[ saved_input, chatbot, system_prompt, max_new_tokens, temperature, top_p, top_k, ], outputs=chatbot, api_name=False, ) retry_button.click( fn=delete_prev_fn, inputs=chatbot, outputs=[chatbot, saved_input], api_name=False, queue=False, ).then( fn=display_input, inputs=[saved_input, chatbot], outputs=chatbot, api_name=False, queue=False, ).then( fn=generate, inputs=[ saved_input, chatbot, max_new_tokens, temperature, top_p, top_k, ], outputs=chatbot, api_name=False, ) undo_button.click( fn=delete_prev_fn, inputs=chatbot, outputs=[chatbot, saved_input], api_name=False, queue=False, ).then( fn=lambda x: x, inputs=[saved_input], outputs=textbox, api_name=False, queue=False, ) clear_button.click( fn=lambda: ([], ''), outputs=[chatbot, saved_input], queue=False, api_name=False, ) demo.queue(max_size=20).launch(server_name="0.0.0.0") ================================================ FILE: llama2-13b/model.py ================================================ from threading import Thread from typing import Iterator import torch from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer model_id = 'meta-llama/Llama-2-13b-chat-hf' if torch.cuda.is_available(): config = AutoConfig.from_pretrained(model_id) config.pretraining_tp = 1 model = AutoModelForCausalLM.from_pretrained( model_id, local_files_only=True, config=config, torch_dtype=torch.float16, load_in_4bit=True, device_map='auto' ) else: model = None tokenizer = AutoTokenizer.from_pretrained(model_id) def get_prompt(message: str, chat_history: list[tuple[str, str]], system_prompt: str) -> str: texts = [f'[INST] <>\n{system_prompt}\n<>\n\n'] for user_input, response in chat_history: texts.append(f'{user_input} [/INST] {response} [INST] ') texts.append(f'{message.strip()} [/INST]') return ''.join(texts) def run(message: str, chat_history: list[tuple[str, str]], system_prompt: str, max_new_tokens: int = 1024, temperature: float = 0.8, top_p: float = 0.95, top_k: int = 50) -> Iterator[str]: prompt = get_prompt(message, chat_history, system_prompt) inputs = tokenizer([prompt], return_tensors='pt').to("cuda") streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True) generate_kwargs = dict( inputs, streamer=streamer, max_new_tokens=max_new_tokens, do_sample=True, top_p=top_p, top_k=top_k, temperature=temperature, num_beams=1, ) t = Thread(target=model.generate, kwargs=generate_kwargs) t.start() outputs = [] for text in streamer: outputs.append(text) yield ''.join(outputs) ================================================ FILE: llama2-7b/app.py ================================================ from typing import Iterator import gradio as gr import torch from model import run DEFAULT_SYSTEM_PROMPT = """\ You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature. If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.\ """ MAX_MAX_NEW_TOKENS = 2048 DEFAULT_MAX_NEW_TOKENS = 1024 DESCRIPTION = """ # Llama-2 7B Chat This Space demonstrates model [Llama-2-7b-chat](https://huggingface.co/meta-llama/Llama-2-7b-chat) by Meta, a Llama 2 model with 7B parameters fine-tuned for chat instructions. Feel free to play with it, or duplicate to run generations without a queue! If you want to run your own service, you can also [deploy the model on Inference Endpoints](https://huggingface.co/inference-endpoints). 🔎 For more details about the Llama 2 family of models and how to use them with `transformers`, take a look [at our blog post](https://huggingface.co/blog/llama2). 🔨 Looking for an even more powerful model? Check out the [13B version](https://huggingface.co/spaces/huggingface-projects/llama-2-13b-chat) or the large [70B model demo](https://huggingface.co/spaces/ysharma/Explore_llamav2_with_TGI). """ LICENSE = """

--- As a derivate work of [Llama-2-7b-chat](https://huggingface.co/meta-llama/Llama-2-7b-chat) by Meta, this demo is governed by the original [license](https://huggingface.co/spaces/huggingface-projects/llama-2-7b-chat/blob/main/LICENSE.txt) and [acceptable use policy](https://huggingface.co/spaces/huggingface-projects/llama-2-7b-chat/blob/main/USE_POLICY.md). """ if not torch.cuda.is_available(): DESCRIPTION += '\n

Running on CPU 🥶 This demo does not work on CPU.

' def clear_and_save_textbox(message: str) -> tuple[str, str]: return '', message def display_input(message: str, history: list[tuple[str, str]]) -> list[tuple[str, str]]: history.append((message, '')) return history def delete_prev_fn( history: list[tuple[str, str]]) -> tuple[list[tuple[str, str]], str]: try: message, _ = history.pop() except IndexError: message = '' return history, message or '' def generate( message: str, history_with_input: list[tuple[str, str]], system_prompt: str, max_new_tokens: int, top_p: float, temperature: float, top_k: int, ) -> Iterator[list[tuple[str, str]]]: if max_new_tokens > MAX_MAX_NEW_TOKENS: raise ValueError history = history_with_input[:-1] generator = run(message, history, system_prompt, max_new_tokens, temperature, top_p, top_k) try: first_response = next(generator) yield history + [(message, first_response)] except StopIteration: yield history + [(message, '')] for response in generator: yield history + [(message, response)] def process_example(message: str) -> tuple[str, list[tuple[str, str]]]: generator = generate(message, [], DEFAULT_SYSTEM_PROMPT, 1024, 0.95, 1, 1000) for x in generator: pass return '', x with gr.Blocks(css='style.css') as demo: gr.Markdown(DESCRIPTION) gr.DuplicateButton(value='Duplicate Space for private use', elem_id='duplicate-button') with gr.Group(): chatbot = gr.Chatbot(label='Chatbot') with gr.Row(): textbox = gr.Textbox( container=False, show_label=False, placeholder='Type a message...', scale=10, ) submit_button = gr.Button('Submit', variant='primary', scale=1, min_width=0) with gr.Row(): retry_button = gr.Button('🔄 Retry', variant='secondary') undo_button = gr.Button('↩️ Undo', variant='secondary') clear_button = gr.Button('🗑️ Clear', variant='secondary') saved_input = gr.State() with gr.Accordion(label='Advanced options', open=False): system_prompt = gr.Textbox(label='System prompt', value=DEFAULT_SYSTEM_PROMPT, lines=6) max_new_tokens = gr.Slider( label='Max new tokens', minimum=1, maximum=MAX_MAX_NEW_TOKENS, step=1, value=DEFAULT_MAX_NEW_TOKENS, ) temperature = gr.Slider( label='Temperature', minimum=0.1, maximum=4.0, step=0.1, value=1.0, ) top_p = gr.Slider( label='Top-p (nucleus sampling)', minimum=0.05, maximum=1.0, step=0.05, value=0.95, ) top_k = gr.Slider( label='Top-k', minimum=1, maximum=1000, step=1, value=50, ) gr.Examples( examples=[ 'Hello there! How are you doing?', 'Can you explain briefly to me what is the Python programming language?', 'Explain the plot of Cinderella in a sentence.', 'How many hours does it take a man to eat a Helicopter?', "Write a 100-word article on 'Benefits of Open-Source in AI research'", ], inputs=textbox, outputs=[textbox, chatbot], fn=process_example, cache_examples=True, ) gr.Markdown(LICENSE) textbox.submit( fn=clear_and_save_textbox, inputs=textbox, outputs=[textbox, saved_input], api_name=False, queue=False, ).then( fn=display_input, inputs=[saved_input, chatbot], outputs=chatbot, api_name=False, queue=False, ).then( fn=generate, inputs=[ saved_input, chatbot, system_prompt, max_new_tokens, temperature, top_p, top_k, ], outputs=chatbot, api_name=False, ) button_event_preprocess = submit_button.click( fn=clear_and_save_textbox, inputs=textbox, outputs=[textbox, saved_input], api_name=False, queue=False, ).then( fn=display_input, inputs=[saved_input, chatbot], outputs=chatbot, api_name=False, queue=False, ).then( fn=generate, inputs=[ saved_input, chatbot, system_prompt, max_new_tokens, temperature, top_p, top_k, ], outputs=chatbot, api_name=False, ) retry_button.click( fn=delete_prev_fn, inputs=chatbot, outputs=[chatbot, saved_input], api_name=False, queue=False, ).then( fn=display_input, inputs=[saved_input, chatbot], outputs=chatbot, api_name=False, queue=False, ).then( fn=generate, inputs=[ saved_input, chatbot, max_new_tokens, temperature, top_p, top_k, ], outputs=chatbot, api_name=False, ) undo_button.click( fn=delete_prev_fn, inputs=chatbot, outputs=[chatbot, saved_input], api_name=False, queue=False, ).then( fn=lambda x: x, inputs=[saved_input], outputs=textbox, api_name=False, queue=False, ) clear_button.click( fn=lambda: ([], ''), outputs=[chatbot, saved_input], queue=False, api_name=False, ) demo.queue(max_size=20).launch(server_name="0.0.0.0") ================================================ FILE: llama2-7b/model.py ================================================ from threading import Thread from typing import Iterator import torch from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer model_id = 'meta-llama/Llama-2-7b-chat-hf' if torch.cuda.is_available(): model = AutoModelForCausalLM.from_pretrained( model_id, local_files_only=True, torch_dtype=torch.float16, device_map='auto' ) else: model = None tokenizer = AutoTokenizer.from_pretrained(model_id) def get_prompt(message: str, chat_history: list[tuple[str, str]], system_prompt: str) -> str: texts = [f'[INST] <>\n{system_prompt}\n<>\n\n'] for user_input, response in chat_history: texts.append(f'{user_input.strip()} [/INST] {response.strip()} [INST] ') texts.append(f'{message.strip()} [/INST]') return ''.join(texts) def run(message: str, chat_history: list[tuple[str, str]], system_prompt: str, max_new_tokens: int = 1024, temperature: float = 0.8, top_p: float = 0.95, top_k: int = 50) -> Iterator[str]: prompt = get_prompt(message, chat_history, system_prompt) inputs = tokenizer([prompt], return_tensors='pt').to("cuda") streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True) generate_kwargs = dict( inputs, streamer=streamer, max_new_tokens=max_new_tokens, do_sample=True, top_p=top_p, top_k=top_k, temperature=temperature, num_beams=1, ) t = Thread(target=model.generate, kwargs=generate_kwargs) t.start() outputs = [] for text in streamer: outputs.append(text) yield ''.join(outputs) ================================================ FILE: llama2-7b-cn/app.py ================================================ from typing import Iterator import gradio as gr import torch from model import run DEFAULT_SYSTEM_PROMPT = """\ You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature. If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.\ """ MAX_MAX_NEW_TOKENS = 2048 DEFAULT_MAX_NEW_TOKENS = 1024 DESCRIPTION = """ # Llama-2 7B Chat (Chinese) 本空间是对于 [Chinese-Llama-2-7b](https://huggingface.co/LinkSoul/Chinese-Llama-2-7b) 模型的演示。该模型由 LinkSoul 开发,基于 Llama 2 基础模型和自行收集的大规模中英文指令数据集 [instruction_merge_set](https://huggingface.co/datasets/LinkSoul/instruction_merge_set) 训练得到。 """ LICENSE = """

--- As a derivate work of [Chinese-Llama-2-7b](https://huggingface.co/LinkSoul/Chinese-Llama-2-7b) by LinkSoul, [Llama-2-7b-chat](https://huggingface.co/meta-llama/Llama-2-7b-chat) by Meta, this demo is governed by the original [license](https://huggingface.co/spaces/huggingface-projects/llama-2-7b-chat/blob/main/LICENSE.txt) and [acceptable use policy](https://huggingface.co/spaces/huggingface-projects/llama-2-7b-chat/blob/main/USE_POLICY.md). """ if not torch.cuda.is_available(): DESCRIPTION += '\n

Running on CPU 🥶 This demo does not work on CPU.

' def clear_and_save_textbox(message: str) -> tuple[str, str]: return '', message def display_input(message: str, history: list[tuple[str, str]]) -> list[tuple[str, str]]: history.append((message, '')) return history def delete_prev_fn( history: list[tuple[str, str]]) -> tuple[list[tuple[str, str]], str]: try: message, _ = history.pop() except IndexError: message = '' return history, message or '' def generate( message: str, history_with_input: list[tuple[str, str]], system_prompt: str, max_new_tokens: int, top_p: float, temperature: float, top_k: int, ) -> Iterator[list[tuple[str, str]]]: if max_new_tokens > MAX_MAX_NEW_TOKENS: raise ValueError history = history_with_input[:-1] generator = run(message, history, system_prompt, max_new_tokens, temperature, top_p, top_k) try: first_response = next(generator) yield history + [(message, first_response)] except StopIteration: yield history + [(message, '')] for response in generator: yield history + [(message, response)] def process_example(message: str) -> tuple[str, list[tuple[str, str]]]: generator = generate(message, [], DEFAULT_SYSTEM_PROMPT, 1024, 0.95, 1, 1000) for x in generator: pass return '', x with gr.Blocks(css='style.css') as demo: gr.Markdown(DESCRIPTION) gr.DuplicateButton(value='Duplicate Space for private use', elem_id='duplicate-button') with gr.Group(): chatbot = gr.Chatbot(label='Chatbot') with gr.Row(): textbox = gr.Textbox( container=False, show_label=False, placeholder='Type a message...', scale=10, ) submit_button = gr.Button('Submit', variant='primary', scale=1, min_width=0) with gr.Row(): retry_button = gr.Button('🔄 Retry', variant='secondary') undo_button = gr.Button('↩️ Undo', variant='secondary') clear_button = gr.Button('🗑️ Clear', variant='secondary') saved_input = gr.State() with gr.Accordion(label='Advanced options', open=False): system_prompt = gr.Textbox(label='System prompt', value=DEFAULT_SYSTEM_PROMPT, lines=6) max_new_tokens = gr.Slider( label='Max new tokens', minimum=1, maximum=MAX_MAX_NEW_TOKENS, step=1, value=DEFAULT_MAX_NEW_TOKENS, ) temperature = gr.Slider( label='Temperature', minimum=0.1, maximum=4.0, step=0.1, value=1.0, ) top_p = gr.Slider( label='Top-p (nucleus sampling)', minimum=0.05, maximum=1.0, step=0.05, value=0.95, ) top_k = gr.Slider( label='Top-k', minimum=1, maximum=1000, step=1, value=50, ) gr.Examples( examples=[ 'Hello there! How are you doing?', 'Can you explain briefly to me what is the Python programming language?', 'Explain the plot of Cinderella in a sentence.', 'How many hours does it take a man to eat a Helicopter?', "Write a 100-word article on 'Benefits of Open-Source in AI research'", ], inputs=textbox, outputs=[textbox, chatbot], fn=process_example, cache_examples=True, ) gr.Markdown(LICENSE) textbox.submit( fn=clear_and_save_textbox, inputs=textbox, outputs=[textbox, saved_input], api_name=False, queue=False, ).then( fn=display_input, inputs=[saved_input, chatbot], outputs=chatbot, api_name=False, queue=False, ).then( fn=generate, inputs=[ saved_input, chatbot, system_prompt, max_new_tokens, temperature, top_p, top_k, ], outputs=chatbot, api_name=False, ) button_event_preprocess = submit_button.click( fn=clear_and_save_textbox, inputs=textbox, outputs=[textbox, saved_input], api_name=False, queue=False, ).then( fn=display_input, inputs=[saved_input, chatbot], outputs=chatbot, api_name=False, queue=False, ).then( fn=generate, inputs=[ saved_input, chatbot, system_prompt, max_new_tokens, temperature, top_p, top_k, ], outputs=chatbot, api_name=False, ) retry_button.click( fn=delete_prev_fn, inputs=chatbot, outputs=[chatbot, saved_input], api_name=False, queue=False, ).then( fn=display_input, inputs=[saved_input, chatbot], outputs=chatbot, api_name=False, queue=False, ).then( fn=generate, inputs=[ saved_input, chatbot, max_new_tokens, temperature, top_p, top_k, ], outputs=chatbot, api_name=False, ) undo_button.click( fn=delete_prev_fn, inputs=chatbot, outputs=[chatbot, saved_input], api_name=False, queue=False, ).then( fn=lambda x: x, inputs=[saved_input], outputs=textbox, api_name=False, queue=False, ) clear_button.click( fn=lambda: ([], ''), outputs=[chatbot, saved_input], queue=False, api_name=False, ) demo.queue(max_size=20).launch(server_name="0.0.0.0") ================================================ FILE: llama2-7b-cn/model.py ================================================ from threading import Thread from typing import Iterator import torch from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer model_id = 'LinkSoul/Chinese-Llama-2-7b' if torch.cuda.is_available(): model = AutoModelForCausalLM.from_pretrained( model_id, local_files_only=True, torch_dtype=torch.float16, device_map='auto' ) else: model = None tokenizer = AutoTokenizer.from_pretrained(model_id) def get_prompt(message: str, chat_history: list[tuple[str, str]], system_prompt: str) -> str: texts = [f'[INST] <>\n{system_prompt}\n<>\n\n'] for user_input, response in chat_history: texts.append(f'{user_input.strip()} [/INST] {response.strip()}
[INST] ') texts.append(f'{message.strip()} [/INST]') return ''.join(texts) def run(message: str, chat_history: list[tuple[str, str]], system_prompt: str, max_new_tokens: int = 1024, temperature: float = 0.8, top_p: float = 0.95, top_k: int = 50) -> Iterator[str]: prompt = get_prompt(message, chat_history, system_prompt) inputs = tokenizer([prompt], return_tensors='pt').to("cuda") streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True) generate_kwargs = dict( inputs, streamer=streamer, max_new_tokens=max_new_tokens, do_sample=True, top_p=top_p, top_k=top_k, temperature=temperature, num_beams=1, ) t = Thread(target=model.generate, kwargs=generate_kwargs) t.start() outputs = [] for text in streamer: outputs.append(text) yield ''.join(outputs) ================================================ FILE: llama2-7b-cn-4bit/app.py ================================================ from typing import Iterator import gradio as gr import torch from model import run DEFAULT_SYSTEM_PROMPT = """\ You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature. If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.\ """ MAX_MAX_NEW_TOKENS = 2048 DEFAULT_MAX_NEW_TOKENS = 1024 DESCRIPTION = """ # Llama-2 7B Chat (Chinese) 本空间是对于 [Chinese-Llama-2-7b-4bit](https://huggingface.co/soulteary/Chinese-Llama-2-7b-4bit) 模型的演示。该模型由 LinkSoul 开发,基于 Llama 2 基础模型和自行收集的大规模中英文指令数据集 [instruction_merge_set](https://huggingface.co/datasets/LinkSoul/instruction_merge_set) 训练得到。 """ LICENSE = """

--- As a derivate work of [Chinese-Llama-2-7b](https://huggingface.co/LinkSoul/Chinese-Llama-2-7b) by LinkSoul, [Llama-2-7b-chat](https://huggingface.co/meta-llama/Llama-2-7b-chat) by Meta, this demo is governed by the original [license](https://huggingface.co/spaces/huggingface-projects/llama-2-7b-chat/blob/main/LICENSE.txt) and [acceptable use policy](https://huggingface.co/spaces/huggingface-projects/llama-2-7b-chat/blob/main/USE_POLICY.md). """ if not torch.cuda.is_available(): DESCRIPTION += '\n

Running on CPU 🥶 This demo does not work on CPU.

' def clear_and_save_textbox(message: str) -> tuple[str, str]: return '', message def display_input(message: str, history: list[tuple[str, str]]) -> list[tuple[str, str]]: history.append((message, '')) return history def delete_prev_fn( history: list[tuple[str, str]]) -> tuple[list[tuple[str, str]], str]: try: message, _ = history.pop() except IndexError: message = '' return history, message or '' def generate( message: str, history_with_input: list[tuple[str, str]], system_prompt: str, max_new_tokens: int, top_p: float, temperature: float, top_k: int, ) -> Iterator[list[tuple[str, str]]]: if max_new_tokens > MAX_MAX_NEW_TOKENS: raise ValueError history = history_with_input[:-1] generator = run(message, history, system_prompt, max_new_tokens, temperature, top_p, top_k) try: first_response = next(generator) yield history + [(message, first_response)] except StopIteration: yield history + [(message, '')] for response in generator: yield history + [(message, response)] def process_example(message: str) -> tuple[str, list[tuple[str, str]]]: generator = generate(message, [], DEFAULT_SYSTEM_PROMPT, 1024, 0.95, 1, 1000) for x in generator: pass return '', x with gr.Blocks(css='style.css') as demo: gr.Markdown(DESCRIPTION) gr.DuplicateButton(value='Duplicate Space for private use', elem_id='duplicate-button') with gr.Group(): chatbot = gr.Chatbot(label='Chatbot') with gr.Row(): textbox = gr.Textbox( container=False, show_label=False, placeholder='Type a message...', scale=10, ) submit_button = gr.Button('Submit', variant='primary', scale=1, min_width=0) with gr.Row(): retry_button = gr.Button('🔄 Retry', variant='secondary') undo_button = gr.Button('↩️ Undo', variant='secondary') clear_button = gr.Button('🗑️ Clear', variant='secondary') saved_input = gr.State() with gr.Accordion(label='Advanced options', open=False): system_prompt = gr.Textbox(label='System prompt', value=DEFAULT_SYSTEM_PROMPT, lines=6) max_new_tokens = gr.Slider( label='Max new tokens', minimum=1, maximum=MAX_MAX_NEW_TOKENS, step=1, value=DEFAULT_MAX_NEW_TOKENS, ) temperature = gr.Slider( label='Temperature', minimum=0.1, maximum=4.0, step=0.1, value=1.0, ) top_p = gr.Slider( label='Top-p (nucleus sampling)', minimum=0.05, maximum=1.0, step=0.05, value=0.95, ) top_k = gr.Slider( label='Top-k', minimum=1, maximum=1000, step=1, value=50, ) gr.Examples( examples=[ 'Hello there! How are you doing?', 'Can you explain briefly to me what is the Python programming language?', 'Explain the plot of Cinderella in a sentence.', 'How many hours does it take a man to eat a Helicopter?', "Write a 100-word article on 'Benefits of Open-Source in AI research'", ], inputs=textbox, outputs=[textbox, chatbot], fn=process_example, cache_examples=True, ) gr.Markdown(LICENSE) textbox.submit( fn=clear_and_save_textbox, inputs=textbox, outputs=[textbox, saved_input], api_name=False, queue=False, ).then( fn=display_input, inputs=[saved_input, chatbot], outputs=chatbot, api_name=False, queue=False, ).then( fn=generate, inputs=[ saved_input, chatbot, system_prompt, max_new_tokens, temperature, top_p, top_k, ], outputs=chatbot, api_name=False, ) button_event_preprocess = submit_button.click( fn=clear_and_save_textbox, inputs=textbox, outputs=[textbox, saved_input], api_name=False, queue=False, ).then( fn=display_input, inputs=[saved_input, chatbot], outputs=chatbot, api_name=False, queue=False, ).then( fn=generate, inputs=[ saved_input, chatbot, system_prompt, max_new_tokens, temperature, top_p, top_k, ], outputs=chatbot, api_name=False, ) retry_button.click( fn=delete_prev_fn, inputs=chatbot, outputs=[chatbot, saved_input], api_name=False, queue=False, ).then( fn=display_input, inputs=[saved_input, chatbot], outputs=chatbot, api_name=False, queue=False, ).then( fn=generate, inputs=[ saved_input, chatbot, max_new_tokens, temperature, top_p, top_k, ], outputs=chatbot, api_name=False, ) undo_button.click( fn=delete_prev_fn, inputs=chatbot, outputs=[chatbot, saved_input], api_name=False, queue=False, ).then( fn=lambda x: x, inputs=[saved_input], outputs=textbox, api_name=False, queue=False, ) clear_button.click( fn=lambda: ([], ''), outputs=[chatbot, saved_input], queue=False, api_name=False, ) demo.queue(max_size=20).launch(server_name="0.0.0.0") ================================================ FILE: llama2-7b-cn-4bit/model.py ================================================ from threading import Thread from typing import Iterator import torch from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer model_id = 'soulteary/Chinese-Llama-2-7b-4bit' if torch.cuda.is_available(): model = AutoModelForCausalLM.from_pretrained( model_id, load_in_4bit=True, local_files_only=True, torch_dtype=torch.float16, device_map='auto' ) else: model = None tokenizer = AutoTokenizer.from_pretrained(model_id) def get_prompt(message: str, chat_history: list[tuple[str, str]], system_prompt: str) -> str: texts = [f'[INST] <>\n{system_prompt}\n<>\n\n'] for user_input, response in chat_history: texts.append(f'{user_input.strip()} [/INST] {response.strip()}
[INST] ') texts.append(f'{message.strip()} [/INST]') return ''.join(texts) def run(message: str, chat_history: list[tuple[str, str]], system_prompt: str, max_new_tokens: int = 1024, temperature: float = 0.8, top_p: float = 0.95, top_k: int = 50) -> Iterator[str]: prompt = get_prompt(message, chat_history, system_prompt) inputs = tokenizer([prompt], return_tensors='pt').to("cuda") streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True) generate_kwargs = dict( inputs, streamer=streamer, max_new_tokens=max_new_tokens, do_sample=True, top_p=top_p, top_k=top_k, temperature=temperature, num_beams=1, ) t = Thread(target=model.generate, kwargs=generate_kwargs) t.start() outputs = [] for text in streamer: outputs.append(text) yield ''.join(outputs) ================================================ FILE: llama2-7b-cn-4bit/quantization_4bit.py ================================================ import torch from transformers import AutoModelForCausalLM, BitsAndBytesConfig # 使用中文版 model_id = 'LinkSoul/Chinese-Llama-2-7b' # 或者,使用原版 # model_id = 'meta-llama/Llama-2-7b-chat-hf' model = AutoModelForCausalLM.from_pretrained( model_id, local_files_only=True, torch_dtype=torch.float16, quantization_config = BitsAndBytesConfig( bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.bfloat16 ), device_map='auto' ) import os output = "soulteary/Chinese-Llama-2-7b-4bit" if not os.path.exists(output): os.mkdir(output) model.save_pretrained(output) print("done") ================================================ FILE: scripts/make-13b.sh ================================================ #!/bin/bash docker build -t soulteary/llama2:base . -f docker/Dockerfile.base docker build -t soulteary/llama2:13b . -f docker/Dockerfile.13b ================================================ FILE: scripts/make-7b-cn-4bit.sh ================================================ #!/bin/bash docker build -t soulteary/llama2:base . -f docker/Dockerfile.base docker build -t soulteary/chinese-llama2:7b-4bit . -f docker/Dockerfile.7b-cn-4bit ================================================ FILE: scripts/make-7b-cn.sh ================================================ #!/bin/bash docker build -t soulteary/llama2:base . -f docker/Dockerfile.base docker build -t soulteary/llama2:7b-cn . -f docker/Dockerfile.7b-cn ================================================ FILE: scripts/make-7b.sh ================================================ #!/bin/bash docker build -t soulteary/llama2:base . -f docker/Dockerfile.base docker build -t soulteary/llama2:7b . -f docker/Dockerfile.7b ================================================ FILE: scripts/run-13b.sh ================================================ #!/bin/bash docker run --gpus all --ipc=host --ulimit memlock=-1 --ulimit stack=67108864 --rm -it -v `pwd`/meta-llama:/app/meta-llama -p 7860:7860 soulteary/llama2:13b ================================================ FILE: scripts/run-7b-cn-4bit.sh ================================================ #!/bin/bash docker run --gpus all --ipc=host --ulimit memlock=-1 --ulimit stack=67108864 --rm -it -v `pwd`/soulteary:/app/soulteary -p 7860:7860 soulteary/chinese-llama2:7b-4bit ================================================ FILE: scripts/run-7b-cn.sh ================================================ #!/bin/bash docker run --gpus all --ipc=host --ulimit memlock=-1 --ulimit stack=67108864 --rm -it -v `pwd`/LinkSoul:/app/LinkSoul -p 7860:7860 soulteary/llama2:7b-cn ================================================ FILE: scripts/run-7b.sh ================================================ #!/bin/bash docker run --gpus all --ipc=host --ulimit memlock=-1 --ulimit stack=67108864 --rm -it -v `pwd`/meta-llama:/app/meta-llama -p 7860:7860 soulteary/llama2:7b