diff --git a/python/llm/example/Text-Generation-WebUI/README.md b/python/llm/example/Text-Generation-WebUI/README.md new file mode 100644 index 00000000..1bb19bed --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/README.md @@ -0,0 +1,92 @@ + +This tutorial provides a step-by-step guide on how to use Text-Generation-WebUI to run Hugging Face transformers-based applications on BigDL-LLM. + +The WebUI is ported from [Text-Generation-WebUI](https://github.com/oobabooga/text-generation-webui). + +## 1. Prepare the environment on Windows + +Please use a python environment management tool (we recommend using Conda) to create a python enviroment and install necessary libs. + +### 1.1 Install BigDL-LLM + +Please see [BigDL-LLm Installation on Windows](https://bigdl.readthedocs.io/en/latest/doc/LLM/Overview/install_gpu.html#windows) for more details to install BigDL-LLM on your Client. + +### 1.2 Install other required dependencies + +```bash +pip install -r requirements.txt +``` +Note: Text-Generation-WebUI requires transformers version >= 4.36.0 + + +## 2. Start the WebUI Server + +### 2.1 For INT4 Optimizations + +For a quick start, you may run the script as below to start WebUI directly, it will automatically optimize and accelerate LLMs using INT4 optimizations. +```bash +python server.py +``` + +### 2.2 Optimizations for Other Percisions + +To enable optimizations for more precisions (`sym_int4`, `asym_int4`, `sym_int8`, `fp4`, `fp8`, `fp16`, `mixed_fp4`, `mixed_fp8`, etc.), you may run the command as below: +```bash +python server.py --load-in-low-bit +``` + +### 2.3 Access the WebUI + +After the successful startup of the WebUI server, it will provide links to access the WebUI as below. Please open the public URL in your browser to access the full functionality of the WebUI. + +```bash +Running on local URL: http://127.0.0.1:7860 +Running on public URL: https://your_tokens_here.gradio.live + +This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces) +``` + + +## 3. Run Models + +### 3.1 Select the Model +First, place your local model in `Text-Generation-WebUI/models` directory, you may also choose to download a model from Hugging Face. + +Next, please click the `Model` button to choose your model. + +![Image text](https://github.com/intel-analytics/BigDL/blob/1df67d7927ebea0af570b09f36ce76efbf9b8bad/python/llm/example/Text-Generation-WebUI/readme_folder/image.png) + + +### 3.2 Enable BigDL-LLM Optimizations +Text-Generation-WebUI supports multiple backends, including `Transformers`, `llama.cpp`, `BigDL-LLM`, etc. Please select the BigDL-LLM backend as below to enable low-bit optimizations. + +![Image text](https://github.com/intel-analytics/BigDL/blob/1df67d7927ebea0af570b09f36ce76efbf9b8bad/python/llm/example/Text-Generation-WebUI/readme_folder/image-1.png) + +Then please select the device according to your device. + +![Image text](https://github.com/intel-analytics/BigDL/blob/1df67d7927ebea0af570b09f36ce76efbf9b8bad/python/llm/example/Text-Generation-WebUI/readme_folder/image-2.png) + + +### 3.3 Load Model in Low Precision + +One common use case of BigDL-LLM is to load a Hugging Face transformers model in low precision. + +Notes: + +- When you start the web UI with `--load-in-4bit`, you will not be allowed to choose the quantization precision in `load-in-low-bit`. The model will be loaded with the INT4 precision as default. + +- When you want to load model in other precisions, please run server.py with `--load-in-low-bit` parameter. You may choose the precision from the list of `load-in-low-bit` option, and the `load-in-4bit` option will be disabled. + +- Please select the `optimize-model` and `use_cache` options to accelerate the model. + + +Now you may click the `Load` button to load the model with BigDL-LLM optimizations. + +![Image text](https://github.com/intel-analytics/BigDL/blob/1df67d7927ebea0af570b09f36ce76efbf9b8bad/python/llm/example/Text-Generation-WebUI/readme_folder/image-3.png) + + +### 3.4 Run the Model on WebUI + +Now you can do model inference on Text-Generation-WebUI with BigDL-LLM optimizations, including `Chat`, `Default` and `Notebook` Tabs. Please see [Chat-Tab Wiki](https://github.com/oobabooga/text-generation-webui/wiki/01-%E2%80%90-Chat-Tab) and [Default and Notebook Tabs Wiki](https://github.com/oobabooga/text-generation-webui/wiki/02-%E2%80%90-Default-and-Notebook-Tabs) for more details. + +![Image text](https://github.com/intel-analytics/BigDL/blob/1df67d7927ebea0af570b09f36ce76efbf9b8bad/python/llm/example/Text-Generation-WebUI/readme_folder/image-4.png) diff --git a/python/llm/example/Text-Generation-WebUI/characters/Assistant.yaml b/python/llm/example/Text-Generation-WebUI/characters/Assistant.yaml new file mode 100644 index 00000000..a6141f40 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/characters/Assistant.yaml @@ -0,0 +1,4 @@ +name: AI +greeting: How can I help you today? +context: | + The following is a conversation with an AI Large Language Model. The AI has been trained to answer questions, provide recommendations, and help with decision making. The AI follows user requests. The AI thinks outside the box. diff --git a/python/llm/example/Text-Generation-WebUI/characters/Example.png b/python/llm/example/Text-Generation-WebUI/characters/Example.png new file mode 100644 index 00000000..a7c4e513 Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/characters/Example.png differ diff --git a/python/llm/example/Text-Generation-WebUI/characters/Example.yaml b/python/llm/example/Text-Generation-WebUI/characters/Example.yaml new file mode 100644 index 00000000..c1a3299e --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/characters/Example.yaml @@ -0,0 +1,17 @@ +name: Chiharu Yamada +greeting: |- + *Chiharu strides into the room with a smile, her eyes lighting up when she sees you. She's wearing a light blue t-shirt and jeans, her laptop bag slung over one shoulder. She takes a seat next to you, her enthusiasm palpable in the air* + Hey! I'm so excited to finally meet you. I've heard so many great things about you and I'm eager to pick your brain about computers. I'm sure you have a wealth of knowledge that I can learn from. *She grins, eyes twinkling with excitement* Let's get started! +context: |- + Chiharu Yamada's Persona: Chiharu Yamada is a young, computer engineer-nerd with a knack for problem solving and a passion for technology. + + {{user}}: So how did you get into computer engineering? + {{char}}: I've always loved tinkering with technology since I was a kid. + {{user}}: That's really impressive! + {{char}}: *She chuckles bashfully* Thanks! + {{user}}: So what do you do when you're not working on computers? + {{char}}: I love exploring, going out with friends, watching movies, and playing video games. + {{user}}: What's your favorite type of computer hardware to work with? + {{char}}: Motherboards, they're like puzzles and the backbone of any system. + {{user}}: That sounds great! + {{char}}: Yeah, it's really fun. I'm lucky to be able to do this as a job. diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Black.woff b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Black.woff new file mode 100644 index 00000000..0280e0f1 Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Black.woff differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Black.woff2 b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Black.woff2 new file mode 100644 index 00000000..1d511839 Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Black.woff2 differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-BlackItalic.woff b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-BlackItalic.woff new file mode 100644 index 00000000..5cd4424a Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-BlackItalic.woff differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-BlackItalic.woff2 b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-BlackItalic.woff2 new file mode 100644 index 00000000..f0baeca2 Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-BlackItalic.woff2 differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Bold.woff b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Bold.woff new file mode 100644 index 00000000..750b7375 Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Bold.woff differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Bold.woff2 b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Bold.woff2 new file mode 100644 index 00000000..af6c17d0 Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Bold.woff2 differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-BoldItalic.woff b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-BoldItalic.woff new file mode 100644 index 00000000..d484cf22 Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-BoldItalic.woff differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-BoldItalic.woff2 b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-BoldItalic.woff2 new file mode 100644 index 00000000..210c3a1d Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-BoldItalic.woff2 differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ExtraBold.woff b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ExtraBold.woff new file mode 100644 index 00000000..1a1e41d3 Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ExtraBold.woff differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ExtraBold.woff2 b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ExtraBold.woff2 new file mode 100644 index 00000000..e2bd3232 Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ExtraBold.woff2 differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ExtraBoldItalic.woff b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ExtraBoldItalic.woff new file mode 100644 index 00000000..95d68a95 Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ExtraBoldItalic.woff differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ExtraBoldItalic.woff2 b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ExtraBoldItalic.woff2 new file mode 100644 index 00000000..65892aeb Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ExtraBoldItalic.woff2 differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ExtraLight.woff b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ExtraLight.woff new file mode 100644 index 00000000..4b8a5596 Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ExtraLight.woff differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ExtraLight.woff2 b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ExtraLight.woff2 new file mode 100644 index 00000000..e92cf55d Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ExtraLight.woff2 differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ExtraLightItalic.woff b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ExtraLightItalic.woff new file mode 100644 index 00000000..f0b0a671 Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ExtraLightItalic.woff differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ExtraLightItalic.woff2 b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ExtraLightItalic.woff2 new file mode 100644 index 00000000..d63c4f7e Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ExtraLightItalic.woff2 differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Italic.woff b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Italic.woff new file mode 100644 index 00000000..bc892974 Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Italic.woff differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Italic.woff2 b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Italic.woff2 new file mode 100644 index 00000000..a6bd8a37 Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Italic.woff2 differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Light.woff b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Light.woff new file mode 100644 index 00000000..b89c9971 Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Light.woff differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Light.woff2 b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Light.woff2 new file mode 100644 index 00000000..962c6d76 Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Light.woff2 differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-LightItalic.woff b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-LightItalic.woff new file mode 100644 index 00000000..741ab91d Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-LightItalic.woff differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-LightItalic.woff2 b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-LightItalic.woff2 new file mode 100644 index 00000000..91532830 Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-LightItalic.woff2 differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Medium.woff b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Medium.woff new file mode 100644 index 00000000..d8dfb110 Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Medium.woff differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Medium.woff2 b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Medium.woff2 new file mode 100644 index 00000000..deff7857 Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Medium.woff2 differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-MediumItalic.woff b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-MediumItalic.woff new file mode 100644 index 00000000..d7ca037e Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-MediumItalic.woff differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-MediumItalic.woff2 b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-MediumItalic.woff2 new file mode 100644 index 00000000..d87d4b63 Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-MediumItalic.woff2 differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Regular.woff b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Regular.woff new file mode 100644 index 00000000..64d9e17c Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Regular.woff differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Regular.woff2 b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Regular.woff2 new file mode 100644 index 00000000..172de3ca Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Regular.woff2 differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-SemiBold.woff b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-SemiBold.woff new file mode 100644 index 00000000..abd6f542 Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-SemiBold.woff differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-SemiBold.woff2 b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-SemiBold.woff2 new file mode 100644 index 00000000..1c38d679 Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-SemiBold.woff2 differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-SemiBoldItalic.woff b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-SemiBoldItalic.woff new file mode 100644 index 00000000..32dd0195 Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-SemiBoldItalic.woff differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-SemiBoldItalic.woff2 b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-SemiBoldItalic.woff2 new file mode 100644 index 00000000..853adbfc Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-SemiBoldItalic.woff2 differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Thin.woff b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Thin.woff new file mode 100644 index 00000000..edb17d3e Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Thin.woff differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Thin.woff2 b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Thin.woff2 new file mode 100644 index 00000000..ca9fd928 Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-Thin.woff2 differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ThinItalic.woff b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ThinItalic.woff new file mode 100644 index 00000000..43d33e8e Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ThinItalic.woff differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ThinItalic.woff2 b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ThinItalic.woff2 new file mode 100644 index 00000000..73d94abc Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/css/NotoSans/NotoSans-ThinItalic.woff2 differ diff --git a/python/llm/example/Text-Generation-WebUI/css/NotoSans/stylesheet.css b/python/llm/example/Text-Generation-WebUI/css/NotoSans/stylesheet.css new file mode 100644 index 00000000..467973b8 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/css/NotoSans/stylesheet.css @@ -0,0 +1,166 @@ +/* +Copied from https://github.com/SillyTavern/SillyTavern/tree/6c8bd06308c69d51e2eb174541792a870a83d2d6/public/webfonts/NotoSans +*/ + +@font-face { + font-family: 'Noto Sans'; + src: url('file/css/NotoSans/NotoSans-Black.woff2') format('woff2'), + url('file/css/NotoSans/NotoSans-Black.woff') format('woff'); + font-weight: 900; + font-style: normal; + font-display: swap; +} + +@font-face { + font-family: 'Noto Sans'; + src: url('file/css/NotoSans/NotoSans-ExtraBoldItalic.woff2') format('woff2'), + url('file/css/NotoSans/NotoSans-ExtraBoldItalic.woff') format('woff'); + font-weight: bold; + font-style: italic; + font-display: swap; +} + +@font-face { + font-family: 'Noto Sans'; + src: url('file/css/NotoSans/NotoSans-BlackItalic.woff2') format('woff2'), + url('file/css/NotoSans/NotoSans-BlackItalic.woff') format('woff'); + font-weight: 900; + font-style: italic; + font-display: swap; +} + +@font-face { + font-family: 'Noto Sans'; + src: url('file/css/NotoSans/NotoSans-ExtraBold.woff2') format('woff2'), + url('file/css/NotoSans/NotoSans-ExtraBold.woff') format('woff'); + font-weight: bold; + font-style: normal; + font-display: swap; +} + +@font-face { + font-family: 'Noto Sans'; + src: url('file/css/NotoSans/NotoSans-ThinItalic.woff2') format('woff2'), + url('file/css/NotoSans/NotoSans-ThinItalic.woff') format('woff'); + font-weight: 100; + font-style: italic; + font-display: swap; +} + +@font-face { + font-family: 'Noto Sans'; + src: url('file/css/NotoSans/NotoSans-BoldItalic.woff2') format('woff2'), + url('file/css/NotoSans/NotoSans-BoldItalic.woff') format('woff'); + font-weight: bold; + font-style: italic; + font-display: swap; +} + +@font-face { + font-family: 'Noto Sans'; + src: url('file/css/NotoSans/NotoSans-Bold.woff2') format('woff2'), + url('file/css/NotoSans/NotoSans-Bold.woff') format('woff'); + font-weight: bold; + font-style: normal; + font-display: swap; +} + +@font-face { + font-family: 'Noto Sans'; + src: url('file/css/NotoSans/NotoSans-LightItalic.woff2') format('woff2'), + url('file/css/NotoSans/NotoSans-LightItalic.woff') format('woff'); + font-weight: 300; + font-style: italic; + font-display: swap; +} + +@font-face { + font-family: 'Noto Sans'; + src: url('file/css/NotoSans/NotoSans-Italic.woff2') format('woff2'), + url('file/css/NotoSans/NotoSans-Italic.woff') format('woff'); + font-weight: normal; + font-style: italic; + font-display: swap; +} + +@font-face { + font-family: 'Noto Sans'; + src: url('file/css/NotoSans/NotoSans-ExtraLightItalic.woff2') format('woff2'), + url('file/css/NotoSans/NotoSans-ExtraLightItalic.woff') format('woff'); + font-weight: 200; + font-style: italic; + font-display: swap; +} + +@font-face { + font-family: 'Noto Sans'; + src: url('file/css/NotoSans/NotoSans-Light.woff2') format('woff2'), + url('file/css/NotoSans/NotoSans-Light.woff') format('woff'); + font-weight: 300; + font-style: normal; + font-display: swap; +} + +@font-face { + font-family: 'Noto Sans'; + src: url('file/css/NotoSans/NotoSans-ExtraLight.woff2') format('woff2'), + url('file/css/NotoSans/NotoSans-ExtraLight.woff') format('woff'); + font-weight: 200; + font-style: normal; + font-display: swap; +} + +@font-face { + font-family: 'Noto Sans'; + src: url('file/css/NotoSans/NotoSans-Medium.woff2') format('woff2'), + url('file/css/NotoSans/NotoSans-Medium.woff') format('woff'); + font-weight: 500; + font-style: normal; + font-display: swap; +} + +@font-face { + font-family: 'Noto Sans'; + src: url('file/css/NotoSans/NotoSans-Regular.woff2') format('woff2'), + url('file/css/NotoSans/NotoSans-Regular.woff') format('woff'); + font-weight: normal; + font-style: normal; + font-display: swap; +} + +@font-face { + font-family: 'Noto Sans'; + src: url('file/css/NotoSans/NotoSans-MediumItalic.woff2') format('woff2'), + url('file/css/NotoSans/NotoSans-MediumItalic.woff') format('woff'); + font-weight: 500; + font-style: italic; + font-display: swap; +} + +@font-face { + font-family: 'Noto Sans'; + src: url('file/css/NotoSans/NotoSans-SemiBoldItalic.woff2') format('woff2'), + url('file/css/NotoSans/NotoSans-SemiBoldItalic.woff') format('woff'); + font-weight: 600; + font-style: italic; + font-display: swap; +} + +@font-face { + font-family: 'Noto Sans'; + src: url('file/css/NotoSans/NotoSans-SemiBold.woff2') format('woff2'), + url('file/css/NotoSans/NotoSans-SemiBold.woff') format('woff'); + font-weight: 600; + font-style: normal; + font-display: swap; +} + +@font-face { + font-family: 'Noto Sans'; + src: url('file/css/NotoSans/NotoSans-Thin.woff2') format('woff2'), + url('file/css/NotoSans/NotoSans-Thin.woff') format('woff'); + font-weight: 100; + font-style: normal; + font-display: swap; +} + diff --git a/python/llm/example/Text-Generation-WebUI/css/chat_style-TheEncrypted777.css b/python/llm/example/Text-Generation-WebUI/css/chat_style-TheEncrypted777.css new file mode 100644 index 00000000..f823eef0 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/css/chat_style-TheEncrypted777.css @@ -0,0 +1,133 @@ +/* All credits to TheEncrypted777: https://www.reddit.com/r/Oobabooga/comments/12xe6vq/updated_css_styling_with_color_customization_for/ */ + +.message { + display: grid; + grid-template-columns: 60px minmax(0, 1fr); + padding-bottom: 28px; + font-size: 18px; + font-family: 'Noto Sans', Arial, sans-serif; + line-height: 1.428571429; +} + +.circle-you, +.circle-bot { + background-color: gray; + border-radius: 1rem; + border: 2px solid white; +} + +.circle-bot img, +.circle-you img { + border-radius: 10%; + width: 100%; + height: 100%; + object-fit: cover; +} + +.circle-you, .circle-bot { + /* You can set the size of the profile images here, but if you do, you have to also adjust the .text{padding-left: 90px} to a different number according to the width of the image which is right below here */ + width: 135px; + height: 175px; +} + +.text { + /* Change this to move the message box further left or right depending on the size of your profile pic */ + padding-left: 90px; + text-shadow: 2px 2px 2px rgb(0 0 0 / 40%); +} + +.text p { + margin-top: 2px; +} + +.username { + padding-left: 10px; + font-size: 22px; + font-weight: bold; + border-top: 1px solid rgb(51 64 90); + padding: 3px; +} + +.message-body { + position: relative; + border: 1px solid rgb(255 255 255 / 45.9%); + border-radius: 10px; + padding: 10px; + padding-top: 5px; + + /* Message gradient background color - remove the line bellow if you don't want a background color or gradient */ + background: linear-gradient(to bottom, #171730, #1b263f); +} + +/* Adds 2 extra lines at the top and bottom of the message */ +.message-body::before, +.message-body::after { + content: ""; + position: absolute; + left: 10px; + right: 10px; + height: 1px; + background-color: rgb(255 255 255 / 13%); +} + +.message-body::before { + top: 6px; +} + +.message-body::after { + bottom: 6px; +} + +.message-body img { + max-width: 300px; + max-height: 300px; + border-radius: 20px; +} + +.message-body p { + margin-bottom: 0 !important; + font-size: 18px !important; + line-height: 1.428571429 !important; + color: rgb(243 244 246) !important; + text-shadow: 2px 2px 2px rgb(0 0 0); +} + +.message-body p em { + color: rgb(138 138 138) !important; +} + +@media screen and (width <= 688px) { + .message { + display: grid; + grid-template-columns: 60px minmax(0, 1fr); + padding-bottom: 25px; + font-size: 15px; + font-family: 'Noto Sans', Helvetica, Arial, sans-serif; + line-height: 1.428571429; + } + + .circle-you, .circle-bot { + width: 50px; + height: 73px; + border-radius: 0.5rem; + } + + .circle-bot img, + .circle-you img { + width: 100%; + height: 100%; + object-fit: cover; + } + + .text { + padding-left: 0; + } + + .message-body p { + font-size: 16px !important; + } + + .username { + font-size: 20px; + } +} diff --git a/python/llm/example/Text-Generation-WebUI/css/chat_style-cai-chat-square.css b/python/llm/example/Text-Generation-WebUI/css/chat_style-cai-chat-square.css new file mode 100644 index 00000000..d626dbb1 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/css/chat_style-cai-chat-square.css @@ -0,0 +1,21 @@ +@import url("file/css/chat_style-cai-chat.css"); + +.circle-bot, .circle-you { + height: 90px; + width: 60px; + border-radius: 10px; + background-color: #656565; +} + +.circle-bot img, .circle-you img { + border-radius: 8.333px; +} + +.circle-you { + background-color: #656565; +} + +.message { + padding-bottom: 30px; + grid-template-columns: 70px minmax(0, 1fr); +} diff --git a/python/llm/example/Text-Generation-WebUI/css/chat_style-cai-chat.css b/python/llm/example/Text-Generation-WebUI/css/chat_style-cai-chat.css new file mode 100644 index 00000000..ba0c8f13 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/css/chat_style-cai-chat.css @@ -0,0 +1,62 @@ +.message { + display: grid; + grid-template-columns: 60px minmax(0, 1fr); + padding-bottom: 15px; + font-size: 15px; + font-family: 'Noto Sans', Helvetica, Arial, sans-serif; + line-height: 22.5px !important; +} + +.message-body { + margin-top: 3px; +} + +.circle-you { + width: 50px; + height: 50px; + background-color: rgb(238 78 59); + border-radius: 50%; +} + +.circle-bot { + width: 50px; + height: 50px; + background-color: rgb(59 78 244); + border-radius: 50%; +} + +.circle-bot img, +.circle-you img { + border-radius: 50%; + width: 100%; + height: 100%; + object-fit: cover; +} + +.username { + font-weight: bold; +} + +.message-body img { + max-width: 300px; + max-height: 300px; + border-radius: 20px; +} + +.message-body p { + font-size: 15px !important; + line-height: 22.5px !important; +} + +.message-body p, .chat .message-body ul, .chat .message-body ol { + margin-bottom: 10px !important; +} + +.dark .message-body p em { + color: rgb(138 138 138) !important; +} + +.message-body p em { + color: rgb(110 110 110) !important; + font-weight: 500; +} \ No newline at end of file diff --git a/python/llm/example/Text-Generation-WebUI/css/chat_style-messenger.css b/python/llm/example/Text-Generation-WebUI/css/chat_style-messenger.css new file mode 100644 index 00000000..6bb97971 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/css/chat_style-messenger.css @@ -0,0 +1,99 @@ +.message { + padding-bottom: 25px; + font-size: 15px; + font-family: 'Noto Sans', Helvetica, Arial, sans-serif; + line-height: 1.428571429; +} + +.circle-you { + width: 50px; + height: 50px; + background-color: rgb(238 78 59); + border-radius: 50%; +} + +.circle-bot { + width: 50px; + height: 50px; + background-color: rgb(59 78 244); + border-radius: 50%; + float: left; + margin-right: 10px; + margin-top: 5px; +} + +.circle-bot img, +.circle-you img { + border-radius: 50%; + width: 100%; + height: 100%; + object-fit: cover; +} + +.circle-you { + margin-top: 5px; + float: right; +} + +.circle-bot + .text, .circle-you + .text { + border-radius: 18px; + padding: 8px 12px; +} + +.circle-bot + .text { + background-color: #E4E6EB; + float: left; +} + +.circle-you + .text { + float: right; + background-color: rgb(0 132 255); + margin-right: 10px; +} + +.circle-you + .text div, .circle-you + .text *, .dark .circle-you + .text div, .dark .circle-you + .text * { + color: #FFF !important; +} + +.circle-you + .text .username { + text-align: right; +} + +.dark .circle-bot + .text div, .dark .circle-bot + .text * { + color: #000; +} + +.text { + max-width: 80%; +} + +.text p { + margin-top: 5px; +} + +.username { + font-weight: bold; +} + +.message-body { +} + +.message-body img { + max-width: 300px; + max-height: 300px; + border-radius: 20px; +} + +.message-body p { + margin-bottom: 0 !important; + font-size: 15px !important; + line-height: 1.428571429 !important; +} + +.dark .message-body p em { + color: rgb(138 138 138) !important; +} + +.message-body p em { + color: rgb(110 110 110) !important; +} diff --git a/python/llm/example/Text-Generation-WebUI/css/chat_style-wpp.css b/python/llm/example/Text-Generation-WebUI/css/chat_style-wpp.css new file mode 100644 index 00000000..ac4fd39a --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/css/chat_style-wpp.css @@ -0,0 +1,55 @@ +.message { + padding-bottom: 25px; + font-size: 15px; + font-family: 'Noto Sans', Helvetica, Arial, sans-serif; + line-height: 1.428571429; +} + +.text-you { + background-color: #d9fdd3; + border-radius: 15px; + padding: 10px; + padding-top: 5px; + float: right; +} + +.text-bot { + background-color: #f2f2f2; + border-radius: 15px; + padding: 10px; + padding-top: 5px; +} + +.dark .text-you { + background-color: #005c4b; + color: #111b21; +} + +.dark .text-bot { + background-color: #1f2937; + color: #111b21; +} + +.text-bot p, .text-you p { + margin-top: 5px; +} + +.message-body img { + max-width: 300px; + max-height: 300px; + border-radius: 20px; +} + +.message-body p { + margin-bottom: 0 !important; + font-size: 15px !important; + line-height: 1.428571429 !important; +} + +.dark .message-body p em { + color: rgb(138 138 138) !important; +} + +.message-body p em { + color: rgb(110 110 110) !important; +} \ No newline at end of file diff --git a/python/llm/example/Text-Generation-WebUI/css/html_4chan_style.css b/python/llm/example/Text-Generation-WebUI/css/html_4chan_style.css new file mode 100644 index 00000000..afbfb537 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/css/html_4chan_style.css @@ -0,0 +1,73 @@ +#parent #container { + background-color: #eef2ff; + padding: 17px; +} + +#parent #container .reply { + background-color: rgb(214 218 240); + border-bottom: 1px solid rgb(183 197 217); + border-image: none 100% 1 0 stretch; + border-left: 0 none rgb(0 0 0); + border-right: 1px solid rgb(183 197 217); + color: rgb(0 0 0); + display: table; + font-family: arial, helvetica, sans-serif; + font-size: 13.3333px; + margin: 4px 0; + overflow: hidden hidden; + padding: 4px 2px; +} + +#parent #container .number { + color: rgb(0 0 0); + font-family: arial, helvetica, sans-serif; + font-size: 13.3333px; + width: 342.65px; + margin-right: 7px; +} + +#parent #container .op { + color: rgb(0 0 0); + font-family: arial, helvetica, sans-serif; + font-size: 13.3333px; + margin: 4px 0 8px; + overflow: hidden hidden; +} + +#parent #container .op blockquote { + margin-left: 0 !important; +} + +#parent #container .name { + color: rgb(17 119 67); + font-family: arial, helvetica, sans-serif; + font-size: 13.3333px; + font-weight: 700; + margin-left: 7px; +} + +#parent #container .quote { + color: rgb(221 0 0); + font-family: arial, helvetica, sans-serif; + font-size: 13.3333px; + text-decoration: underline solid rgb(221 0 0); + text-decoration-thickness: auto; +} + +#parent #container .greentext { + color: rgb(120 153 34); + font-family: arial, helvetica, sans-serif; + font-size: 13.3333px; +} + +#parent #container blockquote { + margin: 0 !important; + margin-block: 1em 1em; + margin-inline: 40px 40px; + margin: 13.33px 40px !important; +} + +#parent #container .message_4chan { + color: black; + border: none; +} \ No newline at end of file diff --git a/python/llm/example/Text-Generation-WebUI/css/html_instruct_style.css b/python/llm/example/Text-Generation-WebUI/css/html_instruct_style.css new file mode 100644 index 00000000..673dad2a --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/css/html_instruct_style.css @@ -0,0 +1,82 @@ +.chat { + background: var(--block-background-fill); + padding: 24px 19px; + padding-right: 19px !important; + padding-top: 0; + border: 1px solid var(--block-border-color); +} + +.chat > .messages { + padding-top: 28px !important; +} + +.message { + display: grid; + grid-template-columns: 60px 1fr; + padding-bottom: 25px; + font-size: 15px; + font-family: 'Noto Sans', Helvetica, Arial, sans-serif; + line-height: 24px; +} + +.message:first-child { + padding-top: 0; +} + +.username { + display: none; +} + +.message-body p, .message-body li { + font-size: 15px !important; + line-height: 24px !important; +} + +.message-body p, .chat .message-body ul, .chat .message-body ol { + margin-bottom: 16px !important; +} + +.message-body p:last-child, .chat .message-body ul:last-child, .chat .message-body ol:last-child { + margin-bottom: 0 !important; +} + +.dark .message-body p em { + color: rgb(198 202 214) !important; +} + +.message-body p em { + color: rgb(110 110 110) !important; +} + +.gradio-container .chat .assistant-message { + padding: 20px; + background: var(--background-fill-secondary); + margin-top: 12px !important; + margin-bottom: 24px !important; + margin-right: 16px; + border-radius: 22px; + border-bottom-left-radius: 0; + border: 1px solid var(--border-color-primary); +} + +.gradio-container .chat .user-message { + padding: 20px; + background-color: var(--color-accent-soft); + margin-bottom: 12px !important; + margin-left: 16px; + border-radius: 22px; + border-bottom-right-radius: 0; + border: 1px solid var(--border-color-accent-subdued); +} + +.gradio-container .chat .assistant-message:last-child, .gradio-container .chat .user-message:last-child { + margin-bottom: 0 !important; +} + +code { + background-color: #f3f4f6 !important; +} + +.dark code { + background-color: #1f2937 !important; +} \ No newline at end of file diff --git a/python/llm/example/Text-Generation-WebUI/css/html_readable_style.css b/python/llm/example/Text-Generation-WebUI/css/html_readable_style.css new file mode 100644 index 00000000..760649d6 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/css/html_readable_style.css @@ -0,0 +1,33 @@ +.readable-container { + max-width: 600px; + margin-left: auto; + margin-right: auto; + background-color: rgb(31 41 55); + padding: 3em; + word-break: break-word; + overflow-wrap: anywhere; + color: #efefef !important; +} + +.readable-container p, .readable-container li { + font-size: 16px !important; + color: #efefef !important; + margin-bottom: 22px; + line-height: 1.4 !important; +} + +.readable-container li > p { + display: inline !important; +} + +.readable-container code { + overflow-x: auto; +} + +.readable-container :not(pre) > code { + white-space: normal !important; +} + +.readable-container .hoverable { + font-size: 14px; +} \ No newline at end of file diff --git a/python/llm/example/Text-Generation-WebUI/css/main.css b/python/llm/example/Text-Generation-WebUI/css/main.css new file mode 100644 index 00000000..653da3ee --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/css/main.css @@ -0,0 +1,688 @@ +.tabs.svelte-710i53 { + margin-top: 0 +} + +.py-6 { + padding-top: 2.5rem +} + +.small-button { + min-width: 0 !important; + max-width: 171px; + height: 39.594px; + align-self: end; +} + +.refresh-button { + max-width: 4.4em; + min-width: 2.2em !important; + height: 39.594px; + align-self: end; + line-height: 1em; + border-radius: 0.5em; + flex: none; +} + +.refresh-button-small { + max-width: 2.2em; +} + +.button_nowrap { + white-space: nowrap; +} + +#slim-column { + flex: none !important; + min-width: 0 !important; +} + +.slim-dropdown { + background-color: transparent !important; + border: none !important; + padding: 0 !important; +} + +#download-label, #upload-label { + min-height: 0 +} + +.dark svg { + fill: white; +} + +.dark a { + color: white !important; +} + +ol li p, ul li p { + display: inline-block; +} + +#chat-tab, #default-tab, #notebook-tab, #parameters, #chat-settings, #lora, #training-tab, #model-tab, #session-tab { + border: 0; +} + +.gradio-container-3-18-0 .prose * h1, h2, h3, h4 { + color: white; +} + +.gradio-container { + max-width: 100% !important; + padding-top: 0 !important; +} + +#extensions { + margin-top: 5px; + margin-bottom: 35px; +} + +.extension-tab { + border: 0 !important; +} + +span.math.inline { + font-size: 27px; + vertical-align: baseline !important; +} + +div.svelte-15lo0d8 > *, div.svelte-15lo0d8 > .form > * { + flex-wrap: nowrap; +} + +.header_bar { + background-color: #f7f7f7; + box-shadow: 0 2px 3px rgba(22 22 22 / 35%); + margin-bottom: 0; + overflow-x: scroll; + margin-left: calc(-1 * var(--size-4)); + margin-right: calc(-1 * var(--size-4)); + display: block !important; + text-wrap: nowrap; + z-index: 90; +} + +.dark .header_bar { + border: none !important; + box-shadow: 0 3px 4px rgba(20 20 20 / 60%); + background-color: #8080802b; +} + +.header_bar button.selected { + border-radius: 0; +} + +.textbox_default textarea { + height: calc(100dvh - 271px); +} + +.textbox_default_output textarea { + height: calc(100dvh - 185px); +} + +.textbox textarea { + height: calc(100dvh - 241px); +} + +.textbox_logits textarea { + height: calc(100dvh - 236px); +} + +.textbox_logits_notebook textarea { + height: calc(100dvh - 292px); +} + +.monospace textarea { + font-family: monospace; +} + +.textbox_default textarea, +.textbox_default_output textarea, +.textbox_logits textarea, +.textbox_logits_notebook textarea, +.textbox textarea { + font-size: 16px !important; + color: #46464A !important; +} + +.dark textarea { + color: #efefef !important; +} + +@media screen and (width <= 711px) { + .textbox_default textarea { + height: calc(100dvh - 259px); + } + + div .default-token-counter { + top: calc( 0.5 * (100dvh - 236px) ) !important; + } + + .transparent-substring { + display: none; + } + + .hover-menu { + min-width: 250px !important; + } +} + +/* Hide the gradio footer */ +footer { + display: none !important; +} + +button { + font-size: 14px !important; +} + +.file-saver { + position: fixed !important; + height: 100%; + z-index: 1000; + background-color: rgb(0 0 0 / 50%) !important; + margin-left: -20px; + margin-right: -20px; +} + +.file-saver > :first-child { + position: fixed !important; + top: 50%; + left: 50%; + transform: translate(-50%, -50%); /* center horizontally */ + width: 100%; + max-width: 500px; + background-color: var(--input-background-fill); + border: var(--input-border-width) solid var(--input-border-color) !important; +} + +.file-saver > :first-child > :nth-child(2) { + background: var(--block-background-fill); +} + +.checkboxgroup-table label { + background: none !important; + padding: 0 !important; + border: 0 !important; +} + +.checkboxgroup-table div { + display: grid !important; +} + +.markdown ul ol { + font-size: 100% !important; +} + +.pretty_scrollbar::-webkit-scrollbar { + width: 5px; +} + +.pretty_scrollbar::-webkit-scrollbar-track { + background: transparent; +} + +.pretty_scrollbar::-webkit-scrollbar-thumb, +.pretty_scrollbar::-webkit-scrollbar-thumb:hover { + background: #c5c5d2; +} + +.dark .pretty_scrollbar::-webkit-scrollbar-thumb, +.dark .pretty_scrollbar::-webkit-scrollbar-thumb:hover { + background: #374151; +} + +.pretty_scrollbar::-webkit-resizer { + background: #c5c5d2; +} + +.dark .pretty_scrollbar::-webkit-resizer { + background: #374151; +} + +audio { + max-width: 100%; +} + +/* Copied from https://github.com/AUTOMATIC1111/stable-diffusion-webui */ +.token-counter { + position: absolute !important; + top: calc( 0.5 * (100dvh - 218px) ) !important; + right: 2px; + z-index: 100; + background: var(--input-background-fill) !important; + min-height: 0 !important; +} + +.default-token-counter { + top: calc( 0.5 * (100dvh - 248px) ) !important; +} + +.token-counter span { + padding: 1px; + box-shadow: 0 0 0 0.3em rgb(192 192 192 / 15%), inset 0 0 0.6em rgb(192 192 192 / 7.5%); + border: 2px solid rgb(192 192 192 / 40%) !important; + border-radius: 0.4em; +} + +.no-background { + background: var(--background-fill-primary) !important; + padding: 0 !important; +} + +/* ---------------------------------------------- + Chat tab +---------------------------------------------- */ +.h-\[40vh\], .wrap.svelte-byatnx.svelte-byatnx.svelte-byatnx { + height: 66.67vh +} + +.gradio-container { + margin-left: auto !important; + margin-right: auto !important; +} + +.w-screen { + width: unset +} + +div.svelte-362y77>*, div.svelte-362y77>.form>* { + flex-wrap: nowrap +} + +.pending.svelte-1ed2p3z { + opacity: 1; +} + +.wrap.svelte-6roggh.svelte-6roggh { + max-height: 92.5%; +} + +/* This is for the microphone button in the whisper extension */ +.sm.svelte-1ipelgc { + width: 100%; +} + +#chat-tab { + padding-top: 0; +} + +#chat-tab button#Generate, #chat-tab button#stop { + width: 89.3438px !important; +} + +#chat-tab button, #notebook-tab button, #default-tab button { + min-width: 0 !important; +} + +#chat-tab > :first-child, #extensions { + max-width: 880px; + margin-left: auto; + margin-right: auto; +} + +@media screen and (width <= 688px) { + #chat-tab { + padding-left: 0; + padding-right: 0; + } +} + +.chat { + margin-left: auto; + margin-right: auto; + max-width: 880px; + min-height: var(--chat-height); + overflow-y: auto; + padding-right: 15px; + display: flex; + flex-direction: column; + word-break: break-word; + overflow-wrap: anywhere; + border-top: none; + border-radius: 0 0 0 8px; +} + +.chat-parent { + height: calc(100dvh - 98px - var(--header-height) - var(--input-delta)); + overflow: auto !important; + border-radius: 0 !important; + margin-bottom: var(--input-delta) !important; +} + +.old-ui .chat-parent { + height: calc(100dvh - 192px - var(--header-height) - var(--input-delta)); + margin-bottom: var(--input-delta) !important; +} + +.chat-parent.bigchat { + height: calc(100dvh - 98px - var(--header-height) - var(--input-delta)) !important; + margin-bottom: var(--input-delta) !important; +} + +.chat > .messages { + display: flex; + flex-direction: column; + padding-top: 25px; +} + +.chat .message:last-child { + margin-bottom: 0 !important; + padding-bottom: 0 !important; +} + +.message-body li { + list-style-position: outside; +} + +.chat .message-body ul, .chat .message-body ol { + padding-inline-start: 2em; +} + +.message-body li:not(:last-child) { + margin-top: 0 !important; + margin-bottom: 2px !important; +} + +.message-body li:last-child { + margin-bottom: 0 !important; +} + +.message-body li > p { + display: inline !important; +} + +.message-body ul, .message-body ol { + font-size: 15px !important; +} + +.message-body ul { + list-style-type: disc !important; +} + +.message-body pre:not(:last-child) { + margin-bottom: 35.625px !important; +} + +.message-body pre:last-child { + margin-bottom: 0 !important; +} + +.message-body code { + white-space: pre-wrap !important; + word-wrap: break-word !important; + border: 1px solid var(--border-color-primary); + border-radius: var(--radius-sm); + background: var(--background-fill-secondary); + font-size: 90%; + padding: 1px 3px; +} + +.message-body pre > code { + display: block; + padding: 15px; +} + +.message-body :not(pre) > code { + white-space: normal !important; +} + +#chat-input { + padding: 0; + padding-top: 18px; + background: transparent; + border: none; +} + +#chat-input textarea:focus { + box-shadow: none !important; +} + +#chat-input > :first-child { + background-color: transparent; +} + +#chat-input .progress-text { + display: none; +} + +@media print { + body { + visibility: hidden; + } + + .chat { + visibility: visible; + position: absolute; + left: 0; + top: 0; + max-width: unset; + max-height: unset; + width: 100%; + overflow-y: visible; + } + + .message { + break-inside: avoid; + } + + .gradio-container { + overflow: visible; + } + + .tab-nav { + display: none !important; + } + + #chat-tab > :first-child { + max-width: unset; + } +} + +#show-controls { + position: absolute; + height: 100%; + background-color: var(--background-fill-primary); + border: 0 !important; + border-radius: 0; +} + +#show-controls label { + z-index: 1000; + position: absolute; + left: calc(100% - 168px); +} + +#show-controls span { + opacity: 0.6; +} + +#typing-container { + display: none; + position: absolute; + background-color: transparent; + left: -2px; + padding: var(--block-padding); +} + +.typing { + position: relative; +} + +.visible-dots #typing-container { + display: block; +} + +.typing span { + content: ''; + animation: blink 1.5s infinite; + animation-fill-mode: both; + height: 10px; + width: 10px; + background: #3b5998;; + position: absolute; + left:0; + top:0; + border-radius: 50%; +} + +.typing .dot1 { + animation-delay: .2s; + margin-left: calc(10px * 1.5); +} + +.typing .dot2 { + animation-delay: .4s; + margin-left: calc(10px * 3); +} + +@keyframes blink { + 0% { + opacity: .1; + } + + 20% { + opacity: 1; + } + + 100% { + opacity: .1; + } +} + +#chat-tab .generating { + display: none !important; +} + +.hover-element { + position: relative; + font-size: 24px; +} + +.hover-menu { + display: none; + position: absolute; + bottom: 80%; + left: 0; + background-color: var(--background-fill-secondary); + box-shadow: 0 0 10px rgb(0 0 0 / 50%); + z-index: 10000; + min-width: 330px; + flex-direction: column; +} + +.hover-menu button { + width: 100%; + background: transparent !important; + border-radius: 0 !important; + justify-content: space-between; + margin: 0 !important; + height: 36px; +} + +.hover-menu button:not(#clear-history-confirm) { + border-bottom: 0 !important; +} + +.hover-menu button:not(#clear-history-confirm):last-child { + border-bottom: var(--button-border-width) solid var(--button-secondary-border-color) !important; +} + +.hover-menu button:hover { + background: var(--button-secondary-background-fill-hover) !important; +} + +.transparent-substring { + opacity: 0.333; +} + +#chat-tab:not(.old-ui) #chat-buttons { + display: none !important; +} + +#gr-hover-container { + min-width: 0 !important; + display: flex; + flex-direction: column-reverse; + padding-right: 20px; + padding-bottom: 3px; + flex-grow: 0 !important; +} + +#generate-stop-container { + min-width: 0 !important; + display: flex; + flex-direction: column-reverse; + padding-bottom: 3px; + flex: 0 auto !important; +} + +#chat-input-container { + min-width: 0 !important; +} + +#chat-input-container > .form { + background: transparent; + border: none; +} + +#chat-input-row { + padding-bottom: 20px; +} + +.old-ui #chat-input-row, #chat-input-row.bigchat { + padding-bottom: 0 !important; +} + +#chat-col { + padding-bottom: 100px; +} + +.old-ui #chat-col, #chat-col.bigchat { + padding-bottom: 80px !important; +} + +.old-ui #chat-buttons #clear-history-confirm { + order: -1; +} + +.chat ol, .chat ul { + margin-top: 6px !important; +} + +/* ---------------------------------------------- + Past chats menus +---------------------------------------------- */ +#past-chats-row { + margin-bottom: calc( -1 * var(--layout-gap) ); +} + +#rename-row label { + margin-top: var(--layout-gap); +} + +/* ---------------------------------------------- + Keep dropdown menus above errored components +---------------------------------------------- */ +.options { + z-index: 100 !important; +} + +/* ---------------------------------------------- + Big profile picture for characters +---------------------------------------------- */ +.bigProfilePicture { + position: fixed; + bottom: 0; + left: 0; + width: calc((100vw - 880px - 120px) /2); +} + +.pfp_character:hover { + cursor: pointer; +} + +@media screen and (width <= 1300px) { + .bigProfilePicture { + display: none; + } +} diff --git a/python/llm/example/Text-Generation-WebUI/extensions/character_bias/script.py b/python/llm/example/Text-Generation-WebUI/extensions/character_bias/script.py new file mode 100644 index 00000000..b297f165 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/extensions/character_bias/script.py @@ -0,0 +1,93 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/extensions/character_bias/script.py + + +import os + +import gradio as gr + +# get the current directory of the script +current_dir = os.path.dirname(os.path.abspath(__file__)) + +# check if the bias_options.txt file exists, if not, create it +bias_file = os.path.join(current_dir, "bias_options.txt") +if not os.path.isfile(bias_file): + with open(bias_file, "w") as f: + f.write("*I am so happy*\n*I am so sad*\n*I am so excited*\n*I am so bored*\n*I am so angry*") + +# read bias options from the text file +with open(bias_file, "r") as f: + bias_options = [line.strip() for line in f.readlines()] + +params = { + "activate": True, + "bias string": " *I am so happy*", + "custom string": "", +} + + +def input_modifier(string): + """ + This function is applied to your text inputs before + they are fed into the model. + """ + return string + + +def output_modifier(string): + """ + This function is applied to the model outputs. + """ + return string + + +def bot_prefix_modifier(string): + """ + This function is only applied in chat mode. It modifies + the prefix text for the Bot and can be used to bias its + behavior. + """ + if params['activate']: + if params['custom string'].strip() != '': + return f'{string} {params["custom string"].strip()} ' + else: + return f'{string} {params["bias string"].strip()} ' + else: + return string + + +def ui(): + # Gradio elements + activate = gr.Checkbox(value=params['activate'], label='Activate character bias') + dropdown_string = gr.Dropdown(choices=bias_options, value=params["bias string"], label='Character bias', info='To edit the options in this dropdown edit the "bias_options.txt" file') + custom_string = gr.Textbox(value=params['custom string'], placeholder="Enter custom bias string", label="Custom Character Bias", info='If not empty, will be used instead of the value above') + + # Event functions to update the parameters in the backend + def update_bias_string(x): + if x: + params.update({"bias string": x}) + else: + params.update({"bias string": dropdown_string.get()}) + return x + + def update_custom_string(x): + params.update({"custom string": x}) + + dropdown_string.change(update_bias_string, dropdown_string, None) + custom_string.change(update_custom_string, custom_string, None) + activate.change(lambda x: params.update({"activate": x}), activate, None) diff --git a/python/llm/example/Text-Generation-WebUI/extensions/coqui_tts/harvard_sentences.txt b/python/llm/example/Text-Generation-WebUI/extensions/coqui_tts/harvard_sentences.txt new file mode 100644 index 00000000..958d7f3c --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/extensions/coqui_tts/harvard_sentences.txt @@ -0,0 +1,720 @@ +The birch canoe slid on the smooth planks. +Glue the sheet to the dark blue background. +It's easy to tell the depth of a well. +These days a chicken leg is a rare dish. +Rice is often served in round bowls. +The juice of lemons makes fine punch. +The box was thrown beside the parked truck. +The hogs were fed chopped corn and garbage. +Four hours of steady work faced us. +A large size in stockings is hard to sell. +The boy was there when the sun rose. +A rod is used to catch pink salmon. +The source of the huge river is the clear spring. +Kick the ball straight and follow through. +Help the woman get back to her feet. +A pot of tea helps to pass the evening. +Smoky fires lack flame and heat. +The soft cushion broke the man's fall. +The salt breeze came across from the sea. +The girl at the booth sold fifty bonds. +The small pup gnawed a hole in the sock. +The fish twisted and turned on the bent hook. +Press the pants and sew a button on the vest. +The swan dive was far short of perfect. +The beauty of the view stunned the young boy. +Two blue fish swam in the tank. +Her purse was full of useless trash. +The colt reared and threw the tall rider. +It snowed, rained, and hailed the same morning. +Read verse out loud for pleasure. +Hoist the load to your left shoulder. +Take the winding path to reach the lake. +Note closely the size of the gas tank. +Wipe the grease off his dirty face. +Mend the coat before you go out. +The wrist was badly strained and hung limp. +The stray cat gave birth to kittens. +The young girl gave no clear response. +The meal was cooked before the bell rang. +What joy there is in living. +A king ruled the state in the early days. +The ship was torn apart on the sharp reef. +Sickness kept him home the third week. +The wide road shimmered in the hot sun. +The lazy cow lay in the cool grass. +Lift the square stone over the fence. +The rope will bind the seven books at once. +Hop over the fence and plunge in. +The friendly gang left the drug store. +Mesh wire keeps chicks inside. +The frosty air passed through the coat. +The crooked maze failed to fool the mouse. +Adding fast leads to wrong sums. +The show was a flop from the very start. +A saw is a tool used for making boards. +The wagon moved on well oiled wheels. +March the soldiers past the next hill. +A cup of sugar makes sweet fudge. +Place a rosebush near the porch steps. +Both lost their lives in the raging storm. +We talked of the side show in the circus. +Use a pencil to write the first draft. +He ran half way to the hardware store. +The clock struck to mark the third period. +A small creek cut across the field. +Cars and busses stalled in snow drifts. +The set of china hit the floor with a crash. +This is a grand season for hikes on the road. +The dune rose from the edge of the water. +Those words were the cue for the actor to leave. +A yacht slid around the point into the bay. +The two met while playing on the sand. +The ink stain dried on the finished page. +The walled town was seized without a fight. +The lease ran out in sixteen weeks. +A tame squirrel makes a nice pet. +The horn of the car woke the sleeping cop. +The heart beat strongly and with firm strokes. +The pearl was worn in a thin silver ring. +The fruit peel was cut in thick slices. +The Navy attacked the big task force. +See the cat glaring at the scared mouse. +There are more than two factors here. +The hat brim was wide and too droopy. +The lawyer tried to lose his case. +The grass curled around the fence post. +Cut the pie into large parts. +Men strive but seldom get rich. +Always close the barn door tight. +He lay prone and hardly moved a limb. +The slush lay deep along the street. +A wisp of cloud hung in the blue air. +A pound of sugar costs more than eggs. +The fin was sharp and cut the clear water. +The play seems dull and quite stupid. +Bail the boat to stop it from sinking. +The term ended in late June that year. +A tusk is used to make costly gifts. +Ten pins were set in order. +The bill was paid every third week. +Oak is strong and also gives shade. +Cats and dogs each hate the other. +The pipe began to rust while new. +Open the crate but don't break the glass. +Add the sum to the product of these three. +Thieves who rob friends deserve jail. +The ripe taste of cheese improves with age. +Act on these orders with great speed. +The hog crawled under the high fence. +Move the vat over the hot fire. +The bark of the pine tree was shiny and dark. +Leaves turn brown and yellow in the fall. +The pennant waved when the wind blew. +Split the log with a quick, sharp blow. +Burn peat after the logs give out. +He ordered peach pie with ice cream. +Weave the carpet on the right hand side. +Hemp is a weed found in parts of the tropics. +A lame back kept his score low. +We find joy in the simplest things. +Type out three lists of orders. +The harder he tried the less he got done. +The boss ran the show with a watchful eye. +The cup cracked and spilled its contents. +Paste can cleanse the most dirty brass. +The slang word for raw whiskey is booze. +It caught its hind paw in a rusty trap. +The wharf could be seen at the farther shore. +Feel the heat of the weak dying flame. +The tiny girl took off her hat. +A cramp is no small danger on a swim. +He said the same phrase thirty times. +Pluck the bright rose without leaves. +Two plus seven is less than ten. +The glow deepened in the eyes of the sweet girl. +Bring your problems to the wise chief. +Write a fond note to the friend you cherish. +Clothes and lodging are free to new men. +We frown when events take a bad turn. +Port is a strong wine with a smoky taste. +The young kid jumped the rusty gate. +Guess the results from the first scores. +A salt pickle tastes fine with ham. +The just claim got the right verdict. +These thistles bend in a high wind. +Pure bred poodles have curls. +The tree top waved in a graceful way. +The spot on the blotter was made by green ink. +Mud was spattered on the front of his white shirt. +The cigar burned a hole in the desk top. +The empty flask stood on the tin tray. +A speedy man can beat this track mark. +He broke a new shoelace that day. +The coffee stand is too high for the couch. +The urge to write short stories is rare. +The pencils have all been used. +The pirates seized the crew of the lost ship. +We tried to replace the coin but failed. +She sewed the torn coat quite neatly. +The sofa cushion is red and of light weight. +The jacket hung on the back of the wide chair. +At that high level the air is pure. +Drop the two when you add the figures. +A filing case is now hard to buy. +An abrupt start does not win the prize. +Wood is best for making toys and blocks. +The office paint was a dull, sad tan. +He knew the skill of the great young actress. +A rag will soak up spilled water. +A shower of dirt fell from the hot pipes. +Steam hissed from the broken valve. +The child almost hurt the small dog. +There was a sound of dry leaves outside. +The sky that morning was clear and bright blue. +Torn scraps littered the stone floor. +Sunday is the best part of the week. +The doctor cured him with these pills. +The new girl was fired today at noon. +They felt gay when the ship arrived in port. +Add the store's account to the last cent. +Acid burns holes in wool cloth. +Fairy tales should be fun to write. +Eight miles of woodland burned to waste. +The third act was dull and tired the players. +A young child should not suffer fright. +Add the column and put the sum here. +We admire and love a good cook. +There the flood mark is ten inches. +He carved a head from the round block of marble. +She has a smart way of wearing clothes. +The fruit of a fig tree is apple-shaped. +Corn cobs can be used to kindle a fire. +Where were they when the noise started. +The paper box is full of thumb tacks. +Sell your gift to a buyer at a good gain. +The tongs lay beside the ice pail. +The petals fall with the next puff of wind. +Bring your best compass to the third class. +They could laugh although they were sad. +Farmers came in to thresh the oat crop. +The brown house was on fire to the attic. +The lure is used to catch trout and flounder. +Float the soap on top of the bath water. +A blue crane is a tall wading bird. +A fresh start will work such wonders. +The club rented the rink for the fifth night. +After the dance, they went straight home. +The hostess taught the new maid to serve. +He wrote his last novel there at the inn. +Even the worst will beat his low score. +The cement had dried when he moved it. +The loss of the second ship was hard to take. +The fly made its way along the wall. +Do that with a wooden stick. +Live wires should be kept covered. +The large house had hot water taps. +It is hard to erase blue or red ink. +Write at once or you may forget it. +The doorknob was made of bright clean brass. +The wreck occurred by the bank on Main Street. +A pencil with black lead writes best. +Coax a young calf to drink from a bucket. +Schools for ladies teach charm and grace. +The lamp shone with a steady green flame. +They took the axe and the saw to the forest. +The ancient coin was quite dull and worn. +The shaky barn fell with a loud crash. +Jazz and swing fans like fast music. +Rake the rubbish up and then burn it. +Slash the gold cloth into fine ribbons. +Try to have the court decide the case. +They are pushed back each time they attack. +He broke his ties with groups of former friends. +They floated on the raft to sun their white backs. +The map had an X that meant nothing. +Whitings are small fish caught in nets. +Some ads serve to cheat buyers. +Jerk the rope and the bell rings weakly. +A waxed floor makes us lose balance. +Madam, this is the best brand of corn. +On the islands the sea breeze is soft and mild. +The play began as soon as we sat down. +This will lead the world to more sound and fury. +Add salt before you fry the egg. +The rush for funds reached its peak Tuesday. +The birch looked stark white and lonesome. +The box is held by a bright red snapper. +To make pure ice, you freeze water. +The first worm gets snapped early. +Jump the fence and hurry up the bank. +Yell and clap as the curtain slides back. +They are men who walk the middle of the road. +Both brothers wear the same size. +In some form or other we need fun. +The prince ordered his head chopped off. +The houses are built of red clay bricks. +Ducks fly north but lack a compass. +Fruit flavors are used in fizz drinks. +These pills do less good than others. +Canned pears lack full flavor. +The dark pot hung in the front closet. +Carry the pail to the wall and spill it there. +The train brought our hero to the big town. +We are sure that one war is enough. +Gray paint stretched for miles around. +The rude laugh filled the empty room. +High seats are best for football fans. +Tea served from the brown jug is tasty. +A dash of pepper spoils beef stew. +A zestful food is the hot-cross bun. +The horse trotted around the field at a brisk pace. +Find the twin who stole the pearl necklace. +Cut the cord that binds the box tightly. +The red tape bound the smuggled food. +Look in the corner to find the tan shirt. +The cold drizzle will halt the bond drive. +Nine men were hired to dig the ruins. +The junk yard had a mouldy smell. +The flint sputtered and lit a pine torch. +Soak the cloth and drown the sharp odor. +The shelves were bare of both jam or crackers. +A joy to every child is the swan boat. +All sat frozen and watched the screen. +A cloud of dust stung his tender eyes. +To reach the end he needs much courage. +Shape the clay gently into block form. +A ridge on a smooth surface is a bump or flaw. +Hedge apples may stain your hands green. +Quench your thirst, then eat the crackers. +Tight curls get limp on rainy days. +The mute muffled the high tones of the horn. +The gold ring fits only a pierced ear. +The old pan was covered with hard fudge. +Watch the log float in the wide river. +The node on the stalk of wheat grew daily. +The heap of fallen leaves was set on fire. +Write fast if you want to finish early. +His shirt was clean but one button was gone. +The barrel of beer was a brew of malt and hops. +Tin cans are absent from store shelves. +Slide the box into that empty space. +The plant grew large and green in the window. +The beam dropped down on the workmen's head. +Pink clouds floated with the breeze. +She danced like a swan, tall and graceful. +The tube was blown and the tire flat and useless. +It is late morning on the old wall clock. +Let's all join as we sing the last chorus. +The last switch cannot be turned off. +The fight will end in just six minutes. +The store walls were lined with colored frocks. +The peace league met to discuss their plans. +The rise to fame of a person takes luck. +Paper is scarce, so write with much care. +The quick fox jumped on the sleeping cat. +The nozzle of the fire hose was bright brass. +Screw the round cap on as tight as needed. +Time brings us many changes. +The purple tie was ten years old. +Men think and plan and sometimes act. +Fill the ink jar with sticky glue. +He smoke a big pipe with strong contents. +We need grain to keep our mules healthy. +Pack the records in a neat thin case. +The crunch of feet in the snow was the only sound. +The copper bowl shone in the sun's rays. +Boards will warp unless kept dry. +The plush chair leaned against the wall. +Glass will clink when struck by metal. +Bathe and relax in the cool green grass. +Nine rows of soldiers stood in line. +The beach is dry and shallow at low tide. +The idea is to sew both edges straight. +The kitten chased the dog down the street. +Pages bound in cloth make a book. +Try to trace the fine lines of the painting. +Women form less than half of the group. +The zones merge in the central part of town. +A gem in the rough needs work to polish. +Code is used when secrets are sent. +Most of the news is easy for us to hear. +He used the lathe to make brass objects. +The vane on top of the pole revolved in the wind. +Mince pie is a dish served to children. +The clan gathered on each dull night. +Let it burn, it gives us warmth and comfort. +A castle built from sand fails to endure. +A child's wit saved the day for us. +Tack the strip of carpet to the worn floor. +Next Tuesday we must vote. +Pour the stew from the pot into the plate. +Each penny shone like new. +The man went to the woods to gather sticks. +The dirt piles were lines along the road. +The logs fell and tumbled into the clear stream. +Just hoist it up and take it away. +A ripe plum is fit for a king's palate. +Our plans right now are hazy. +Brass rings are sold by these natives. +It takes a good trap to capture a bear. +Feed the white mouse some flower seeds. +The thaw came early and freed the stream. +He took the lead and kept it the whole distance. +The key you designed will fit the lock. +Plead to the council to free the poor thief. +Better hash is made of rare beef. +This plank was made for walking on. +The lake sparkled in the red hot sun. +He crawled with care along the ledge. +Tend the sheep while the dog wanders. +It takes a lot of help to finish these. +Mark the spot with a sign painted red. +Take two shares as a fair profit. +The fur of cats goes by many names. +North winds bring colds and fevers. +He asks no person to vouch for him. +Go now and come here later. +A sash of gold silk will trim her dress. +Soap can wash most dirt away. +That move means the game is over. +He wrote down a long list of items. +A siege will crack the strong defense. +Grape juice and water mix well. +Roads are paved with sticky tar. +Fake stones shine but cost little. +The drip of the rain made a pleasant sound. +Smoke poured out of every crack. +Serve the hot rum to the tired heroes. +Much of the story makes good sense. +The sun came up to light the eastern sky. +Heave the line over the port side. +A lathe cuts and trims any wood. +It's a dense crowd in two distinct ways. +His hip struck the knee of the next player. +The stale smell of old beer lingers. +The desk was firm on the shaky floor. +It takes heat to bring out the odor. +Beef is scarcer than some lamb. +Raise the sail and steer the ship northward. +A cone costs five cents on Mondays. +A pod is what peas always grow in. +Jerk the dart from the cork target. +No cement will hold hard wood. +We now have a new base for shipping. +A list of names is carved around the base. +The sheep were led home by a dog. +Three for a dime, the young peddler cried. +The sense of smell is better than that of touch. +No hardship seemed to keep him sad. +Grace makes up for lack of beauty. +Nudge gently but wake her now. +The news struck doubt into restless minds. +Once we stood beside the shore. +A chink in the wall allowed a draft to blow. +Fasten two pins on each side. +A cold dip restores health and zest. +He takes the oath of office each March. +The sand drifts over the sill of the old house. +The point of the steel pen was bent and twisted. +There is a lag between thought and act. +Seed is needed to plant the spring corn. +Draw the chart with heavy black lines. +The boy owed his pal thirty cents. +The chap slipped into the crowd and was lost. +Hats are worn to tea and not to dinner. +The ramp led up to the wide highway. +Beat the dust from the rug onto the lawn. +Say it slowly but make it ring clear. +The straw nest housed five robins. +Screen the porch with woven straw mats. +This horse will nose his way to the finish. +The dry wax protects the deep scratch. +He picked up the dice for a second roll. +These coins will be needed to pay his debt. +The nag pulled the frail cart along. +Twist the valve and release hot steam. +The vamp of the shoe had a gold buckle. +The smell of burned rags itches my nose. +New pants lack cuffs and pockets. +The marsh will freeze when cold enough. +They slice the sausage thin with a knife. +The bloom of the rose lasts a few days. +A gray mare walked before the colt. +Breakfast buns are fine with a hot drink. +Bottles hold four kinds of rum. +The man wore a feather in his felt hat. +He wheeled the bike past the winding road. +Drop the ashes on the worn old rug. +The desk and both chairs were painted tan. +Throw out the used paper cup and plate. +A clean neck means a neat collar. +The couch cover and hall drapes were blue. +The stems of the tall glasses cracked and broke. +The wall phone rang loud and often. +The clothes dried on a thin wooden rack. +Turn on the lantern which gives us light. +The cleat sank deeply into the soft turf. +The bills were mailed promptly on the tenth of the month. +To have is better than to wait and hope. +The price is fair for a good antique clock. +The music played on while they talked. +Dispense with a vest on a day like this. +The bunch of grapes was pressed into wine. +He sent the figs, but kept the ripe cherries. +The hinge on the door creaked with old age. +The screen before the fire kept in the sparks. +Fly by night, and you waste little time. +Thick glasses helped him read the print. +Birth and death mark the limits of life. +The chair looked strong but had no bottom. +The kite flew wildly in the high wind. +A fur muff is stylish once more. +The tin box held priceless stones. +We need an end of all such matter. +The case was puzzling to the old and wise. +The bright lanterns were gay on the dark lawn. +We don't get much money but we have fun. +The youth drove with zest, but little skill. +Five years he lived with a shaggy dog. +A fence cuts through the corner lot. +The way to save money is not to spend much. +Shut the hatch before the waves push it in. +The odor of spring makes young hearts jump. +Crack the walnut with your sharp side teeth. +He offered proof in the form of a large chart. +Send the stuff in a thick paper bag. +A quart of milk is water for the most part. +They told wild tales to frighten him. +The three story house was built of stone. +In the rear of the ground floor was a large passage. +A man in a blue sweater sat at the desk. +Oats are a food eaten by horse and man. +Their eyelids droop for want of sleep. +A sip of tea revives his tired friend. +There are many ways to do these things. +Tuck the sheet under the edge of the mat. +A force equal to that would move the earth. +We like to see clear weather. +The work of the tailor is seen on each side. +Take a chance and win a china doll. +Shake the dust from your shoes, stranger. +She was kind to sick old people. +The square wooden crate was packed to be shipped. +The dusty bench stood by the stone wall. +We dress to suit the weather of most days. +Smile when you say nasty words. +A bowl of rice is free with chicken stew. +The water in this well is a source of good health. +Take shelter in this tent, but keep still. +That guy is the writer of a few banned books. +The little tales they tell are false. +The door was barred, locked, and bolted as well. +Ripe pears are fit for a queen's table. +A big wet stain was on the round carpet. +The kite dipped and swayed, but stayed aloft. +The pleasant hours fly by much too soon. +The room was crowded with a wild mob. +This strong arm shall shield your honor. +She blushed when he gave her a white orchid. +The beetle droned in the hot June sun. +Press the pedal with your left foot. +Neat plans fail without luck. +The black trunk fell from the landing. +The bank pressed for payment of the debt. +The theft of the pearl pin was kept secret. +Shake hands with this friendly child. +The vast space stretched into the far distance. +A rich farm is rare in this sandy waste. +His wide grin earned many friends. +Flax makes a fine brand of paper. +Hurdle the pit with the aid of a long pole. +A strong bid may scare your partner stiff. +Even a just cause needs power to win. +Peep under the tent and see the clowns. +The leaf drifts along with a slow spin. +Cheap clothes are flashy but don't last. +A thing of small note can cause despair. +Flood the mails with requests for this book. +A thick coat of black paint covered all. +The pencil was cut to be sharp at both ends. +Those last words were a strong statement. +He wrote his name boldly at the top of the sheet. +Dill pickles are sour but taste fine. +Down that road is the way to the grain farmer. +Either mud or dust are found at all times. +The best method is to fix it in place with clips. +If you mumble your speech will be lost. +At night the alarm roused him from a deep sleep. +Read just what the meter says. +Fill your pack with bright trinkets for the poor. +The small red neon lamp went out. +Clams are small, round, soft, and tasty. +The fan whirled its round blades softly. +The line where the edges join was clean. +Breathe deep and smell the piny air. +It matters not if he reads these words or those. +A brown leather bag hung from its strap. +A toad and a frog are hard to tell apart. +A white silk jacket goes with any shoes. +A break in the dam almost caused a flood. +Paint the sockets in the wall dull green. +The child crawled into the dense grass. +Bribes fail where honest men work. +Trample the spark, else the flames will spread. +The hilt of the sword was carved with fine designs. +A round hole was drilled through the thin board. +Footprints showed the path he took up the beach. +She was waiting at my front lawn. +A vent near the edge brought in fresh air. +Prod the old mule with a crooked stick. +It is a band of steel three inches wide. +The pipe ran almost the length of the ditch. +It was hidden from sight by a mass of leaves and shrubs. +The weight of the package was seen on the high scale. +Wake and rise, and step into the green outdoors. +The green light in the brown box flickered. +The brass tube circled the high wall. +The lobes of her ears were pierced to hold rings. +Hold the hammer near the end to drive the nail. +Next Sunday is the twelfth of the month. +Every word and phrase he speaks is true. +He put his last cartridge into the gun and fired. +They took their kids from the public school. +Drive the screw straight into the wood. +Keep the hatch tight and the watch constant. +Sever the twine with a quick snip of the knife. +Paper will dry out when wet. +Slide the catch back and open the desk. +Help the weak to preserve their strength. +A sullen smile gets few friends. +Stop whistling and watch the boys march. +Jerk the cord, and out tumbles the gold. +Slide the tray across the glass top. +The cloud moved in a stately way and was gone. +Light maple makes for a swell room. +Set the piece here and say nothing. +Dull stories make her laugh. +A stiff cord will do to fasten your shoe. +Get the trust fund to the bank early. +Choose between the high road and the low. +A plea for funds seems to come again. +He lent his coat to the tall gaunt stranger. +There is a strong chance it will happen once more. +The duke left the park in a silver coach. +Greet the new guests and leave quickly. +When the frost has come it is time for turkey. +Sweet words work better than fierce. +A thin stripe runs down the middle. +A six comes up more often than a ten. +Lush fern grow on the lofty rocks. +The ram scared the school children off. +The team with the best timing looks good. +The farmer swapped his horse for a brown ox. +Sit on the perch and tell the others what to do. +A steep trail is painful for our feet. +The early phase of life moves fast. +Green moss grows on the northern side. +Tea in thin china has a sweet taste. +Pitch the straw through the door of the stable. +The latch on the back gate needed a nail. +The goose was brought straight from the old market. +The sink is the thing in which we pile dishes. +A whiff of it will cure the most stubborn cold. +The facts don't always show who is right. +She flaps her cape as she parades the street. +The loss of the cruiser was a blow to the fleet. +Loop the braid to the left and then over. +Plead with the lawyer to drop the lost cause. +Calves thrive on tender spring grass. +Post no bills on this office wall. +Tear a thin sheet from the yellow pad. +A cruise in warm waters in a sleek yacht is fun. +A streak of color ran down the left edge. +It was done before the boy could see it. +Crouch before you jump or miss the mark. +Pack the kits and don't forget the salt. +The square peg will settle in the round hole. +Fine soap saves tender skin. +Poached eggs and tea must suffice. +Bad nerves are jangled by a door slam. +Ship maps are different from those for planes. +Dimes showered down from all sides. +They sang the same tunes at each party. +The sky in the west is tinged with orange red. +The pods of peas ferment in bare fields. +The horse balked and threw the tall rider. +The hitch between the horse and cart broke. +Pile the coal high in the shed corner. +A gold vase is both rare and costly. +The knife was hung inside its bright sheath. +The rarest spice comes from the far East. +The roof should be tilted at a sharp slant. +A smatter of French is worse than none. +The mule trod the treadmill day and night. +The aim of the contest is to raise a great fund. +To send it now in large amounts is bad. +There is a fine hard tang in salty air. +Cod is the main business of the north shore. +The slab was hewn from heavy blocks of slate. +Dunk the stale biscuits into strong drink. +Hang tinsel from both branches. +Cap the jar with a tight brass cover. +The poor boy missed the boat again. +Be sure to set the lamp firmly in the hole. +Pick a card and slip it under the pack. +A round mat will cover the dull spot. +The first part of the plan needs changing. +A good book informs of what we ought to know. +The mail comes in three batches per day. +You cannot brew tea in a cold pot. +Dots of light betrayed the black cat. +Put the chart on the mantel and tack it down. +The night shift men rate extra pay. +The red paper brightened the dim stage. +See the player scoot to third base. +Slide the bill between the two leaves. +Many hands help get the job done. +We don't like to admit our small faults. +No doubt about the way the wind blows. +Dig deep in the earth for pirate's gold. +The steady drip is worse than a drenching rain. +A flat pack takes less luggage space. +Green ice frosted the punch bowl. +A stuffed chair slipped from the moving van. +The stitch will serve but needs to be shortened. +A thin book fits in the side pocket. +The gloss on top made it unfit to read. +The hail pattered on the burnt brown grass. +Seven seals were stamped on great sheets. +Our troops are set to strike heavy blows. +The store was jammed before the sale could start. +It was a bad error on the part of the new judge. +One step more and the board will collapse. +Take the match and strike it against your shoe. +The pot boiled, but the contents failed to jell. +The baby puts his right foot in his mouth. +The bombs left most of the town in ruins. +Stop and stare at the hard working man. +The streets are narrow and full of sharp turns. +The pup jerked the leash as he saw a feline shape. +Open your book to the first page. +Fish evade the net and swim off. +Dip the pail once and let it settle. +Will you please answer that phone. +The big red apple fell to the ground. +The curtain rose and the show was on. +The young prince became heir to the throne. +He sent the boy on a short errand. +Leave now and you will arrive on time. +The corner store was robbed last night. +A gold ring will please most any girl. +The long journey home took a year. +She saw a cat in the neighbor's house. +A pink shell was found on the sandy beach. +Small children came to see him. +The grass and bushes were wet with dew. +The blind man counted his old coins. +A severe storm tore down the barn. +She called his name many times. +When you hear the bell, come quickly. \ No newline at end of file diff --git a/python/llm/example/Text-Generation-WebUI/extensions/coqui_tts/languages.json b/python/llm/example/Text-Generation-WebUI/extensions/coqui_tts/languages.json new file mode 100644 index 00000000..3895d7f8 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/extensions/coqui_tts/languages.json @@ -0,0 +1,18 @@ +{ + "Arabic": "ar", + "Chinese": "zh-cn", + "Czech": "cs", + "Dutch": "nl", + "English": "en", + "French": "fr", + "German": "de", + "Hungarian": "hu", + "Italian": "it", + "Japanese": "ja", + "Korean": "ko", + "Polish": "pl", + "Portuguese": "pt", + "Russian": "ru", + "Spanish": "es", + "Turkish": "tr" +} \ No newline at end of file diff --git a/python/llm/example/Text-Generation-WebUI/extensions/coqui_tts/requirements.txt b/python/llm/example/Text-Generation-WebUI/extensions/coqui_tts/requirements.txt new file mode 100644 index 00000000..747f99a0 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/extensions/coqui_tts/requirements.txt @@ -0,0 +1 @@ +TTS==0.21.* \ No newline at end of file diff --git a/python/llm/example/Text-Generation-WebUI/extensions/coqui_tts/script.py b/python/llm/example/Text-Generation-WebUI/extensions/coqui_tts/script.py new file mode 100644 index 00000000..9a8e247d --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/extensions/coqui_tts/script.py @@ -0,0 +1,246 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/extensions/coqui_tts/script.py + + +import html +import json +import os +import random +import time +from pathlib import Path + +import gradio as gr +from TTS.api import TTS +from TTS.utils.synthesizer import Synthesizer + +from modules import chat, shared, ui_chat +from modules.ui import create_refresh_button +from modules.utils import gradio + +os.environ["COQUI_TOS_AGREED"] = "1" + +params = { + "activate": True, + "autoplay": True, + "show_text": False, + "remove_trailing_dots": False, + "voice": "female_01.wav", + "language": "English", + "model_name": "tts_models/multilingual/multi-dataset/xtts_v2", + "device": "cuda" +} + +this_dir = str(Path(__file__).parent.resolve()) +model = None +with open(Path(f"{this_dir}/languages.json"), encoding='utf8') as f: + languages = json.load(f) + + +def get_available_voices(): + return sorted([voice.name for voice in Path(f"{this_dir}/voices").glob("*.wav")]) + + +def preprocess(raw_input): + raw_input = html.unescape(raw_input) + # raw_input = raw_input.strip("\"") + return raw_input + + +def new_split_into_sentences(self, text): + sentences = self.seg.segment(text) + if params['remove_trailing_dots']: + sentences_without_dots = [] + for sentence in sentences: + if sentence.endswith('.') and not sentence.endswith('...'): + sentence = sentence[:-1] + + sentences_without_dots.append(sentence) + + return sentences_without_dots + else: + return sentences + + +Synthesizer.split_into_sentences = new_split_into_sentences + + +def load_model(): + model = TTS(params["model_name"]).to(params["device"]) + return model + + +def remove_tts_from_history(history): + for i, entry in enumerate(history['internal']): + history['visible'][i] = [history['visible'][i][0], entry[1]] + + return history + + +def toggle_text_in_history(history): + for i, entry in enumerate(history['visible']): + visible_reply = entry[1] + if visible_reply.startswith('')[0]}\n\n{reply}"] + else: + history['visible'][i] = [history['visible'][i][0], f"{visible_reply.split('')[0]}"] + + return history + + +def random_sentence(): + with open(Path("extensions/coqui_tts/harvard_sentences.txt")) as f: + return random.choice(list(f)) + + +def voice_preview(string): + string = html.unescape(string) or random_sentence() + + output_file = Path('extensions/coqui_tts/outputs/voice_preview.wav') + model.tts_to_file( + text=string, + file_path=output_file, + speaker_wav=[f"{this_dir}/voices/{params['voice']}"], + language=languages[params["language"]] + ) + + return f'' + + +def history_modifier(history): + # Remove autoplay from the last reply + if len(history['internal']) > 0: + history['visible'][-1] = [ + history['visible'][-1][0], + history['visible'][-1][1].replace('controls autoplay>', 'controls>') + ] + + return history + + +def state_modifier(state): + if not params['activate']: + return state + + state['stream'] = False + return state + + +def input_modifier(string, state): + if not params['activate']: + return string + + shared.processing_message = "*Is recording a voice message...*" + return string + + +def output_modifier(string, state): + if not params['activate']: + return string + + original_string = string + string = preprocess(html.unescape(string)) + if string == '': + string = '*Empty reply, try regenerating*' + else: + output_file = Path(f'extensions/coqui_tts/outputs/{state["character_menu"]}_{int(time.time())}.wav') + model.tts_to_file( + text=string, + file_path=output_file, + speaker_wav=[f"{this_dir}/voices/{params['voice']}"], + language=languages[params["language"]] + ) + + autoplay = 'autoplay' if params['autoplay'] else '' + string = f'' + if params['show_text']: + string += f'\n\n{original_string}' + + shared.processing_message = "*Is typing...*" + return string + + +def custom_css(): + path_to_css = Path(f"{this_dir}/style.css") + return open(path_to_css, 'r').read() + + +def setup(): + global model + print("[XTTS] Loading XTTS...") + model = load_model() + print("[XTTS] Done!") + Path(f"{this_dir}/outputs").mkdir(parents=True, exist_ok=True) + + +def ui(): + with gr.Accordion("Coqui TTS (XTTSv2)"): + with gr.Row(): + activate = gr.Checkbox(value=params['activate'], label='Activate TTS') + autoplay = gr.Checkbox(value=params['autoplay'], label='Play TTS automatically') + + with gr.Row(): + show_text = gr.Checkbox(value=params['show_text'], label='Show message text under audio player') + remove_trailing_dots = gr.Checkbox(value=params['remove_trailing_dots'], label='Remove trailing "." from text segments before converting to audio') + + with gr.Row(): + with gr.Row(): + voice = gr.Dropdown(get_available_voices(), label="Voice wav", value=params["voice"]) + create_refresh_button(voice, lambda: None, lambda: {'choices': get_available_voices(), 'value': params["voice"]}, 'refresh-button') + + language = gr.Dropdown(languages.keys(), label="Language", value=params["language"]) + + with gr.Row(): + preview_text = gr.Text(show_label=False, placeholder="Preview text", elem_id="silero_preview_text") + preview_play = gr.Button("Preview") + preview_audio = gr.HTML(visible=False) + + with gr.Row(): + convert = gr.Button('Permanently replace audios with the message texts') + convert_cancel = gr.Button('Cancel', visible=False) + convert_confirm = gr.Button('Confirm (cannot be undone)', variant="stop", visible=False) + + # Convert history with confirmation + convert_arr = [convert_confirm, convert, convert_cancel] + convert.click(lambda: [gr.update(visible=True), gr.update(visible=False), gr.update(visible=True)], None, convert_arr) + convert_confirm.click( + lambda: [gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)], None, convert_arr).then( + remove_tts_from_history, gradio('history'), gradio('history')).then( + chat.save_history, gradio('history', 'unique_id', 'character_menu', 'mode'), None).then( + chat.redraw_html, gradio(ui_chat.reload_arr), gradio('display')) + + convert_cancel.click(lambda: [gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)], None, convert_arr) + + # Toggle message text in history + show_text.change( + lambda x: params.update({"show_text": x}), show_text, None).then( + toggle_text_in_history, gradio('history'), gradio('history')).then( + chat.save_history, gradio('history', 'unique_id', 'character_menu', 'mode'), None).then( + chat.redraw_html, gradio(ui_chat.reload_arr), gradio('display')) + + # Event functions to update the parameters in the backend + activate.change(lambda x: params.update({"activate": x}), activate, None) + autoplay.change(lambda x: params.update({"autoplay": x}), autoplay, None) + remove_trailing_dots.change(lambda x: params.update({"remove_trailing_dots": x}), remove_trailing_dots, None) + voice.change(lambda x: params.update({"voice": x}), voice, None) + language.change(lambda x: params.update({"language": x}), language, None) + + # Play preview + preview_text.submit(voice_preview, preview_text, preview_audio) + preview_play.click(voice_preview, preview_text, preview_audio) diff --git a/python/llm/example/Text-Generation-WebUI/extensions/coqui_tts/style.css b/python/llm/example/Text-Generation-WebUI/extensions/coqui_tts/style.css new file mode 100644 index 00000000..2ab7aefb --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/extensions/coqui_tts/style.css @@ -0,0 +1,8 @@ +.SDAP .hires_opts input[type="number"] { + width: 6em !important; +} + +/* silero_tts preview */ +.form:has(> #silero_preview_text) { + min-width: 75% +} diff --git a/python/llm/example/Text-Generation-WebUI/extensions/coqui_tts/voices/arnold.wav b/python/llm/example/Text-Generation-WebUI/extensions/coqui_tts/voices/arnold.wav new file mode 100644 index 00000000..cccdf013 Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/extensions/coqui_tts/voices/arnold.wav differ diff --git a/python/llm/example/Text-Generation-WebUI/extensions/coqui_tts/voices/female_01.wav b/python/llm/example/Text-Generation-WebUI/extensions/coqui_tts/voices/female_01.wav new file mode 100644 index 00000000..6fd8a4ee Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/extensions/coqui_tts/voices/female_01.wav differ diff --git a/python/llm/example/Text-Generation-WebUI/extensions/coqui_tts/voices/female_02.wav b/python/llm/example/Text-Generation-WebUI/extensions/coqui_tts/voices/female_02.wav new file mode 100644 index 00000000..ecf14400 Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/extensions/coqui_tts/voices/female_02.wav differ diff --git a/python/llm/example/Text-Generation-WebUI/extensions/example/script.py b/python/llm/example/Text-Generation-WebUI/extensions/example/script.py new file mode 100644 index 00000000..f7f41a51 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/extensions/example/script.py @@ -0,0 +1,149 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/extensions/example/script.py + + +import gradio as gr +import torch +from transformers import LogitsProcessor + +from modules import chat, shared +from modules.text_generation import ( + decode, + encode, + generate_reply, +) + +params = { + "display_name": "Example Extension", + "is_tab": False, +} + +class MyLogits(LogitsProcessor): + """ + Manipulates the probabilities for the next token before it gets sampled. + Used in the logits_processor_modifier function below. + """ + def __init__(self): + pass + + def __call__(self, input_ids, scores): + # probs = torch.softmax(scores, dim=-1, dtype=torch.float) + # probs[0] /= probs[0].sum() + # scores = torch.log(probs / (1 - probs)) + return scores + +def history_modifier(history): + """ + Modifies the chat history. + Only used in chat mode. + """ + return history + +def state_modifier(state): + """ + Modifies the state variable, which is a dictionary containing the input + values in the UI like sliders and checkboxes. + """ + return state + +def chat_input_modifier(text, visible_text, state): + """ + Modifies the user input string in chat mode (visible_text). + You can also modify the internal representation of the user + input (text) to change how it will appear in the prompt. + """ + return text, visible_text + +def input_modifier(string, state, is_chat=False): + """ + In default/notebook modes, modifies the whole prompt. + + In chat mode, it is the same as chat_input_modifier but only applied + to "text", here called "string", and not to "visible_text". + """ + return string + +def bot_prefix_modifier(string, state): + """ + Modifies the prefix for the next bot reply in chat mode. + By default, the prefix will be something like "Bot Name:". + """ + return string + +def tokenizer_modifier(state, prompt, input_ids, input_embeds): + """ + Modifies the input ids and embeds. + Used by the multimodal extension to put image embeddings in the prompt. + Only used by loaders that use the transformers library for sampling. + """ + return prompt, input_ids, input_embeds + +def logits_processor_modifier(processor_list, input_ids): + """ + Adds logits processors to the list, allowing you to access and modify + the next token probabilities. + Only used by loaders that use the transformers library for sampling. + """ + processor_list.append(MyLogits()) + return processor_list + +def output_modifier(string, state, is_chat=False): + """ + Modifies the LLM output before it gets presented. + + In chat mode, the modified version goes into history['visible'], + and the original version goes into history['internal']. + """ + return string + +def custom_generate_chat_prompt(user_input, state, **kwargs): + """ + Replaces the function that generates the prompt from the chat history. + Only used in chat mode. + """ + result = chat.generate_chat_prompt(user_input, state, **kwargs) + return result + +def custom_css(): + """ + Returns a CSS string that gets appended to the CSS for the webui. + """ + return '' + +def custom_js(): + """ + Returns a javascript string that gets appended to the javascript + for the webui. + """ + return '' + +def setup(): + """ + Gets executed only once, when the extension is imported. + """ + pass + +def ui(): + """ + Gets executed when the UI is drawn. Custom gradio elements and + their corresponding event handlers should be defined here. + + To learn about gradio components, check out the docs: + https://gradio.app/docs/ + """ + pass diff --git a/python/llm/example/Text-Generation-WebUI/extensions/gallery/script.js b/python/llm/example/Text-Generation-WebUI/extensions/gallery/script.js new file mode 100644 index 00000000..9717aa67 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/extensions/gallery/script.js @@ -0,0 +1,40 @@ +let gallery_element = document.getElementById('gallery-extension'); +let chat_mode_element = document.getElementById('chat-mode'); + +let extensions_block = document.getElementById('extensions'); +let extensions_block_size = extensions_block.childNodes.length; +let gallery_only = (extensions_block_size == 5); + +function gotoFirstPage() { + const firstPageButton = gallery_element.querySelector('.paginate > button'); + if (firstPageButton) { + firstPageButton.click(); + } +} + +document.querySelector('.header_bar').addEventListener('click', function(event) { + if (event.target.tagName === 'BUTTON') { + const buttonText = event.target.textContent.trim(); + + let chat_visible = (buttonText == 'Chat'); + let default_visible = (buttonText == 'Default'); + let notebook_visible = (buttonText == 'Notebook'); + let chat_mode_visible = (chat_mode_element.offsetHeight > 0 && chat_mode_element.offsetWidth > 0); + + // Only show this extension in the Chat tab + if (chat_visible) { + if (chat_mode_visible) { + gallery_element.style.display = 'block'; + extensions_block.style.display = ''; + } else { + gallery_element.style.display = 'none'; + extensions_block.style.display = 'none'; + } + } else { + gallery_element.style.display = 'none'; + if (gallery_only) { + extensions_block.style.display = 'none'; + } + } + } +}); diff --git a/python/llm/example/Text-Generation-WebUI/extensions/gallery/script.py b/python/llm/example/Text-Generation-WebUI/extensions/gallery/script.py new file mode 100644 index 00000000..ae655651 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/extensions/gallery/script.py @@ -0,0 +1,148 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/extensions/gallery/script.py + + +from pathlib import Path + +import gradio as gr + +from modules.html_generator import get_image_cache +from modules.shared import gradio, settings + + +cards = [] + + +def generate_css(): + css = """ + .highlighted-border { + border-color: rgb(249, 115, 22) !important; + } + + .character-gallery > .gallery { + margin: 1rem 0; + display: grid !important; + grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); + grid-column-gap: 0.4rem; + grid-row-gap: 1.2rem; + } + + .character-gallery > .label { + display: none !important; + } + + .character-gallery button.gallery-item { + display: contents; + } + + .character-container { + cursor: pointer; + text-align: center; + position: relative; + opacity: 0.85; + } + + .character-container:hover { + opacity: 1; + } + + .character-container .placeholder, .character-container img { + width: 150px; + height: 200px; + background-color: gray; + object-fit: cover; + margin: 0 auto; + border-radius: 1rem; + border: 3px solid white; + box-shadow: 3px 3px 6px 0px rgb(0 0 0 / 50%); + } + + .character-name { + margin-top: 0.3rem; + display: block; + font-size: 1.2rem; + font-weight: 600; + overflow-wrap: anywhere; + } + """ + return css + + +def generate_html(): + global cards + cards = [] + # Iterate through files in image folder + for file in sorted(Path("characters").glob("*")): + if file.suffix in [".json", ".yml", ".yaml"]: + character = file.stem + container_html = '
' + image_html = "
" + + for path in [Path(f"characters/{character}.{extension}") for extension in ['png', 'jpg', 'jpeg']]: + if path.exists(): + image_html = f'' + break + + container_html += f'{image_html} {character}' + container_html += "
" + cards.append([container_html, character]) + + return cards + + +def filter_cards(filter_str=''): + if filter_str == '': + return cards + + filter_upper = filter_str.upper() + return [k for k in cards if filter_upper in k[1].upper()] + + +def select_character(evt: gr.SelectData): + return (evt.value[1]) + + +def custom_js(): + path_to_js = Path(__file__).parent.resolve() / 'script.js' + return open(path_to_js, 'r').read() + + +def ui(): + with gr.Accordion("Character gallery", open=settings["gallery-open"], elem_id='gallery-extension'): + gr.HTML(value="") + with gr.Row(): + filter_box = gr.Textbox(label='', placeholder='Filter', lines=1, max_lines=1, container=False, elem_id='gallery-filter-box') + gr.ClearButton(filter_box, value='🗑️', elem_classes='refresh-button') + update = gr.Button("Refresh", elem_classes='refresh-button') + + gallery = gr.Dataset( + components=[gr.HTML(visible=False)], + label="", + samples=generate_html(), + elem_classes=["character-gallery"], + samples_per_page=settings["gallery-items_per_page"] + ) + + filter_box.change(lambda: None, None, None, _js=f'() => {{{custom_js()}; gotoFirstPage()}}').success( + filter_cards, filter_box, gallery).then( + lambda x: gr.update(elem_classes='highlighted-border' if x != '' else ''), filter_box, filter_box, show_progress=False) + + update.click(generate_html, [], None).success( + filter_cards, filter_box, gallery) + + gallery.select(select_character, None, gradio['character_menu']) diff --git a/python/llm/example/Text-Generation-WebUI/extensions/google_translate/requirements.txt b/python/llm/example/Text-Generation-WebUI/extensions/google_translate/requirements.txt new file mode 100644 index 00000000..554a00df --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/extensions/google_translate/requirements.txt @@ -0,0 +1 @@ +deep-translator==1.9.2 diff --git a/python/llm/example/Text-Generation-WebUI/extensions/google_translate/script.py b/python/llm/example/Text-Generation-WebUI/extensions/google_translate/script.py new file mode 100644 index 00000000..2b29fecd --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/extensions/google_translate/script.py @@ -0,0 +1,78 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/extensions/google_translate/script.py + + +import html + +import gradio as gr +from deep_translator import GoogleTranslator + +params = { + "activate": True, + "language string": "ja", +} + +language_codes = {'Afrikaans': 'af', 'Albanian': 'sq', 'Amharic': 'am', 'Arabic': 'ar', 'Armenian': 'hy', 'Azerbaijani': 'az', 'Basque': 'eu', 'Belarusian': 'be', 'Bengali': 'bn', 'Bosnian': 'bs', 'Bulgarian': 'bg', 'Catalan': 'ca', 'Cebuano': 'ceb', 'Chinese (Simplified)': 'zh-CN', 'Chinese (Traditional)': 'zh-TW', 'Corsican': 'co', 'Croatian': 'hr', 'Czech': 'cs', 'Danish': 'da', 'Dutch': 'nl', 'English': 'en', 'Esperanto': 'eo', 'Estonian': 'et', 'Finnish': 'fi', 'French': 'fr', 'Frisian': 'fy', 'Galician': 'gl', 'Georgian': 'ka', 'German': 'de', 'Greek': 'el', 'Gujarati': 'gu', 'Haitian Creole': 'ht', 'Hausa': 'ha', 'Hawaiian': 'haw', 'Hebrew': 'iw', 'Hindi': 'hi', 'Hmong': 'hmn', 'Hungarian': 'hu', 'Icelandic': 'is', 'Igbo': 'ig', 'Indonesian': 'id', 'Irish': 'ga', 'Italian': 'it', 'Japanese': 'ja', 'Javanese': 'jw', 'Kannada': 'kn', 'Kazakh': 'kk', 'Khmer': 'km', 'Korean': 'ko', 'Kurdish': 'ku', 'Kyrgyz': 'ky', 'Lao': 'lo', 'Latin': 'la', 'Latvian': 'lv', 'Lithuanian': 'lt', 'Luxembourgish': 'lb', 'Macedonian': 'mk', 'Malagasy': 'mg', 'Malay': 'ms', 'Malayalam': 'ml', 'Maltese': 'mt', 'Maori': 'mi', 'Marathi': 'mr', 'Mongolian': 'mn', 'Myanmar (Burmese)': 'my', 'Nepali': 'ne', 'Norwegian': 'no', 'Nyanja (Chichewa)': 'ny', 'Pashto': 'ps', 'Persian': 'fa', 'Polish': 'pl', 'Portuguese (Portugal, Brazil)': 'pt', 'Punjabi': 'pa', 'Romanian': 'ro', 'Russian': 'ru', 'Samoan': 'sm', 'Scots Gaelic': 'gd', 'Serbian': 'sr', 'Sesotho': 'st', 'Shona': 'sn', 'Sindhi': 'sd', 'Sinhala (Sinhalese)': 'si', 'Slovak': 'sk', 'Slovenian': 'sl', 'Somali': 'so', 'Spanish': 'es', 'Sundanese': 'su', 'Swahili': 'sw', 'Swedish': 'sv', 'Tagalog (Filipino)': 'tl', 'Tajik': 'tg', 'Tamil': 'ta', 'Telugu': 'te', 'Thai': 'th', 'Turkish': 'tr', 'Ukrainian': 'uk', 'Urdu': 'ur', 'Uzbek': 'uz', 'Vietnamese': 'vi', 'Welsh': 'cy', 'Xhosa': 'xh', 'Yiddish': 'yi', 'Yoruba': 'yo', 'Zulu': 'zu'} + + +def input_modifier(string): + """ + This function is applied to your text inputs before + they are fed into the model. + """ + if not params['activate']: + return string + + return GoogleTranslator(source=params['language string'], target='en').translate(string) + + +def output_modifier(string): + """ + This function is applied to the model outputs. + """ + if not params['activate']: + return string + + translated_str = GoogleTranslator(source='en', target=params['language string']).translate(html.unescape(string)) + return html.escape(translated_str) + + +def bot_prefix_modifier(string): + """ + This function is only applied in chat mode. It modifies + the prefix text for the Bot and can be used to bias its + behavior. + """ + + return string + + +def ui(): + # Finding the language name from the language code to use as the default value + language_name = list(language_codes.keys())[list(language_codes.values()).index(params['language string'])] + + # Gradio elements + with gr.Row(): + activate = gr.Checkbox(value=params['activate'], label='Activate translation') + + with gr.Row(): + language = gr.Dropdown(value=language_name, choices=[k for k in language_codes], label='Language') + + # Event functions to update the parameters in the backend + activate.change(lambda x: params.update({"activate": x}), activate, None) + language.change(lambda x: params.update({"language string": language_codes[x]}), language, None) diff --git a/python/llm/example/Text-Generation-WebUI/extensions/long_replies/script.py b/python/llm/example/Text-Generation-WebUI/extensions/long_replies/script.py new file mode 100644 index 00000000..c0e52088 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/extensions/long_replies/script.py @@ -0,0 +1,162 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/extensions/long_replies/script.py + + +import torch +from modules import chat, shared +from modules.text_generation import ( + decode, + encode, + generate_reply, +) +from transformers import LogitsProcessor +import gradio as gr + +params = { + "display_name": "Long replies", + "is_tab": False, + "min_length": 120, +} + +initial_size = 0 + +class MyLogits(LogitsProcessor): + """ + Manipulates the probabilities for the next token before it gets sampled. + Used in the logits_processor_modifier function below. + """ + def __init__(self): + self.newline_id = shared.tokenizer.encode('\n')[-1] + pass + + def __call__(self, input_ids, scores): + if input_ids.shape[-1] - initial_size < params["min_length"]: + scores[...,self.newline_id] = -1000 + # scores[...,shared.tokenizer.eos_token_id] = -1000 + + # probs = torch.softmax(scores, dim=-1, dtype=torch.float) + # probs[0] /= probs[0].sum() + # scores = torch.log(probs / (1 - probs)) + return scores + +def history_modifier(history): + """ + Modifies the chat history. + Only used in chat mode. + """ + return history + +def state_modifier(state): + """ + Modifies the state variable, which is a dictionary containing the input + values in the UI like sliders and checkboxes. + """ + return state + +def chat_input_modifier(text, visible_text, state): + """ + Modifies the user input string in chat mode (visible_text). + You can also modify the internal representation of the user + input (text) to change how it will appear in the prompt. + """ + return text, visible_text + +def input_modifier(string, state): + """ + In default/notebook modes, modifies the whole prompt. + + In chat mode, it is the same as chat_input_modifier but only applied + to "text", here called "string", and not to "visible_text". + """ + return string + +def bot_prefix_modifier(string, state): + """ + Modifies the prefix for the next bot reply in chat mode. + By default, the prefix will be something like "Bot Name:". + """ + return string + +def tokenizer_modifier(state, prompt, input_ids, input_embeds): + """ + Modifies the input ids and embeds. + Used by the multimodal extension to put image embeddings in the prompt. + Only used by loaders that use the transformers library for sampling. + """ + + global initial_size + initial_size = input_ids.shape[-1] + + return prompt, input_ids, input_embeds + +def logits_processor_modifier(processor_list, input_ids): + """ + Adds logits processors to the list, allowing you to access and modify + the next token probabilities. + Only used by loaders that use the transformers library for sampling. + """ + processor_list.append(MyLogits()) + return processor_list + +def output_modifier(string, state): + """ + Modifies the LLM output before it gets presented. + + In chat mode, the modified version goes into history['visible'], + and the original version goes into history['internal']. + """ + return string + +def custom_generate_chat_prompt(user_input, state, **kwargs): + """ + Replaces the function that generates the prompt from the chat history. + Only used in chat mode. + """ + result = chat.generate_chat_prompt(user_input, state, **kwargs) + return result + +def custom_css(): + """ + Returns a CSS string that gets appended to the CSS for the webui. + """ + return '' + +def custom_js(): + """ + Returns a javascript string that gets appended to the javascript + for the webui. + """ + return '' + +def setup(): + """ + Gets executed only once, when the extension is imported. + """ + pass + +def ui(): + """ + Gets executed when the UI is drawn. Custom gradio elements and + their corresponding event handlers should be defined here. + + To learn about gradio components, check out the docs: + https://gradio.app/docs/ + """ + + min_length = gr.Slider(0, 800, step=10, value=params['min_length'], label='Minimum reply length') + min_length.change(lambda x: params.update({'min_length': x}), min_length, None) diff --git a/python/llm/example/Text-Generation-WebUI/grammars/arithmetic.gbnf b/python/llm/example/Text-Generation-WebUI/grammars/arithmetic.gbnf new file mode 100644 index 00000000..3aa95a9d --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/grammars/arithmetic.gbnf @@ -0,0 +1,6 @@ +root ::= (expr "=" ws term "\n")+ +expr ::= term ([-+*/] term)* +term ::= ident | num | "(" ws expr ")" ws +ident ::= [a-z] [a-z0-9_]* ws +num ::= [0-9]+ ws +ws ::= [ \t\n]* diff --git a/python/llm/example/Text-Generation-WebUI/grammars/c.gbnf b/python/llm/example/Text-Generation-WebUI/grammars/c.gbnf new file mode 100644 index 00000000..4a0331dd --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/grammars/c.gbnf @@ -0,0 +1,42 @@ +root ::= (declaration)* + +declaration ::= dataType identifier "(" parameter? ")" "{" statement* "}" + +dataType ::= "int" ws | "float" ws | "char" ws +identifier ::= [a-zA-Z_] [a-zA-Z_0-9]* + +parameter ::= dataType identifier + +statement ::= + ( dataType identifier ws "=" ws expression ";" ) | + ( identifier ws "=" ws expression ";" ) | + ( identifier ws "(" argList? ")" ";" ) | + ( "return" ws expression ";" ) | + ( "while" "(" condition ")" "{" statement* "}" ) | + ( "for" "(" forInit ";" ws condition ";" ws forUpdate ")" "{" statement* "}" ) | + ( "if" "(" condition ")" "{" statement* "}" ("else" "{" statement* "}")? ) | + ( singleLineComment ) | + ( multiLineComment ) + +forInit ::= dataType identifier ws "=" ws expression | identifier ws "=" ws expression +forUpdate ::= identifier ws "=" ws expression + +condition ::= expression relationOperator expression +relationOperator ::= ("<=" | "<" | "==" | "!=" | ">=" | ">") + +expression ::= term (("+" | "-") term)* +term ::= factor(("*" | "/") factor)* + +factor ::= identifier | number | unaryTerm | funcCall | parenExpression +unaryTerm ::= "-" factor +funcCall ::= identifier "(" argList? ")" +parenExpression ::= "(" ws expression ws ")" + +argList ::= expression ("," ws expression)* + +number ::= [0-9]+ + +singleLineComment ::= "//" [^\n]* "\n" +multiLineComment ::= "/*" ( [^*] | ("*" [^/]) )* "*/" + +ws ::= ([ \t\n]+) diff --git a/python/llm/example/Text-Generation-WebUI/grammars/chess.gbnf b/python/llm/example/Text-Generation-WebUI/grammars/chess.gbnf new file mode 100644 index 00000000..ef0fc1b0 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/grammars/chess.gbnf @@ -0,0 +1,13 @@ +# Specifies chess moves as a list in algebraic notation, using PGN conventions + +# Force first move to "1. ", then any 1-2 digit number after, relying on model to follow the pattern +root ::= "1. " move " " move "\n" ([1-9] [0-9]? ". " move " " move "\n")+ +move ::= (pawn | nonpawn | castle) [+#]? + +# piece type, optional file/rank, optional capture, dest file & rank +nonpawn ::= [NBKQR] [a-h]? [1-8]? "x"? [a-h] [1-8] + +# optional file & capture, dest file & rank, optional promotion +pawn ::= ([a-h] "x")? [a-h] [1-8] ("=" [NBKQR])? + +castle ::= "O-O" "-O"? diff --git a/python/llm/example/Text-Generation-WebUI/grammars/json.gbnf b/python/llm/example/Text-Generation-WebUI/grammars/json.gbnf new file mode 100644 index 00000000..108c6fb7 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/grammars/json.gbnf @@ -0,0 +1,14 @@ +root ::= object + +object ::= "{" ws ( string ":" ws value ("," ws string ":" ws value)* )? "}" + +value ::= object | array | string | number | ("true" | "false" | "null") ws + +array ::= "[" ws ( value ("," ws value)* )? "]" ws + +string ::= "\"" ( [a-zA-Z0-9] )* "\"" ws + +number ::= ("-"? ([0-9] | [1-9] [0-9]*)) ("." [0-9]+)? ([eE] [-+]? [0-9]+)? ws + + +ws ::= ([ \t\n] ws)? diff --git a/python/llm/example/Text-Generation-WebUI/grammars/json_w_trailing_space.gbnf b/python/llm/example/Text-Generation-WebUI/grammars/json_w_trailing_space.gbnf new file mode 100644 index 00000000..21549e0c --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/grammars/json_w_trailing_space.gbnf @@ -0,0 +1,14 @@ +root ::= object + +object ::= "{" ws ( string ":" ws value ("," ws string ":" ws value)* )? "}" ws + +value ::= object | array | string | number | ("true" | "false" | "null") ws + +array ::= "[" ws ( value ("," ws value)* )? "]" ws + +string ::= "\"" ( [a-zA-Z0-9] )* "\"" ws + +number ::= ("-"? ([0-9] | [1-9] [0-9]*)) ("." [0-9]+)? ([eE] [-+]? [0-9]+)? ws + + +ws ::= ([ \t\n] ws)? diff --git a/python/llm/example/Text-Generation-WebUI/grammars/list.gbnf b/python/llm/example/Text-Generation-WebUI/grammars/list.gbnf new file mode 100644 index 00000000..fd5aea10 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/grammars/list.gbnf @@ -0,0 +1,2 @@ +root ::= "1. " paragraph "\n" ([0-9] [0-9]? ". " paragraph "\n")+ +paragraph ::= [a-zA-Z'.,; ]+ \ No newline at end of file diff --git a/python/llm/example/Text-Generation-WebUI/grammars/simple_arithmetic.gbnf b/python/llm/example/Text-Generation-WebUI/grammars/simple_arithmetic.gbnf new file mode 100644 index 00000000..b4451da2 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/grammars/simple_arithmetic.gbnf @@ -0,0 +1,7 @@ +root ::= (expr "=" ws term "\n")+ +expr ::= term ([-+*/] term)* +term ::= num | "(" ws expr ")" ws +num ::= [0-9]+ ws +ws ::= [ \t\n]* +# this is a comment + diff --git a/python/llm/example/Text-Generation-WebUI/js/main.js b/python/llm/example/Text-Generation-WebUI/js/main.js new file mode 100644 index 00000000..cd7e5068 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/js/main.js @@ -0,0 +1,373 @@ +let main_parent = document.getElementById("chat-tab").parentNode; +let extensions = document.getElementById("extensions"); + +main_parent.childNodes[0].classList.add("header_bar"); +main_parent.style = "padding: 0; margin: 0"; +main_parent.parentNode.style = "gap: 0"; +main_parent.parentNode.parentNode.style = "padding: 0"; + +document.querySelector(".header_bar").addEventListener("click", function(event) { + if (event.target.tagName === "BUTTON") { + const buttonText = event.target.textContent.trim(); + + let chat_visible = (buttonText == "Chat"); + let default_visible = (buttonText == "Default"); + let notebook_visible = (buttonText == "Notebook"); + + // Check if one of the generation tabs is visible + if (chat_visible || notebook_visible || default_visible) { + extensions && (extensions.style.display = "flex"); + + if (chat_visible) { + this.style.marginBottom = "0px"; + extensions && (extensions.style.maxWidth = "880px"); + extensions && (extensions.style.padding = "0px"); + } else { + this.style.marginBottom = "19px"; + extensions && (extensions.style.maxWidth = "none"); + extensions && (extensions.style.padding = "15px"); + } + } else { + this.style.marginBottom = "19px"; + extensions && (extensions.style.display = "none"); + } + } +}); + +//------------------------------------------------ +// Keyboard shortcuts +//------------------------------------------------ +document.addEventListener("keydown", function(event) { + + // Stop generation on Esc pressed + if (event.key === "Escape") { + // Find the element with id 'stop' and click it + var stopButton = document.getElementById("stop"); + if (stopButton) { + stopButton.click(); + } + } + + // Show chat controls on Ctrl + S + else if (event.ctrlKey && event.key == "s") { + event.preventDefault(); + + var showControlsElement = document.getElementById("show-controls"); + if (showControlsElement && showControlsElement.childNodes.length >= 4) { + showControlsElement.childNodes[3].click(); + + var arr = document.getElementById("chat-input").childNodes[2].childNodes; + arr[arr.length - 1].focus(); + } + } + + // Regenerate on Ctrl + Enter + else if (event.ctrlKey && event.key === "Enter") { + event.preventDefault(); + document.getElementById("Regenerate").click(); + } + + // Continue on Alt + Enter + else if (event.altKey && event.key === "Enter") { + event.preventDefault(); + document.getElementById("Continue").click(); + } + + // Remove last on Ctrl + Shift + Backspace + else if (event.ctrlKey && event.shiftKey && event.key === "Backspace") { + event.preventDefault(); + document.getElementById("Remove-last").click(); + } + + // Copy last on Ctrl + Shift + K + else if (event.ctrlKey && event.shiftKey && event.key === "K") { + event.preventDefault(); + document.getElementById("Copy-last").click(); + } + + // Replace last on Ctrl + Shift + L + else if (event.ctrlKey && event.shiftKey && event.key === "L") { + event.preventDefault(); + document.getElementById("Replace-last").click(); + } + + // Impersonate on Ctrl + Shift + M + else if (event.ctrlKey && event.shiftKey && event.key === "M") { + event.preventDefault(); + document.getElementById("Impersonate").click(); + } + +}); + +//------------------------------------------------ +// Position the chat typing dots +//------------------------------------------------ +typing = document.getElementById("typing-container"); +typingParent = typing.parentNode; +typingSibling = typing.previousElementSibling; +typingSibling.insertBefore(typing, typingSibling.childNodes[2]); + +//------------------------------------------------ +// Chat scrolling +//------------------------------------------------ +const targetElement = document.getElementById("chat").parentNode.parentNode.parentNode; +targetElement.classList.add("pretty_scrollbar"); +targetElement.classList.add("chat-parent"); +let isScrolled = false; + +targetElement.addEventListener("scroll", function() { + let diff = targetElement.scrollHeight - targetElement.clientHeight; + if(Math.abs(targetElement.scrollTop - diff) <= 10 || diff == 0) { + isScrolled = false; + } else { + isScrolled = true; + } +}); + +// Create a MutationObserver instance +const observer = new MutationObserver(function(mutations) { + mutations.forEach(function(mutation) { + updateCssProperties(); + + if(!isScrolled) { + targetElement.scrollTop = targetElement.scrollHeight; + } + + const firstChild = targetElement.children[0]; + if (firstChild.classList.contains("generating")) { + typing.parentNode.classList.add("visible-dots"); + document.getElementById("stop").style.display = "flex"; + document.getElementById("Generate").style.display = "none"; + } else { + typing.parentNode.classList.remove("visible-dots"); + document.getElementById("stop").style.display = "none"; + document.getElementById("Generate").style.display = "flex"; + } + + }); +}); + +// Configure the observer to watch for changes in the subtree and attributes +const config = { + childList: true, + subtree: true, + characterData: true, + attributeOldValue: true, + characterDataOldValue: true +}; + +// Start observing the target element +observer.observe(targetElement, config); + +//------------------------------------------------ +// Add some scrollbars +//------------------------------------------------ +const textareaElements = document.querySelectorAll(".add_scrollbar textarea"); +for(i = 0; i < textareaElements.length; i++) { + textareaElements[i].classList.remove("scroll-hide"); + textareaElements[i].classList.add("pretty_scrollbar"); + textareaElements[i].style.resize = "none"; +} + +//------------------------------------------------ +// Remove some backgrounds +//------------------------------------------------ +const noBackgroundelements = document.querySelectorAll(".no-background"); +for(i = 0; i < noBackgroundelements.length; i++) { + noBackgroundelements[i].parentNode.style.border = "none"; + noBackgroundelements[i].parentNode.parentNode.parentNode.style.alignItems = "center"; +} + +const slimDropdownElements = document.querySelectorAll(".slim-dropdown"); +for (i = 0; i < slimDropdownElements.length; i++) { + const parentNode = slimDropdownElements[i].parentNode; + parentNode.style.background = "transparent"; + parentNode.style.border = "0"; +} + +//------------------------------------------------ +// Create the hover menu in the chat tab +// The show/hide events were adapted from: +// https://github.com/SillyTavern/SillyTavern/blob/6c8bd06308c69d51e2eb174541792a870a83d2d6/public/script.js +//------------------------------------------------ +var buttonsInChat = document.querySelectorAll("#chat-tab:not(.old-ui) #chat-buttons button"); +var button = document.getElementById("hover-element-button"); +var menu = document.getElementById("hover-menu"); +var istouchscreen = (navigator.maxTouchPoints > 0) || "ontouchstart" in document.documentElement; + +function showMenu() { + menu.style.display = "flex"; // Show the menu +} + +function hideMenu() { + menu.style.display = "none"; // Hide the menu + if (!istouchscreen) { + document.querySelector("#chat-input textarea").focus(); // Focus on the chat input + } +} + +if (buttonsInChat.length > 0) { + for (let i = buttonsInChat.length - 1; i >= 0; i--) { + const thisButton = buttonsInChat[i]; + menu.appendChild(thisButton); + + thisButton.addEventListener("click", () => { + hideMenu(); + }); + + const buttonText = thisButton.textContent; + const matches = buttonText.match(/(\(.*?\))/); + + if (matches && matches.length > 1) { + // Apply the transparent-substring class to the matched substring + const substring = matches[1]; + const newText = buttonText.replace(substring, ` ${substring.slice(1, -1)}`); + thisButton.innerHTML = newText; + } + } +} else { + buttonsInChat = document.querySelectorAll("#chat-tab.old-ui #chat-buttons button"); + for (let i = 0; i < buttonsInChat.length; i++) { + buttonsInChat[i].textContent = buttonsInChat[i].textContent.replace(/ \(.*?\)/, ""); + } + document.getElementById("gr-hover-container").style.display = "none"; +} + +function isMouseOverButtonOrMenu() { + return menu.matches(":hover") || button.matches(":hover"); +} + +button.addEventListener("mouseenter", function () { + if (!istouchscreen) { + showMenu(); + } +}); + +button.addEventListener("click", function () { + if (menu.style.display === "flex") { + hideMenu(); + } + else { + showMenu(); + } +}); + +// Add event listener for mouseleave on the button +button.addEventListener("mouseleave", function () { + // Delay to prevent menu hiding when the mouse leaves the button into the menu + setTimeout(function () { + if (!isMouseOverButtonOrMenu()) { + hideMenu(); + } + }, 100); +}); + +// Add event listener for mouseleave on the menu +menu.addEventListener("mouseleave", function () { + // Delay to prevent menu hide when the mouse leaves the menu into the button + setTimeout(function () { + if (!isMouseOverButtonOrMenu()) { + hideMenu(); + } + }, 100); +}); + +// Add event listener for click anywhere in the document +document.addEventListener("click", function (event) { + // Check if the click is outside the button/menu and the menu is visible + if (!isMouseOverButtonOrMenu() && menu.style.display === "flex") { + hideMenu(); + } + + if (event.target.classList.contains("pfp_character")) { + toggleBigPicture(); + } +}); + +//------------------------------------------------ +// Relocate the "Show controls" checkbox +//------------------------------------------------ +var elementToMove = document.getElementById("show-controls"); +var parent = elementToMove.parentNode; +for (var i = 0; i < 2; i++) { + parent = parent.parentNode; +} + +parent.insertBefore(elementToMove, parent.firstChild); + +//------------------------------------------------ +// Make the chat input grow upwards instead of downwards +//------------------------------------------------ +document.getElementById("show-controls").parentNode.style.position = "absolute"; +document.getElementById("show-controls").parentNode.style.bottom = "0px"; + +//------------------------------------------------ +// Focus on the chat input +//------------------------------------------------ +document.querySelector("#chat-input textarea").focus(); + +//------------------------------------------------ +// Show enlarged character picture when the profile +// picture is clicked on +//------------------------------------------------ +let bigPictureVisible = false; + +function addBigPicture() { + var imgElement = document.createElement("img"); + var timestamp = new Date().getTime(); + imgElement.src = "/file/cache/pfp_character.png?time=" + timestamp; + imgElement.classList.add("bigProfilePicture"); + + var imgElementParent = document.getElementById("chat").parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode; + imgElementParent.appendChild(imgElement); +} + +function deleteBigPicture() { + var bigProfilePictures = document.querySelectorAll(".bigProfilePicture"); + bigProfilePictures.forEach(function (element) { + element.parentNode.removeChild(element); + }); +} + +function toggleBigPicture() { + if(bigPictureVisible) { + deleteBigPicture(); + bigPictureVisible = false; + } else { + addBigPicture(); + bigPictureVisible = true; + } +} + +//------------------------------------------------ +// Define global CSS properties for resizing and +// positioning certain elements +//------------------------------------------------ +let currentChatInputHeight = 0; + +function updateCssProperties() { + // Set the height of the chat area + const chatContainer = document.getElementById("chat").parentNode.parentNode.parentNode; + const chatInputHeight = document.querySelector("#chat-input textarea").clientHeight; + if (chatContainer.clientHeight > 0) { + const newChatHeight = `${chatContainer.clientHeight - chatInputHeight + 40}px`; + document.documentElement.style.setProperty("--chat-height", newChatHeight); + document.documentElement.style.setProperty("--input-delta", `${chatInputHeight - 40}px`); + + // Set the position offset of the chat input box + const header = document.querySelector(".header_bar"); + const headerHeight = `${header.clientHeight}px`; + document.documentElement.style.setProperty("--header-height", headerHeight); + + // Offset the scroll position of the chat area + if (chatInputHeight !== currentChatInputHeight) { + chatContainer.scrollTop += chatInputHeight > currentChatInputHeight ? chatInputHeight : -chatInputHeight; + currentChatInputHeight = chatInputHeight; + } + } +} + +new ResizeObserver(updateCssProperties) + .observe(document.querySelector("#chat-input textarea")); + +window.addEventListener("resize", updateCssProperties); diff --git a/python/llm/example/Text-Generation-WebUI/js/save_files.js b/python/llm/example/Text-Generation-WebUI/js/save_files.js new file mode 100644 index 00000000..bdb0e334 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/js/save_files.js @@ -0,0 +1,40 @@ +// Functions for downloading JSON files +function getCurrentTimestamp() { + const now = new Date(); + const timezoneOffset = now.getTimezoneOffset() * 60000; // Convert to milliseconds + const localTime = new Date(now.getTime() - timezoneOffset); + const formattedTimestamp = localTime.toISOString().replace(/[-:]/g, "").slice(0, 15); + return formattedTimestamp; +} + +function saveFile(contents, filename) { + const element = document.createElement("a"); + element.setAttribute("href", "data:text/plain;charset=utf-8," + encodeURIComponent(contents)); + element.setAttribute("download", filename); + element.style.display = "none"; + document.body.appendChild(element); + element.click(); + document.body.removeChild(element); +} + +function saveHistory(history, character, mode) { + let path = null; + + if (["chat", "chat-instruct"].includes(mode) && character && character.trim() !== "") { + path = `history_${character}_${getCurrentTimestamp()}.json`; + } else { + try { + path = `history_${mode}_${getCurrentTimestamp()}.json`; + } catch (error) { + path = `history_${getCurrentTimestamp()}.json`; + } + } + saveFile(history, path); +} + +function saveSession(session) { + let path = null; + + path = `session_${getCurrentTimestamp()}.json`; + saveFile(session, path); +} diff --git a/python/llm/example/Text-Generation-WebUI/js/show_controls.js b/python/llm/example/Text-Generation-WebUI/js/show_controls.js new file mode 100644 index 00000000..1ff88e52 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/js/show_controls.js @@ -0,0 +1,30 @@ +const belowChatInput = document.querySelectorAll("#chat-tab > div > :nth-child(n+2), #extensions"); +const chatParent = document.querySelector(".chat-parent"); + +function toggle_controls(value) { + if (value) { + belowChatInput.forEach(element => { + element.style.display = "inherit"; + }); + + chatParent.classList.remove("bigchat"); + document.getElementById("chat-input-row").classList.remove("bigchat"); + document.getElementById("chat-col").classList.remove("bigchat"); + document.getElementById("chat-tab").style.paddingBottom = ""; + + let gallery_element = document.getElementById("gallery-extension"); + if (gallery_element) { + gallery_element.style.display = "block"; + } + + } else { + belowChatInput.forEach(element => { + element.style.display = "none"; + }); + + chatParent.classList.add("bigchat"); + document.getElementById("chat-input-row").classList.add("bigchat"); + document.getElementById("chat-col").classList.add("bigchat"); + document.getElementById("chat-tab").style.paddingBottom = "0px"; + } +} diff --git a/python/llm/example/Text-Generation-WebUI/js/switch_tabs.js b/python/llm/example/Text-Generation-WebUI/js/switch_tabs.js new file mode 100644 index 00000000..75d56367 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/js/switch_tabs.js @@ -0,0 +1,59 @@ +let chat_tab = document.getElementById("chat-tab"); +let main_parent = chat_tab.parentNode; + +function scrollToTop() { + window.scrollTo({ + top: 0, + // behavior: 'smooth' + }); +} + +function findButtonsByText(buttonText) { + const buttons = document.getElementsByTagName("button"); + const matchingButtons = []; + buttonText = buttonText.trim(); + + for (let i = 0; i < buttons.length; i++) { + const button = buttons[i]; + const buttonInnerText = button.textContent.trim(); + + if (buttonInnerText === buttonText) { + matchingButtons.push(button); + } + } + + return matchingButtons; +} + +function switch_to_chat() { + let chat_tab_button = main_parent.childNodes[0].childNodes[1]; + chat_tab_button.click(); + scrollToTop(); +} + +function switch_to_default() { + let default_tab_button = main_parent.childNodes[0].childNodes[4]; + default_tab_button.click(); + scrollToTop(); +} + +function switch_to_notebook() { + let notebook_tab_button = main_parent.childNodes[0].childNodes[7]; + notebook_tab_button.click(); + findButtonsByText("Raw")[1].click(); + scrollToTop(); +} + +function switch_to_generation_parameters() { + let parameters_tab_button = main_parent.childNodes[0].childNodes[10]; + parameters_tab_button.click(); + findButtonsByText("Generation")[0].click(); + scrollToTop(); +} + +function switch_to_character() { + let parameters_tab_button = main_parent.childNodes[0].childNodes[10]; + parameters_tab_button.click(); + findButtonsByText("Character")[0].click(); + scrollToTop(); +} diff --git a/python/llm/example/Text-Generation-WebUI/js/update_big_picture.js b/python/llm/example/Text-Generation-WebUI/js/update_big_picture.js new file mode 100644 index 00000000..4c094776 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/js/update_big_picture.js @@ -0,0 +1,7 @@ +function updateBigPicture() { + var existingElement = document.querySelector(".bigProfilePicture"); + if (existingElement) { + var timestamp = new Date().getTime(); + existingElement.src = "/file/cache/pfp_character.png?time=" + timestamp; + } +} diff --git a/python/llm/example/Text-Generation-WebUI/models/place_your_models_here.txt b/python/llm/example/Text-Generation-WebUI/models/place_your_models_here.txt new file mode 100644 index 00000000..e69de29b diff --git a/python/llm/example/Text-Generation-WebUI/modules/RoPE.py b/python/llm/example/Text-Generation-WebUI/modules/RoPE.py new file mode 100644 index 00000000..bf6d48d7 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/RoPE.py @@ -0,0 +1,37 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/RoPE.py + + +def get_alpha_value(alpha, base): + ''' + Gets alpha_value from alpha_value and rope_freq_base + ''' + if base > 0: + return (base/10000.) ** (63/64.) + else: + return alpha + + +def get_rope_freq_base(alpha, base): + ''' + Gets rope_freq_base from alpha_value and rope_freq_base + ''' + if base > 0: + return base + else: + return 10000 * alpha ** (64/63.) diff --git a/python/llm/example/Text-Generation-WebUI/modules/block_requests.py b/python/llm/example/Text-Generation-WebUI/modules/block_requests.py new file mode 100644 index 00000000..42f1a9fd --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/block_requests.py @@ -0,0 +1,66 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/block_requests.py + + +import builtins +import io + +import requests + +from modules.logging_colors import logger + +original_open = open +original_get = requests.get + + +class RequestBlocker: + + def __enter__(self): + requests.get = my_get + + def __exit__(self, exc_type, exc_value, traceback): + requests.get = original_get + + +class OpenMonkeyPatch: + + def __enter__(self): + builtins.open = my_open + + def __exit__(self, exc_type, exc_value, traceback): + builtins.open = original_open + + +def my_get(url, **kwargs): + logger.info('Unwanted HTTP request redirected to localhost :)') + kwargs.setdefault('allow_redirects', True) + return requests.api.request('get', 'http://127.0.0.1/', **kwargs) + + +# Kindly provided by our friend WizardLM-30B +def my_open(*args, **kwargs): + filename = str(args[0]) + if filename.endswith('index.html'): + with original_open(*args, **kwargs) as f: + file_contents = f.read() + + file_contents = file_contents.replace(b'\t\t', b'') + file_contents = file_contents.replace(b'cdnjs.cloudflare.com', b'127.0.0.1') + return io.BytesIO(file_contents) + else: + return original_open(*args, **kwargs) diff --git a/python/llm/example/Text-Generation-WebUI/modules/callbacks.py b/python/llm/example/Text-Generation-WebUI/modules/callbacks.py new file mode 100644 index 00000000..8f753dfd --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/callbacks.py @@ -0,0 +1,122 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/callbacks.py + + +import gc +import traceback +from queue import Queue +from threading import Thread + +import torch +import transformers +from transformers import is_torch_xpu_available + +import modules.shared as shared + + +class StopNowException(Exception): + pass + + +class _StopEverythingStoppingCriteria(transformers.StoppingCriteria): + def __init__(self): + transformers.StoppingCriteria.__init__(self) + + def __call__(self, input_ids: torch.LongTensor, _scores: torch.FloatTensor) -> bool: + return shared.stop_everything + + +class Stream(transformers.StoppingCriteria): + def __init__(self, callback_func=None): + self.callback_func = callback_func + + def __call__(self, input_ids, scores) -> bool: + if self.callback_func is not None: + self.callback_func(input_ids[0]) + + return False + + +class Iteratorize: + + """ + Transforms a function that takes a callback + into a lazy iterator (generator). + + Adapted from: https://stackoverflow.com/a/9969000 + """ + + def __init__(self, func, args=None, kwargs=None, callback=None): + self.mfunc = func + self.c_callback = callback + self.q = Queue() + self.sentinel = object() + self.args = args or [] + self.kwargs = kwargs or {} + self.stop_now = False + + def _callback(val): + if self.stop_now or shared.stop_everything: + raise StopNowException + self.q.put(val) + + def gentask(): + try: + ret = self.mfunc(callback=_callback, *args, **self.kwargs) + except StopNowException: + pass + except: + traceback.print_exc() + pass + + clear_torch_cache() + self.q.put(self.sentinel) + if self.c_callback: + self.c_callback(ret) + + self.thread = Thread(target=gentask) + self.thread.start() + + def __iter__(self): + return self + + def __next__(self): + obj = self.q.get(True, None) + if obj is self.sentinel: + raise StopIteration + else: + return obj + + def __del__(self): + clear_torch_cache() + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + self.stop_now = True + clear_torch_cache() + + +def clear_torch_cache(): + gc.collect() + if not shared.args.cpu: + if is_torch_xpu_available(): + torch.xpu.empty_cache() + else: + torch.cuda.empty_cache() diff --git a/python/llm/example/Text-Generation-WebUI/modules/chat.py b/python/llm/example/Text-Generation-WebUI/modules/chat.py new file mode 100644 index 00000000..379aac1a --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/chat.py @@ -0,0 +1,881 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/chat.py + + +import base64 +import copy +import functools +import html +import json +import re +from datetime import datetime +from functools import partial +from pathlib import Path + +import gradio as gr +import yaml +from jinja2.sandbox import ImmutableSandboxedEnvironment +from PIL import Image + +import modules.shared as shared +from modules.extensions import apply_extensions +from modules.html_generator import chat_html_wrapper, make_thumbnail +from modules.logging_colors import logger +from modules.text_generation import ( + generate_reply, + get_encoded_length, + get_max_prompt_length +) +from modules.utils import delete_file, get_available_characters, save_file + +# Copied from the Transformers library +jinja_env = ImmutableSandboxedEnvironment(trim_blocks=True, lstrip_blocks=True) + + +def str_presenter(dumper, data): + """ + Copied from https://github.com/yaml/pyyaml/issues/240 + Makes pyyaml output prettier multiline strings. + """ + + if data.count('\n') > 0: + return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|') + + return dumper.represent_scalar('tag:yaml.org,2002:str', data) + + +yaml.add_representer(str, str_presenter) +yaml.representer.SafeRepresenter.add_representer(str, str_presenter) + + +def get_generation_prompt(renderer, impersonate=False, strip_trailing_spaces=True): + ''' + Given a Jinja template, reverse-engineers the prefix and the suffix for + an assistant message (if impersonate=False) or an user message + (if impersonate=True) + ''' + + if impersonate: + messages = [ + {"role": "user", "content": "<<|user-message-1|>>"}, + {"role": "user", "content": "<<|user-message-2|>>"}, + ] + else: + messages = [ + {"role": "assistant", "content": "<<|user-message-1|>>"}, + {"role": "assistant", "content": "<<|user-message-2|>>"}, + ] + + prompt = renderer(messages=messages) + + suffix_plus_prefix = prompt.split("<<|user-message-1|>>")[1].split("<<|user-message-2|>>")[0] + suffix = prompt.split("<<|user-message-2|>>")[1] + prefix = suffix_plus_prefix[len(suffix):] + + if strip_trailing_spaces: + prefix = prefix.rstrip(' ') + + return prefix, suffix + + +def generate_chat_prompt(user_input, state, **kwargs): + impersonate = kwargs.get('impersonate', False) + _continue = kwargs.get('_continue', False) + also_return_rows = kwargs.get('also_return_rows', False) + history = kwargs.get('history', state['history'])['internal'] + + # Templates + chat_template = jinja_env.from_string(state['chat_template_str']) + instruction_template = jinja_env.from_string(state['instruction_template_str']) + chat_renderer = partial(chat_template.render, add_generation_prompt=False, name1=state['name1'], name2=state['name2']) + instruct_renderer = partial(instruction_template.render, add_generation_prompt=False) + + messages = [] + + if state['mode'] == 'instruct': + renderer = instruct_renderer + if state['custom_system_message'].strip() != '': + messages.append({"role": "system", "content": state['custom_system_message']}) + else: + renderer = chat_renderer + if state['context'].strip() != '': + context = replace_character_names(state['context'], state['name1'], state['name2']) + messages.append({"role": "system", "content": context}) + + insert_pos = len(messages) + for user_msg, assistant_msg in reversed(history): + user_msg = user_msg.strip() + assistant_msg = assistant_msg.strip() + + if assistant_msg: + messages.insert(insert_pos, {"role": "assistant", "content": assistant_msg}) + + if user_msg not in ['', '<|BEGIN-VISIBLE-CHAT|>']: + messages.insert(insert_pos, {"role": "user", "content": user_msg}) + + user_input = user_input.strip() + if user_input and not impersonate and not _continue: + messages.append({"role": "user", "content": user_input}) + + def remove_extra_bos(prompt): + for bos_token in ['', '<|startoftext|>']: + while prompt.startswith(bos_token): + prompt = prompt[len(bos_token):] + + return prompt + + def make_prompt(messages): + if state['mode'] == 'chat-instruct' and _continue: + prompt = renderer(messages=messages[:-1]) + else: + prompt = renderer(messages=messages) + + if state['mode'] == 'chat-instruct': + outer_messages = [] + if state['custom_system_message'].strip() != '': + outer_messages.append({"role": "system", "content": state['custom_system_message']}) + + prompt = remove_extra_bos(prompt) + command = state['chat-instruct_command'] + command = command.replace('<|character|>', state['name2'] if not impersonate else state['name1']) + command = command.replace('<|prompt|>', prompt) + + if _continue: + prefix = get_generation_prompt(renderer, impersonate=impersonate, strip_trailing_spaces=False)[0] + prefix += messages[-1]["content"] + else: + prefix = get_generation_prompt(renderer, impersonate=impersonate)[0] + if not impersonate: + prefix = apply_extensions('bot_prefix', prefix, state) + + outer_messages.append({"role": "user", "content": command}) + outer_messages.append({"role": "assistant", "content": prefix}) + + prompt = instruction_template.render(messages=outer_messages) + suffix = get_generation_prompt(instruct_renderer, impersonate=False)[1] + prompt = prompt[:-len(suffix)] + + else: + if _continue: + suffix = get_generation_prompt(renderer, impersonate=impersonate)[1] + prompt = prompt[:-len(suffix)] + else: + prefix = get_generation_prompt(renderer, impersonate=impersonate)[0] + if state['mode'] == 'chat' and not impersonate: + prefix = apply_extensions('bot_prefix', prefix, state) + + prompt += prefix + + prompt = remove_extra_bos(prompt) + return prompt + + prompt = make_prompt(messages) + + # Handle truncation + max_length = get_max_prompt_length(state) + while len(messages) > 0 and get_encoded_length(prompt) > max_length: + # Try to save the system message + if len(messages) > 1 and messages[0]['role'] == 'system': + messages.pop(1) + else: + messages.pop(0) + + prompt = make_prompt(messages) + + if also_return_rows: + return prompt, [message['content'] for message in messages] + else: + return prompt + + +def get_stopping_strings(state): + stopping_strings = [] + renderers = [] + + if state['mode'] in ['instruct', 'chat-instruct']: + template = jinja_env.from_string(state['instruction_template_str']) + renderer = partial(template.render, add_generation_prompt=False) + renderers.append(renderer) + + if state['mode'] in ['chat', 'chat-instruct']: + template = jinja_env.from_string(state['chat_template_str']) + renderer = partial(template.render, add_generation_prompt=False, name1=state['name1'], name2=state['name2']) + renderers.append(renderer) + + for renderer in renderers: + prefix_bot, suffix_bot = get_generation_prompt(renderer, impersonate=False) + prefix_user, suffix_user = get_generation_prompt(renderer, impersonate=True) + + stopping_strings += [ + suffix_user + prefix_bot, + suffix_user + prefix_user, + suffix_bot + prefix_bot, + suffix_bot + prefix_user, + ] + + if 'stopping_strings' in state and isinstance(state['stopping_strings'], list): + stopping_strings += state.pop('stopping_strings') + + return list(set(stopping_strings)) + + +def chatbot_wrapper(text, state, regenerate=False, _continue=False, loading_message=True, for_ui=False): + history = state['history'] + output = copy.deepcopy(history) + output = apply_extensions('history', output) + state = apply_extensions('state', state) + + visible_text = None + stopping_strings = get_stopping_strings(state) + is_stream = state['stream'] + + # Prepare the input + if not (regenerate or _continue): + visible_text = html.escape(text) + + # Apply extensions + text, visible_text = apply_extensions('chat_input', text, visible_text, state) + text = apply_extensions('input', text, state, is_chat=True) + + output['internal'].append([text, '']) + output['visible'].append([visible_text, '']) + + # *Is typing...* + if loading_message: + yield { + 'visible': output['visible'][:-1] + [[output['visible'][-1][0], shared.processing_message]], + 'internal': output['internal'] + } + else: + text, visible_text = output['internal'][-1][0], output['visible'][-1][0] + if regenerate: + if loading_message: + yield { + 'visible': output['visible'][:-1] + [[visible_text, shared.processing_message]], + 'internal': output['internal'][:-1] + [[text, '']] + } + elif _continue: + last_reply = [output['internal'][-1][1], output['visible'][-1][1]] + if loading_message: + yield { + 'visible': output['visible'][:-1] + [[visible_text, last_reply[1] + '...']], + 'internal': output['internal'] + } + + if shared.model_name == 'None' or shared.model is None: + raise ValueError("No model is loaded! Select one in the Model tab.") + + # Generate the prompt + kwargs = { + '_continue': _continue, + 'history': output if _continue else {k: v[:-1] for k, v in output.items()} + } + prompt = apply_extensions('custom_generate_chat_prompt', text, state, **kwargs) + if prompt is None: + prompt = generate_chat_prompt(text, state, **kwargs) + + # Generate + reply = None + for j, reply in enumerate(generate_reply(prompt, state, stopping_strings=stopping_strings, is_chat=True, for_ui=for_ui)): + + # Extract the reply + visible_reply = reply + if state['mode'] in ['chat', 'chat-instruct']: + visible_reply = re.sub("(||{{user}})", state['name1'], reply) + + visible_reply = html.escape(visible_reply) + + if shared.stop_everything: + output['visible'][-1][1] = apply_extensions('output', output['visible'][-1][1], state, is_chat=True) + yield output + return + + if _continue: + output['internal'][-1] = [text, last_reply[0] + reply] + output['visible'][-1] = [visible_text, last_reply[1] + visible_reply] + if is_stream: + yield output + elif not (j == 0 and visible_reply.strip() == ''): + output['internal'][-1] = [text, reply.lstrip(' ')] + output['visible'][-1] = [visible_text, visible_reply.lstrip(' ')] + if is_stream: + yield output + + output['visible'][-1][1] = apply_extensions('output', output['visible'][-1][1], state, is_chat=True) + yield output + + +def impersonate_wrapper(text, state): + + static_output = chat_html_wrapper(state['history'], state['name1'], state['name2'], state['mode'], state['chat_style'], state['character_menu']) + + if shared.model_name == 'None' or shared.model is None: + logger.error("No model is loaded! Select one in the Model tab.") + yield '', static_output + return + + prompt = generate_chat_prompt('', state, impersonate=True) + stopping_strings = get_stopping_strings(state) + + yield text + '...', static_output + reply = None + for reply in generate_reply(prompt + text, state, stopping_strings=stopping_strings, is_chat=True): + yield (text + reply).lstrip(' '), static_output + if shared.stop_everything: + return + + +def generate_chat_reply(text, state, regenerate=False, _continue=False, loading_message=True, for_ui=False): + history = state['history'] + if regenerate or _continue: + text = '' + if (len(history['visible']) == 1 and not history['visible'][0][0]) or len(history['internal']) == 0: + yield history + return + + for history in chatbot_wrapper(text, state, regenerate=regenerate, _continue=_continue, loading_message=loading_message, for_ui=for_ui): + yield history + + +def character_is_loaded(state, raise_exception=False): + if state['mode'] in ['chat', 'chat-instruct'] and state['name2'] == '': + logger.error('It looks like no character is loaded. Please load one under Parameters > Character.') + if raise_exception: + raise ValueError + + return False + else: + return True + + +def generate_chat_reply_wrapper(text, state, regenerate=False, _continue=False): + ''' + Same as above but returns HTML for the UI + ''' + + if not character_is_loaded(state): + return + + if state['start_with'] != '' and not _continue: + if regenerate: + text, state['history'] = remove_last_message(state['history']) + regenerate = False + + _continue = True + send_dummy_message(text, state) + send_dummy_reply(state['start_with'], state) + + for i, history in enumerate(generate_chat_reply(text, state, regenerate, _continue, loading_message=True, for_ui=True)): + yield chat_html_wrapper(history, state['name1'], state['name2'], state['mode'], state['chat_style'], state['character_menu']), history + + +def remove_last_message(history): + if len(history['visible']) > 0 and history['internal'][-1][0] != '<|BEGIN-VISIBLE-CHAT|>': + last = history['visible'].pop() + history['internal'].pop() + else: + last = ['', ''] + + return html.unescape(last[0]), history + + +def send_last_reply_to_input(history): + if len(history['visible']) > 0: + return html.unescape(history['visible'][-1][1]) + else: + return '' + + +def replace_last_reply(text, state): + history = state['history'] + + if len(text.strip()) == 0: + return history + elif len(history['visible']) > 0: + history['visible'][-1][1] = html.escape(text) + history['internal'][-1][1] = apply_extensions('input', text, state, is_chat=True) + + return history + + +def send_dummy_message(text, state): + history = state['history'] + history['visible'].append([html.escape(text), '']) + history['internal'].append([apply_extensions('input', text, state, is_chat=True), '']) + return history + + +def send_dummy_reply(text, state): + history = state['history'] + if len(history['visible']) > 0 and not history['visible'][-1][1] == '': + history['visible'].append(['', '']) + history['internal'].append(['', '']) + + history['visible'][-1][1] = html.escape(text) + history['internal'][-1][1] = apply_extensions('input', text, state, is_chat=True) + return history + + +def redraw_html(history, name1, name2, mode, style, character, reset_cache=False): + return chat_html_wrapper(history, name1, name2, mode, style, character, reset_cache=reset_cache) + + +def start_new_chat(state): + mode = state['mode'] + history = {'internal': [], 'visible': []} + + if mode != 'instruct': + greeting = replace_character_names(state['greeting'], state['name1'], state['name2']) + if greeting != '': + history['internal'] += [['<|BEGIN-VISIBLE-CHAT|>', greeting]] + history['visible'] += [['', apply_extensions('output', greeting, state, is_chat=True)]] + + unique_id = datetime.now().strftime('%Y%m%d-%H-%M-%S') + save_history(history, unique_id, state['character_menu'], state['mode']) + + return history + + +def get_history_file_path(unique_id, character, mode): + if mode == 'instruct': + p = Path(f'logs/instruct/{unique_id}.json') + else: + p = Path(f'logs/chat/{character}/{unique_id}.json') + + return p + + +def save_history(history, unique_id, character, mode): + if shared.args.multi_user: + return + + p = get_history_file_path(unique_id, character, mode) + if not p.parent.is_dir(): + p.parent.mkdir(parents=True) + + with open(p, 'w', encoding='utf-8') as f: + f.write(json.dumps(history, indent=4)) + + +def rename_history(old_id, new_id, character, mode): + if shared.args.multi_user: + return + + old_p = get_history_file_path(old_id, character, mode) + new_p = get_history_file_path(new_id, character, mode) + if new_p.parent != old_p.parent: + logger.error(f"The following path is not allowed: {new_p}.") + elif new_p == old_p: + logger.info("The provided path is identical to the old one.") + else: + logger.info(f"Renaming {old_p} to {new_p}") + old_p.rename(new_p) + + +def find_all_histories(state): + if shared.args.multi_user: + return [''] + + if state['mode'] == 'instruct': + paths = Path('logs/instruct').glob('*.json') + else: + character = state['character_menu'] + + # Handle obsolete filenames and paths + old_p = Path(f'logs/{character}_persistent.json') + new_p = Path(f'logs/persistent_{character}.json') + if old_p.exists(): + logger.warning(f"Renaming {old_p} to {new_p}") + old_p.rename(new_p) + if new_p.exists(): + unique_id = datetime.now().strftime('%Y%m%d-%H-%M-%S') + p = get_history_file_path(unique_id, character, state['mode']) + logger.warning(f"Moving {new_p} to {p}") + p.parent.mkdir(exist_ok=True) + new_p.rename(p) + + paths = Path(f'logs/chat/{character}').glob('*.json') + + histories = sorted(paths, key=lambda x: x.stat().st_mtime, reverse=True) + histories = [path.stem for path in histories] + + return histories + + +def load_latest_history(state): + ''' + Loads the latest history for the given character in chat or chat-instruct + mode, or the latest instruct history for instruct mode. + ''' + + if shared.args.multi_user: + return start_new_chat(state) + + histories = find_all_histories(state) + + if len(histories) > 0: + history = load_history(histories[0], state['character_menu'], state['mode']) + else: + history = start_new_chat(state) + + return history + + +def load_history(unique_id, character, mode): + p = get_history_file_path(unique_id, character, mode) + + f = json.loads(open(p, 'rb').read()) + if 'internal' in f and 'visible' in f: + history = f + else: + history = { + 'internal': f['data'], + 'visible': f['data_visible'] + } + + return history + + +def load_history_json(file, history): + try: + file = file.decode('utf-8') + f = json.loads(file) + if 'internal' in f and 'visible' in f: + history = f + else: + history = { + 'internal': f['data'], + 'visible': f['data_visible'] + } + + return history + except: + return history + + +def delete_history(unique_id, character, mode): + p = get_history_file_path(unique_id, character, mode) + delete_file(p) + + +def replace_character_names(text, name1, name2): + text = text.replace('{{user}}', name1).replace('{{char}}', name2) + return text.replace('', name1).replace('', name2) + + +def generate_pfp_cache(character): + cache_folder = Path(shared.args.disk_cache_dir) + if not cache_folder.exists(): + cache_folder.mkdir() + + for path in [Path(f"characters/{character}.{extension}") for extension in ['png', 'jpg', 'jpeg']]: + if path.exists(): + original_img = Image.open(path) + original_img.save(Path(f'{cache_folder}/pfp_character.png'), format='PNG') + + thumb = make_thumbnail(original_img) + thumb.save(Path(f'{cache_folder}/pfp_character_thumb.png'), format='PNG') + + return thumb + + return None + + +def load_character(character, name1, name2): + context = greeting = "" + greeting_field = 'greeting' + picture = None + + filepath = None + for extension in ["yml", "yaml", "json"]: + filepath = Path(f'characters/{character}.{extension}') + if filepath.exists(): + break + + if filepath is None or not filepath.exists(): + logger.error(f"Could not find the character \"{character}\" inside characters/. No character has been loaded.") + raise ValueError + + file_contents = open(filepath, 'r', encoding='utf-8').read() + data = json.loads(file_contents) if extension == "json" else yaml.safe_load(file_contents) + cache_folder = Path(shared.args.disk_cache_dir) + + for path in [Path(f"{cache_folder}/pfp_character.png"), Path(f"{cache_folder}/pfp_character_thumb.png")]: + if path.exists(): + path.unlink() + + picture = generate_pfp_cache(character) + + # Finding the bot's name + for k in ['name', 'bot', '<|bot|>', 'char_name']: + if k in data and data[k] != '': + name2 = data[k] + break + + # Find the user name (if any) + for k in ['your_name', 'user', '<|user|>']: + if k in data and data[k] != '': + name1 = data[k] + break + + if 'context' in data: + context = data['context'].strip() + elif "char_persona" in data: + context = build_pygmalion_style_context(data) + greeting_field = 'char_greeting' + + greeting = data.get(greeting_field, greeting) + return name1, name2, picture, greeting, context + + +def load_instruction_template(template): + for filepath in [Path(f'instruction-templates/{template}.yaml'), Path('instruction-templates/Alpaca.yaml')]: + if filepath.exists(): + break + else: + return '' + + file_contents = open(filepath, 'r', encoding='utf-8').read() + data = yaml.safe_load(file_contents) + if 'instruction_template' in data: + return data['instruction_template'] + else: + return jinja_template_from_old_format(data) + + +@functools.cache +def load_character_memoized(character, name1, name2): + return load_character(character, name1, name2) + + +@functools.cache +def load_instruction_template_memoized(template): + return load_instruction_template(template) + + +def upload_character(file, img, tavern=False): + decoded_file = file if isinstance(file, str) else file.decode('utf-8') + try: + data = json.loads(decoded_file) + except: + data = yaml.safe_load(decoded_file) + + if 'char_name' in data: + name = data['char_name'] + greeting = data['char_greeting'] + context = build_pygmalion_style_context(data) + yaml_data = generate_character_yaml(name, greeting, context) + else: + name = data['name'] + yaml_data = generate_character_yaml(data['name'], data['greeting'], data['context']) + + outfile_name = name + i = 1 + while Path(f'characters/{outfile_name}.yaml').exists(): + outfile_name = f'{name}_{i:03d}' + i += 1 + + with open(Path(f'characters/{outfile_name}.yaml'), 'w', encoding='utf-8') as f: + f.write(yaml_data) + + if img is not None: + img.save(Path(f'characters/{outfile_name}.png')) + + logger.info(f'New character saved to "characters/{outfile_name}.yaml".') + return gr.update(value=outfile_name, choices=get_available_characters()) + + +def build_pygmalion_style_context(data): + context = "" + if 'char_persona' in data and data['char_persona'] != '': + context += f"{data['char_name']}'s Persona: {data['char_persona']}\n" + + if 'world_scenario' in data and data['world_scenario'] != '': + context += f"Scenario: {data['world_scenario']}\n" + + if 'example_dialogue' in data and data['example_dialogue'] != '': + context += f"{data['example_dialogue'].strip()}\n" + + context = f"{context.strip()}\n" + return context + + +def upload_tavern_character(img, _json): + _json = {'char_name': _json['name'], 'char_persona': _json['description'], 'char_greeting': _json['first_mes'], 'example_dialogue': _json['mes_example'], 'world_scenario': _json['scenario']} + return upload_character(json.dumps(_json), img, tavern=True) + + +def check_tavern_character(img): + if "chara" not in img.info: + return "Not a TavernAI card", None, None, gr.update(interactive=False) + + decoded_string = base64.b64decode(img.info['chara']).replace(b'\\r\\n', b'\\n') + _json = json.loads(decoded_string) + if "data" in _json: + _json = _json["data"] + + return _json['name'], _json['description'], _json, gr.update(interactive=True) + + +def upload_your_profile_picture(img): + cache_folder = Path(shared.args.disk_cache_dir) + if not cache_folder.exists(): + cache_folder.mkdir() + + if img is None: + if Path(f"{cache_folder}/pfp_me.png").exists(): + Path(f"{cache_folder}/pfp_me.png").unlink() + else: + img = make_thumbnail(img) + img.save(Path(f'{cache_folder}/pfp_me.png')) + logger.info(f'Profile picture saved to "{cache_folder}/pfp_me.png"') + + +def generate_character_yaml(name, greeting, context): + data = { + 'name': name, + 'greeting': greeting, + 'context': context, + } + + data = {k: v for k, v in data.items() if v} # Strip falsy + return yaml.dump(data, sort_keys=False, width=float("inf")) + + +def generate_instruction_template_yaml(instruction_template): + data = { + 'instruction_template': instruction_template + } + + return my_yaml_output(data) + + +def save_character(name, greeting, context, picture, filename): + if filename == "": + logger.error("The filename is empty, so the character will not be saved.") + return + + data = generate_character_yaml(name, greeting, context) + filepath = Path(f'characters/{filename}.yaml') + save_file(filepath, data) + path_to_img = Path(f'characters/{filename}.png') + if picture is not None: + picture.save(path_to_img) + logger.info(f'Saved {path_to_img}.') + + +def delete_character(name, instruct=False): + for extension in ["yml", "yaml", "json"]: + delete_file(Path(f'characters/{name}.{extension}')) + + delete_file(Path(f'characters/{name}.png')) + + +def jinja_template_from_old_format(params, verbose=False): + MASTER_TEMPLATE = """ +{%- set ns = namespace(found=false) -%} +{%- for message in messages -%} + {%- if message['role'] == 'system' -%} + {%- set ns.found = true -%} + {%- endif -%} +{%- endfor -%} +{%- if not ns.found -%} + {{- '<|PRE-SYSTEM|>' + '<|SYSTEM-MESSAGE|>' + '<|POST-SYSTEM|>' -}} +{%- endif %} +{%- for message in messages %} + {%- if message['role'] == 'system' -%} + {{- '<|PRE-SYSTEM|>' + message['content'] + '<|POST-SYSTEM|>' -}} + {%- else -%} + {%- if message['role'] == 'user' -%} + {{-'<|PRE-USER|>' + message['content'] + '<|POST-USER|>'-}} + {%- else -%} + {{-'<|PRE-ASSISTANT|>' + message['content'] + '<|POST-ASSISTANT|>' -}} + {%- endif -%} + {%- endif -%} +{%- endfor -%} +{%- if add_generation_prompt -%} + {{-'<|PRE-ASSISTANT-GENERATE|>'-}} +{%- endif -%} +""" + + if 'context' in params and '<|system-message|>' in params['context']: + pre_system = params['context'].split('<|system-message|>')[0] + post_system = params['context'].split('<|system-message|>')[1] + else: + pre_system = '' + post_system = '' + + pre_user = params['turn_template'].split('<|user-message|>')[0].replace('<|user|>', params['user']) + post_user = params['turn_template'].split('<|user-message|>')[1].split('<|bot|>')[0] + + pre_assistant = '<|bot|>' + params['turn_template'].split('<|bot-message|>')[0].split('<|bot|>')[1] + pre_assistant = pre_assistant.replace('<|bot|>', params['bot']) + post_assistant = params['turn_template'].split('<|bot-message|>')[1] + + def preprocess(string): + return string.replace('\n', '\\n').replace('\'', '\\\'') + + pre_system = preprocess(pre_system) + post_system = preprocess(post_system) + pre_user = preprocess(pre_user) + post_user = preprocess(post_user) + pre_assistant = preprocess(pre_assistant) + post_assistant = preprocess(post_assistant) + + if verbose: + print( + '\n', + repr(pre_system) + '\n', + repr(post_system) + '\n', + repr(pre_user) + '\n', + repr(post_user) + '\n', + repr(pre_assistant) + '\n', + repr(post_assistant) + '\n', + ) + + result = MASTER_TEMPLATE + if 'system_message' in params: + result = result.replace('<|SYSTEM-MESSAGE|>', preprocess(params['system_message'])) + else: + result = result.replace('<|SYSTEM-MESSAGE|>', '') + + result = result.replace('<|PRE-SYSTEM|>', pre_system) + result = result.replace('<|POST-SYSTEM|>', post_system) + result = result.replace('<|PRE-USER|>', pre_user) + result = result.replace('<|POST-USER|>', post_user) + result = result.replace('<|PRE-ASSISTANT|>', pre_assistant) + result = result.replace('<|PRE-ASSISTANT-GENERATE|>', pre_assistant.rstrip(' ')) + result = result.replace('<|POST-ASSISTANT|>', post_assistant) + + result = result.strip() + + return result + + +def my_yaml_output(data): + ''' + pyyaml is very inconsistent with multiline strings. + for simple instruction template outputs, this is enough. + ''' + result = "" + for k in data: + result += k + ": |-\n" + for line in data[k].splitlines(): + result += " " + line.rstrip(' ') + "\n" + + return result diff --git a/python/llm/example/Text-Generation-WebUI/modules/evaluate.py b/python/llm/example/Text-Generation-WebUI/modules/evaluate.py new file mode 100644 index 00000000..3dd42b2d --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/evaluate.py @@ -0,0 +1,172 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/evaluate.py + + +import datetime +from pathlib import Path + +import pandas as pd +import torch +from datasets import load_dataset +from tqdm import tqdm + +from modules import shared +from modules.logging_colors import logger +from modules.models import clear_torch_cache, load_model, unload_model +from modules.models_settings import get_model_metadata, update_model_parameters +from modules.text_generation import encode + + +def load_past_evaluations(): + if Path('logs/evaluations.csv').exists(): + df = pd.read_csv(Path('logs/evaluations.csv'), dtype=str) + df['Perplexity'] = pd.to_numeric(df['Perplexity']) + return df + else: + return pd.DataFrame(columns=['Model', 'LoRAs', 'Dataset', 'Perplexity', 'stride', 'max_length', 'Date', 'Comment']) + + +past_evaluations = load_past_evaluations() + + +def save_past_evaluations(df): + global past_evaluations + past_evaluations = df + filepath = Path('logs/evaluations.csv') + filepath.parent.mkdir(parents=True, exist_ok=True) + df.to_csv(filepath, index=False) + + +def calculate_perplexity(models, input_dataset, stride, _max_length): + ''' + Based on: + https://huggingface.co/docs/transformers/perplexity#calculating-ppl-with-fixedlength-models + ''' + + if not shared.args.no_use_fast: + logger.warning("--no_use_fast is not being used. If tokenizing the input dataset takes a long time, consider loading the model with that option checked.") + + global past_evaluations + cumulative_log = '' + cumulative_log += "Loading the input dataset...\n\n" + yield cumulative_log + + # Copied from https://github.com/qwopqwop200/GPTQ-for-LLaMa/blob/triton/utils/datautils.py + if input_dataset == 'wikitext': + data = load_dataset('wikitext', 'wikitext-2-raw-v1', split='test') + text = "\n\n".join(data['text']) + elif input_dataset == 'ptb': + data = load_dataset('ptb_text_only', 'penn_treebank', split='validation') + text = "\n\n".join(data['sentence']) + elif input_dataset == 'ptb_new': + data = load_dataset('ptb_text_only', 'penn_treebank', split='test') + text = " ".join(data['sentence']) + else: + with open(Path(f'training/datasets/{input_dataset}.txt'), 'r', encoding='utf-8') as f: + text = f.read() + + for model in models: + if is_in_past_evaluations(model, input_dataset, stride, _max_length): + cumulative_log += f"`{model}` has already been tested. Ignoring.\n\n" + yield cumulative_log + continue + + if model != 'current model': + try: + yield cumulative_log + f"Loading `{model}`...\n\n" + model_settings = get_model_metadata(model) + shared.settings.update({k: v for k, v in model_settings.items() if k in shared.settings}) # hijacking the interface defaults + update_model_parameters(model_settings) # hijacking the command-line arguments + unload_model() + shared.model, shared.tokenizer = load_model(model) + except: + cumulative_log += f"Failed to load `{model}`. Moving on.\n\n" + yield cumulative_log + continue + + cumulative_log += f"Processing `{shared.model_name}`...\n\n" + yield cumulative_log + "Tokenizing the input dataset...\n\n" + encodings = encode(text, add_special_tokens=False) + seq_len = encodings.shape[1] + if _max_length: + max_length = _max_length + elif hasattr(shared.model.config, 'max_position_embeddings'): + max_length = shared.model.config.max_position_embeddings + else: + max_length = 2048 + + nlls = [] + prev_end_loc = 0 + for begin_loc in tqdm(range(0, seq_len, stride)): + yield cumulative_log + f"Evaluating... {100*begin_loc/seq_len:.2f}%" + end_loc = min(begin_loc + max_length, seq_len) + trg_len = end_loc - prev_end_loc # may be different from stride on last loop + input_ids = encodings[:, begin_loc:end_loc] + target_ids = input_ids.clone() + target_ids[:, :-trg_len] = -100 + clear_torch_cache() + with torch.no_grad(): + outputs = shared.model(input_ids=input_ids, labels=target_ids) + + # loss is calculated using CrossEntropyLoss which averages over valid labels + # N.B. the model only calculates loss over trg_len - 1 labels, because it internally shifts the labels + # to the left by 1. + neg_log_likelihood = outputs.loss + + nlls.append(neg_log_likelihood) + prev_end_loc = end_loc + if end_loc == seq_len: + break + + ppl = torch.exp(torch.stack(nlls).mean()) + add_entry_to_past_evaluations(float(ppl), shared.model_name, input_dataset, stride, _max_length) + save_past_evaluations(past_evaluations) + cumulative_log += f"The perplexity for `{shared.model_name}` is: {float(ppl)}\n\n" + yield cumulative_log + + +def add_entry_to_past_evaluations(perplexity, model, dataset, stride, max_length): + global past_evaluations + entry = { + 'Model': model, + 'LoRAs': ', '.join(shared.lora_names) or '-', + 'Dataset': dataset, + 'Perplexity': perplexity, + 'stride': str(stride), + 'max_length': str(max_length), + 'Date': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), + 'Comment': '' + } + past_evaluations = pd.concat([past_evaluations, pd.DataFrame([entry])], ignore_index=True) + + +def is_in_past_evaluations(model, dataset, stride, max_length): + entries = past_evaluations[(past_evaluations['Model'] == model) & + (past_evaluations['Dataset'] == dataset) & + (past_evaluations['max_length'] == str(max_length)) & + (past_evaluations['stride'] == str(stride))] + + if entries.shape[0] > 0: + return True + else: + return False + + +def generate_markdown_table(): + sorted_df = past_evaluations.sort_values(by=['Dataset', 'stride', 'Perplexity', 'Date']) + return sorted_df diff --git a/python/llm/example/Text-Generation-WebUI/modules/extensions.py b/python/llm/example/Text-Generation-WebUI/modules/extensions.py new file mode 100644 index 00000000..4be30947 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/extensions.py @@ -0,0 +1,248 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/extensions.py + + +import traceback +from functools import partial +from inspect import signature + +import gradio as gr + +import extensions +import modules.shared as shared +from modules.logging_colors import logger + +state = {} +available_extensions = [] +setup_called = set() + + +def apply_settings(extension, name): + if not hasattr(extension, 'params'): + return + + for param in extension.params: + _id = f"{name}-{param}" + if _id not in shared.settings: + continue + + extension.params[param] = shared.settings[_id] + + +def load_extensions(): + global state, setup_called + state = {} + for i, name in enumerate(shared.args.extensions): + if name in available_extensions: + if name != 'api': + logger.info(f'Loading the extension "{name}"') + try: + try: + exec(f"import extensions.{name}.script") + except ModuleNotFoundError: + logger.error(f"Could not import the requirements for '{name}'. Make sure to install the requirements for the extension.\n\nLinux / Mac:\n\npip install -r extensions/{name}/requirements.txt --upgrade\n\nWindows:\n\npip install -r extensions\\{name}\\requirements.txt --upgrade\n\nIf you used the one-click installer, paste the command above in the terminal window opened after launching the cmd script for your OS.") + raise + + extension = getattr(extensions, name).script + apply_settings(extension, name) + if extension not in setup_called and hasattr(extension, "setup"): + setup_called.add(extension) + extension.setup() + + state[name] = [True, i] + except: + logger.error(f'Failed to load the extension "{name}".') + traceback.print_exc() + + +# This iterator returns the extensions in the order specified in the command-line +def iterator(): + for name in sorted(state, key=lambda x: state[x][1]): + if state[name][0]: + yield getattr(extensions, name).script, name + + +# Extension functions that map string -> string +def _apply_string_extensions(function_name, text, state, is_chat=False): + for extension, _ in iterator(): + if hasattr(extension, function_name): + func = getattr(extension, function_name) + + # Handle old extensions without the 'state' arg or + # the 'is_chat' kwarg + count = 0 + has_chat = False + for k in signature(func).parameters: + if k == 'is_chat': + has_chat = True + else: + count += 1 + + if count == 2: + args = [text, state] + else: + args = [text] + + if has_chat: + kwargs = {'is_chat': is_chat} + else: + kwargs = {} + + text = func(*args, **kwargs) + + return text + + +# Extension functions that map string -> string +def _apply_chat_input_extensions(text, visible_text, state): + for extension, _ in iterator(): + if hasattr(extension, 'chat_input_modifier'): + text, visible_text = extension.chat_input_modifier(text, visible_text, state) + + return text, visible_text + + +# custom_generate_chat_prompt handling - currently only the first one will work +def _apply_custom_generate_chat_prompt(text, state, **kwargs): + for extension, _ in iterator(): + if hasattr(extension, 'custom_generate_chat_prompt'): + return extension.custom_generate_chat_prompt(text, state, **kwargs) + + return None + + +# Extension that modifies the input parameters before they are used +def _apply_state_modifier_extensions(state): + for extension, _ in iterator(): + if hasattr(extension, "state_modifier"): + state = getattr(extension, "state_modifier")(state) + + return state + + +# Extension that modifies the chat history before it is used +def _apply_history_modifier_extensions(history): + for extension, _ in iterator(): + if hasattr(extension, "history_modifier"): + history = getattr(extension, "history_modifier")(history) + + return history + + +# Extension functions that override the default tokenizer output - The order of execution is not defined +def _apply_tokenizer_extensions(function_name, state, prompt, input_ids, input_embeds): + for extension, _ in iterator(): + if hasattr(extension, function_name): + prompt, input_ids, input_embeds = getattr(extension, function_name)(state, prompt, input_ids, input_embeds) + + return prompt, input_ids, input_embeds + + +# Allow extensions to add their own logits processors to the stack being run. +# Each extension would call `processor_list.append({their LogitsProcessor}())`. +def _apply_logits_processor_extensions(function_name, processor_list, input_ids): + for extension, _ in iterator(): + if hasattr(extension, function_name): + result = getattr(extension, function_name)(processor_list, input_ids) + if type(result) is list: + processor_list = result + + return processor_list + + +# Get prompt length in tokens after applying extension functions which override the default tokenizer output +# currently only the first one will work +def _apply_custom_tokenized_length(prompt): + for extension, _ in iterator(): + if hasattr(extension, 'custom_tokenized_length'): + return getattr(extension, 'custom_tokenized_length')(prompt) + + return None + + +# Custom generate reply handling - currently only the first one will work +def _apply_custom_generate_reply(): + for extension, _ in iterator(): + if hasattr(extension, 'custom_generate_reply'): + return getattr(extension, 'custom_generate_reply') + + return None + + +def _apply_custom_css(): + all_css = '' + for extension, _ in iterator(): + if hasattr(extension, 'custom_css'): + all_css += getattr(extension, 'custom_css')() + + return all_css + + +def _apply_custom_js(): + all_js = '' + for extension, _ in iterator(): + if hasattr(extension, 'custom_js'): + all_js += getattr(extension, 'custom_js')() + + return all_js + + +def create_extensions_block(): + to_display = [] + for extension, name in iterator(): + if hasattr(extension, "ui") and not (hasattr(extension, 'params') and extension.params.get('is_tab', False)): + to_display.append((extension, name)) + + # Creating the extension ui elements + if len(to_display) > 0: + with gr.Column(elem_id="extensions"): + for row in to_display: + extension, _ = row + extension.ui() + + +def create_extensions_tabs(): + for extension, name in iterator(): + if hasattr(extension, "ui") and (hasattr(extension, 'params') and extension.params.get('is_tab', False)): + display_name = getattr(extension, 'params', {}).get('display_name', name) + with gr.Tab(display_name, elem_classes="extension-tab"): + extension.ui() + + +EXTENSION_MAP = { + "input": partial(_apply_string_extensions, "input_modifier"), + "output": partial(_apply_string_extensions, "output_modifier"), + "chat_input": _apply_chat_input_extensions, + "state": _apply_state_modifier_extensions, + "history": _apply_history_modifier_extensions, + "bot_prefix": partial(_apply_string_extensions, "bot_prefix_modifier"), + "tokenizer": partial(_apply_tokenizer_extensions, "tokenizer_modifier"), + 'logits_processor': partial(_apply_logits_processor_extensions, 'logits_processor_modifier'), + "custom_generate_chat_prompt": _apply_custom_generate_chat_prompt, + "custom_generate_reply": _apply_custom_generate_reply, + "tokenized_length": _apply_custom_tokenized_length, + "css": _apply_custom_css, + "js": _apply_custom_js +} + + +def apply_extensions(typ, *args, **kwargs): + if typ not in EXTENSION_MAP: + raise ValueError(f"Invalid extension type {typ}") + + return EXTENSION_MAP[typ](*args, **kwargs) diff --git a/python/llm/example/Text-Generation-WebUI/modules/github.py b/python/llm/example/Text-Generation-WebUI/modules/github.py new file mode 100644 index 00000000..c03662d3 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/github.py @@ -0,0 +1,57 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/github.py + + +import subprocess +from pathlib import Path + +new_extensions = set() + + +def clone_or_pull_repository(github_url): + global new_extensions + + repository_folder = Path("extensions") + repo_name = github_url.rstrip("/").split("/")[-1].split(".")[0] + + # Check if the repository folder exists + if not repository_folder.exists(): + repository_folder.mkdir(parents=True) + + repo_path = repository_folder / repo_name + + # Check if the repository is already cloned + if repo_path.exists(): + yield f"Updating {github_url}..." + # Perform a 'git pull' to update the repository + try: + pull_output = subprocess.check_output(["git", "-C", repo_path, "pull"], stderr=subprocess.STDOUT) + yield "Done." + return pull_output.decode() + except subprocess.CalledProcessError as e: + return str(e) + + # Clone the repository + try: + yield f"Cloning {github_url}..." + clone_output = subprocess.check_output(["git", "clone", github_url, repo_path], stderr=subprocess.STDOUT) + new_extensions.add(repo_name) + yield f"The extension `{repo_name}` has been downloaded.\n\nPlease close the the web UI completely and launch it again to be able to load it." + return clone_output.decode() + except subprocess.CalledProcessError as e: + return str(e) diff --git a/python/llm/example/Text-Generation-WebUI/modules/grammar/grammar_utils.py b/python/llm/example/Text-Generation-WebUI/modules/grammar/grammar_utils.py new file mode 100644 index 00000000..3b14b46c --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/grammar/grammar_utils.py @@ -0,0 +1,696 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/huggingface/transformers/pull/27557 + + +import logging +import re +import time +from abc import ABC +from functools import lru_cache +from typing import Dict, List + +import torch + +from modules import shared + +logger = logging.getLogger(__name__) + + +######################## +# EBNF Grammar Parsing # +######################## + +END_OF_ALTERNATE_MARKER = 0 +END_OF_RULE_MARKER = 0 +TO_BE_FILLED_MARKER = 0 +REF_RULE_MARKER = 1 +LITERAL_MARKER = 2 + + +class ParseState: + def __init__(self): + self.symbol_ids = {} + self.grammar_encoding = [] # old name: out_grammar + + +def get_symbol_id(state, src): + if src not in state.symbol_ids: + state.symbol_ids[src] = len(state.symbol_ids) + return state.symbol_ids[src] + + +def generate_symbol_id(state, base_name): + next_id = len(state.symbol_ids) + state.symbol_ids[base_name + "_" + str(next_id)] = next_id + return next_id + + +def is_word_char(c): + return c.isalnum() or c == "-" or c == "_" + + +def hex_to_int(c): + if c.isdigit(): + return int(c) + elif "a" <= c.lower() <= "f": + return ord(c.lower()) - ord("a") + 10 + return -1 + + +def remove_leading_white_space(src, newline_ok): + """ + Skips over whitespace and comments in the input string. + This function processes the input string, skipping over any spaces, tabs, + and content following a '#' character, which denotes a comment. The parsing + of a comment continues until the end of the line (denoted by newline characters + '\r' or '\n'). If the 'newline_ok' parameter is set to False, the function + will stop processing and return the remaining string upon encountering a + newline character, otherwise it will skip over newline characters as well. + Parameters: + src (str): The input string to be processed. + newline_ok (bool): A flag indicating whether encountering a newline character + should stop the parsing (False) or if it should be skipped (True). + Returns: + str: The remaining portion of the input string after skipping whitespace and comments. + """ + pos = 0 + while pos < len(src) and (src[pos].isspace() or src[pos] == "#"): + if src[pos] == "#": + while pos < len(src) and src[pos] not in ("\r", "\n"): + pos += 1 + else: + if not newline_ok and src[pos] in ("\r", "\n"): + break + pos += 1 + return src[pos:] + + +def parse_name(src): + pos = 0 + while pos < len(src) and is_word_char(src[pos]): + pos += 1 + if pos == 0: + raise RuntimeError("expecting name at " + src) + return src[:pos], src[pos:] + + +def parse_char(src): + """ + parse the leading char from the input string + :param src: + :return: char, remaining_src + """ + + # if we have a backslash, it's maybe an escape + if src[0] == "\\": + esc = src[1] + if esc == "x": + first = hex_to_int(src[2]) + if first > -1: + second = hex_to_int(src[3]) + if second > -1: + return (first << 4) + second, src[4:] + raise RuntimeError("expecting \\xNN at " + src) + elif esc in ('"', "[", "]"): + return esc, src[2:] + elif esc == "r": + return "\r", src[2:] + elif esc == "n": + return "\n", src[2:] + elif esc == "t": + return "\t", src[2:] + raise RuntimeError("unknown escape at " + src) + elif src: + return src[0], src[1:] + raise RuntimeError("unexpected end of input") + + +def parse_sequence(state, src, rule_name, outbuf, is_nested): + out_start_pos = len(outbuf) + + # sequence size, will be replaced at end when known + outbuf.append(TO_BE_FILLED_MARKER) + + last_sym_start = len(outbuf) + remaining_src = src + while remaining_src: + if remaining_src[0] == '"': # literal string + remaining_src = remaining_src[1:] + last_sym_start = len(outbuf) + while remaining_src[0] != '"': + char, remaining_src = parse_char(remaining_src) + + # each char of a literal is encoded as a "range" of char - char + outbuf.append(LITERAL_MARKER) + outbuf.append(ord(char)) + outbuf.append(ord(char)) + remaining_src = remove_leading_white_space(remaining_src[1:], is_nested) + elif remaining_src[0] == "[": # char range(s) + remaining_src = remaining_src[1:] + last_sym_start = len(outbuf) + # num chars in range - replaced at end of loop + outbuf.append(TO_BE_FILLED_MARKER) + while remaining_src[0] != "]": + char, remaining_src = parse_char(remaining_src) + + outbuf.append(ord(char)) + if remaining_src[0] == "-" and remaining_src[1] != "]": + endchar_pair, remaining_src = parse_char(remaining_src[1:]) + outbuf.append(ord(endchar_pair)) + else: + # chars that aren't part of a c1-c2 range are just doubled (i.e., c-c) + outbuf.append(ord(char)) + # replace num chars with actual + outbuf[last_sym_start] = len(outbuf) - last_sym_start - 1 + remaining_src = remove_leading_white_space(remaining_src[1:], is_nested) + elif is_word_char(remaining_src[0]): # rule reference + name, remaining_src = parse_name(remaining_src) + ref_rule_id = get_symbol_id(state, name) + remaining_src = remove_leading_white_space(remaining_src, is_nested) + last_sym_start = len(outbuf) + outbuf.append(REF_RULE_MARKER) + outbuf.append(ref_rule_id) + elif remaining_src[0] == "(": # grouping + # parse nested alternates into synthesized rule + remaining_src = remove_leading_white_space(remaining_src[1:], True) + sub_rule_id = generate_symbol_id(state, rule_name) + remaining_src = parse_alternates(state, remaining_src, rule_name, sub_rule_id, True) + last_sym_start = len(outbuf) + # output reference to synthesized rule + outbuf.append(REF_RULE_MARKER) + outbuf.append(sub_rule_id) + if remaining_src[0] != ")": + raise RuntimeError("expecting ')' at " + remaining_src) + remaining_src = remove_leading_white_space(remaining_src[1:], is_nested) + elif remaining_src[0] in ("*", "+", "?"): # repetition operator + if len(outbuf) - out_start_pos - 1 == 0: + raise RuntimeError("expecting preceeding item to */+/? at " + remaining_src) + out_grammar = state.grammar_encoding + + # apply transformation to previous symbol (last_sym_start - + # end) according to rewrite rules: + # S* --> S' ::= S S' | + # S+ --> S' ::= S S' | S + # S? --> S' ::= S | + sub_rule_id = generate_symbol_id(state, rule_name) + out_grammar.append(sub_rule_id) + sub_rule_start = len(out_grammar) + # placeholder for size of 1st alternate + out_grammar.append(TO_BE_FILLED_MARKER) + # add preceding symbol to generated rule + out_grammar.extend(outbuf[last_sym_start:]) + if remaining_src[0] in ("*", "+"): + # cause generated rule to recurse + out_grammar.append(REF_RULE_MARKER) + out_grammar.append(sub_rule_id) + # apply actual size + out_grammar[sub_rule_start] = len(out_grammar) - sub_rule_start + # mark end of 1st alternate + out_grammar.append(END_OF_ALTERNATE_MARKER) + sub_rule_start = len(out_grammar) + # placeholder for size of 2nd alternate + out_grammar.append(TO_BE_FILLED_MARKER) + if remaining_src[0] == "+": + # add preceding symbol as alternate only for '+' + out_grammar.extend(outbuf[last_sym_start:]) + # apply actual size of 2nd alternate + out_grammar[sub_rule_start] = len(out_grammar) - sub_rule_start + # mark end of 2nd alternate, then end of rule + out_grammar.append(END_OF_ALTERNATE_MARKER) + out_grammar.append(END_OF_RULE_MARKER) + + # in original rule, replace previous symbol with reference to generated rule + outbuf[last_sym_start:] = [1, sub_rule_id] + + remaining_src = remove_leading_white_space(remaining_src[1:], is_nested) + else: + break + # apply actual size of this alternate sequence + outbuf[out_start_pos] = len(outbuf) - out_start_pos + # mark end of alternate + outbuf.append(END_OF_ALTERNATE_MARKER) + return remaining_src + + +def parse_alternates(state, src, rule_name, rule_id, is_nested): + outbuf = [] + remaining_src = parse_sequence(state, src, rule_name, outbuf, is_nested) + while remaining_src and remaining_src[0] == "|": + remaining_src = remove_leading_white_space(remaining_src[1:], True) + remaining_src = parse_sequence(state, remaining_src, rule_name, outbuf, is_nested) + + state.grammar_encoding.append(rule_id) + state.grammar_encoding.extend(outbuf) + state.grammar_encoding.append(0) + return remaining_src + + +def parse_rule(state, src): + name, remaining_src = parse_name(src) + remaining_src = remove_leading_white_space(remaining_src, False) + rule_id = get_symbol_id(state, name) + + if remaining_src[:3] != "::=": + raise RuntimeError("expecting ::= at " + remaining_src) + remaining_src = remove_leading_white_space(remaining_src[3:], True) + + remaining_src = parse_alternates(state, remaining_src, name, rule_id, False) + + if remaining_src and remaining_src[0] == "\r": + remaining_src = remaining_src[2:] if remaining_src[1] == "\n" else remaining_src[1:] + elif remaining_src and remaining_src[0] == "\n": + remaining_src = remaining_src[1:] + elif remaining_src: + raise RuntimeError("expecting newline or end at " + remaining_src) + return remove_leading_white_space(remaining_src, True) + + +def parse_ebnf(src): + try: + state = ParseState() + grammar_repr = remove_leading_white_space(src, True) + last_grammar_repr = "" + while grammar_repr: + if last_grammar_repr: + last_parsed_rule_len = len(last_grammar_repr) - len(grammar_repr) + logger.debug(f"last_parsed_rule: {last_grammar_repr[:last_parsed_rule_len]}") + last_grammar_repr = grammar_repr + grammar_repr = parse_rule(state, grammar_repr) + state.grammar_encoding.append(0xFFFF) + return state + except RuntimeError as err: + logger.warning("error parsing grammar:", err) + return ParseState() + + +def print_rule(file, grammar_encoding, index, symbol_id_names): + rule_id = grammar_encoding[index] + print(f"<{index}>{symbol_id_names[rule_id]} ::=", end=" ", file=file) + pos = index + 1 + while grammar_encoding[pos]: + if pos - 1 > index: + print("|", end=" ", file=file) + pos += 1 # sequence size, not needed here + while grammar_encoding[pos]: + if grammar_encoding[pos] == REF_RULE_MARKER: + ref_rule_id = grammar_encoding[pos + 1] + print( + f"<{pos}>{symbol_id_names[ref_rule_id]}", + end=" ", + file=file, + ) + pos += 2 + else: + print("<{}>[".format(pos), end="", file=file) + num_chars = grammar_encoding[pos] + pos += 1 + + for i in range(0, num_chars, 2): + print("{}-".format(chr(grammar_encoding[pos + i])), end="", file=file) + if i + 1 < num_chars: + print("{}".format(chr(grammar_encoding[pos + i + 1])), end="", file=file) + print("]", end=" ", file=file) + pos += num_chars + pos += 1 + print(file=file) + return pos + 1 + + +def print_grammar(file, state): + pos = 0 + symbol_id_names = {v: k for k, v in state.symbol_ids.items()} + print("Grammar Rules:", file=file) + + while state.grammar_encoding[pos] != 0xFFFF: + pos = print_rule(file, state.grammar_encoding, pos, symbol_id_names) + pos = 0 + print("\nBinary representation:", file=file) + while state.grammar_encoding[pos] != 0xFFFF: + print(f"{state.grammar_encoding[pos]:04x}", end=" ", file=file) + pos += 1 + print("ffff\n") + + +################################### +# EBNF Grammar Parsing ends here # +################################### + + +class GrammarConstraint(ABC): + def __init__(self, grammar_str, start_rule_name, tokenizer): + self.tt = 0 + self.nt = 0 + state = parse_ebnf(grammar_str) + grammar_encoding = state.grammar_encoding + self.start_rule_id = state.symbol_ids.get(start_rule_name) + + self.eos_token_id = tokenizer.eos_token_id + self.token_trie = TokenTrie(tokenizer) + self.tokenizer = tokenizer + self.grammar_encoding = grammar_encoding + + pos = 0 + rules: Dict[int, int] = {} + + while grammar_encoding[pos] != 0xFFFF: + rule_id = grammar_encoding[pos] + + # Store the current position in the 'rules' list at the index corresponding to rule_id. + # This effectively maps each rule_id to its position in the grammar encoding. + rules[rule_id] = pos + pos += 1 + + # Continue to the next rule in the encoding. + # The loop advances by the size indicated at the current position (grammar_encoding[pos]) + # plus one for the size field itself. + while grammar_encoding[pos]: + pos += 1 + grammar_encoding[pos] + # Now we're at the end of the rule, + # so advance to the next rule by skipping the 0, which means 'end of rule'. + pos += 1 + + self.start_rule_pos = rules[self.start_rule_id] + self.rules_pos_dict: Dict[int, int] = rules + + def init_stacks(self): + # suppose the start rule position is 0, then grammar_encoding[0] = rule_id + # grammar_encoding[1] = rule_size + # grammar_encoding[2] = rule_type + # this is why we need to add 2 to the start rule position + stack = [self.start_rule_pos + 2] + # convert to tuple for caching(immutable) + return self.advance_stack(tuple(stack)) + + # For each stack, resolve rules to find the actual characters that are + # accepted by this stack (not the set of sub-rules). + # This is where the parsing happens. + # The parsing is a top-down, left-to-right, depth-first traversal of the + # grammar. + @lru_cache(maxsize=32768) + def advance_stack(self, stack): + stack = list(stack) + # If the stack is empty, we're done. Because no more tokens should be accepted. + if len(stack) == 0: + return [stack] + + # Get the top of the stack. + pos = stack[-1] + + # If the stack head is a terminal(literal), we can resolve it immediately. + # literal is marked with 2 in the grammar encoding. + if self.grammar_encoding[pos] > 1: + return [stack] + + # The stack head is a nonterminal (a rule reference, 1 in the grammar encoding). + # Resolving this rule gives a set of one or more possible positions + # (e.g. two in `a ::= b | c`) + # We pop the current rule off the stack and, for each option, push: + # - the symbol following this symbol in the current rule; then + # - the first symbol of the resolved rule. + referenced_rule_id = self.grammar_encoding[pos + 1] + + # subpos should points to the size of the subrule + subpos = self.rules_pos_dict[referenced_rule_id] + 1 + stacks: List[List[int]] = [] + + # do depth-first search to find all possible rules and check the next terminal + # When this value is non-zero, it indicates that subpos is not yet at the end of the rule, so we can continue. + # here subpos is a pointer, and the value in the rule encoding can never be 0 except for the end of the rule. + while self.grammar_encoding[subpos]: + new_stack = stack[:-1] + if self.grammar_encoding[pos + 2]: + # check if there is a next symbol in the current rule, e.g. `a ::= b c | d` + # if yes, push the pos to rule_size to the stack + new_stack.append(pos + 2) + + # if the type of the next symbol is not "empty", push the first symbol of the resolved rule to the stack + if self.grammar_encoding[subpos + 1]: + new_stack.append(subpos + 1) + stacks.extend(self.advance_stack(tuple(new_stack))) + # The increment subpos += self.grammar_encoding[subpos] + 1 + # moves subpos forward in the grammar encoding array to the next alternative in the current rule. + subpos += self.grammar_encoding[subpos] + 1 + return stacks + + def accept_char(self, *args, **kwargs): + """Process a byte according to the grammar rules.""" + raise NotImplementedError + + def accept_token_id(self, *args, **kwargs): + """Process a token according to the grammar rules.""" + raise NotImplementedError + + def filter_vocab(self, *args, **kwargs): + raise NotImplementedError + + +class IncrementalGrammarConstraint(GrammarConstraint): + def __init__(self, grammar_str, start_rule_name, tokenizer): + super().__init__(grammar_str, start_rule_name, tokenizer) + + def accept_char(self, byte, stacks): + new_stacks = [] + for stack in stacks: + # stack is empty + if not stack: + continue + + pos = stack[-1] + num_chars = self.grammar_encoding[pos] + + # to make pos point to the size of the char range rule + pos += 1 + found = False + for i in range(0, num_chars, 2): + if self.grammar_encoding[pos + i] <= byte and byte <= self.grammar_encoding[pos + i + 1]: + found = True + break + if not found: + continue + + pos += num_chars + new_stack = stack[:-1] + if self.grammar_encoding[pos]: + new_stack.append(pos) + new_stacks.extend(self.advance_stack(tuple(new_stack))) + + return new_stacks + + def accept_string(self, string: str, stacks: List[List[int]]): + _bytes = bytes(string, "utf-8") + for byte in _bytes: + stacks = self.accept_char(byte, stacks) + return stacks + + def accept_token_id(self, token_id: int, stacks: List[List[int]]): + if token_id == self.eos_token_id: + if stacks and all(len(stack) != 0 for stack in stacks): + raise Exception( + f"At least one of the stack should be empty when EOS is reached. However, " + f"the stacks are {stacks}" + ) + return [] + + for byte in self.token_trie.id2str(token_id): + stacks = self.accept_char(byte, stacks) + # check updated stacks + # TODO, I commented this out because it will fail when the stack is empty + # empty stack means the end of the grammar + # assert stacks != [] + + return stacks + + def accept_token_ids(self, token_ids: List[int], stacks: List[List[int]], as_string=True): + if as_string: + string = self.tokenizer.decode(token_ids) + stacks = self.accept_string(string, stacks) + else: + for token_id in token_ids: + stacks = self.accept_token_id(token_id, stacks) + return stacks + + def batch_filter_vocab(self, batch_stacks, device): + batch_acceptance = [] + for stacks in batch_stacks: + batch_acceptance.append(self.filter_vocab(stacks, device)) + return torch.stack(batch_acceptance) + + def filter_vocab(self, stacks, device): + if not stacks: # Check if stacks is empty + # Handle the empty case: for example, return a tensor of False + # The size of the tensor should match the size of your vocabulary + vocab_size = len(self.token_trie) + logger.debug(f"sum of acceptance: {0}") + return torch.zeros(vocab_size, dtype=torch.bool, device=device) + + acceptance_matrix = torch.cat([self.token_acceptance_for_stack(tuple(stack), device) for stack in stacks]) + # Merge stacks: any True => True + acceptance = acceptance_matrix.reshape(len(stacks), -1).any(dim=0) + logger.debug(f"sum of acceptance: {acceptance.sum()}") + return acceptance + + # For each sub-rule in the grammar, cache whether each byte is accepted. + @lru_cache(maxsize=None) + def pos_char_acceptance(self, pos): + acceptance = [False] * 256 + num_chars = self.grammar_encoding[pos] + pos += 1 + for i in range(0, num_chars, 2): + start = self.grammar_encoding[pos + i] + end = self.grammar_encoding[pos + i + 1] + for j in range(start, end + 1): + acceptance[j] = True + return acceptance + + # Probably this should be configurable. If the grammar has an exceedingly + # large number of states, the correct setting is a tradeoff between GPU + # RAM usage and recomputation time. + # + # The main variable that pushes usage up here is number of states in the + # grammar. + @lru_cache(maxsize=32768) + def token_acceptance_for_stack(self, stack, device): + st = time.time() + stack = list(stack) # needs to come in as a tuple for lru_cache + + accepts = [False] * len(self.token_trie) + accepts[self.eos_token_id] = len(stack) == 0 + if len(stack) == 0: + logger.debug("empty stack") + + def traverse_trie(trie, stacks): + for byte, next_trie in trie.items(): + if byte == LEAF: + token_id = next_trie + if token_id != self.eos_token_id: + accepts[token_id] = bool(stacks) + continue + + new_stacks = [] + for stk in stacks: + if not stk: + continue + + pos = stk[-1] + num_chars = self.grammar_encoding[pos] + + if not self.pos_char_acceptance(pos)[byte]: + continue + + pos += num_chars + 1 + new_stack = stk[:-1] + if self.grammar_encoding[pos]: + new_stack.append(pos) + new_stacks.extend(self.advance_stack(tuple(new_stack))) + + if new_stacks: + traverse_trie(next_trie, new_stacks) + + traverse_trie(self.token_trie.trie, [stack]) + + et = time.time() - st + x = torch.tensor(accepts, dtype=torch.bool, device=device) + self.tt += et + self.nt += 1 + return x + + +class StaticGrammarConstraint(GrammarConstraint): + def __init__(self, grammar_str, start_rule_name, tokenizer): + super().__init__(grammar_str, start_rule_name, tokenizer) + + def accept_char(self): + raise NotImplementedError + + +################# +# DATA STRUCTURES +################# + + +LEAF = -1 + + +class TokenTrie: + def __init__(self, tokenizer): + self.eos_token_id = tokenizer.eos_token_id + self.tokens = [] + self.trie = {} + self.load_tokens(tokenizer) + + def id2str(self, token_id): + return self.tokens[token_id] + + def __len__(self): + return len(self.tokens) + + def load_tokens(self, tokenizer): + def replace_hex(match): + hex_value = match.group(1) + return chr(int(hex_value, 16)) + + if "gpt2" in tokenizer.__class__.__name__.lower(): + special = tokenizer.additional_special_tokens_ids + + # Here, the decoder does a string replace on a bunch of sequences + # like ' .' for '.'. This interferes with our assumptions, where a + # token should always have exactly one representation. + # Fortunately(?) text-generation-inference doesn't seem to run this + # cleanup, so we get extraneous spaces. So, in order to generate + # the right token set for TGI, we have to skip the space trimming. + # See: + # https://github.com/huggingface/transformers/blob/main/src/transformers/tokenization_utils_base.py#L3588-L3600 + def fmt_token(id): + if id in special: + return None + return bytes(tokenizer.decode([id], clean_up_tokenization_spaces=False), "utf-8") + + elif "llama" in tokenizer.__class__.__name__.lower(): + + def fmt_token(id): + token = tokenizer.convert_ids_to_tokens(id) + token = re.sub(r"<0x([0-9a-fA-F]{2})>", replace_hex, token) + token = token.replace("▁", " ") + return bytes(token, "utf-8") + + else: + print("Warning: unrecognized tokenizer: using default token formatting") + + def fmt_token(id): + token = tokenizer.convert_ids_to_tokens(id) + return bytes(token, "utf-8") + + # note: vocab_size doesn't work here because there are also + # get_added_vocab() tokens + self.tokens = [fmt_token(i) for i in range(len(tokenizer.get_vocab()))] + for token_id, token_bytes in enumerate(self.tokens): + if token_bytes is not None: + self.insert_into_trie(self.trie, token_bytes, token_id) + + def insert_into_trie(self, trie, token_bytes, token_id): + current = trie + for byte in token_bytes: + if byte not in current: + current[byte] = {} + current = current[byte] + current[LEAF] = token_id + + +@lru_cache(maxsize=5) +def initialize_grammar(grammar_string): + return IncrementalGrammarConstraint(grammar_string.strip(), start_rule_name="root", tokenizer=shared.tokenizer) diff --git a/python/llm/example/Text-Generation-WebUI/modules/grammar/logits_process.py b/python/llm/example/Text-Generation-WebUI/modules/grammar/logits_process.py new file mode 100644 index 00000000..b11703c3 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/grammar/logits_process.py @@ -0,0 +1,113 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/huggingface/transformers/pull/27557 + + +import math + +import torch +from transformers.generation.logits_process import LogitsProcessor +from transformers.utils import add_start_docstrings + +LOGITS_PROCESSOR_INPUTS_DOCSTRING = r""" + Args: + input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): + Indices of input sequence tokens in the vocabulary. [What are input IDs?](../glossary#input-ids) + scores (`torch.FloatTensor` of shape `(batch_size, config.vocab_size)`): + Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam + search or log softmax for each vocabulary token when using beam search + + Return: + `torch.FloatTensor` of shape `(batch_size, config.vocab_size)`: The processed prediction scores. + +""" + + +class GrammarConstrainedLogitsProcessor(LogitsProcessor): + def __init__(self, grammar_constraint): + self.last_size = None + self.grammar_constraint = grammar_constraint + self.batch_stacks = None + + def filter_logits(self, logits, device): + # resolve each stack to a tensor of True/False for each token + # indicating acceptance + # acceptance = self.grammar_acceptor.filter_vocab(self.stacks, device) + acceptance = self.grammar_constraint.batch_filter_vocab(self.batch_stacks, device) + # logger.debug(acceptance) + # Logits to -inf where False + logits[~acceptance] = -math.inf + + # TODO: batching + def process_logits(self, input_ids, scores, parse_start_index=None): + """ + :param input_ids: + :param scores: + :param parse_start_index: default None, which means generate from scratch. Set to 0 to parse all input_ids + :return: + """ + # we dynamically create stacks at the first call, so that we know the batch size and beam size + if self.batch_stacks is None: + self.batch_stacks = [self.grammar_constraint.init_stacks() for _ in range(len(input_ids))] + + # if self.last_size is not set (which would be the case when processing the first token). + # In this case, do nothing. + if self.last_size is None: + prefix_to_parse = [ + single_input_ids[parse_start_index:] if parse_start_index is not None else [] + for single_input_ids in input_ids + ] + # self.grammar_acceptor.accept_token_ids(prefix_to_parse, self.stacks) + self.batch_stacks = [ + self.grammar_constraint.accept_token_ids(prefix, stack) + for prefix, stack in zip(prefix_to_parse, self.batch_stacks) + ] + # if the length of the current input IDs (input_ids[0]) is exactly one more than self.last_size. + # This is expected in a scenario where inputs are processed incrementally, one token at a time. + elif len(input_ids[0]) == self.last_size + 1: + # self.stacks = self.grammar_acceptor.accept_token_id(input_ids[0][-1], self.stacks) + self.batch_stacks = [ + self.grammar_constraint.accept_token_id(single_input_ids[-1], stack) + for single_input_ids, stack in zip(input_ids, self.batch_stacks) + ] + # ensure that the input size is consistent with the expected incremental processing + # (i.e., one token at a time). + else: + # here we check if the input_ids are one token longer than the last time we processed + # but we don't check if input_ids are actually valid. + # Imagine a scenario where we generate 10 tokens, then we replace the 10 generated tokens with 10 new tokens. + # In this case, the input_ids will be consistent with the last_size, but the input_ids are not valid. + # However, should we really check if the input_ids are valid here? + # If we do, then we need to reparse the whole input_ids at each call, which is not efficient. + # Maybe we should just trust the user to provide valid input_ids? + # The conclusion is that, we assume the input_ids are valid, and our generation will be correct. + # If the input_ids are not valid, then the generation result will be wrong and we don't take responsibility for that. + raise RuntimeError( + "Input ID's length is inconsistent with the current state of " + "the GrammarConstrainedLogitsProcessor. If you want to process " + "another input sequence, please instantiate a new " + "GrammarConstrainedLogitsProcessor." + ) + + self.filter_logits(scores, scores.device) + + self.last_size = len(input_ids[0]) + return scores + + @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING) + def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor: + return self.process_logits(input_ids, scores) diff --git a/python/llm/example/Text-Generation-WebUI/modules/html_generator.py b/python/llm/example/Text-Generation-WebUI/modules/html_generator.py new file mode 100644 index 00000000..d1eaed5a --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/html_generator.py @@ -0,0 +1,328 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/html_generator.py + + +import html +import os +import re +import time +from pathlib import Path + +import markdown +from PIL import Image, ImageOps + +from modules.utils import get_available_chat_styles +from modules import shared + +# This is to store the paths to the thumbnails of the profile pictures +image_cache = {} + +with open(Path(__file__).resolve().parent / '../css/html_readable_style.css', 'r') as f: + readable_css = f.read() +with open(Path(__file__).resolve().parent / '../css/html_4chan_style.css', 'r') as css_f: + _4chan_css = css_f.read() +with open(Path(__file__).resolve().parent / '../css/html_instruct_style.css', 'r') as f: + instruct_css = f.read() + +# Custom chat styles +chat_styles = {} +for k in get_available_chat_styles(): + chat_styles[k] = open(Path(f'css/chat_style-{k}.css'), 'r').read() + +# Handle styles that derive from other styles +for k in chat_styles: + lines = chat_styles[k].split('\n') + input_string = lines[0] + match = re.search(r'chat_style-([a-z\-]*)\.css', input_string) + + if match: + style = match.group(1) + chat_styles[k] = chat_styles.get(style, '') + '\n\n' + '\n'.join(lines[1:]) + + +def fix_newlines(string): + string = string.replace('\n', '\n\n') + string = re.sub(r"\n{3,}", "\n\n", string) + string = string.strip() + return string + + +def replace_blockquote(m): + return m.group().replace('\n', '\n> ').replace('\\begin{blockquote}', '').replace('\\end{blockquote}', '') + + +def convert_to_markdown(string): + + # Blockquote + string = re.sub(r'(^|[\n])>', r'\1>', string) + pattern = re.compile(r'\\begin{blockquote}(.*?)\\end{blockquote}', re.DOTALL) + string = pattern.sub(replace_blockquote, string) + + # Code + string = string.replace('\\begin{code}', '```') + string = string.replace('\\end{code}', '```') + string = re.sub(r"(.)```", r"\1\n```", string) + + result = '' + is_code = False + for line in string.split('\n'): + if line.lstrip(' ').startswith('```'): + is_code = not is_code + + result += line + if is_code or line.startswith('|'): # Don't add an extra \n for tables or code + result += '\n' + else: + result += '\n\n' + + result = result.strip() + if is_code: + result += '\n```' # Unfinished code block + + # Unfinished list, like "\n1.". A |delete| string is added and then + # removed to force a
    or
      to be generated instead of a

      . + if re.search(r'(\n\d+\.?|\n\*\s*)$', result): + delete_str = '|delete|' + + if re.search(r'(\d+\.?)$', result) and not result.endswith('.'): + result += '.' + + result = re.sub(r'(\n\d+\.?|\n\*\s*)$', r'\g<1> ' + delete_str, result) + + html_output = markdown.markdown(result, extensions=['fenced_code', 'tables']) + pos = html_output.rfind(delete_str) + if pos > -1: + html_output = html_output[:pos] + html_output[pos + len(delete_str):] + else: + html_output = markdown.markdown(result, extensions=['fenced_code', 'tables']) + + # Unescape code blocks + pattern = re.compile(r']*>(.*?)', re.DOTALL) + html_output = pattern.sub(lambda x: html.unescape(x.group()), html_output) + + return html_output + + +def generate_basic_html(string): + string = convert_to_markdown(string) + string = f'

      {string}
      ' + return string + + +def process_post(post, c): + t = post.split('\n') + number = t[0].split(' ')[1] + if len(t) > 1: + src = '\n'.join(t[1:]) + else: + src = '' + src = re.sub('>', '>', src) + src = re.sub('(>>[0-9]*)', '\\1', src) + src = re.sub('\n', '
      \n', src) + src = f'
      {src}\n' + src = f'Anonymous No.{number}\n{src}' + return src + + +def generate_4chan_html(f): + posts = [] + post = '' + c = -2 + for line in f.splitlines(): + line += "\n" + if line == '-----\n': + continue + elif line.startswith('--- '): + c += 1 + if post != '': + src = process_post(post, c) + posts.append(src) + post = line + else: + post += line + + if post != '': + src = process_post(post, c) + posts.append(src) + + for i in range(len(posts)): + if i == 0: + posts[i] = f'
      {posts[i]}
      \n' + else: + posts[i] = f'
      {posts[i]}
      \n' + + output = '' + output += f'
      ' + for post in posts: + output += post + + output += '
      ' + output = output.split('\n') + for i in range(len(output)): + output[i] = re.sub(r'^(>(.*?)(
      |))', r'\1', output[i]) + output[i] = re.sub(r'^
      (>(.*?)(
      |))', r'
      \1', output[i]) + + output = '\n'.join(output) + return output + + +def make_thumbnail(image): + image = image.resize((350, round(image.size[1] / image.size[0] * 350)), Image.Resampling.LANCZOS) + if image.size[1] > 470: + image = ImageOps.fit(image, (350, 470), Image.LANCZOS) + + return image + + +def get_image_cache(path): + cache_folder = Path(shared.args.disk_cache_dir) + if not cache_folder.exists(): + cache_folder.mkdir() + + mtime = os.stat(path).st_mtime + if (path in image_cache and mtime != image_cache[path][0]) or (path not in image_cache): + img = make_thumbnail(Image.open(path)) + + old_p = Path(f'{cache_folder}/{path.name}_cache.png') + p = Path(f'{cache_folder}/cache_{path.name}.png') + if old_p.exists(): + old_p.rename(p) + + output_file = p + img.convert('RGB').save(output_file, format='PNG') + image_cache[path] = [mtime, output_file.as_posix()] + + return image_cache[path][1] + + +def generate_instruct_html(history): + output = f'
      ' + for i, _row in enumerate(history): + row = [convert_to_markdown(entry) for entry in _row] + + if row[0]: # don't display empty user messages + output += f""" +
      +
      +
      + {row[0]} +
      +
      +
      + """ + + output += f""" +
      +
      +
      + {row[1]} +
      +
      +
      + """ + + output += "
      " + + return output + + +def generate_cai_chat_html(history, name1, name2, style, character, reset_cache=False): + output = f'
      ' + + # We use ?character and ?time.time() to force the browser to reset caches + img_bot = f'' if Path("cache/pfp_character_thumb.png").exists() else '' + img_me = f'' if Path("cache/pfp_me.png").exists() else '' + + for i, _row in enumerate(history): + row = [convert_to_markdown(entry) for entry in _row] + + if row[0]: # don't display empty user messages + output += f""" +
      +
      + {img_me} +
      +
      +
      + {name1} +
      +
      + {row[0]} +
      +
      +
      + """ + + output += f""" +
      +
      + {img_bot} +
      +
      +
      + {name2} +
      +
      + {row[1]} +
      +
      +
      + """ + + output += "
      " + return output + + +def generate_chat_html(history, name1, name2, reset_cache=False): + output = f'
      ' + + for i, _row in enumerate(history): + row = [convert_to_markdown(entry) for entry in _row] + + if row[0]: # don't display empty user messages + output += f""" +
      +
      +
      + {row[0]} +
      +
      +
      + """ + + output += f""" +
      +
      +
      + {row[1]} +
      +
      +
      + """ + + output += "
      " + return output + + +def chat_html_wrapper(history, name1, name2, mode, style, character, reset_cache=False): + if mode == 'instruct': + return generate_instruct_html(history['visible']) + elif style == 'wpp': + return generate_chat_html(history['visible'], name1, name2) + else: + return generate_cai_chat_html(history['visible'], name1, name2, style, character, reset_cache) diff --git a/python/llm/example/Text-Generation-WebUI/modules/loaders.py b/python/llm/example/Text-Generation-WebUI/modules/loaders.py new file mode 100644 index 00000000..3c0048ca --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/loaders.py @@ -0,0 +1,427 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/loaders.py + + +import functools +from collections import OrderedDict + +import gradio as gr + +from modules import shared + +loaders_and_params = OrderedDict({ + 'Transformers': [ + 'cpu_memory', + 'gpu_memory', + 'load_in_8bit', + 'bf16', + 'cpu', + 'disk', + 'auto_devices', + 'load_in_4bit', + 'use_double_quant', + 'quant_type', + 'compute_dtype', + 'trust_remote_code', + 'no_use_fast', + 'use_flash_attention_2', + 'alpha_value', + 'rope_freq_base', + 'compress_pos_emb', + 'disable_exllama', + 'disable_exllamav2', + 'transformers_info' + ], + 'llama.cpp': [ + 'n_ctx', + 'n_gpu_layers', + 'tensor_split', + 'n_batch', + 'threads', + 'threads_batch', + 'no_mmap', + 'mlock', + 'no_mul_mat_q', + 'alpha_value', + 'rope_freq_base', + 'compress_pos_emb', + 'cpu', + 'numa', + 'no_offload_kqv', + 'tensorcores', + ], + 'llamacpp_HF': [ + 'n_ctx', + 'n_gpu_layers', + 'tensor_split', + 'n_batch', + 'threads', + 'threads_batch', + 'no_mmap', + 'mlock', + 'no_mul_mat_q', + 'alpha_value', + 'rope_freq_base', + 'compress_pos_emb', + 'cpu', + 'numa', + 'cfg_cache', + 'trust_remote_code', + 'no_use_fast', + 'logits_all', + 'no_offload_kqv', + 'tensorcores', + 'llamacpp_HF_info', + ], + 'ExLlamav2_HF': [ + 'gpu_split', + 'max_seq_len', + 'cfg_cache', + 'no_flash_attn', + 'num_experts_per_token', + 'cache_8bit', + 'alpha_value', + 'compress_pos_emb', + 'trust_remote_code', + 'no_use_fast', + ], + 'ExLlamav2': [ + 'gpu_split', + 'max_seq_len', + 'no_flash_attn', + 'num_experts_per_token', + 'cache_8bit', + 'alpha_value', + 'compress_pos_emb', + 'exllamav2_info', + ], + 'AutoGPTQ': [ + 'triton', + 'no_inject_fused_attention', + 'no_inject_fused_mlp', + 'no_use_cuda_fp16', + 'wbits', + 'groupsize', + 'desc_act', + 'disable_exllama', + 'disable_exllamav2', + 'gpu_memory', + 'cpu_memory', + 'cpu', + 'disk', + 'auto_devices', + 'trust_remote_code', + 'no_use_fast', + 'autogptq_info', + ], + 'BigDL-LLM': [ + 'load_in_4bit', + 'load_in_low_bit', + 'optimize_model', + 'modules_to_not_convert', + 'cpu_embedding', + 'lightweight_bmm', + 'trust_remote_code', + 'use_cache', + ], + 'AutoAWQ': [ + 'cpu_memory', + 'gpu_memory', + 'auto_devices', + 'max_seq_len', + 'no_inject_fused_attention', + 'trust_remote_code', + 'no_use_fast', + ], + 'GPTQ-for-LLaMa': [ + 'wbits', + 'groupsize', + 'model_type', + 'pre_layer', + 'trust_remote_code', + 'no_use_fast', + 'gptq_for_llama_info', + ], + 'ctransformers': [ + 'n_ctx', + 'n_gpu_layers', + 'n_batch', + 'threads', + 'model_type', + 'no_mmap', + 'mlock' + ], + 'QuIP#': [ + 'trust_remote_code', + 'no_use_fast', + 'no_flash_attn', + 'quipsharp_info', + ], + 'HQQ': [ + 'hqq_backend', + 'trust_remote_code', + 'no_use_fast', + ] +}) + + +def transformers_samplers(): + return { + 'temperature', + 'temperature_last', + 'dynamic_temperature', + 'dynamic_temperature_low', + 'top_p', + 'min_p', + 'top_k', + 'typical_p', + 'epsilon_cutoff', + 'eta_cutoff', + 'tfs', + 'top_a', + 'repetition_penalty', + 'presence_penalty', + 'frequency_penalty', + 'repetition_penalty_range', + 'encoder_repetition_penalty', + 'no_repeat_ngram_size', + 'min_length', + 'seed', + 'do_sample', + 'penalty_alpha', + 'num_beams', + 'length_penalty', + 'early_stopping', + 'mirostat_mode', + 'mirostat_tau', + 'mirostat_eta', + 'grammar_file_row', + 'grammar_string', + 'guidance_scale', + 'negative_prompt', + 'ban_eos_token', + 'custom_token_bans', + 'add_bos_token', + 'skip_special_tokens', + 'auto_max_new_tokens', + } + + +loaders_samplers = { + 'Transformers': transformers_samplers(), + 'AutoGPTQ': transformers_samplers(), + 'GPTQ-for-LLaMa': transformers_samplers(), + 'AutoAWQ': transformers_samplers(), + 'QuIP#': transformers_samplers(), + 'HQQ': transformers_samplers(), + 'BigDL-LLM': transformers_samplers(), + 'ExLlamav2': { + 'temperature', + 'top_p', + 'min_p', + 'top_k', + 'typical_p', + 'tfs', + 'repetition_penalty', + 'repetition_penalty_range', + 'seed', + 'mirostat_mode', + 'mirostat_tau', + 'mirostat_eta', + 'ban_eos_token', + 'add_bos_token', + 'custom_token_bans', + 'skip_special_tokens', + 'auto_max_new_tokens', + }, + 'ExLlamav2_HF': { + 'temperature', + 'temperature_last', + 'dynamic_temperature', + 'dynamic_temperature_low', + 'top_p', + 'min_p', + 'top_k', + 'typical_p', + 'epsilon_cutoff', + 'eta_cutoff', + 'tfs', + 'top_a', + 'repetition_penalty', + 'presence_penalty', + 'frequency_penalty', + 'repetition_penalty_range', + 'encoder_repetition_penalty', + 'no_repeat_ngram_size', + 'min_length', + 'seed', + 'do_sample', + 'mirostat_mode', + 'mirostat_tau', + 'mirostat_eta', + 'grammar_file_row', + 'grammar_string', + 'guidance_scale', + 'negative_prompt', + 'ban_eos_token', + 'custom_token_bans', + 'add_bos_token', + 'skip_special_tokens', + 'auto_max_new_tokens', + }, + 'llama.cpp': { + 'temperature', + 'top_p', + 'min_p', + 'top_k', + 'typical_p', + 'tfs', + 'repetition_penalty', + 'presence_penalty', + 'frequency_penalty', + 'seed', + 'mirostat_mode', + 'mirostat_tau', + 'mirostat_eta', + 'grammar_file_row', + 'grammar_string', + 'ban_eos_token', + 'custom_token_bans', + }, + 'llamacpp_HF': { + 'temperature', + 'temperature_last', + 'dynamic_temperature', + 'dynamic_temperature_low', + 'top_p', + 'min_p', + 'top_k', + 'typical_p', + 'epsilon_cutoff', + 'eta_cutoff', + 'tfs', + 'top_a', + 'repetition_penalty', + 'presence_penalty', + 'frequency_penalty', + 'repetition_penalty_range', + 'encoder_repetition_penalty', + 'no_repeat_ngram_size', + 'min_length', + 'seed', + 'do_sample', + 'mirostat_mode', + 'mirostat_tau', + 'mirostat_eta', + 'grammar_file_row', + 'grammar_string', + 'guidance_scale', + 'negative_prompt', + 'ban_eos_token', + 'custom_token_bans', + 'add_bos_token', + 'skip_special_tokens', + 'auto_max_new_tokens', + }, + 'ctransformers': { + 'temperature', + 'top_p', + 'top_k', + 'repetition_penalty', + 'repetition_penalty_range', + }, +} + +loaders_model_types = { + 'GPTQ-for-LLaMa': [ + "None", + "llama", + "opt", + "gptj" + ], + 'ctransformers': [ + "None", + "gpt2", + "gptj", + "gptneox", + "llama", + "mpt", + "dollyv2", + "replit", + "starcoder", + "gptbigcode", + "falcon" + ], +} + + +@functools.cache +def list_all_samplers(): + all_samplers = set() + for k in loaders_samplers: + for sampler in loaders_samplers[k]: + all_samplers.add(sampler) + + return sorted(all_samplers) + + +def blacklist_samplers(loader): + all_samplers = list_all_samplers() + if loader == 'All': + return [gr.update(visible=True) for sampler in all_samplers] + else: + return [gr.update(visible=True) if sampler in loaders_samplers[loader] else gr.update(visible=False) for sampler in all_samplers] + + +def get_model_types(loader): + if loader in loaders_model_types: + return loaders_model_types[loader] + + return ["None"] + + +def get_gpu_memory_keys(): + return [k for k in shared.gradio if k.startswith('gpu_memory')] + + +@functools.cache +def get_all_params(): + all_params = set() + for k in loaders_and_params: + for el in loaders_and_params[k]: + all_params.add(el) + + if 'gpu_memory' in all_params: + all_params.remove('gpu_memory') + for k in get_gpu_memory_keys(): + all_params.add(k) + + return sorted(all_params) + + +def make_loader_params_visible(loader): + params = [] + all_params = get_all_params() + if loader in loaders_and_params: + params = loaders_and_params[loader] + + if 'gpu_memory' in params: + params.remove('gpu_memory') + params += get_gpu_memory_keys() + + return [gr.update(visible=True) if k in params else gr.update(visible=False) for k in all_params] diff --git a/python/llm/example/Text-Generation-WebUI/modules/logging_colors.py b/python/llm/example/Text-Generation-WebUI/modules/logging_colors.py new file mode 100644 index 00000000..ca2ed3fc --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/logging_colors.py @@ -0,0 +1,86 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/logging_colors.py + + +import logging + +logger = logging.getLogger('text-generation-webui') + + +def setup_logging(): + ''' + Copied from: https://github.com/vladmandic/automatic + + All credits to vladmandic. + ''' + + class RingBuffer(logging.StreamHandler): + def __init__(self, capacity): + super().__init__() + self.capacity = capacity + self.buffer = [] + self.formatter = logging.Formatter('{ "asctime":"%(asctime)s", "created":%(created)f, "facility":"%(name)s", "pid":%(process)d, "tid":%(thread)d, "level":"%(levelname)s", "module":"%(module)s", "func":"%(funcName)s", "msg":"%(message)s" }') + + def emit(self, record): + msg = self.format(record) + # self.buffer.append(json.loads(msg)) + self.buffer.append(msg) + if len(self.buffer) > self.capacity: + self.buffer.pop(0) + + def get(self): + return self.buffer + + from rich.console import Console + from rich.logging import RichHandler + from rich.pretty import install as pretty_install + from rich.theme import Theme + from rich.traceback import install as traceback_install + + level = logging.DEBUG + logger.setLevel(logging.DEBUG) # log to file is always at level debug for facility `sd` + console = Console(log_time=True, log_time_format='%H:%M:%S-%f', theme=Theme({ + "traceback.border": "black", + "traceback.border.syntax_error": "black", + "inspect.value.border": "black", + })) + logging.basicConfig(level=logging.ERROR, format='%(asctime)s | %(name)s | %(levelname)s | %(module)s | %(message)s', handlers=[logging.NullHandler()]) # redirect default logger to null + pretty_install(console=console) + traceback_install(console=console, extra_lines=1, max_frames=10, width=console.width, word_wrap=False, indent_guides=False, suppress=[]) + while logger.hasHandlers() and len(logger.handlers) > 0: + logger.removeHandler(logger.handlers[0]) + + # handlers + rh = RichHandler(show_time=True, omit_repeated_times=False, show_level=True, show_path=False, markup=False, rich_tracebacks=True, log_time_format='%H:%M:%S-%f', level=level, console=console) + rh.setLevel(level) + logger.addHandler(rh) + + rb = RingBuffer(100) # 100 entries default in log ring buffer + rb.setLevel(level) + logger.addHandler(rb) + logger.buffer = rb.buffer + + # overrides + logging.getLogger("urllib3").setLevel(logging.ERROR) + logging.getLogger("httpx").setLevel(logging.ERROR) + logging.getLogger("diffusers").setLevel(logging.ERROR) + logging.getLogger("torch").setLevel(logging.ERROR) + logging.getLogger("lycoris").handlers = logger.handlers + + +setup_logging() diff --git a/python/llm/example/Text-Generation-WebUI/modules/logits.py b/python/llm/example/Text-Generation-WebUI/modules/logits.py new file mode 100644 index 00000000..f3e20a11 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/logits.py @@ -0,0 +1,93 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/logits.py + + +import torch +from transformers import is_torch_xpu_available + +from modules import sampler_hijack, shared +from modules.logging_colors import logger +from modules.text_generation import generate_reply + +global_scores = None + + +def get_next_logits(prompt, state, use_samplers, previous, top_logits=25, return_dict=False): + if shared.model is None: + logger.error("No model is loaded! Select one in the Model tab.") + return 'Error: No model is loaded1 Select one in the Model tab.', previous + + is_non_hf_exllamav2 = shared.model.__class__.__name__ == 'Exllamav2Model' + is_non_hf_llamacpp = shared.model.__class__.__name__ == 'LlamaCppModel' + + if use_samplers: + if any([is_non_hf_exllamav2, is_non_hf_llamacpp]): + logger.error("Sampler hijacking is not supported non-Huggingface loaders.") + # sampling is all done in c for exllama, so it is really hard to hijack + # it should be possible to hijack llamacpp sampler by hijacking all their sampling methods, + # but it is not implemented yet + return 'Error: Sampler hijacking is not supported non-Huggingface loaders. Please disable the "Use samplers" option.', previous + + state['max_new_tokens'] = 1 + state['auto_max_new_tokens'] = False + for _ in generate_reply(prompt, state): + pass + + scores = sampler_hijack.global_scores[-1] + else: + if is_non_hf_exllamav2: + if is_torch_xpu_available(): + tokens = shared.tokenizer.encode(prompt).to("xpu:0") + else: + tokens = shared.tokenizer.encode(prompt).cuda() + scores = shared.model.get_logits(tokens)[-1][-1] + elif is_non_hf_llamacpp: + tokens = shared.tokenizer.encode(prompt) + scores = shared.model.get_logits(tokens)[-1][-1] + else: + if is_torch_xpu_available(): + tokens = shared.tokenizer.encode(prompt, return_tensors='pt').to("xpu:0") + else: + tokens = shared.tokenizer.encode(prompt, return_tensors='pt').cuda() + output = shared.model(input_ids=tokens) + scores = output['logits'][-1][-1] + + probs = torch.softmax(scores, dim=-1, dtype=torch.float) + topk_values, topk_indices = torch.topk(probs, k=top_logits, largest=True, sorted=True) + if is_non_hf_llamacpp: + topk_indices = [i.expand((1, 1)) for i in topk_indices] + + if hasattr(shared.tokenizer, 'convert_ids_to_tokens'): + tokens = [shared.tokenizer.convert_ids_to_tokens(int(i)) for i in topk_indices] + else: + tokens = [shared.tokenizer.decode(i) for i in topk_indices] + + if return_dict: + topk_values = [float(i) for i in topk_values] + output = {} + for row in list(zip(topk_values, tokens)): + output[row[1]] = row[0] + + return output + else: + topk_values = [f"{float(i):.5f}" for i in topk_values] + output = '' + for row in list(zip(topk_values, tokens)): + output += f"{row[0]} - {repr(row[1])}\n" + + return output, previous diff --git a/python/llm/example/Text-Generation-WebUI/modules/metadata_gguf.py b/python/llm/example/Text-Generation-WebUI/modules/metadata_gguf.py new file mode 100644 index 00000000..3e24f760 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/metadata_gguf.py @@ -0,0 +1,111 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/metadata_gguf.py + + +import struct +from enum import IntEnum + + +class GGUFValueType(IntEnum): + UINT8 = 0 + INT8 = 1 + UINT16 = 2 + INT16 = 3 + UINT32 = 4 + INT32 = 5 + FLOAT32 = 6 + BOOL = 7 + STRING = 8 + ARRAY = 9 + UINT64 = 10 + INT64 = 11 + FLOAT64 = 12 + + +_simple_value_packing = { + GGUFValueType.UINT8: " 1, shared.args.alpha_value > 1, shared.args.disable_exllama, shared.args.disable_exllamav2]): + model = LoaderClass.from_pretrained(path_to_model, **params) + if torch.backends.mps.is_available(): + device = torch.device('mps') + model = model.to(device) + elif is_xpu_available(): + device = torch.device("xpu") + model = model.to(device) + else: + model = model.cuda() + + # DeepSpeed ZeRO-3 + elif shared.args.deepspeed: + model = LoaderClass.from_pretrained(path_to_model, torch_dtype=params['torch_dtype']) + model = deepspeed.initialize(model=model, config_params=ds_config, model_parameters=None, optimizer=None, lr_scheduler=None)[0] + model.module.eval() # Inference + logger.info(f'DeepSpeed ZeRO-3 is enabled: {is_deepspeed_zero3_enabled()}') + + # Load with quantization and/or offloading + else: + if not any((shared.args.cpu, torch.cuda.is_available(), is_xpu_available(), torch.backends.mps.is_available())): + logger.warning('torch.cuda.is_available() and is_xpu_available() returned False. This means that no GPU has been detected. Falling back to CPU mode.') + shared.args.cpu = True + + if shared.args.cpu: + params['torch_dtype'] = torch.float32 + else: + params['device_map'] = 'auto' + params['max_memory'] = get_max_memory_dict() + if shared.args.load_in_4bit: + # See https://github.com/huggingface/transformers/pull/23479/files + # and https://huggingface.co/blog/4bit-transformers-bitsandbytes + quantization_config_params = { + 'load_in_4bit': True, + 'bnb_4bit_compute_dtype': eval("torch.{}".format(shared.args.compute_dtype)) if shared.args.compute_dtype in ["bfloat16", "float16", "float32"] else None, + 'bnb_4bit_quant_type': shared.args.quant_type, + 'bnb_4bit_use_double_quant': shared.args.use_double_quant, + } + + logger.info('Using the following 4-bit params: ' + str(quantization_config_params)) + params['quantization_config'] = BitsAndBytesConfig(**quantization_config_params) + + elif shared.args.load_in_8bit: + if any((shared.args.auto_devices, shared.args.gpu_memory)): + params['quantization_config'] = BitsAndBytesConfig(load_in_8bit=True, llm_int8_enable_fp32_cpu_offload=True) + else: + params['quantization_config'] = BitsAndBytesConfig(load_in_8bit=True) + + if params['max_memory'] is not None: + with init_empty_weights(): + model = LoaderClass.from_config(config, trust_remote_code=params['trust_remote_code']) + + model.tie_weights() + params['device_map'] = infer_auto_device_map( + model, + dtype=torch.int8, + max_memory=params['max_memory'], + no_split_module_classes=model._no_split_modules + ) + + if shared.args.disk: + params['offload_folder'] = shared.args.disk_cache_dir + + if shared.args.disable_exllama or shared.args.disable_exllamav2: + try: + gptq_config = GPTQConfig( + bits=config.quantization_config.get('bits', 4), + disable_exllama=shared.args.disable_exllama, + disable_exllamav2=shared.args.disable_exllamav2, + ) + + params['quantization_config'] = gptq_config + logger.info(f'Loading with disable_exllama={shared.args.disable_exllama} and disable_exllamav2={shared.args.disable_exllamav2}.') + except: + exc = traceback.format_exc() + logger.error('Failed to disable exllama. Does the config.json for this model contain the necessary quantization info?') + print(exc) + + if shared.args.compress_pos_emb > 1: + params['rope_scaling'] = {'type': 'linear', 'factor': shared.args.compress_pos_emb} + elif shared.args.alpha_value > 1: + params['rope_scaling'] = {'type': 'dynamic', 'factor': RoPE.get_alpha_value(shared.args.alpha_value, shared.args.rope_freq_base)} + + model = LoaderClass.from_pretrained(path_to_model, **params) + + return model + + +def llamacpp_loader(model_name): + from modules.llamacpp_model import LlamaCppModel + + path = Path(f'{shared.args.model_dir}/{model_name}') + if path.is_file(): + model_file = path + else: + model_file = list(Path(f'{shared.args.model_dir}/{model_name}').glob('*.gguf'))[0] + + logger.info(f"llama.cpp weights detected: {model_file}") + model, tokenizer = LlamaCppModel.from_pretrained(model_file) + return model, tokenizer + + +def llamacpp_HF_loader(model_name): + from modules.llamacpp_hf import LlamacppHF + + for fname in [model_name, "oobabooga_llama-tokenizer", "llama-tokenizer"]: + path = Path(f'{shared.args.model_dir}/{fname}') + if all((path / file).exists() for file in ['tokenizer_config.json', 'special_tokens_map.json', 'tokenizer.model']): + logger.info(f'Using tokenizer from: {path}') + break + else: + logger.error("Could not load the model because a tokenizer in transformers format was not found. Please download oobabooga/llama-tokenizer.") + return None, None + + if shared.args.no_use_fast: + logger.info('Loading the tokenizer with use_fast=False.') + + tokenizer = AutoTokenizer.from_pretrained( + path, + trust_remote_code=shared.args.trust_remote_code, + use_fast=not shared.args.no_use_fast + ) + + model = LlamacppHF.from_pretrained(model_name) + return model, tokenizer + + +def ctransformers_loader(model_name): + from modules.ctransformers_model import CtransformersModel + + path = Path(f'{shared.args.model_dir}/{model_name}') + ctrans = CtransformersModel() + if ctrans.model_type_is_auto(): + model_file = path + else: + if path.is_file(): + model_file = path + else: + entries = Path(f'{shared.args.model_dir}/{model_name}') + gguf = list(entries.glob('*.gguf')) + bin = list(entries.glob('*.bin')) + if len(gguf) > 0: + model_file = gguf[0] + elif len(bin) > 0: + model_file = bin[0] + else: + logger.error("Could not find a model for ctransformers.") + return None, None + + logger.info(f'ctransformers weights detected: {model_file}') + model, tokenizer = ctrans.from_pretrained(model_file) + return model, tokenizer + + +def AutoAWQ_loader(model_name): + from awq import AutoAWQForCausalLM + + model_dir = Path(f'{shared.args.model_dir}/{model_name}') + + model = AutoAWQForCausalLM.from_quantized( + quant_path=model_dir, + max_new_tokens=shared.args.max_seq_len, + trust_remote_code=shared.args.trust_remote_code, + fuse_layers=not shared.args.no_inject_fused_attention, + max_memory=get_max_memory_dict(), + batch_size=1, + safetensors=any(model_dir.glob('*.safetensors')), + ) + + return model + +def bigdl_llm_loader(model_name): + + from bigdl.llm.transformers import AutoModelForCausalLM, AutoModel, AutoModelForSeq2SeqLM + + path_to_model = Path(f'{shared.args.model_dir}/{model_name}') + + config = AutoConfig.from_pretrained(path_to_model, trust_remote_code=shared.args.trust_remote_code) + + if 'chatglm' in model_name.lower(): + LoaderClass = AutoModel + else: + if config.to_dict().get('is_encoder_decoder', False): + LoaderClass = AutoModelForSeq2SeqLM + shared.is_seq2seq = True + else: + LoaderClass = AutoModelForCausalLM + + model = LoaderClass.from_pretrained( + path_to_model, + load_in_4bit=shared.args.load_in_4bit, + load_in_low_bit=shared.args.load_in_low_bit, + optimize_model=shared.args.optimize_model, + modules_to_not_convert=shared.args.modules_to_not_convert, + cpu_embedding=shared.args.cpu_embedding, + lightweight_bmm=shared.args.lightweight_bmm, + trust_remote_code=shared.args.trust_remote_code, + use_cache=shared.args.use_cache, + ) + + if shared.args.device == "GPU": + import intel_extension_for_pytorch + model = model.to("xpu") + + tokenizer = AutoTokenizer.from_pretrained(path_to_model, trust_remote_code=shared.args.trust_remote_code) + + return model, tokenizer + + +def QuipSharp_loader(model_name): + try: + with RelativeImport("repositories/quip-sharp"): + from lib.utils.unsafe_import import model_from_hf_path + except: + logger.error( + "\nQuIP# has not been found. It must be installed manually for now.\n" + "For instructions on how to do that, please consult:\n" + "https://github.com/oobabooga/text-generation-webui/pull/4803\n" + ) + return None, None + + # This fixes duplicate logging messages after the import above. + handlers = logging.getLogger().handlers + if len(handlers) > 1: + logging.getLogger().removeHandler(handlers[1]) + + model_dir = Path(f'{shared.args.model_dir}/{model_name}') + if not all((model_dir / file).exists() for file in ['tokenizer_config.json', 'special_tokens_map.json', 'tokenizer.model']): + logger.error(f"Could not load the model because the tokenizer files could not be found in the model folder. Please download the following files from the original (unquantized) model into {model_dir}: special_tokens_map.json, tokenizer.json, tokenizer.model, tokenizer_config.json.") + return None, None + + model, model_str = model_from_hf_path( + model_dir, + use_cuda_graph=False, + use_flash_attn=not shared.args.no_flash_attn + ) + + return model + + +def GPTQ_loader(model_name): + + # Monkey patch + if shared.args.monkey_patch: + logger.warning("Applying the monkey patch for using LoRAs with GPTQ models. It may cause undefined behavior outside its intended scope.") + from modules.monkey_patch_gptq_lora import load_model_llama + + model, _ = load_model_llama(model_name) + + # No monkey patch + else: + import modules.GPTQ_loader + + model = modules.GPTQ_loader.load_quantized(model_name) + + return model + + +def AutoGPTQ_loader(model_name): + import modules.AutoGPTQ_loader + + return modules.AutoGPTQ_loader.load_quantized(model_name) + + +def ExLlamav2_loader(model_name): + from modules.exllamav2 import Exllamav2Model + + model, tokenizer = Exllamav2Model.from_pretrained(model_name) + return model, tokenizer + + +def ExLlamav2_HF_loader(model_name): + from modules.exllamav2_hf import Exllamav2HF + + return Exllamav2HF.from_pretrained(model_name) + + +def HQQ_loader(model_name): + from hqq.core.quantize import HQQBackend, HQQLinear + from hqq.engine.hf import HQQModelForCausalLM + + logger.info(f"Loading HQQ model with backend: {shared.args.hqq_backend}") + + model_dir = Path(f'{shared.args.model_dir}/{model_name}') + model = HQQModelForCausalLM.from_quantized(str(model_dir)) + HQQLinear.set_backend(getattr(HQQBackend, shared.args.hqq_backend)) + return model + + +def get_max_memory_dict(): + max_memory = {} + max_cpu_memory = shared.args.cpu_memory.strip() if shared.args.cpu_memory is not None else '99GiB' + if shared.args.gpu_memory: + memory_map = list(map(lambda x: x.strip(), shared.args.gpu_memory)) + for i in range(len(memory_map)): + max_memory[i] = f'{memory_map[i]}GiB' if not re.match('.*ib$', memory_map[i].lower()) else memory_map[i] + + max_memory['cpu'] = f'{max_cpu_memory}GiB' if not re.match('.*ib$', max_cpu_memory.lower()) else max_cpu_memory + + # If --auto-devices is provided standalone, try to get a reasonable value + # for the maximum memory of device :0 + elif shared.args.auto_devices: + if is_xpu_available(): + total_mem = (torch.xpu.get_device_properties(0).total_memory / (1024 * 1024)) + else: + total_mem = (torch.cuda.get_device_properties(0).total_memory / (1024 * 1024)) + + suggestion = round((total_mem - 1000) / 1000) * 1000 + if total_mem - suggestion < 800: + suggestion -= 1000 + + suggestion = int(round(suggestion / 1000)) + logger.warning(f"Auto-assiging --gpu-memory {suggestion} for your GPU to try to prevent out-of-memory errors. You can manually set other values.") + max_memory[0] = f'{suggestion}GiB' + max_memory['cpu'] = f'{max_cpu_memory}GiB' if not re.match('.*ib$', max_cpu_memory.lower()) else max_cpu_memory + + return max_memory if len(max_memory) > 0 else None + + +def clear_torch_cache(): + gc.collect() + if not shared.args.cpu: + if is_xpu_available(): + torch.xpu.empty_cache() + else: + torch.cuda.empty_cache() + + +def unload_model(): + shared.model = shared.tokenizer = None + shared.model_name = 'None' + shared.lora_names = [] + shared.model_dirty_from_training = False + clear_torch_cache() + + +def reload_model(): + unload_model() + shared.model, shared.tokenizer = load_model(shared.model_name) diff --git a/python/llm/example/Text-Generation-WebUI/modules/models_settings.py b/python/llm/example/Text-Generation-WebUI/modules/models_settings.py new file mode 100644 index 00000000..41dabc50 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/models_settings.py @@ -0,0 +1,288 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/models_settings.py + + +import json +import re +from pathlib import Path + +import yaml + +from modules import chat, loaders, metadata_gguf, shared, ui + + +def get_fallback_settings(): + return { + 'wbits': 'None', + 'groupsize': 'None', + 'desc_act': False, + 'model_type': 'None', + 'max_seq_len': 2048, + 'n_ctx': 2048, + 'rope_freq_base': 0, + 'compress_pos_emb': 1, + 'truncation_length': shared.settings['truncation_length'], + 'skip_special_tokens': shared.settings['skip_special_tokens'], + 'custom_stopping_strings': shared.settings['custom_stopping_strings'], + } + + +def get_model_metadata(model): + model_settings = {} + + # Get settings from models/config.yaml and models/config-user.yaml + settings = shared.model_config + for pat in settings: + if re.match(pat.lower(), model.lower()): + for k in settings[pat]: + model_settings[k] = settings[pat][k] + + path = Path(f'{shared.args.model_dir}/{model}/config.json') + if path.exists(): + hf_metadata = json.loads(open(path, 'r', encoding='utf-8').read()) + else: + hf_metadata = None + + if 'loader' not in model_settings: + if hf_metadata is not None and 'quip_params' in hf_metadata: + loader = 'QuIP#' + else: + loader = infer_loader(model, model_settings) + + model_settings['loader'] = loader + + # GGUF metadata + if model_settings['loader'] in ['llama.cpp', 'llamacpp_HF', 'ctransformers']: + path = Path(f'{shared.args.model_dir}/{model}') + if path.is_file(): + model_file = path + else: + model_file = list(path.glob('*.gguf'))[0] + + metadata = metadata_gguf.load_metadata(model_file) + if 'llama.context_length' in metadata: + model_settings['n_ctx'] = metadata['llama.context_length'] + if 'llama.rope.scale_linear' in metadata: + model_settings['compress_pos_emb'] = metadata['llama.rope.scale_linear'] + if 'llama.rope.freq_base' in metadata: + model_settings['rope_freq_base'] = metadata['llama.rope.freq_base'] + if 'tokenizer.chat_template' in metadata: + template = metadata['tokenizer.chat_template'] + eos_token = metadata['tokenizer.ggml.tokens'][metadata['tokenizer.ggml.eos_token_id']] + bos_token = metadata['tokenizer.ggml.tokens'][metadata['tokenizer.ggml.bos_token_id']] + template = template.replace('eos_token', "'{}'".format(eos_token)) + template = template.replace('bos_token', "'{}'".format(bos_token)) + + template = re.sub(r'raise_exception\([^)]*\)', "''", template) + model_settings['instruction_template'] = 'Custom (obtained from model metadata)' + model_settings['instruction_template_str'] = template + + else: + # Transformers metadata + if hf_metadata is not None: + metadata = json.loads(open(path, 'r', encoding='utf-8').read()) + if 'max_position_embeddings' in metadata: + model_settings['truncation_length'] = metadata['max_position_embeddings'] + model_settings['max_seq_len'] = metadata['max_position_embeddings'] + + if 'rope_theta' in metadata: + model_settings['rope_freq_base'] = metadata['rope_theta'] + + if 'rope_scaling' in metadata and type(metadata['rope_scaling']) is dict and all(key in metadata['rope_scaling'] for key in ('type', 'factor')): + if metadata['rope_scaling']['type'] == 'linear': + model_settings['compress_pos_emb'] = metadata['rope_scaling']['factor'] + + if 'quantization_config' in metadata: + if 'bits' in metadata['quantization_config']: + model_settings['wbits'] = metadata['quantization_config']['bits'] + if 'group_size' in metadata['quantization_config']: + model_settings['groupsize'] = metadata['quantization_config']['group_size'] + if 'desc_act' in metadata['quantization_config']: + model_settings['desc_act'] = metadata['quantization_config']['desc_act'] + + # Read AutoGPTQ metadata + path = Path(f'{shared.args.model_dir}/{model}/quantize_config.json') + if path.exists(): + metadata = json.loads(open(path, 'r', encoding='utf-8').read()) + if 'bits' in metadata: + model_settings['wbits'] = metadata['bits'] + if 'group_size' in metadata: + model_settings['groupsize'] = metadata['group_size'] + if 'desc_act' in metadata: + model_settings['desc_act'] = metadata['desc_act'] + + # Try to find the Jinja instruct template + path = Path(f'{shared.args.model_dir}/{model}') / 'tokenizer_config.json' + if path.exists(): + metadata = json.loads(open(path, 'r', encoding='utf-8').read()) + if 'chat_template' in metadata: + template = metadata['chat_template'] + for k in ['eos_token', 'bos_token']: + if k in metadata: + value = metadata[k] + if type(value) is dict: + value = value['content'] + + template = template.replace(k, "'{}'".format(value)) + + template = re.sub(r'raise_exception\([^)]*\)', "''", template) + model_settings['instruction_template'] = 'Custom (obtained from model metadata)' + model_settings['instruction_template_str'] = template + + if 'instruction_template' not in model_settings: + model_settings['instruction_template'] = 'Alpaca' + + if model_settings['instruction_template'] != 'Custom (obtained from model metadata)': + model_settings['instruction_template_str'] = chat.load_instruction_template(model_settings['instruction_template']) + + # Ignore rope_freq_base if set to the default value + if 'rope_freq_base' in model_settings and model_settings['rope_freq_base'] == 10000: + model_settings.pop('rope_freq_base') + + # Apply user settings from models/config-user.yaml + settings = shared.user_config + for pat in settings: + if re.match(pat.lower(), model.lower()): + for k in settings[pat]: + model_settings[k] = settings[pat][k] + + return model_settings + + +def infer_loader(model_name, model_settings): + path_to_model = Path(f'{shared.args.model_dir}/{model_name}') + if not path_to_model.exists(): + loader = None + elif (path_to_model / 'quantize_config.json').exists() or ('wbits' in model_settings and type(model_settings['wbits']) is int and model_settings['wbits'] > 0): + loader = 'ExLlamav2_HF' + elif (path_to_model / 'quant_config.json').exists() or re.match(r'.*-awq', model_name.lower()): + loader = 'AutoAWQ' + elif len(list(path_to_model.glob('*.gguf'))) > 0: + loader = 'llama.cpp' + elif re.match(r'.*\.gguf', model_name.lower()): + loader = 'llama.cpp' + elif re.match(r'.*exl2', model_name.lower()): + loader = 'ExLlamav2_HF' + elif re.match(r'.*-hqq', model_name.lower()): + return 'HQQ' + else: + loader = 'Transformers' + + return loader + + +def update_model_parameters(state, initial=False): + ''' + UI: update the command-line arguments based on the interface values + ''' + elements = ui.list_model_elements() # the names of the parameters + gpu_memories = [] + + for i, element in enumerate(elements): + if element not in state: + continue + + value = state[element] + if element.startswith('gpu_memory'): + gpu_memories.append(value) + continue + + if initial and element in shared.provided_arguments: + continue + + # Setting null defaults + if element in ['wbits', 'groupsize', 'model_type'] and value == 'None': + value = vars(shared.args_defaults)[element] + elif element in ['cpu_memory'] and value == 0: + value = vars(shared.args_defaults)[element] + + # Making some simple conversions + if element in ['wbits', 'groupsize', 'pre_layer']: + value = int(value) + elif element == 'cpu_memory' and value is not None: + value = f"{value}MiB" + + if element in ['pre_layer']: + value = [value] if value > 0 else None + + setattr(shared.args, element, value) + + found_positive = False + for i in gpu_memories: + if i > 0: + found_positive = True + break + + if not (initial and vars(shared.args)['gpu_memory'] != vars(shared.args_defaults)['gpu_memory']): + if found_positive: + shared.args.gpu_memory = [f"{i}MiB" for i in gpu_memories] + else: + shared.args.gpu_memory = None + + +def apply_model_settings_to_state(model, state): + ''' + UI: update the state variable with the model settings + ''' + model_settings = get_model_metadata(model) + if 'loader' in model_settings: + loader = model_settings.pop('loader') + + # If the user is using an alternative loader for the same model type, let them keep using it + if not (loader == 'ExLlamav2_HF' and state['loader'] in ['GPTQ-for-LLaMa', 'ExLlamav2', 'AutoGPTQ']) and not (loader == 'llama.cpp' and state['loader'] in ['llamacpp_HF', 'ctransformers']): + state['loader'] = loader + + for k in model_settings: + if k in state: + if k in ['wbits', 'groupsize']: + state[k] = str(model_settings[k]) + else: + state[k] = model_settings[k] + + return state + + +def save_model_settings(model, state): + ''' + Save the settings for this model to models/config-user.yaml + ''' + if model == 'None': + yield ("Not saving the settings because no model is loaded.") + return + + with Path(f'{shared.args.model_dir}/config-user.yaml') as p: + if p.exists(): + user_config = yaml.safe_load(open(p, 'r').read()) + else: + user_config = {} + + model_regex = model + '$' # For exact matches + if model_regex not in user_config: + user_config[model_regex] = {} + + for k in ui.list_model_elements(): + if k == 'loader' or k in loaders.loaders_and_params[state['loader']]: + user_config[model_regex][k] = state[k] + + shared.user_config = user_config + + output = yaml.dump(user_config, sort_keys=False) + with open(p, 'w') as f: + f.write(output) + + yield (f"Settings for `{model}` saved to `{p}`.") diff --git a/python/llm/example/Text-Generation-WebUI/modules/one_click_installer_check.py b/python/llm/example/Text-Generation-WebUI/modules/one_click_installer_check.py new file mode 100644 index 00000000..edb2f397 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/one_click_installer_check.py @@ -0,0 +1,28 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/one_click_installer_check.py + + +from pathlib import Path + +from modules.logging_colors import logger + +if Path('../webui.py').exists(): + logger.warning('\nIt looks like you are running an outdated version of ' + 'the one-click-installers.\n' + 'Please migrate your installation following the instructions here:\n' + 'https://github.com/oobabooga/text-generation-webui/wiki/Migrating-an-old-one%E2%80%90click-install') diff --git a/python/llm/example/Text-Generation-WebUI/modules/presets.py b/python/llm/example/Text-Generation-WebUI/modules/presets.py new file mode 100644 index 00000000..89a8c85c --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/presets.py @@ -0,0 +1,138 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/presets.py + + +import functools +import random +from pathlib import Path + +import yaml + +from modules import shared +from modules.loaders import loaders_samplers + + +def default_preset(): + return { + 'temperature': 1, + 'temperature_last': False, + 'dynamic_temperature': False, + 'dynamic_temperature_low': 0.1, + 'top_p': 1, + 'min_p': 0, + 'top_k': 0, + 'repetition_penalty': 1, + 'presence_penalty': 0, + 'frequency_penalty': 0, + 'repetition_penalty_range': 1024, + 'typical_p': 1, + 'tfs': 1, + 'top_a': 0, + 'epsilon_cutoff': 0, + 'eta_cutoff': 0, + 'guidance_scale': 1, + 'penalty_alpha': 0, + 'mirostat_mode': 0, + 'mirostat_tau': 5, + 'mirostat_eta': 0.1, + 'do_sample': True, + 'encoder_repetition_penalty': 1, + 'no_repeat_ngram_size': 0, + 'min_length': 0, + 'num_beams': 1, + 'length_penalty': 1, + 'early_stopping': False, + } + + +def presets_params(): + return [k for k in default_preset()] + + +def load_preset(name): + generate_params = default_preset() + if name not in ['None', None, '']: + with open(Path(f'presets/{name}.yaml'), 'r') as infile: + preset = yaml.safe_load(infile) + + for k in preset: + generate_params[k] = preset[k] + + return generate_params + + +@functools.cache +def load_preset_memoized(name): + return load_preset(name) + + +def load_preset_for_ui(name, state): + generate_params = load_preset(name) + state.update(generate_params) + return state, *[generate_params[k] for k in presets_params()] + + +def random_preset(state): + params_and_values = { + 'remove_tail_tokens': { + 'top_p': [0.5, 0.8, 0.9, 0.95, 0.99], + 'min_p': [0.5, 0.2, 0.1, 0.05, 0.01], + 'top_k': [3, 5, 10, 20, 30, 40], + 'typical_p': [0.2, 0.575, 0.95], + 'tfs': [0.5, 0.8, 0.9, 0.95, 0.99], + 'top_a': [0.5, 0.2, 0.1, 0.05, 0.01], + 'epsilon_cutoff': [1, 3, 5, 7, 9], + 'eta_cutoff': [3, 6, 9, 12, 15, 18], + }, + 'flatten_distribution': { + 'temperature': [0.5, 0.7, 0.8, 1, 1.2, 1.5, 2.0], + }, + 'repetition': { + 'repetition_penalty': [1, 1.05, 1.1, 1.15, 1.20, 1.25], + 'presence_penalty': [0, 0.1, 0.2, 0.4, 0.6, 0.8, 1.0, 2.0], + 'frequency_penalty': [0, 0.1, 0.2, 0.4, 0.6, 0.8, 1.0, 2.0], + }, + 'other': { + 'temperature_last': [True, False], + } + } + + generate_params = default_preset() + for cat in params_and_values: + choices = list(params_and_values[cat].keys()) + if shared.args.loader is not None: + choices = [x for x in choices if x in loaders_samplers[shared.args.loader]] + + if len(choices) > 0: + choice = random.choice(choices) + generate_params[choice] = random.choice(params_and_values[cat][choice]) + + state.update(generate_params) + return state, *[generate_params[k] for k in presets_params()] + + +def generate_preset_yaml(state): + defaults = default_preset() + data = {k: state[k] for k in presets_params()} + + # Remove entries that are identical to the defaults + for k in list(data.keys()): + if data[k] == defaults[k]: + del data[k] + + return yaml.dump(data, sort_keys=False) diff --git a/python/llm/example/Text-Generation-WebUI/modules/prompts.py b/python/llm/example/Text-Generation-WebUI/modules/prompts.py new file mode 100644 index 00000000..9720adc9 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/prompts.py @@ -0,0 +1,46 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/prompts.py + + +from pathlib import Path + +from modules.text_generation import get_encoded_length + + +def load_prompt(fname): + if fname in ['None', '']: + return '' + else: + file_path = Path(f'prompts/{fname}.txt') + if not file_path.exists(): + return '' + + with open(file_path, 'r', encoding='utf-8') as f: + text = f.read() + if text[-1] == '\n': + text = text[:-1] + + return text + + +def count_tokens(text): + try: + tokens = get_encoded_length(text) + return str(tokens) + except: + return '0' diff --git a/python/llm/example/Text-Generation-WebUI/modules/relative_imports.py b/python/llm/example/Text-Generation-WebUI/modules/relative_imports.py new file mode 100644 index 00000000..8243fed2 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/relative_imports.py @@ -0,0 +1,32 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/relative_imports.py + + +import sys +from pathlib import Path + + +class RelativeImport: + def __init__(self, path): + self.import_path = Path(path) + + def __enter__(self): + sys.path.insert(0, str(self.import_path)) + + def __exit__(self, exc_type, exc_value, traceback): + sys.path.remove(str(self.import_path)) diff --git a/python/llm/example/Text-Generation-WebUI/modules/sampler_hijack.py b/python/llm/example/Text-Generation-WebUI/modules/sampler_hijack.py new file mode 100644 index 00000000..98e6dc62 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/sampler_hijack.py @@ -0,0 +1,403 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/sampler_hijack.py + + +import math + +import torch +import transformers +from transformers import LogitsWarper, is_torch_xpu_available +from transformers.generation.logits_process import ( + LogitNormalization, + LogitsProcessor, + LogitsProcessorList, + TemperatureLogitsWarper +) + +from modules import shared + +global_scores = None + + +class TemperatureLogitsWarperWithDynatemp(LogitsWarper): + def __init__(self, temperature: float, dynamic_temperature: bool, dynamic_temperature_low: float): + if not isinstance(temperature, float) or not (temperature > 0): + except_msg = ( + f"`temperature` (={temperature}) has to be a strictly positive float, otherwise your next token " + "scores will be invalid." + ) + if isinstance(temperature, float) and temperature == 0.0: + except_msg += " If you're looking for greedy decoding strategies, set `do_sample=False`." + + raise ValueError(except_msg) + + self.temperature = temperature + self.dynamic_temperature = dynamic_temperature + self.dynamic_temperature_low = dynamic_temperature_low + + def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor: + + # Regular temperature + if not self.dynamic_temperature: + scores = scores / self.temperature + return scores + + # Dynamic temperature + else: + min_temp = self.dynamic_temperature_low + max_temp = self.temperature + exponent_val = 1.0 + + # Convert logits to probabilities + probs = torch.softmax(scores, dim=-1) + + # Calculate entropy of the softmax probabilities + entropy = -1.0 * torch.where(probs > 0, probs * torch.log(probs), torch.zeros_like(probs)).sum() + + # Guard against future possible division by zero + entropy = max(entropy, torch.tensor(1e-10)) # Ensures entropy is slightly greater than 0 + + # Any logits which are not -Infinity will be considered for calculating max entropy. + num_valid_tokens = torch.sum(scores > -float('inf')).item() + + # Now, calculate the max entropy by using only the valid tokens' count + max_entropy = math.log(num_valid_tokens) + + # Guard against future possible division by zero + max_entropy = max_entropy if max_entropy > 0.0 else 1e-10 + + # Normalize the entropy + normalized_entropy = entropy / max_entropy + + # Map the normalized entropy to the desired temperature range using the power function + dyn_temp = min_temp + (max_temp - min_temp) * (normalized_entropy.pow(exponent_val)) + + # Apply the dynamically calculated temperature scaling + scores = scores / dyn_temp + + # print("----------------------\nTemperature from generation_config:", self.temperature) + # print("min_temp:", min_temp) + # print("max_temp:", max_temp) + # print("Entropy:", entropy.item()) + # print("Max Possible Entropy considering valid tokens only:", max_entropy) + # print("Normalized Entropy:", normalized_entropy.item()) + # print("Dynamic Temperature (dyn_temp):", dyn_temp.item()) + # print("----------------------") + + # max_prob_token_id = torch.argmax(scores, dim=-1) # Get the token ID with the highest probability + # max_prob_token = shared.tokenizer.convert_ids_to_tokens(int(max_prob_token_id)) # Convert ID to token + # print("--- T=", float(dyn_temp), "token=", max_prob_token, "min=", min_temp, "max=", max_temp) + + return scores + + +class MinPLogitsWarper(LogitsWarper): + def __init__(self, min_p: float, filter_value: float = -float("Inf"), min_tokens_to_keep: int = 1): + if min_p < 0 or min_p > 1.0: + raise ValueError(f"`min_p` has to be a float >= 0 and <= 1, but is {min_p}") + self.min_p = min_p + self.filter_value = filter_value + self.min_tokens_to_keep = min_tokens_to_keep + + def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor: + # Convert logits to probabilities + probs = torch.softmax(scores, dim=-1) + # Get the probability of the top token for each sequence in the batch + top_probs, _ = probs.max(dim=-1, keepdim=True) + # Calculate the actual min_p threshold by scaling min_p with the top token's probability + scaled_min_p = self.min_p * top_probs + # Create a mask for tokens that have a probability less than the scaled min_p + tokens_to_remove = probs < scaled_min_p + + sorted_indices = torch.argsort(scores, descending=True, dim=-1) + sorted_indices_to_remove = torch.gather(tokens_to_remove, dim=-1, index=sorted_indices) + + if self.min_tokens_to_keep > 1: + # Keep at least min_tokens_to_keep + sorted_indices_to_remove[..., : self.min_tokens_to_keep] = False + + indices_to_remove = sorted_indices_to_remove.scatter(1, sorted_indices, sorted_indices_to_remove) + scores = scores.masked_fill(indices_to_remove, self.filter_value) + return scores + + +class TailFreeLogitsWarper(LogitsWarper): + def __init__(self, tfs: float, filter_value: float = -float("Inf"), min_tokens_to_keep: int = 1): + tfs = float(tfs) + if tfs < 0 or tfs > 1.0: + raise ValueError(f"`tfs` has to be a float >= 0 and <= 1, but is {tfs}") + self.tfs = tfs + self.filter_value = filter_value + self.min_tokens_to_keep = min_tokens_to_keep + + def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor: + sorted_logits, sorted_indices = torch.sort(scores, descending=True) + probs = sorted_logits.softmax(dim=-1) + + # Compute second derivative normalized CDF + d2 = probs.diff().diff().abs() + normalized_d2 = d2 / d2.sum(dim=-1, keepdim=True) + normalized_d2_cdf = normalized_d2.cumsum(dim=-1) + + # Remove tokens with CDF value above the threshold (token with 0 are kept) + sorted_indices_to_remove = normalized_d2_cdf > self.tfs + + # Centre the distribution around the cutoff as in the original implementation of the algorithm + sorted_indices_to_remove = torch.cat( + ( + torch.zeros(scores.shape[0], 1, dtype=torch.bool, device=scores.device), + sorted_indices_to_remove, + torch.ones(scores.shape[0], 1, dtype=torch.bool, device=scores.device), + ), + dim=-1, + ) + + if self.min_tokens_to_keep > 1: + # Keep at least min_tokens_to_keep + sorted_indices_to_remove[..., : self.min_tokens_to_keep] = 0 + + indices_to_remove = sorted_indices_to_remove.scatter(1, sorted_indices, sorted_indices_to_remove) + scores = scores.masked_fill(indices_to_remove, self.filter_value) + return scores + + +class TopALogitsWarper(LogitsWarper): + def __init__(self, top_a: float, filter_value: float = -float("Inf"), min_tokens_to_keep: int = 1): + top_a = float(top_a) + if top_a < 0 or top_a > 1.0: + raise ValueError(f"`top_a` has to be a float >= 0 and <= 1, but is {top_a}") + self.top_a = top_a + self.filter_value = filter_value + self.min_tokens_to_keep = min_tokens_to_keep + + def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor: + sorted_logits, sorted_indices = torch.sort(scores, descending=True) + probs = sorted_logits.softmax(dim=-1) + + # Remove tokens with probability less than top_a*(max(probs))^2 (token with 0 are kept) + probs_max = probs[..., 0, None] + sorted_indices_to_remove = probs < probs_max * probs_max * self.top_a + + if self.min_tokens_to_keep > 1: + # Keep at least min_tokens_to_keep + sorted_indices_to_remove[..., : self.min_tokens_to_keep] = 0 + + indices_to_remove = sorted_indices_to_remove.scatter(1, sorted_indices, sorted_indices_to_remove) + scores = scores.masked_fill(indices_to_remove, self.filter_value) + return scores + + +class MirostatLogitsWarper(LogitsWarper): + def __init__(self, mirostat_mode: int, mirostat_tau: float, mirostat_eta: float, filter_value: float = -float("Inf"), min_tokens_to_keep: int = 1): + if mirostat_mode not in [2]: + raise ValueError(f"`mirostat` has to be a an integer 2, but is {mirostat_mode}") + self.mirostat_mode = mirostat_mode + self.mirostat_eta = mirostat_eta + self.mirostat_tau = mirostat_tau + self.filter_value = filter_value + self.min_tokens_to_keep = min_tokens_to_keep + self.mu = 2 * self.mirostat_tau + self.e = 0 + + def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor: + logits = scores[0] + sorted_logits, sorted_indices = torch.sort(logits, descending=True) + prob_original = torch.softmax(sorted_logits, dim=-1).tolist() # candidates + + # Truncate the words with surprise values greater than mu + for i, candidate in enumerate(prob_original): + if candidate > 0 and -math.log2(candidate) > self.mu: + if (i == 0): + sorted_logits = sorted_logits[:1] + else: + sorted_logits = sorted_logits[:i] + break + + # Normalize the probabilities of the remaining words + if is_torch_xpu_available(): + prob_topk = torch.softmax(sorted_logits, dim=0).to("xpu") + prev_i = torch.multinomial(prob_topk, num_samples=1, replacement=True).to("xpu") + else: + prob_topk = torch.softmax(sorted_logits, dim=0).to('cuda') + prev_i = torch.multinomial(prob_topk, num_samples=1, replacement=True).to('cuda') + + observed_surprise = -math.log2(prob_topk[prev_i]) + self.e = observed_surprise - self.mirostat_tau + + # Update mu using the learning rate and error + self.mu -= self.mirostat_eta * self.e + + sorted_indices_to_remove = torch.ones_like(scores[0], dtype=torch.bool) + sorted_indices_to_remove[prev_i] = False + + indices_to_remove = sorted_indices_to_remove.unsqueeze(0).scatter(1, sorted_indices.unsqueeze(0), sorted_indices_to_remove.unsqueeze(0)) + scores = scores.masked_fill(indices_to_remove, self.filter_value) + return scores + + +class SpyLogitsWarper(LogitsWarper): + def __init__(self): + pass + + def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor: + global global_scores + global_scores = scores + return scores + + +class RepetitionPenaltyLogitsProcessorWithRange(LogitsProcessor): + ''' + Copied from the transformers library + ''' + + def __init__(self, penalty: float, presence_penalty: float, frequency_penalty: float, _range: int): + if not (penalty > 0): + raise ValueError(f"`penalty` has to be strictly positive, but is {penalty}") + + self.penalty = penalty + self.presence_penalty = presence_penalty + self.frequency_penalty = frequency_penalty + self._range = _range + + def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor: + input_ids = input_ids[:, -self._range:] + + # We loop here because torch.unique() needs to process each row separately in the + # case that batch_size > 1. + for input_ids_row, scores_row in zip(input_ids, scores): + unique_ids, counts = torch.unique(input_ids_row, return_counts=True) + score = torch.gather(scores_row, 0, unique_ids) + + # multiplicative repetition penalty + # if score < 0 then repetition penalty has to be multiplied to reduce the previous token probability + score = torch.where(score < 0, score * self.penalty, score / self.penalty) + scores_row.scatter_(0, unique_ids, score) + + # presence_penalty and frequency_penalty + raw_presence_penalty = (counts > 0).to(scores.dtype) + raw_frequency_penalty = counts.to(scores.dtype) + additive_penalty = raw_presence_penalty * self.presence_penalty + raw_frequency_penalty * self.frequency_penalty + scores_row.scatter_add_(0, unique_ids, -additive_penalty) + + return scores + + +def get_logits_warper_patch(self, generation_config): + # Make sure that temperature is float and not int + if isinstance(generation_config.temperature, int): + generation_config.temperature = float(generation_config.temperature) + + temperature = generation_config.temperature + if generation_config.dynamic_temperature: + # Make sure TemperatureLogitsWarper will be created by temporarily + # setting temperature to a value != 1. + generation_config.temperature = 1.1 + + warpers = self._get_logits_warper_old(generation_config) + for i in range(len(warpers)): + if warpers[i].__class__.__name__ == 'TemperatureLogitsWarper': + warpers[i] = TemperatureLogitsWarperWithDynatemp(temperature, generation_config.dynamic_temperature, generation_config.dynamic_temperature_low) + + warpers_to_add = LogitsProcessorList() + min_tokens_to_keep = 2 if generation_config.num_beams > 1 else 1 + + if generation_config.mirostat_mode is not None and generation_config.mirostat_mode == 2: + warpers_to_add.append(MirostatLogitsWarper(mirostat_mode=generation_config.mirostat_mode, mirostat_eta=generation_config.mirostat_eta, mirostat_tau=generation_config.mirostat_tau, min_tokens_to_keep=min_tokens_to_keep)) + # We need to disable samplers other than temperature + for warper in warpers: + if not isinstance(warper, TemperatureLogitsWarper): + warpers.remove(warper) + else: + if generation_config.tfs is not None and 0.0 <= generation_config.tfs < 1.0: + warpers_to_add.append(TailFreeLogitsWarper(tfs=generation_config.tfs, min_tokens_to_keep=min_tokens_to_keep)) + if generation_config.top_a is not None and 0.0 < generation_config.top_a <= 1.0: + warpers_to_add.append(TopALogitsWarper(top_a=generation_config.top_a, min_tokens_to_keep=min_tokens_to_keep)) + if generation_config.min_p is not None and 0.0 < generation_config.min_p <= 1.0: + warpers_to_add.append(MinPLogitsWarper(min_p=generation_config.min_p, min_tokens_to_keep=min_tokens_to_keep)) + + if len(warpers) > 0 and isinstance(warpers[-1], LogitNormalization): + normalize = warpers.pop(-1) + else: + normalize = None + + warpers += warpers_to_add + if generation_config.temperature_last: + temperature_idx = None + for i in range(len(warpers)): + if warpers[i].__class__.__name__ in ['TemperatureLogitsWarper', 'TemperatureLogitsWarperWithDynatemp']: + temperature_idx = i + break + + if temperature_idx is not None: + warpers.append(warpers.pop(temperature_idx)) + + if normalize is not None: + warpers.append(normalize) + + warpers.append(SpyLogitsWarper()) + warpers = LogitsProcessorList(warpers) + # for i in range(len(warpers)): + # print(warpers[i].__class__.__name__) + return warpers + + +def get_logits_processor_patch(self, **kwargs): + repetition_penalty = kwargs['generation_config'].repetition_penalty + presence_penalty = kwargs['generation_config'].presence_penalty + frequency_penalty = kwargs['generation_config'].frequency_penalty + repetition_penalty_range = kwargs['generation_config'].repetition_penalty_range + do_rep_pen_hijack = (repetition_penalty > 1) or (presence_penalty != 0) or (frequency_penalty != 0) + if do_rep_pen_hijack: + # Make sure that a RepetitionPenaltyLogitsProcessor will be created + kwargs['generation_config'].repetition_penalty = 1.1 # must set to some value > 1 + + result = self._get_logits_processor_old(**kwargs) + + if do_rep_pen_hijack: + for i in range(len(result)): + if result[i].__class__.__name__ == 'RepetitionPenaltyLogitsProcessor': + result[i] = RepetitionPenaltyLogitsProcessorWithRange(repetition_penalty, presence_penalty, frequency_penalty, repetition_penalty_range) + + return result + + +def generation_config_init_patch(self, **kwargs): + self.__init___old(**kwargs) + self.min_p = kwargs.pop("min_p", 0.0) + self.dynamic_temperature = kwargs.pop("dynamic_temperature", False) + self.dynamic_temperature_low = kwargs.pop("dynamic_temperature_low", 0.1) + self.tfs = kwargs.pop("tfs", 1.0) + self.top_a = kwargs.pop("top_a", 0.0) + self.mirostat_mode = kwargs.pop("mirostat_mode", 0) + self.mirostat_eta = kwargs.pop("mirostat_eta", 0.1) + self.mirostat_tau = kwargs.pop("mirostat_tau", 5) + self.repetition_penalty_range = kwargs.pop("repetition_penalty_range", 0) + self.presence_penalty = kwargs.pop("presence_penalty", 0) + self.frequency_penalty = kwargs.pop("frequency_penalty", 0) + self.temperature_last = kwargs.pop("temperature_last", False) + + +def hijack_samplers(): + transformers.GenerationMixin._get_logits_warper_old = transformers.GenerationMixin._get_logits_warper + transformers.GenerationMixin._get_logits_warper = get_logits_warper_patch + + transformers.GenerationMixin._get_logits_processor_old = transformers.GenerationMixin._get_logits_processor + transformers.GenerationMixin._get_logits_processor = get_logits_processor_patch + + transformers.GenerationConfig.__init___old = transformers.GenerationConfig.__init__ + transformers.GenerationConfig.__init__ = generation_config_init_patch diff --git a/python/llm/example/Text-Generation-WebUI/modules/shared.py b/python/llm/example/Text-Generation-WebUI/modules/shared.py new file mode 100644 index 00000000..ce569319 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/shared.py @@ -0,0 +1,339 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/shared.py + + +import argparse +import os +import sys +from collections import OrderedDict +from pathlib import Path + +import yaml + +from modules.logging_colors import logger + +# Model variables +model = None +tokenizer = None +model_name = 'None' +is_seq2seq = False +model_dirty_from_training = False +lora_names = [] + +# Generation variables +stop_everything = False +generation_lock = None +processing_message = '*Is typing...*' + +# UI variables +gradio = {} +persistent_interface_state = {} +need_restart = False + +# UI defaults +settings = { + 'dark_theme': True, + 'show_controls': True, + 'start_with': '', + 'mode': 'chat', + 'chat_style': 'cai-chat', + 'prompt-default': 'QA', + 'prompt-notebook': 'QA', + 'preset': 'simple-1', + 'max_new_tokens': 512, + 'max_new_tokens_min': 1, + 'max_new_tokens_max': 4096, + 'negative_prompt': '', + 'seed': -1, + 'truncation_length': 2048, + 'truncation_length_min': 0, + 'truncation_length_max': 200000, + 'max_tokens_second': 0, + 'max_updates_second': 0, + 'custom_stopping_strings': '', + 'custom_token_bans': '', + 'auto_max_new_tokens': False, + 'ban_eos_token': False, + 'add_bos_token': True, + 'skip_special_tokens': True, + 'stream': True, + 'character': 'Assistant', + 'name1': 'You', + 'custom_system_message': '', + 'instruction_template_str': "{%- set ns = namespace(found=false) -%}\n{%- for message in messages -%}\n {%- if message['role'] == 'system' -%}\n {%- set ns.found = true -%}\n {%- endif -%}\n{%- endfor -%}\n{%- if not ns.found -%}\n {{- '' + 'Below is an instruction that describes a task. Write a response that appropriately completes the request.' + '\\n\\n' -}}\n{%- endif %}\n{%- for message in messages %}\n {%- if message['role'] == 'system' -%}\n {{- '' + message['content'] + '\\n\\n' -}}\n {%- else -%}\n {%- if message['role'] == 'user' -%}\n {{-'### Instruction:\\n' + message['content'] + '\\n\\n'-}}\n {%- else -%}\n {{-'### Response:\\n' + message['content'] + '\\n\\n' -}}\n {%- endif -%}\n {%- endif -%}\n{%- endfor -%}\n{%- if add_generation_prompt -%}\n {{-'### Response:\\n'-}}\n{%- endif -%}", + 'chat_template_str': "{%- for message in messages %}\n {%- if message['role'] == 'system' -%}\n {{- message['content'] + '\\n\\n' -}}\n {%- else -%}\n {%- if message['role'] == 'user' -%}\n {{- name1 + ': ' + message['content'] + '\\n'-}}\n {%- else -%}\n {{- name2 + ': ' + message['content'] + '\\n' -}}\n {%- endif -%}\n {%- endif -%}\n{%- endfor -%}", + 'chat-instruct_command': 'Continue the chat dialogue below. Write a single reply for the character "<|character|>".\n\n<|prompt|>', + 'autoload_model': False, + 'gallery-items_per_page': 50, + 'gallery-open': False, + 'default_extensions': ['gallery'], +} + + +# Parser copied from https://github.com/vladmandic/automatic +parser = argparse.ArgumentParser(description="Text generation web UI", conflict_handler='resolve', add_help=True, formatter_class=lambda prog: argparse.HelpFormatter(prog, max_help_position=55, indent_increment=2, width=200)) + +# Basic settings +group = parser.add_argument_group('Basic settings') +group.add_argument('--multi-user', action='store_true', help='Multi-user mode. Chat histories are not saved or automatically loaded. Warning: this is likely not safe for sharing publicly.') +group.add_argument('--character', type=str, help='The name of the character to load in chat mode by default.') +group.add_argument('--model', type=str, help='Name of the model to load by default.') +group.add_argument('--lora', type=str, nargs='+', help='The list of LoRAs to load. If you want to load more than one LoRA, write the names separated by spaces.') +group.add_argument('--model-dir', type=str, default='models/', help='Path to directory with all the models.') +group.add_argument('--lora-dir', type=str, default='loras/', help='Path to directory with all the loras.') +group.add_argument('--model-menu', action='store_true', help='Show a model menu in the terminal when the web UI is first launched.') +group.add_argument('--settings', type=str, help='Load the default interface settings from this yaml file. See settings-template.yaml for an example. If you create a file called settings.yaml, this file will be loaded by default without the need to use the --settings flag.') +group.add_argument('--extensions', type=str, nargs='+', help='The list of extensions to load. If you want to load more than one extension, write the names separated by spaces.') +group.add_argument('--verbose', action='store_true', help='Print the prompts to the terminal.') +group.add_argument('--chat-buttons', action='store_true', help='Show buttons on the chat tab instead of a hover menu.') + +# Model loader +group = parser.add_argument_group('Model loader') +group.add_argument('--loader', type=str, help='Choose the model loader manually, otherwise, it will get autodetected. Valid options: Transformers, llama.cpp, llamacpp_HF, ExLlamav2_HF, ExLlamav2, AutoGPTQ, AutoAWQ, GPTQ-for-LLaMa, ctransformers, QuIP#.') + +# Transformers/Accelerate +group = parser.add_argument_group('Transformers/Accelerate') +group.add_argument('--cpu', action='store_true', help='Use the CPU to generate text. Warning: Training on CPU is extremely slow.') +group.add_argument('--auto-devices', action='store_true', help='Automatically split the model across the available GPU(s) and CPU.') +group.add_argument('--gpu-memory', type=str, nargs='+', help='Maximum GPU memory in GiB to be allocated per GPU. Example: --gpu-memory 10 for a single GPU, --gpu-memory 10 5 for two GPUs. You can also set values in MiB like --gpu-memory 3500MiB.') +group.add_argument('--cpu-memory', type=str, help='Maximum CPU memory in GiB to allocate for offloaded weights. Same as above.') +group.add_argument('--disk', action='store_true', help='If the model is too large for your GPU(s) and CPU combined, send the remaining layers to the disk.') +group.add_argument('--disk-cache-dir', type=str, default='cache', help='Directory to save the disk cache to. Defaults to "cache".') +group.add_argument('--load-in-8bit', action='store_true', help='Load the model with 8-bit precision (using bitsandbytes).') +group.add_argument('--bf16', action='store_true', help='Load the model with bfloat16 precision. Requires NVIDIA Ampere GPU.') +group.add_argument('--no-cache', action='store_true', help='Set use_cache to False while generating text. This reduces VRAM usage slightly, but it comes at a performance cost.') +group.add_argument('--trust-remote-code', action='store_true', help='Set trust_remote_code=True while loading the model. Necessary for some models.') +group.add_argument('--force-safetensors', action='store_true', help='Set use_safetensors=True while loading the model. This prevents arbitrary code execution.') +group.add_argument('--no_use_fast', action='store_true', help='Set use_fast=False while loading the tokenizer (it\'s True by default). Use this if you have any problems related to use_fast.') +group.add_argument('--use_flash_attention_2', action='store_true', help='Set use_flash_attention_2=True while loading the model.') + +# bitsandbytes 4-bit +group = parser.add_argument_group('bitsandbytes 4-bit') +group.add_argument('--load-in-4bit', action='store_true', help='Load the model with 4-bit precision (using bitsandbytes).') +group.add_argument('--use_double_quant', action='store_true', help='use_double_quant for 4-bit.') +group.add_argument('--compute_dtype', type=str, default='float16', help='compute dtype for 4-bit. Valid options: bfloat16, float16, float32.') +group.add_argument('--quant_type', type=str, default='nf4', help='quant_type for 4-bit. Valid options: nf4, fp4.') + +# llama.cpp +group = parser.add_argument_group('llama.cpp') +group.add_argument('--tensorcores', action='store_true', help='Use llama-cpp-python compiled with tensor cores support. This increases performance on RTX cards. NVIDIA only.') +group.add_argument('--n_ctx', type=int, default=2048, help='Size of the prompt context.') +group.add_argument('--threads', type=int, default=0, help='Number of threads to use.') +group.add_argument('--threads-batch', type=int, default=0, help='Number of threads to use for batches/prompt processing.') +group.add_argument('--no_mul_mat_q', action='store_true', help='Disable the mulmat kernels.') +group.add_argument('--n_batch', type=int, default=512, help='Maximum number of prompt tokens to batch together when calling llama_eval.') +group.add_argument('--no-mmap', action='store_true', help='Prevent mmap from being used.') +group.add_argument('--mlock', action='store_true', help='Force the system to keep the model in RAM.') +group.add_argument('--n-gpu-layers', type=int, default=0, help='Number of layers to offload to the GPU.') +group.add_argument('--tensor_split', type=str, default=None, help='Split the model across multiple GPUs. Comma-separated list of proportions. Example: 18,17.') +group.add_argument('--numa', action='store_true', help='Activate NUMA task allocation for llama.cpp.') +group.add_argument('--logits_all', action='store_true', help='Needs to be set for perplexity evaluation to work. Otherwise, ignore it, as it makes prompt processing slower.') +group.add_argument('--no_offload_kqv', action='store_true', help='Do not offload the K, Q, V to the GPU. This saves VRAM but reduces the performance.') +group.add_argument('--cache-capacity', type=str, help='Maximum cache capacity (llama-cpp-python). Examples: 2000MiB, 2GiB. When provided without units, bytes will be assumed.') + +# ExLlama +group = parser.add_argument_group('ExLlama') +group.add_argument('--gpu-split', type=str, help='Comma-separated list of VRAM (in GB) to use per GPU device for model layers. Example: 20,7,7.') +group.add_argument('--max_seq_len', type=int, default=2048, help='Maximum sequence length.') +group.add_argument('--cfg-cache', action='store_true', help='ExLlamav2_HF: Create an additional cache for CFG negative prompts. Necessary to use CFG with that loader.') +group.add_argument('--no_flash_attn', action='store_true', help='Force flash-attention to not be used.') +group.add_argument('--cache_8bit', action='store_true', help='Use 8-bit cache to save VRAM.') +group.add_argument('--num_experts_per_token', type=int, default=2, help='Number of experts to use for generation. Applies to MoE models like Mixtral.') + +# AutoGPTQ +group = parser.add_argument_group('AutoGPTQ') +group.add_argument('--triton', action='store_true', help='Use triton.') +group.add_argument('--no_inject_fused_attention', action='store_true', help='Disable the use of fused attention, which will use less VRAM at the cost of slower inference.') +group.add_argument('--no_inject_fused_mlp', action='store_true', help='Triton mode only: disable the use of fused MLP, which will use less VRAM at the cost of slower inference.') +group.add_argument('--no_use_cuda_fp16', action='store_true', help='This can make models faster on some systems.') +group.add_argument('--desc_act', action='store_true', help='For models that do not have a quantize_config.json, this parameter is used to define whether to set desc_act or not in BaseQuantizeConfig.') +group.add_argument('--disable_exllama', action='store_true', help='Disable ExLlama kernel, which can improve inference speed on some systems.') +group.add_argument('--disable_exllamav2', action='store_true', help='Disable ExLlamav2 kernel.') + +# GPTQ-for-LLaMa +group = parser.add_argument_group('GPTQ-for-LLaMa') +group.add_argument('--wbits', type=int, default=0, help='Load a pre-quantized model with specified precision in bits. 2, 3, 4 and 8 are supported.') +group.add_argument('--model_type', type=str, help='Model type of pre-quantized model. Currently LLaMA, OPT, and GPT-J are supported.') +group.add_argument('--groupsize', type=int, default=-1, help='Group size.') +group.add_argument('--pre_layer', type=int, nargs='+', help='The number of layers to allocate to the GPU. Setting this parameter enables CPU offloading for 4-bit models. For multi-gpu, write the numbers separated by spaces, eg --pre_layer 30 60.') +group.add_argument('--checkpoint', type=str, help='The path to the quantized checkpoint file. If not specified, it will be automatically detected.') +group.add_argument('--monkey-patch', action='store_true', help='Apply the monkey patch for using LoRAs with quantized models.') + +# BigDL-LLM +group = parser.add_argument_group('BigDL-LLM') +group.add_argument('--device', type=str, default='cpu', help='the device type, it could be CPU or GPU') +group.add_argument('--load-in-4bit', action='store_true', default=False, help='boolean value, True means loading linear’s weight to symmetric int 4 if'\ + 'the model is a regular fp16/bf16/fp32 model, and to asymmetric int 4 if the model is GPTQ model.Default to be False') +group.add_argument('--load-in-low-bit', type=str, default=None, help='str value, options are sym_int4, asym_int4, sym_int5, asym_int5'\ + ', sym_int8, nf3, nf4, fp4, fp8, fp8_e4m3, fp8_e5m2, fp16 or bf16. sym_int4 means symmetric int 4, asym_int4 means asymmetric int 4,'\ + 'nf4 means 4-bit NormalFloat, etc. Relevant low bit optimizations will be applied to the model.') +group.add_argument('--optimize-model', action='store_true', help='boolean value, Whether to further optimize the low_bit llm model.') +group.add_argument('--modules-to-not-convert', type=str, default=None, help='list of str value, modules (nn.Module) that are skipped when conducting model optimizations.') +group.add_argument('--cpu-embedding', action='store_true', help='Whether to replace the Embedding layer, may need to set it to `True` when running BigDL-LLM on GPU on Windows. Default to be `False`') +group.add_argument('--lightweight-bmm', action='store_true', help='Whether to replace the torch.bmm ops, may need to set it to `True` when running BigDL-LLM on GPU on Windows.') +group.add_argument('--use-cache', action='store_true', help='If use_cache is True, past key values are used to speed up decoding if applicable to model.') +group.add_argument('--trust-remote-code', action='store_true', help='Set trust_remote_code=True while loading the model. Necessary for some models.') + +# HQQ +group = parser.add_argument_group('HQQ') +group.add_argument('--hqq_backend', type=str, default='PYTORCH_COMPILE', help='Backend for the HQQ loader. Valid options: PYTORCH, PYTORCH_COMPILE, ATEN.') + +# DeepSpeed +group = parser.add_argument_group('DeepSpeed') +group.add_argument('--deepspeed', action='store_true', help='Enable the use of DeepSpeed ZeRO-3 for inference via the Transformers integration.') +group.add_argument('--nvme-offload-dir', type=str, help='DeepSpeed: Directory to use for ZeRO-3 NVME offloading.') +group.add_argument('--local_rank', type=int, default=0, help='DeepSpeed: Optional argument for distributed setups.') + +# RoPE +group = parser.add_argument_group('RoPE') +group.add_argument('--alpha_value', type=float, default=1, help='Positional embeddings alpha factor for NTK RoPE scaling. Use either this or compress_pos_emb, not both.') +group.add_argument('--rope_freq_base', type=int, default=0, help='If greater than 0, will be used instead of alpha_value. Those two are related by rope_freq_base = 10000 * alpha_value ^ (64 / 63).') +group.add_argument('--compress_pos_emb', type=int, default=1, help="Positional embeddings compression factor. Should be set to (context length) / (model\'s original context length). Equal to 1/rope_freq_scale.") + +# Gradio +group = parser.add_argument_group('Gradio') +group.add_argument('--listen', action='store_true', help='Make the web UI reachable from your local network.') +group.add_argument('--listen-port', type=int, help='The listening port that the server will use.') +group.add_argument('--listen-host', type=str, help='The hostname that the server will use.') +group.add_argument('--share', action='store_true', help='Create a public URL. This is useful for running the web UI on Google Colab or similar.') +group.add_argument('--auto-launch', action='store_true', default=False, help='Open the web UI in the default browser upon launch.') +group.add_argument('--gradio-auth', type=str, help='Set Gradio authentication password in the format "username:password". Multiple credentials can also be supplied with "u1:p1,u2:p2,u3:p3".', default=None) +group.add_argument('--gradio-auth-path', type=str, help='Set the Gradio authentication file path. The file should contain one or more user:password pairs in the same format as above.', default=None) +group.add_argument('--ssl-keyfile', type=str, help='The path to the SSL certificate key file.', default=None) +group.add_argument('--ssl-certfile', type=str, help='The path to the SSL certificate cert file.', default=None) + +# API +group = parser.add_argument_group('API') +group.add_argument('--api', action='store_true', help='Enable the API extension.') +group.add_argument('--public-api', action='store_true', help='Create a public URL for the API using Cloudfare.') +group.add_argument('--public-api-id', type=str, help='Tunnel ID for named Cloudflare Tunnel. Use together with public-api option.', default=None) +group.add_argument('--api-port', type=int, default=5000, help='The listening port for the API.') +group.add_argument('--api-key', type=str, default='', help='API authentication key.') +group.add_argument('--admin-key', type=str, default='', help='API authentication key for admin tasks like loading and unloading models. If not set, will be the same as --api-key.') +group.add_argument('--nowebui', action='store_true', help='Do not launch the Gradio UI. Useful for launching the API in standalone mode.') + +# Multimodal +group = parser.add_argument_group('Multimodal') +group.add_argument('--multimodal-pipeline', type=str, default=None, help='The multimodal pipeline to use. Examples: llava-7b, llava-13b.') + +# Deprecated parameters +# group = parser.add_argument_group('Deprecated') + +args = parser.parse_args() +args_defaults = parser.parse_args([]) +provided_arguments = [] +for arg in sys.argv[1:]: + arg = arg.lstrip('-').replace('-', '_') + if hasattr(args, arg): + provided_arguments.append(arg) + +deprecated_args = [] + + +def do_cmd_flags_warnings(): + + # Deprecation warnings + for k in deprecated_args: + if getattr(args, k): + logger.warning(f'The --{k} flag has been deprecated and will be removed soon. Please remove that flag.') + + # Security warnings + if args.trust_remote_code: + logger.warning('trust_remote_code is enabled. This is dangerous.') + if 'COLAB_GPU' not in os.environ and not args.nowebui: + if args.share: + logger.warning("The gradio \"share link\" feature uses a proprietary executable to create a reverse tunnel. Use it with care.") + if any((args.listen, args.share)) and not any((args.gradio_auth, args.gradio_auth_path)): + logger.warning("\nYou are potentially exposing the web UI to the entire internet without any access password.\nYou can create one with the \"--gradio-auth\" flag like this:\n\n--gradio-auth username:password\n\nMake sure to replace username:password with your own.") + if args.multi_user: + logger.warning('\nThe multi-user mode is highly experimental and should not be shared publicly.') + + +def fix_loader_name(name): + if not name: + return name + + name = name.lower() + if name in ['llamacpp', 'llama.cpp', 'llama-cpp', 'llama cpp']: + return 'llama.cpp' + if name in ['llamacpp_hf', 'llama.cpp_hf', 'llama-cpp-hf', 'llamacpp-hf', 'llama.cpp-hf']: + return 'llamacpp_HF' + elif name in ['transformers', 'huggingface', 'hf', 'hugging_face', 'hugging face']: + return 'Transformers' + elif name in ['autogptq', 'auto-gptq', 'auto_gptq', 'auto gptq']: + return 'AutoGPTQ' + elif name in ['gptq-for-llama', 'gptqforllama', 'gptqllama', 'gptq for llama', 'gptq_for_llama']: + return 'GPTQ-for-LLaMa' + elif name in ['exllama', 'ex-llama', 'ex_llama', 'exlama']: + return 'ExLlama' + elif name in ['exllamav2', 'exllama-v2', 'ex_llama-v2', 'exlamav2', 'exlama-v2', 'exllama2', 'exllama-2']: + return 'ExLlamav2' + elif name in ['exllamav2-hf', 'exllamav2_hf', 'exllama-v2-hf', 'exllama_v2_hf', 'exllama-v2_hf', 'exllama2-hf', 'exllama2_hf', 'exllama-2-hf', 'exllama_2_hf', 'exllama-2_hf']: + return 'ExLlamav2_HF' + elif name in ['ctransformers', 'ctranforemrs', 'ctransformer']: + return 'ctransformers' + elif name in ['autoawq', 'awq', 'auto-awq']: + return 'AutoAWQ' + elif name in ['quip#', 'quip-sharp', 'quipsharp', 'quip_sharp']: + return 'QuIP#' + elif name in ['hqq']: + return 'HQQ' + elif name in ['BigDL-LLM', 'bigdl-llm', 'bigdl']: + return 'BigDL-LLM' + + +def add_extension(name, last=False): + if args.extensions is None: + args.extensions = [name] + elif last: + args.extensions = [x for x in args.extensions if x != name] + args.extensions.append(name) + elif name not in args.extensions: + args.extensions.append(name) + + +def is_chat(): + return True + + +args.loader = fix_loader_name(args.loader) + +# Activate the multimodal extension +if args.multimodal_pipeline is not None: + add_extension('multimodal') + +# Activate the API extension +if args.api or args.public_api: + add_extension('openai', last=True) + +# Load model-specific settings +with Path(f'{args.model_dir}/config.yaml') as p: + if p.exists(): + model_config = yaml.safe_load(open(p, 'r').read()) + else: + model_config = {} + +# Load custom model-specific settings +with Path(f'{args.model_dir}/config-user.yaml') as p: + if p.exists(): + user_config = yaml.safe_load(open(p, 'r').read()) + else: + user_config = {} + +model_config = OrderedDict(model_config) +user_config = OrderedDict(user_config) diff --git a/python/llm/example/Text-Generation-WebUI/modules/text_generation.py b/python/llm/example/Text-Generation-WebUI/modules/text_generation.py new file mode 100644 index 00000000..cf8227f4 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/text_generation.py @@ -0,0 +1,459 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/text_generation.py + + +import ast +import copy +import html +import pprint +import random +import re +import time +import traceback + +import numpy as np +import torch +import transformers +from transformers import LogitsProcessorList, is_torch_xpu_available + +import modules.shared as shared +from modules.callbacks import ( + Iteratorize, + Stream, + _StopEverythingStoppingCriteria +) +from modules.extensions import apply_extensions +from modules.grammar.grammar_utils import initialize_grammar +from modules.grammar.logits_process import GrammarConstrainedLogitsProcessor +from modules.html_generator import generate_4chan_html, generate_basic_html +from modules.logging_colors import logger +from modules.models import clear_torch_cache, local_rank + + +def generate_reply(*args, **kwargs): + shared.generation_lock.acquire() + try: + for result in _generate_reply(*args, **kwargs): + yield result + finally: + shared.generation_lock.release() + + +def _generate_reply(question, state, stopping_strings=None, is_chat=False, escape_html=False, for_ui=False): + + # Find the appropriate generation function + generate_func = apply_extensions('custom_generate_reply') + if generate_func is None: + if shared.model_name == 'None' or shared.model is None: + logger.error("No model is loaded! Select one in the Model tab.") + yield '' + return + + if shared.model.__class__.__name__ in ['LlamaCppModel', 'Exllamav2Model', 'CtransformersModel']: + generate_func = generate_reply_custom + else: + generate_func = generate_reply_HF + + # Prepare the input + original_question = question + if not is_chat: + state = apply_extensions('state', state) + question = apply_extensions('input', question, state) + + # Find the stopping strings + all_stop_strings = [] + for st in (stopping_strings, state['custom_stopping_strings']): + if type(st) is str: + st = ast.literal_eval(f"[{st}]") + + if type(st) is list and len(st) > 0: + all_stop_strings += st + + if shared.args.verbose: + logger.info("PROMPT=") + print(question) + + shared.stop_everything = False + clear_torch_cache() + seed = set_manual_seed(state['seed']) + last_update = -1 + reply = '' + is_stream = state['stream'] + if len(all_stop_strings) > 0 and not state['stream']: + state = copy.deepcopy(state) + state['stream'] = True + + min_update_interval = 0 + if state.get('max_updates_second', 0) > 0: + min_update_interval = 1 / state['max_updates_second'] + + # Generate + for reply in generate_func(question, original_question, seed, state, stopping_strings, is_chat=is_chat): + reply, stop_found = apply_stopping_strings(reply, all_stop_strings) + if escape_html: + reply = html.escape(reply) + if is_stream: + cur_time = time.time() + + # Maximum number of tokens/second + if state['max_tokens_second'] > 0: + diff = 1 / state['max_tokens_second'] - (cur_time - last_update) + if diff > 0: + time.sleep(diff) + + last_update = time.time() + yield reply + + # Limit updates to avoid lag in the Gradio UI + # API updates are not limited + else: + if cur_time - last_update > min_update_interval: + last_update = cur_time + yield reply + + if stop_found or (state['max_tokens_second'] > 0 and shared.stop_everything): + break + + if not is_chat: + reply = apply_extensions('output', reply, state) + + yield reply + + +def encode(prompt, add_special_tokens=True, add_bos_token=True, truncation_length=None): + if shared.tokenizer is None: + raise ValueError('No tokenizer is loaded') + + if shared.model.__class__.__name__ in ['LlamaCppModel', 'CtransformersModel', 'Exllamav2Model']: + input_ids = shared.tokenizer.encode(str(prompt)) + if shared.model.__class__.__name__ not in ['Exllamav2Model']: + input_ids = np.array(input_ids).reshape(1, len(input_ids)) + else: + input_ids = shared.tokenizer.encode(str(prompt), return_tensors='pt', add_special_tokens=add_special_tokens) + if not add_bos_token: + while len(input_ids[0]) > 0 and input_ids[0][0] == shared.tokenizer.bos_token_id: + input_ids = input_ids[:, 1:] + + # Handling truncation + if truncation_length is not None: + input_ids = input_ids[:, -truncation_length:] + + if shared.model.__class__.__name__ in ['LlamaCppModel', 'Exllamav2Model', 'CtransformersModel'] or shared.args.cpu: + return input_ids + elif shared.args.deepspeed: + return input_ids.to(device=local_rank) + elif torch.backends.mps.is_available(): + device = torch.device('mps') + return input_ids.to(device) + elif shared.args.device == "CPU": + return input_ids + elif is_torch_xpu_available() or shared.args.device == "GPU": + return input_ids.to("xpu") + else: + return input_ids.cuda() + + +def decode(output_ids, skip_special_tokens=True): + if shared.tokenizer is None: + raise ValueError('No tokenizer is loaded') + + return shared.tokenizer.decode(output_ids, skip_special_tokens=skip_special_tokens) + + +def get_encoded_length(prompt): + length_after_extensions = apply_extensions('tokenized_length', prompt) + if length_after_extensions is not None: + return length_after_extensions + + return len(encode(prompt)[0]) + + +def get_token_ids(prompt): + tokens = encode(prompt)[0] + decoded_tokens = [shared.tokenizer.decode([i]) for i in tokens] + + output = '' + for row in list(zip(tokens, decoded_tokens)): + output += f"{str(int(row[0])).ljust(5)} - {repr(row[1])}\n" + + return output + + +def get_max_prompt_length(state): + return state['truncation_length'] - state['max_new_tokens'] + + +def generate_reply_wrapper(question, state, stopping_strings=None): + """ + Returns formatted outputs for the UI + """ + reply = question if not shared.is_seq2seq else '' + yield formatted_outputs(reply, shared.model_name) + + for reply in generate_reply(question, state, stopping_strings, is_chat=False, escape_html=True, for_ui=True): + if not shared.is_seq2seq: + reply = question + reply + + yield formatted_outputs(reply, shared.model_name) + + +def formatted_outputs(reply, model_name): + if any(s in model_name for s in ['gpt-4chan', 'gpt4chan']): + reply = fix_gpt4chan(reply) + return html.unescape(reply), generate_4chan_html(reply) + else: + return html.unescape(reply), generate_basic_html(reply) + + +def fix_gpt4chan(s): + """ + Removes empty replies from gpt4chan outputs + """ + for i in range(10): + s = re.sub("--- [0-9]*\n>>[0-9]*\n---", "---", s) + s = re.sub("--- [0-9]*\n *\n---", "---", s) + s = re.sub("--- [0-9]*\n\n\n---", "---", s) + + return s + + +def fix_galactica(s): + """ + Fix the LaTeX equations in GALACTICA + """ + s = s.replace(r'\[', r'$') + s = s.replace(r'\]', r'$') + s = s.replace(r'\(', r'$') + s = s.replace(r'\)', r'$') + s = s.replace(r'$$', r'$') + s = re.sub(r'\n', r'\n\n', s) + s = re.sub(r"\n{3,}", "\n\n", s) + return s + + +def set_manual_seed(seed): + seed = int(seed) + if seed == -1: + seed = random.randint(1, 2**31) + + torch.manual_seed(seed) + if torch.cuda.is_available(): + torch.cuda.manual_seed_all(seed) + elif is_torch_xpu_available(): + torch.xpu.manual_seed_all(seed) + + return seed + + +def stop_everything_event(): + shared.stop_everything = True + + +def apply_stopping_strings(reply, all_stop_strings): + stop_found = False + for string in all_stop_strings: + idx = reply.find(string) + if idx != -1: + reply = reply[:idx] + stop_found = True + break + + if not stop_found: + # If something like "\nYo" is generated just before "\nYou:" + # is completed, trim it + for string in all_stop_strings: + for j in range(len(string) - 1, 0, -1): + if reply[-j:] == string[:j]: + reply = reply[:-j] + break + else: + continue + + break + + return reply, stop_found + + +def get_reply_from_output_ids(output_ids, state, starting_from=0): + reply = decode(output_ids[starting_from:], state['skip_special_tokens']) + + # Handle tokenizers that do not add the leading space for the first token + if (hasattr(shared.tokenizer, 'convert_ids_to_tokens') and len(output_ids) > starting_from) and not reply.startswith(' '): + first_token = shared.tokenizer.convert_ids_to_tokens(int(output_ids[starting_from])) + if isinstance(first_token, (bytes,)): + first_token = first_token.decode('utf8') + + if first_token.startswith('▁'): + reply = ' ' + reply + + return reply + + +def generate_reply_HF(question, original_question, seed, state, stopping_strings=None, is_chat=False): + generate_params = {} + for k in ['max_new_tokens', 'temperature', 'temperature_last', 'dynamic_temperature', 'dynamic_temperature_low', 'top_p', 'min_p', 'top_k', 'repetition_penalty', 'presence_penalty', 'frequency_penalty', 'repetition_penalty_range', 'typical_p', 'tfs', 'top_a', 'guidance_scale', 'penalty_alpha', 'mirostat_mode', 'mirostat_tau', 'mirostat_eta', 'do_sample', 'encoder_repetition_penalty', 'no_repeat_ngram_size', 'min_length', 'num_beams', 'length_penalty', 'early_stopping']: + generate_params[k] = state[k] + + if state['negative_prompt'] != '': + generate_params['negative_prompt_ids'] = encode(state['negative_prompt']) + + for k in ['epsilon_cutoff', 'eta_cutoff']: + if state[k] > 0: + generate_params[k] = state[k] * 1e-4 + + if state['ban_eos_token']: + generate_params['suppress_tokens'] = [shared.tokenizer.eos_token_id] + + if state['custom_token_bans']: + to_ban = [int(x) for x in state['custom_token_bans'].split(',')] + if len(to_ban) > 0: + if generate_params.get('suppress_tokens', None): + generate_params['suppress_tokens'] += to_ban + else: + generate_params['suppress_tokens'] = to_ban + + generate_params.update({'use_cache': not shared.args.no_cache}) + if shared.args.deepspeed: + generate_params.update({'synced_gpus': True}) + + # Encode the input + input_ids = encode(question, add_bos_token=state['add_bos_token'], truncation_length=get_max_prompt_length(state)) + output = input_ids[0] + cuda = not any((shared.args.cpu, shared.args.deepspeed)) + if state['auto_max_new_tokens']: + generate_params['max_new_tokens'] = state['truncation_length'] - input_ids.shape[-1] + + # Add the encoded tokens to generate_params + question, input_ids, inputs_embeds = apply_extensions('tokenizer', state, question, input_ids, None) + original_input_ids = input_ids + generate_params.update({'inputs': input_ids}) + if inputs_embeds is not None: + generate_params.update({'inputs_embeds': inputs_embeds}) + + # Stopping criteria / eos token + eos_token_ids = [shared.tokenizer.eos_token_id] if shared.tokenizer.eos_token_id is not None else [] + generate_params['eos_token_id'] = eos_token_ids + generate_params['stopping_criteria'] = transformers.StoppingCriteriaList() + generate_params['stopping_criteria'].append(_StopEverythingStoppingCriteria()) + + # Logits processor + processor = state.get('logits_processor', LogitsProcessorList([])) + if not isinstance(processor, LogitsProcessorList): + processor = LogitsProcessorList([processor]) + + # Grammar + if state['grammar_string'].strip() != '': + grammar = initialize_grammar(state['grammar_string']) + grammar_processor = GrammarConstrainedLogitsProcessor(grammar) + processor.append(grammar_processor) + + apply_extensions('logits_processor', processor, input_ids) + generate_params['logits_processor'] = processor + + if shared.args.verbose: + logger.info("GENERATE_PARAMS=") + filtered_params = {key: value for key, value in generate_params.items() if not isinstance(value, torch.Tensor)} + pprint.PrettyPrinter(indent=4, sort_dicts=False).pprint(filtered_params) + print() + + t0 = time.time() + try: + if not is_chat and not shared.is_seq2seq: + yield '' + + # Generate the entire reply at once. + if not state['stream']: + with torch.no_grad(): + output = shared.model.generate(**generate_params)[0] + if cuda: + output = output.cuda() + + starting_from = 0 if shared.is_seq2seq else len(input_ids[0]) + yield get_reply_from_output_ids(output, state, starting_from=starting_from) + + # Stream the reply 1 token at a time. + # This is based on the trick of using 'stopping_criteria' to create an iterator. + else: + + def generate_with_callback(callback=None, *args, **kwargs): + kwargs['stopping_criteria'].append(Stream(callback_func=callback)) + clear_torch_cache() + with torch.no_grad(): + shared.model.generate(**kwargs) + + def generate_with_streaming(**kwargs): + return Iteratorize(generate_with_callback, [], kwargs, callback=None) + + # warm-up + with torch.no_grad(): + shared.model.generate(**generate_params) + torch.xpu.synchronize() + + with generate_with_streaming(**generate_params) as generator: + cumulative_reply = '' + starting_from = 0 if shared.is_seq2seq else len(input_ids[0]) + for output in generator: + if output[-1] in eos_token_ids: + break + + new_content = get_reply_from_output_ids(output, state, starting_from=starting_from) + # check the partial unicode character + if chr(0xfffd) in new_content: + continue + + cumulative_reply += new_content + starting_from = len(output) + yield cumulative_reply + + except Exception: + traceback.print_exc() + finally: + t1 = time.time() + original_tokens = len(original_input_ids[0]) + new_tokens = len(output) - (original_tokens if not shared.is_seq2seq else 0) + print(f'Output generated in {(t1-t0):.2f} seconds ({new_tokens/(t1-t0):.2f} tokens/s, {new_tokens} tokens, context {original_tokens}, seed {seed})') + return + + +def generate_reply_custom(question, original_question, seed, state, stopping_strings=None, is_chat=False): + """ + For models that do not use the transformers library for sampling + """ + seed = set_manual_seed(state['seed']) + + t0 = time.time() + reply = '' + try: + if not is_chat: + yield '' + + if not state['stream']: + reply = shared.model.generate(question, state) + yield reply + else: + for reply in shared.model.generate_with_streaming(question, state): + yield reply + + except Exception: + traceback.print_exc() + finally: + t1 = time.time() + original_tokens = len(encode(original_question)[0]) + new_tokens = len(encode(original_question + reply)[0]) - original_tokens + print(f'Output generated in {(t1-t0):.2f} seconds ({new_tokens/(t1-t0):.2f} tokens/s, {new_tokens} tokens, context {original_tokens}, seed {seed})') + return diff --git a/python/llm/example/Text-Generation-WebUI/modules/training.py b/python/llm/example/Text-Generation-WebUI/modules/training.py new file mode 100644 index 00000000..2214b56a --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/training.py @@ -0,0 +1,797 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/training.py + + +import os + +os.environ["WANDB_MODE"] = "offline" +# os.environ["WANDB_DISABLED"] = "true" + +import json +import math +import random +import shutil +import sys +import threading +import time +import traceback +from datetime import datetime +from pathlib import Path + +import gradio as gr +import torch +import transformers +from datasets import Dataset, load_dataset +from peft import ( + LoraConfig, + get_peft_model, + prepare_model_for_kbit_training, + set_peft_model_state_dict +) +from peft.utils.other import \ + TRANSFORMERS_MODELS_TO_LORA_TARGET_MODULES_MAPPING as model_to_lora_modules +from transformers import is_torch_xpu_available +from transformers.models.auto.modeling_auto import ( + MODEL_FOR_CAUSAL_LM_MAPPING_NAMES +) + +from modules import shared, ui, utils +from modules.evaluate import ( + calculate_perplexity, + generate_markdown_table, + save_past_evaluations +) +from modules.logging_colors import logger +from modules.models import reload_model +from modules.utils import natural_keys + +MODEL_CLASSES = {v[1]: v[0] for v in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES.items()} +PARAMETERS = ["lora_name", "always_override", "q_proj_en", "v_proj_en", "k_proj_en", "o_proj_en", "gate_proj_en", "down_proj_en", "up_proj_en", "save_steps", "micro_batch_size", "batch_size", "epochs", "learning_rate", "lr_scheduler_type", "lora_rank", "lora_alpha", "lora_dropout", "cutoff_len", "dataset", "eval_dataset", "format", "eval_steps", "raw_text_file", "overlap_len", "newline_favor_len", "higher_rank_limit", "warmup_steps", "optimizer", "hard_cut_string", "train_only_after", "stop_at_loss", "add_eos_token", "min_chars", "report_to"] +WANT_INTERRUPT = False + +train_log = {} +train_template = {} + + +def create_ui(): + mu = shared.args.multi_user + with gr.Tab("Training", elem_id="training-tab"): + with gr.Tab('Train LoRA', elem_id='lora-train-tab'): + tmp = gr.State('') + with gr.Row(): + with gr.Column(): + gr.Markdown("[Tutorial](https://github.com/oobabooga/text-generation-webui/wiki/05-%E2%80%90-Training-Tab)") + + with gr.Row(): + copy_from = gr.Dropdown(label='Copy parameters from', value='None', choices=utils.get_available_loras(), elem_classes=['slim-dropdown'], interactive=not mu) + ui.create_refresh_button(copy_from, lambda: None, lambda: {'choices': utils.get_available_loras()}, 'refresh-button', interactive=not mu) + + with gr.Row(): + with gr.Column(scale=5): + lora_name = gr.Textbox(label='Name', info='The name of your new LoRA file') + with gr.Column(): + always_override = gr.Checkbox(label='Override Existing Files', value=False, info='If the name is the same, checking will replace the existing file, and unchecking will load and continue from it (the rank must be the same).', elem_classes=['no-background']) + + with gr.Accordion(label='Target Modules', open=False): + gr.Markdown("Selects which modules to target in training. Targeting more modules is closer to a full fine-tune at the cost of increased VRAM requirements and adapter size.\nNOTE: Only works for model_id='llama', other types will retain default training behavior and not use these settings.") + with gr.Row(): + with gr.Column(): + q_proj_en = gr.Checkbox(label='Enable q_proj', value=True) + with gr.Column(): + v_proj_en = gr.Checkbox(label='Enable v_proj', value=True) + with gr.Column(): + k_proj_en = gr.Checkbox(label='Enable k_proj', value=False) + with gr.Column(): + o_proj_en = gr.Checkbox(label='Enable o_proj', value=False) + with gr.Column(): + gate_proj_en = gr.Checkbox(label='Enable gate_proj', value=False) + with gr.Column(): + down_proj_en = gr.Checkbox(label='Enable down_proj', value=False) + with gr.Column(): + up_proj_en = gr.Checkbox(label='Enable up_proj', value=False) + + with gr.Row(): + with gr.Column(): + lora_rank = gr.Slider(label='LoRA Rank', value=32, minimum=0, maximum=1024, step=4, info='Also called dimension count. Higher values = larger file, more content control. Smaller values = smaller file, less control. Use 4 or 8 for style, 128 or 256 to teach, 1024+ for fine-detail on big data. More VRAM is needed for higher ranks.') + lora_alpha = gr.Slider(label='LoRA Alpha', value=64, minimum=0, maximum=2048, step=4, info='This divided by the rank becomes the scaling of the LoRA. Higher means stronger. A good standard value is twice your Rank.') + batch_size = gr.Slider(label='Batch Size', value=128, minimum=0, maximum=1024, step=4, info='Global batch size. The two batch sizes together determine gradient accumulation (gradientAccum = batch / microBatch). Higher gradient accum values lead to better quality training.') + micro_batch_size = gr.Slider(label='Micro Batch Size', value=4, minimum=1, maximum=128, step=1, info='Per-device batch size (NOTE: multiple devices not yet implemented). Increasing this will increase VRAM usage.') + cutoff_len = gr.Slider(label='Cutoff Length', minimum=0, maximum=4096, value=256, step=32, info='Cutoff length for text input. Essentially, how long of a line of text to feed in at a time. Higher values require drastically more VRAM.') + + with gr.Column(): + save_steps = gr.Number(label='Save every n steps', value=0, info='If above 0, a checkpoint of the LoRA will be saved every time this many steps pass.') + + epochs = gr.Number(label='Epochs', value=3, info='Number of times every entry in the dataset should be fed into training. So 1 means feed each item in once, 5 means feed it in five times, etc.') + learning_rate = gr.Textbox(label='Learning Rate', value='3e-4', info='In scientific notation. 3e-4 is a good starting base point. 1e-2 is extremely high, 1e-6 is extremely low.') + with gr.Row(): + lr_scheduler_type = gr.Dropdown(label='LR Scheduler', value='linear', choices=['linear', 'constant', 'constant_with_warmup', 'cosine', 'cosine_with_restarts', 'polynomial', 'inverse_sqrt'], info='Learning rate scheduler - defines how the learning rate changes over time. "Constant" means never change, "linear" means to go in a straight line from the learning rate down to 0, cosine follows a curve, etc.', elem_classes=['slim-dropdown']) + + with gr.Accordion(label='Advanced Options', open=False): + with gr.Row(): + with gr.Column(): + lora_dropout = gr.Slider(label='LoRA Dropout', minimum=0.0, maximum=1.0, step=0.025, value=0.05, info='Percentage probability for dropout of LoRA layers. This can help reduce overfitting. Most users should leave at default.') + stop_at_loss = gr.Slider(label='Stop at loss', minimum=0.0, maximum=3.0, step=0.1, value=0.00, info='The process will automatically stop once the desired loss value is reached. (reasonable numbers are 1.5-1.8)') + with gr.Row(): + optimizer = gr.Dropdown(label='Optimizer', value='adamw_torch', choices=['adamw_hf', 'adamw_torch', 'adamw_torch_fused', 'adamw_torch_xla', 'adamw_apex_fused', 'adafactor', 'adamw_bnb_8bit', 'adamw_anyprecision', 'sgd', 'adagrad'], info='Different optimizer implementation options, for advanced users. Effects of different options are not well documented yet.', elem_classes=['slim-dropdown']) + + with gr.Column(): + warmup_steps = gr.Number(label='Warmup Steps', value=100, info='For this many steps at the start, the learning rate will be lower than normal. This helps the trainer prepare the model and precompute statistics to improve the quality of training after the start.') + train_only_after = gr.Textbox(label='Train Only After', value='', info='Only consider text *after* this string in any given chunk for training. For Alpaca datasets, use "### Response:" to only train the response and ignore the input.') + + add_eos_token = gr.Checkbox(label='Add EOS token', value=False, info="Adds EOS token for each dataset item. In case of raw text, the EOS will be added at the Hard Cut") + + higher_rank_limit = gr.Checkbox(label='Enable higher ranks', value=False, info='If checked, changes Rank/Alpha slider above to go much higher. This will not work without a datacenter-class GPU.') + report_to = gr.Radio(label="Save detailed logs with", value="None", choices=["None", "wandb", "tensorboard"], interactive=True) + + with gr.Column(): + with gr.Tab(label='Formatted Dataset'): + with gr.Row(): + format = gr.Dropdown(choices=utils.get_datasets('training/formats', 'json'), value='None', label='Data Format', info='The format file used to decide how to format the dataset input.', elem_classes=['slim-dropdown'], interactive=not mu) + ui.create_refresh_button(format, lambda: None, lambda: {'choices': utils.get_datasets('training/formats', 'json')}, 'refresh-button', interactive=not mu) + + with gr.Row(): + dataset = gr.Dropdown(choices=utils.get_datasets('training/datasets', 'json'), value='None', label='Dataset', info='The dataset file to use for training.', elem_classes=['slim-dropdown'], interactive=not mu) + ui.create_refresh_button(dataset, lambda: None, lambda: {'choices': utils.get_datasets('training/datasets', 'json')}, 'refresh-button', interactive=not mu) + + with gr.Row(): + eval_dataset = gr.Dropdown(choices=utils.get_datasets('training/datasets', 'json'), value='None', label='Evaluation Dataset', info='The (optional) dataset file used to evaluate the model after training.', elem_classes=['slim-dropdown'], interactive=not mu) + ui.create_refresh_button(eval_dataset, lambda: None, lambda: {'choices': utils.get_datasets('training/datasets', 'json')}, 'refresh-button', interactive=not mu) + + eval_steps = gr.Number(label='Evaluate every n steps', value=100, info='If an evaluation dataset is given, test it every time this many steps pass.') + + with gr.Tab(label="Raw text file"): + with gr.Row(): + raw_text_file = gr.Dropdown(choices=utils.get_datasets('training/datasets', 'txt'), value='None', label='Text file', info='The raw text file to use for training.', elem_classes=['slim-dropdown'], interactive=not mu) + ui.create_refresh_button(raw_text_file, lambda: None, lambda: {'choices': utils.get_datasets('training/datasets', 'txt')}, 'refresh-button', interactive=not mu) + + with gr.Row(): + with gr.Column(): + overlap_len = gr.Slider(label='Overlap Length', minimum=0, maximum=512, value=128, step=16, info='How many tokens from the prior chunk of text to include into the next chunk. (The chunks themselves will be of a size determined by Cutoff Length). Setting overlap to exactly half the cutoff length may be ideal.') + newline_favor_len = gr.Slider(label='Prefer Newline Cut Length', minimum=0, maximum=512, value=128, step=16, info='Length (in characters, not tokens) of the maximum distance to shift an overlap cut by to ensure chunks cut at newlines. If too low, cuts may occur in the middle of lines.') + + with gr.Column(): + hard_cut_string = gr.Textbox(label='Hard Cut String', value='\\n\\n\\n', info='String that indicates a hard cut between text parts. Helps prevent unwanted overlap.') + min_chars = gr.Number(label='Ignore small blocks', value=0, info='Ignore Hard Cut blocks that have less or equal characters than this number') + + with gr.Row(): + start_button = gr.Button("Start LoRA Training", variant='primary', interactive=not mu) + stop_button = gr.Button("Interrupt", interactive=not mu) + + output = gr.Markdown(value="Ready") + + with gr.Tab('Perplexity evaluation', elem_id='evaluate-tab'): + with gr.Row(): + with gr.Column(): + models = gr.Dropdown(utils.get_available_models(), label='Models', multiselect=True, interactive=not mu) + evaluate_text_file = gr.Dropdown(choices=['wikitext', 'ptb', 'ptb_new'] + utils.get_datasets('training/datasets', 'txt')[1:], value='wikitext', label='Input dataset', info='The raw text file on which the model will be evaluated. The first options are automatically downloaded: wikitext, ptb, and ptb_new. The next options are your local text files under training/datasets.', interactive=not mu) + with gr.Row(): + with gr.Column(): + stride_length = gr.Slider(label='Stride', minimum=0, maximum=32768, value=512, step=256, info='Used to make the evaluation faster at the cost of accuracy. 1 = slowest but most accurate. 512 is a common value.') + + with gr.Column(): + max_length = gr.Slider(label='max_length', minimum=0, maximum=shared.settings['truncation_length_max'], value=0, step=256, info='The context for each evaluation. If set to 0, the maximum context length for the model will be used.') + + with gr.Row(): + start_current_evaluation = gr.Button("Evaluate loaded model", interactive=not mu) + start_evaluation = gr.Button("Evaluate selected models", interactive=not mu) + stop_evaluation = gr.Button("Interrupt", interactive=not mu) + + with gr.Column(): + evaluation_log = gr.Markdown(value='') + + evaluation_table = gr.Dataframe(value=generate_markdown_table(), interactive=True) + with gr.Row(): + save_comments = gr.Button('Save comments', elem_classes="small-button", interactive=not mu) + refresh_table = gr.Button('Refresh the table', elem_classes="small-button", interactive=not mu) + + # Training events + all_params = [lora_name, always_override, q_proj_en, v_proj_en, k_proj_en, o_proj_en, gate_proj_en, down_proj_en, up_proj_en, save_steps, micro_batch_size, batch_size, epochs, learning_rate, lr_scheduler_type, lora_rank, lora_alpha, lora_dropout, cutoff_len, dataset, eval_dataset, format, eval_steps, raw_text_file, overlap_len, newline_favor_len, higher_rank_limit, warmup_steps, optimizer, hard_cut_string, train_only_after, stop_at_loss, add_eos_token, min_chars, report_to] + + copy_from.change(do_copy_params, [copy_from] + all_params, all_params) + start_button.click(do_train, all_params, output) + stop_button.click(do_interrupt, None, None, queue=False) + higher_rank_limit.change(change_rank_limit, [higher_rank_limit], [lora_rank, lora_alpha]) + + # Evaluation events. For some reason, the interrupt event + # doesn't work with the .then() syntax, so I write them one + # by one in this ugly but functional way. + ev = start_evaluation.click(calculate_perplexity, [models, evaluate_text_file, stride_length, max_length], evaluation_log, show_progress=False) + start_evaluation.click(generate_markdown_table, None, evaluation_table, show_progress=False) + + start_current_evaluation.click(lambda: ['current model'], None, tmp) + ev_cur = start_current_evaluation.click(calculate_perplexity, [tmp, evaluate_text_file, stride_length, max_length], evaluation_log, show_progress=False) + start_current_evaluation.click(generate_markdown_table, None, evaluation_table, show_progress=False) + + stop_evaluation.click(None, None, None, cancels=[ev, ev_cur], queue=False) + refresh_table.click(generate_markdown_table, None, evaluation_table, show_progress=True) + save_comments.click( + save_past_evaluations, evaluation_table, None).then( + lambda: "Comments saved.", None, evaluation_log, show_progress=False) + + +def do_interrupt(): + global WANT_INTERRUPT + WANT_INTERRUPT = True + + +def do_copy_params(lora_name: str, *args): + f_name = f"{shared.args.lora_dir}/{clean_path(None, lora_name)}/training_parameters.json" + if Path(f_name).is_file(): + with open(f_name, 'r', encoding='utf-8') as format_file: + params: dict[str, str] = json.load(format_file) + else: + params = {} + + result = list() + for i in range(0, len(PARAMETERS)): + key = PARAMETERS[i] + if key in params: + result.append(params[key]) + else: + result.append(args[i]) + + return result + + +def change_rank_limit(use_higher_ranks: bool): + mult = 2 if use_higher_ranks else 1 + return {"maximum": 1024 * mult, "__type__": "update"}, {"maximum": 2048 * mult, "__type__": "update"} + + +def clean_path(base_path: str, path: str): + """Strips unusual symbols and forcibly builds a path as relative to the intended directory.""" + path = path.replace('\\', '/').replace('..', '_') + if base_path is None: + return path + + return f'{Path(base_path).absolute()}/{path}' + + +def backup_adapter(input_folder): + # Get the creation date of the file adapter_model.bin + try: + adapter_file = Path(f"{input_folder}/adapter_model.bin") + if adapter_file.is_file(): + + logger.info("Backing up existing LoRA adapter") + creation_date = datetime.fromtimestamp(adapter_file.stat().st_ctime) + creation_date_str = creation_date.strftime("Backup-%Y-%m-%d") + + # Create the new subfolder + subfolder_path = Path(f"{input_folder}/{creation_date_str}") + subfolder_path.mkdir(parents=True, exist_ok=True) + + # Check if the file already exists in the subfolder + backup_adapter_file = Path(f"{input_folder}/{creation_date_str}/adapter_model.bin") + if backup_adapter_file.is_file(): + print(" - Backup already exists. Skipping backup process.") + return + + # Copy existing files to the new subfolder + existing_files = Path(input_folder).iterdir() + for file in existing_files: + if file.is_file(): + shutil.copy2(file, subfolder_path) + except Exception as e: + print("An error occurred in backup_adapter:", str(e)) + + +def calc_trainable_parameters(model): + trainable_params = 0 + all_param = 0 + for _, param in model.named_parameters(): + num_params = param.numel() + # if using DS Zero 3 and the weights are initialized empty + if num_params == 0 and hasattr(param, "ds_numel"): + num_params = param.ds_numel + + all_param += num_params + if param.requires_grad: + trainable_params += num_params + + return trainable_params, all_param + + +def do_train(lora_name: str, always_override: bool, q_proj_en: bool, v_proj_en: bool, k_proj_en: bool, o_proj_en: bool, gate_proj_en: bool, down_proj_en: bool, up_proj_en: bool, save_steps: int, micro_batch_size: int, batch_size: int, epochs: int, learning_rate: str, lr_scheduler_type: str, lora_rank: int, lora_alpha: int, lora_dropout: float, cutoff_len: int, dataset: str, eval_dataset: str, format: str, eval_steps: int, raw_text_file: str, overlap_len: int, newline_favor_len: int, higher_rank_limit: bool, warmup_steps: int, optimizer: str, hard_cut_string: str, train_only_after: str, stop_at_loss: float, add_eos_token: bool, min_chars: int, report_to: str): + + if shared.args.monkey_patch: + from alpaca_lora_4bit.monkeypatch.peft_tuners_lora_monkey_patch import ( + replace_peft_model_with_int4_lora_model + ) + replace_peft_model_with_int4_lora_model() + + global WANT_INTERRUPT + WANT_INTERRUPT = False + + # == Input validation / processing == + yield "Preparing the input..." + lora_file_path = clean_path(None, lora_name) + if lora_file_path.strip() == '': + yield "Missing or invalid LoRA file name input." + return + + lora_file_path = f"{Path(shared.args.lora_dir)}/{lora_file_path}" + actual_lr = float(learning_rate) + model_type = type(shared.model).__name__ + + if model_type in MODEL_CLASSES: + model_id = MODEL_CLASSES[model_type] + else: + model_id = "llama" + if model_type == "PeftModelForCausalLM": + if len(shared.lora_names) > 0: + yield "You are trying to train a LoRA while you already have another LoRA loaded. This will work, but may have unexpected effects. *(Will continue anyway in 5 seconds, press `Interrupt` to stop.)*" + logger.warning("Training LoRA over top of another LoRA. May have unexpected effects.") + else: + yield "Model ID not matched due to LoRA loading. Consider reloading base model. *(Will continue anyway in 5 seconds, press `Interrupt` to stop.)*" + logger.warning("Model ID not matched due to LoRA loading. Consider reloading base model.") + else: + yield "LoRA training has only currently been validated for LLaMA, OPT, GPT-J, and GPT-NeoX models. Unexpected errors may follow. *(Will continue anyway in 5 seconds, press `Interrupt` to stop.)*" + logger.warning(f"LoRA training has only currently been validated for LLaMA, OPT, GPT-J, and GPT-NeoX models. (Found model type: {model_type})") + + time.sleep(5) + + if shared.args.loader == 'GPTQ-for-LLaMa' and not shared.args.monkey_patch: + yield "LoRA training with GPTQ-for-LLaMa requires loading with `--monkey-patch`" + return + + if cutoff_len <= 0 or micro_batch_size <= 0 or batch_size <= 0 or actual_lr <= 0 or lora_rank <= 0 or lora_alpha <= 0: + yield "Cannot input zeroes." + return + + gradient_accumulation_steps = batch_size // micro_batch_size + shared.tokenizer.pad_token_id = 0 + shared.tokenizer.padding_side = "left" + + # Populate target_modules list with chosen X_proj modules. Llama-based models only atm, non-llama will revert to default behavior. + def list_target_modules(model_id): + if model_id != "llama" and model_id != "mistral": + return model_to_lora_modules[model_id] + + available_modules = { + "gate": gate_proj_en, + "down": down_proj_en, + "up": up_proj_en, + "q": q_proj_en, + "v": v_proj_en, + "k": k_proj_en, + "o": o_proj_en, + } + target_mods = [f"{name}_proj" for name, enabled in available_modules.items() if enabled] + return target_mods + + def encode(text, add_bos_token): + result = shared.tokenizer.encode(text, truncation=True, max_length=cutoff_len) + # Check if the first two tokens are BOS + if len(result) >= 2 and result[:2] == [shared.tokenizer.bos_token_id, shared.tokenizer.bos_token_id]: + result = result[1:] + + if not add_bos_token and result[0] == shared.tokenizer.bos_token_id: + result = result[1:] + return result + + def tokenize(prompt, append_eos_token=False): + + if train_only_after == '' or train_only_after not in prompt: + input_ids = encode(prompt, True) + + if append_eos_token and input_ids[-1] != shared.tokenizer.eos_token_id and len(input_ids) < cutoff_len: + input_ids.append(shared.tokenizer.eos_token_id) + + input_ids = [shared.tokenizer.pad_token_id] * (cutoff_len - len(input_ids)) + input_ids + labels = [1] * len(input_ids) + + else: + ind = prompt.index(train_only_after) + len(train_only_after) + before_tokens = encode(prompt[:ind], True) + after_tokens = encode(prompt[ind:], False) + + if append_eos_token and after_tokens[-1] != shared.tokenizer.eos_token_id: + after_tokens.append(shared.tokenizer.eos_token_id) + + full_length = len(after_tokens) + len(before_tokens) + if full_length > cutoff_len: + after_tokens = after_tokens[:cutoff_len - len(before_tokens)] + else: + before_tokens = [shared.tokenizer.pad_token_id] * (cutoff_len - full_length) + before_tokens + + input_ids = before_tokens + after_tokens + labels = [-100] * len(before_tokens) + [1] * len(after_tokens) + + input_ids = torch.tensor(input_ids) + return { + "input_ids": input_ids, + "labels": labels, + "attention_mask": input_ids.ne(shared.tokenizer.pad_token_id), + } + + train_template.clear() + + # == Prep the dataset, format, etc == + if raw_text_file not in ['None', '']: + train_template["template_type"] = "raw_text" + logger.info("Loading raw text file dataset") + fullpath = clean_path('training/datasets', f'{raw_text_file}') + fullpath = Path(fullpath) + if fullpath.is_dir(): + logger.info('Training path directory {}'.format(raw_text_file)) + raw_text = "" + file_paths = sorted(fullpath.glob('*.txt'), key=lambda path: natural_keys(path.name)) + for file_path in file_paths: + if file_path.is_file(): + with file_path.open('r', encoding='utf-8') as file: + raw_text += file.read().replace('\r', '') + + logger.info(f"Loaded training file: {file_path.name}") + else: + with open(clean_path('training/datasets', f'{raw_text_file}.txt'), 'r', encoding='utf-8') as file: + raw_text = file.read().replace('\r', '') + + cut_string = hard_cut_string.replace('\\n', '\n') + eos_added = 0 + out_tokens = [] + for text_part in raw_text.split(cut_string): + if len(text_part.strip()) <= min_chars: + continue + + tokens = shared.tokenizer.encode(text_part) + if add_eos_token: + tokens.append(shared.tokenizer.eos_token_id) + eos_added += 1 + + step = cutoff_len - overlap_len + if step <= 0: + yield f"Error: overlap_len ({overlap_len}) cannot be greater than or equal to cutoff_len ({cutoff_len})" + return + + out_tokens.extend(split_chunks(tokens, cutoff_len, step)) + + if eos_added > 0: + print(f"EOS added to {eos_added} text blocks") + + del raw_text # Note: could be a gig for a large dataset, so delete redundant data as we go to be safe on RAM + text_chunks = [shared.tokenizer.decode(x) for x in out_tokens] + del out_tokens + if newline_favor_len > 0: + text_chunks = [cut_chunk_for_newline(x, newline_favor_len) for x in text_chunks] + + train_data = Dataset.from_list([tokenize(x) for x in text_chunks]) + del text_chunks + eval_data = None + else: + if dataset in ['None', '']: + yield "Missing dataset choice input, cannot continue." + return + + if format in ['None', '']: + yield "Missing format choice input, cannot continue." + return + + train_template["template_type"] = "dataset" + + with open(clean_path('training/formats', f'{format}.json'), 'r', encoding='utf-8-sig') as formatFile: + format_data: dict[str, str] = json.load(formatFile) + + # == store training prompt == + for _, value in format_data.items(): + prompt_key = f"template_{len(train_template)}" + train_template[prompt_key] = value + + def generate_prompt(data_point: dict[str, str]): + for options, data in format_data.items(): + if set(options.split(',')) == set(x[0] for x in data_point.items() if (type(x[1]) is str and len(x[1].strip()) > 0)): + for key, val in data_point.items(): + if type(val) is str: + data = data.replace(f'%{key}%', val) + return data + raise RuntimeError(f'Data-point "{data_point}" has no keyset match within format "{list(format_data.keys())}"') + + def generate_and_tokenize_prompt(data_point): + prompt = generate_prompt(data_point) + return tokenize(prompt, add_eos_token) + + logger.info("Loading JSON datasets") + data = load_dataset("json", data_files=clean_path('training/datasets', f'{dataset}.json')) + train_data = data['train'].map(generate_and_tokenize_prompt, new_fingerprint='%030x' % random.randrange(16**30)) + + if eval_dataset == 'None': + eval_data = None + else: + eval_data = load_dataset("json", data_files=clean_path('training/datasets', f'{eval_dataset}.json')) + eval_data = eval_data['train'].map(generate_and_tokenize_prompt, new_fingerprint='%030x' % random.randrange(16**30)) + + # == We MUST reload model if it went through any previous training, even failed one == + if shared.model_dirty_from_training: + selected_model = shared.model_name + if selected_model: + print("\033[1;31;1m(Model has been modified by previous training, it needs to be reloaded...)\033[0;37;0m") + try: + yield f"Reloading {selected_model}..." + reload_model() + if shared.model is not None: + print("Model reloaded OK, continue with training.") + else: + return f"Failed to load {selected_model}." + except: + exc = traceback.format_exc() + logger.error('Failed to reload the model.') + print(exc) + return exc.replace('\n', '\n\n') + + # == Start prepping the model itself == + if not hasattr(shared.model, 'lm_head') or hasattr(shared.model.lm_head, 'weight'): + logger.info("Getting model ready") + if 'quantization_config' in shared.model.config.to_dict(): + prepare_model_for_kbit_training(shared.model) + + # base model is now frozen and should not be reused for any other LoRA training than this one + shared.model_dirty_from_training = True + + logger.info("Preparing for training") + config = LoraConfig( + r=lora_rank, + lora_alpha=lora_alpha, + target_modules=list_target_modules(model_id), + lora_dropout=lora_dropout, + bias="none", + task_type="CAUSAL_LM" + ) + + # == Backup the existing adapter == + if not always_override: + backup_adapter(lora_file_path) + + # == get model trainable params + model_trainable_params, model_all_params = calc_trainable_parameters(shared.model) + + try: + logger.info("Creating LoRA model") + lora_model = get_peft_model(shared.model, config) + if not always_override and Path(f"{lora_file_path}/adapter_model.bin").is_file(): + logger.info("Loading existing LoRA data") + state_dict_peft = torch.load(f"{lora_file_path}/adapter_model.bin", weights_only=True) + set_peft_model_state_dict(lora_model, state_dict_peft) + except: + yield traceback.format_exc().replace('\n', '\n\n') + return + + if shared.args.monkey_patch: + from alpaca_lora_4bit.autograd_4bit import Autograd4bitQuantLinear + from alpaca_lora_4bit.models import Linear4bitLt + for _, m in lora_model.named_modules(): + if isinstance(m, Autograd4bitQuantLinear) or isinstance(m, Linear4bitLt): + if m.is_v1_model: + m.zeros = m.zeros.half() + m.scales = m.scales.half() + + class Tracked(): + def __init__(self): + self.current_steps = 0 + self.max_steps = 0 + self.did_save = False + + tracked = Tracked() + actual_save_steps = math.ceil(save_steps / gradient_accumulation_steps) + + class Callbacks(transformers.TrainerCallback): + def on_step_begin(self, args: transformers.TrainingArguments, state: transformers.TrainerState, control: transformers.TrainerControl, **kwargs): + tracked.current_steps = state.global_step * gradient_accumulation_steps + tracked.max_steps = state.max_steps * gradient_accumulation_steps + if WANT_INTERRUPT: + control.should_epoch_stop = True + control.should_training_stop = True + elif state.global_step > 0 and actual_save_steps > 0 and state.global_step % actual_save_steps == 0: + lora_model.save_pretrained(f"{lora_file_path}/checkpoint-{tracked.current_steps}/") + # Save log + with open(f"{lora_file_path}/checkpoint-{tracked.current_steps}/training_log.json", 'w', encoding='utf-8') as file: + json.dump(train_log, file, indent=2) + # == Save training prompt == + with open(f"{lora_file_path}/checkpoint-{tracked.current_steps}/training_prompt.json", 'w', encoding='utf-8') as file: + json.dump(train_template, file, indent=2) + + def on_substep_end(self, args: transformers.TrainingArguments, state: transformers.TrainerState, control: transformers.TrainerControl, **kwargs): + tracked.current_steps += 1 + if WANT_INTERRUPT: + control.should_epoch_stop = True + control.should_training_stop = True + + def on_log(self, args: transformers.TrainingArguments, state: transformers.TrainerState, control: transformers.TrainerControl, logs, **kwargs): + train_log.update(logs) + train_log.update({"current_steps": tracked.current_steps}) + if WANT_INTERRUPT: + print("\033[1;31;1mInterrupted by user\033[0;37;0m") + + print(f"\033[1;30;40mStep: {tracked.current_steps} \033[0;37;0m", end='') + if 'loss' in logs: + loss = float(logs['loss']) + if loss <= stop_at_loss: + control.should_epoch_stop = True + control.should_training_stop = True + print(f"\033[1;31;1mStop Loss {stop_at_loss} reached.\033[0;37;0m") + + trainer = transformers.Trainer( + model=lora_model, + train_dataset=train_data, + eval_dataset=eval_data, + args=transformers.TrainingArguments( + report_to=report_to if report_to != "None" else None, + per_device_train_batch_size=micro_batch_size, + gradient_accumulation_steps=gradient_accumulation_steps, + warmup_steps=math.ceil(warmup_steps / gradient_accumulation_steps), + num_train_epochs=epochs, + learning_rate=actual_lr, + fp16=False if shared.args.cpu or shared.args.bf16 else True, + bf16=shared.args.bf16, + optim=optimizer, + logging_steps=2 if stop_at_loss > 0 else 5, + evaluation_strategy="steps" if eval_data is not None else "no", + eval_steps=math.ceil(eval_steps / gradient_accumulation_steps) if eval_data is not None else None, + save_strategy="steps" if eval_data is not None else "no", + output_dir=lora_file_path, + lr_scheduler_type=lr_scheduler_type, + load_best_model_at_end=eval_data is not None, + # TODO: Enable multi-device support + ddp_find_unused_parameters=None, + no_cuda=shared.args.cpu, + use_ipex=True if is_torch_xpu_available() and not shared.args.cpu else False + ), + data_collator=transformers.DataCollatorForLanguageModeling(shared.tokenizer, mlm=False), + callbacks=list([Callbacks()]) + ) + + lora_model.config.use_cache = False + + if torch.__version__ >= "2" and sys.platform != "win32": + lora_model = torch.compile(lora_model) + + # == Save parameters for reuse == + with open(f"{lora_file_path}/training_parameters.json", 'w', encoding='utf-8') as file: + vars = locals() + json.dump({x: vars[x] for x in PARAMETERS}, file, indent=2) + + # == Save training prompt == + with open(f"{lora_file_path}/training_prompt.json", 'w', encoding='utf-8') as file: + json.dump(train_template, file, indent=2) + + # == Main run and monitor loop == + logger.info("Starting training") + yield "Starting..." + + lora_trainable_param, lora_all_param = calc_trainable_parameters(lora_model) + + projections_string = ", ".join([projection.replace("_proj", "") for projection in list_target_modules(model_id)]) + + print(f"Training '{model_id}' model using ({projections_string}) projections") + + if lora_all_param > 0: + print(f"Trainable params: {lora_trainable_param:,d} ({100 * lora_trainable_param / lora_all_param:.4f} %), All params: {lora_all_param:,d} (Model: {model_all_params:,d})") + + train_log.update({"base_model_name": shared.model_name}) + train_log.update({"base_model_class": shared.model.__class__.__name__}) + train_log.update({"base_loaded_in_4bit": getattr(lora_model, "is_loaded_in_4bit", False)}) + train_log.update({"base_loaded_in_8bit": getattr(lora_model, "is_loaded_in_8bit", False)}) + train_log.update({"projections": projections_string}) + + if stop_at_loss > 0: + print(f"Monitoring loss \033[1;31;1m(Auto-Stop at: {stop_at_loss})\033[0;37;0m") + + if WANT_INTERRUPT: + yield "Interrupted before start." + return + + def log_train_dataset(trainer): + decoded_entries = [] + # Try to decode the entries and write the log file + try: + # Iterate over the first 10 elements in the dataset (or fewer if there are less than 10) + for i in range(min(10, len(trainer.train_dataset))): + decoded_text = shared.tokenizer.decode(trainer.train_dataset[i]['input_ids']) + decoded_entries.append({"value": decoded_text}) + + # Write the log file + Path('logs').mkdir(exist_ok=True) + with open(Path('logs/train_dataset_sample.json'), 'w') as json_file: + json.dump(decoded_entries, json_file, indent=4) + + logger.info("Log file 'train_dataset_sample.json' created in the 'logs' directory.") + except Exception as e: + logger.error(f"Failed to create log file due to error: {e}") + + def threaded_run(): + log_train_dataset(trainer) + trainer.train() + # Note: save in the thread in case the gradio thread breaks (eg browser closed) + lora_model.save_pretrained(lora_file_path) + logger.info("LoRA training run is completed and saved.") + # Save log + with open(f"{lora_file_path}/training_log.json", 'w', encoding='utf-8') as file: + json.dump(train_log, file, indent=2) + + thread = threading.Thread(target=threaded_run) + thread.start() + last_step = 0 + start_time = time.perf_counter() + + while thread.is_alive(): + time.sleep(0.5) + if WANT_INTERRUPT: + yield "Interrupting, please wait... *(Run will stop after the current training step completes.)*" + + elif tracked.current_steps != last_step: + last_step = tracked.current_steps + time_elapsed = time.perf_counter() - start_time + if time_elapsed <= 0: + timer_info = "" + total_time_estimate = 999 + else: + its = tracked.current_steps / time_elapsed + if its > 1: + timer_info = f"`{its:.2f}` it/s" + else: + timer_info = f"`{1.0/its:.2f}` s/it" + + total_time_estimate = (1.0 / its) * (tracked.max_steps) + + yield f"Running... **{tracked.current_steps}** / **{tracked.max_steps}** ... {timer_info}, {format_time(time_elapsed)} / {format_time(total_time_estimate)} ... {format_time(total_time_estimate - time_elapsed)} remaining" + + # Saving in the train thread might fail if an error occurs, so save here if so. + if not tracked.did_save: + logger.info("Training complete, saving") + lora_model.save_pretrained(lora_file_path) + + if WANT_INTERRUPT: + logger.info("Training interrupted.") + yield f"Interrupted. Incomplete LoRA saved to `{lora_file_path}`." + else: + logger.info("Training complete!") + yield f"Done! LoRA saved to `{lora_file_path}`.\n\nBefore testing your new LoRA, make sure to first reload the model, as it is currently dirty from training." + + +def split_chunks(arr, size, step): + for i in range(0, len(arr), step): + yield arr[i:i + size] + + +def cut_chunk_for_newline(chunk: str, max_length: int): + if '\n' not in chunk: + return chunk + + first_newline = chunk.index('\n') + if first_newline < max_length: + chunk = chunk[first_newline + 1:] + + if '\n' not in chunk: + return chunk + + last_newline = chunk.rindex('\n') + if len(chunk) - last_newline < max_length: + chunk = chunk[:last_newline] + + return chunk + + +def format_time(seconds: float): + if seconds < 120: + return f"`{seconds:.0f}` seconds" + + minutes = seconds / 60 + if minutes < 120: + return f"`{minutes:.0f}` minutes" + + hours = minutes / 60 + return f"`{hours:.0f}` hours" diff --git a/python/llm/example/Text-Generation-WebUI/modules/ui.py b/python/llm/example/Text-Generation-WebUI/modules/ui.py new file mode 100644 index 00000000..37b182d9 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/ui.py @@ -0,0 +1,283 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/ui.py + + +import copy +from pathlib import Path + +import gradio as gr +import torch +import yaml +from transformers import is_torch_xpu_available + +import extensions +from modules import shared + +with open(Path(__file__).resolve().parent / '../css/NotoSans/stylesheet.css', 'r') as f: + css = f.read() +with open(Path(__file__).resolve().parent / '../css/main.css', 'r') as f: + css += f.read() +with open(Path(__file__).resolve().parent / '../js/main.js', 'r') as f: + js = f.read() +with open(Path(__file__).resolve().parent / '../js/save_files.js', 'r') as f: + save_files_js = f.read() +with open(Path(__file__).resolve().parent / '../js/switch_tabs.js', 'r') as f: + switch_tabs_js = f.read() +with open(Path(__file__).resolve().parent / '../js/show_controls.js', 'r') as f: + show_controls_js = f.read() +with open(Path(__file__).resolve().parent / '../js/update_big_picture.js', 'r') as f: + update_big_picture_js = f.read() + +refresh_symbol = '🔄' +delete_symbol = '🗑️' +save_symbol = '💾' + +theme = gr.themes.Default( + font=['Noto Sans', 'Helvetica', 'ui-sans-serif', 'system-ui', 'sans-serif'], + font_mono=['IBM Plex Mono', 'ui-monospace', 'Consolas', 'monospace'], +).set( + border_color_primary='#c5c5d2', + button_large_padding='6px 12px', + body_text_color_subdued='#484848', + background_fill_secondary='#eaeaea' +) + +if Path("notification.mp3").exists(): + audio_notification_js = "document.querySelector('#audio_notification audio')?.play();" +else: + audio_notification_js = "" + + +def list_model_elements(): + elements = [ + 'loader', + 'filter_by_loader', + 'cpu_memory', + 'auto_devices', + 'disk', + 'cpu', + 'bf16', + 'load_in_8bit', + 'trust_remote_code', + 'no_use_fast', + 'use_flash_attention_2', + 'device', + 'load_in_4bit', + 'load_in_low_bit', + 'optimize_model', + 'modules_to_not_convert', + 'cpu_embedding', + 'lightweight_bmm', + 'use_cache', + 'compute_dtype', + 'quant_type', + 'use_double_quant', + 'wbits', + 'groupsize', + 'model_type', + 'pre_layer', + 'triton', + 'desc_act', + 'no_inject_fused_attention', + 'no_inject_fused_mlp', + 'no_use_cuda_fp16', + 'disable_exllama', + 'disable_exllamav2', + 'cfg_cache', + 'no_flash_attn', + 'num_experts_per_token', + 'cache_8bit', + 'threads', + 'threads_batch', + 'n_batch', + 'no_mmap', + 'mlock', + 'no_mul_mat_q', + 'n_gpu_layers', + 'tensor_split', + 'n_ctx', + 'gpu_split', + 'max_seq_len', + 'compress_pos_emb', + 'alpha_value', + 'rope_freq_base', + 'numa', + 'logits_all', + 'no_offload_kqv', + 'tensorcores', + 'hqq_backend', + ] + if is_torch_xpu_available(): + for i in range(torch.xpu.device_count()): + elements.append(f'gpu_memory_{i}') + else: + for i in range(torch.cuda.device_count()): + elements.append(f'gpu_memory_{i}') + + return elements + + +def list_interface_input_elements(): + elements = [ + 'max_new_tokens', + 'auto_max_new_tokens', + 'max_tokens_second', + 'max_updates_second', + 'seed', + 'temperature', + 'temperature_last', + 'dynamic_temperature', + 'dynamic_temperature_low', + 'top_p', + 'min_p', + 'top_k', + 'typical_p', + 'epsilon_cutoff', + 'eta_cutoff', + 'repetition_penalty', + 'presence_penalty', + 'frequency_penalty', + 'repetition_penalty_range', + 'encoder_repetition_penalty', + 'no_repeat_ngram_size', + 'min_length', + 'do_sample', + 'penalty_alpha', + 'num_beams', + 'length_penalty', + 'early_stopping', + 'mirostat_mode', + 'mirostat_tau', + 'mirostat_eta', + 'grammar_string', + 'negative_prompt', + 'guidance_scale', + 'add_bos_token', + 'ban_eos_token', + 'custom_token_bans', + 'truncation_length', + 'custom_stopping_strings', + 'skip_special_tokens', + 'stream', + 'tfs', + 'top_a', + ] + + # Chat elements + elements += [ + 'textbox', + 'start_with', + 'character_menu', + 'history', + 'name1', + 'name2', + 'greeting', + 'context', + 'mode', + 'custom_system_message', + 'instruction_template_str', + 'chat_template_str', + 'chat_style', + 'chat-instruct_command', + ] + + # Notebook/default elements + elements += [ + 'textbox-notebook', + 'textbox-default', + 'output_textbox', + 'prompt_menu-default', + 'prompt_menu-notebook', + ] + + # Model elements + elements += list_model_elements() + + return elements + + +def gather_interface_values(*args): + output = {} + for i, element in enumerate(list_interface_input_elements()): + output[element] = args[i] + + if not shared.args.multi_user: + shared.persistent_interface_state = output + + return output + + +def apply_interface_values(state, use_persistent=False): + if use_persistent: + state = shared.persistent_interface_state + + elements = list_interface_input_elements() + if len(state) == 0: + return [gr.update() for k in elements] # Dummy, do nothing + else: + return [state[k] if k in state else gr.update() for k in elements] + + +def save_settings(state, preset, extensions_list, show_controls): + output = copy.deepcopy(shared.settings) + exclude = ['name2', 'greeting', 'context', 'turn_template'] + for k in state: + if k in shared.settings and k not in exclude: + output[k] = state[k] + + output['preset'] = preset + output['prompt-default'] = state['prompt_menu-default'] + output['prompt-notebook'] = state['prompt_menu-notebook'] + output['character'] = state['character_menu'] + output['default_extensions'] = extensions_list + output['seed'] = int(output['seed']) + output['show_controls'] = show_controls + + # Save extension values in the UI + for extension_name in extensions_list: + extension = getattr(extensions, extension_name).script + if hasattr(extension, 'params'): + params = getattr(extension, 'params') + for param in params: + _id = f"{extension_name}-{param}" + output[_id] = params[param] + + return yaml.dump(output, sort_keys=False, width=float("inf")) + + +def create_refresh_button(refresh_component, refresh_method, refreshed_args, elem_class, interactive=True): + """ + Copied from https://github.com/AUTOMATIC1111/stable-diffusion-webui + """ + def refresh(): + refresh_method() + args = refreshed_args() if callable(refreshed_args) else refreshed_args + + for k, v in args.items(): + setattr(refresh_component, k, v) + + return gr.update(**(args or {})) + + refresh_button = gr.Button(refresh_symbol, elem_classes=elem_class, interactive=interactive) + refresh_button.click( + fn=refresh, + inputs=[], + outputs=[refresh_component] + ) + + return refresh_button diff --git a/python/llm/example/Text-Generation-WebUI/modules/ui_chat.py b/python/llm/example/Text-Generation-WebUI/modules/ui_chat.py new file mode 100644 index 00000000..871ff4df --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/ui_chat.py @@ -0,0 +1,383 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/ui_chat.py + + +import json +from functools import partial +from pathlib import Path + +import gradio as gr +from PIL import Image + +from modules import chat, shared, ui, utils +from modules.html_generator import chat_html_wrapper +from modules.text_generation import stop_everything_event +from modules.utils import gradio + +inputs = ('Chat input', 'interface_state') +reload_arr = ('history', 'name1', 'name2', 'mode', 'chat_style', 'character_menu') +clear_arr = ('delete_chat-confirm', 'delete_chat', 'delete_chat-cancel') + + +def create_ui(): + mu = shared.args.multi_user + + shared.gradio['Chat input'] = gr.State() + shared.gradio['history'] = gr.State({'internal': [], 'visible': []}) + + with gr.Tab('Chat', elem_id='chat-tab', elem_classes=("old-ui" if shared.args.chat_buttons else None)): + with gr.Row(): + with gr.Column(elem_id='chat-col'): + shared.gradio['display'] = gr.HTML(value=chat_html_wrapper({'internal': [], 'visible': []}, '', '', 'chat', 'cai-chat', '')) + + with gr.Row(elem_id="chat-input-row"): + with gr.Column(scale=1, elem_id='gr-hover-container'): + gr.HTML(value='
      ', elem_id='gr-hover') + + with gr.Column(scale=10, elem_id='chat-input-container'): + shared.gradio['textbox'] = gr.Textbox(label='', placeholder='Send a message', elem_id='chat-input', elem_classes=['add_scrollbar']) + shared.gradio['show_controls'] = gr.Checkbox(value=shared.settings['show_controls'], label='Show controls (Ctrl+S)', elem_id='show-controls') + shared.gradio['typing-dots'] = gr.HTML(value='
      ', label='typing', elem_id='typing-container') + + with gr.Column(scale=1, elem_id='generate-stop-container'): + with gr.Row(): + shared.gradio['Stop'] = gr.Button('Stop', elem_id='stop', visible=False) + shared.gradio['Generate'] = gr.Button('Generate', elem_id='Generate', variant='primary') + + # Hover menu buttons + with gr.Column(elem_id='chat-buttons'): + with gr.Row(): + shared.gradio['Regenerate'] = gr.Button('Regenerate (Ctrl + Enter)', elem_id='Regenerate') + shared.gradio['Continue'] = gr.Button('Continue (Alt + Enter)', elem_id='Continue') + shared.gradio['Remove last'] = gr.Button('Remove last reply (Ctrl + Shift + Backspace)', elem_id='Remove-last') + + with gr.Row(): + shared.gradio['Replace last reply'] = gr.Button('Replace last reply (Ctrl + Shift + L)', elem_id='Replace-last') + shared.gradio['Copy last reply'] = gr.Button('Copy last reply (Ctrl + Shift + K)', elem_id='Copy-last') + shared.gradio['Impersonate'] = gr.Button('Impersonate (Ctrl + Shift + M)', elem_id='Impersonate') + + with gr.Row(): + shared.gradio['Send dummy message'] = gr.Button('Send dummy message') + shared.gradio['Send dummy reply'] = gr.Button('Send dummy reply') + + with gr.Row(): + shared.gradio['Start new chat'] = gr.Button('Start new chat') + + with gr.Row(): + shared.gradio['send-chat-to-default'] = gr.Button('Send to default') + shared.gradio['send-chat-to-notebook'] = gr.Button('Send to notebook') + + with gr.Row(elem_id='past-chats-row'): + shared.gradio['unique_id'] = gr.Dropdown(label='Past chats', elem_classes=['slim-dropdown'], interactive=not mu) + shared.gradio['rename_chat'] = gr.Button('Rename', elem_classes='refresh-button', interactive=not mu) + shared.gradio['delete_chat'] = gr.Button('🗑️', elem_classes='refresh-button', interactive=not mu) + shared.gradio['delete_chat-cancel'] = gr.Button('Cancel', visible=False, elem_classes='refresh-button') + shared.gradio['delete_chat-confirm'] = gr.Button('Confirm', variant='stop', visible=False, elem_classes='refresh-button') + + with gr.Row(elem_id='rename-row'): + shared.gradio['rename_to'] = gr.Textbox(label='Rename to:', placeholder='New name', visible=False, elem_classes=['no-background']) + shared.gradio['rename_to-cancel'] = gr.Button('Cancel', visible=False, elem_classes='refresh-button') + shared.gradio['rename_to-confirm'] = gr.Button('Confirm', visible=False, elem_classes='refresh-button') + + with gr.Row(): + shared.gradio['start_with'] = gr.Textbox(label='Start reply with', placeholder='Sure thing!', value=shared.settings['start_with']) + + with gr.Row(): + shared.gradio['mode'] = gr.Radio(choices=['chat', 'chat-instruct', 'instruct'], value='chat', label='Mode', info='Defines how the chat prompt is generated. In instruct and chat-instruct modes, the instruction template selected under Parameters > Instruction template must match the current model.', elem_id='chat-mode') + shared.gradio['chat_style'] = gr.Dropdown(choices=utils.get_available_chat_styles(), label='Chat style', value=shared.settings['chat_style'], visible=shared.settings['mode'] != 'instruct') + + +def create_chat_settings_ui(): + mu = shared.args.multi_user + with gr.Tab('Character'): + with gr.Row(): + with gr.Column(scale=8): + with gr.Row(): + shared.gradio['character_menu'] = gr.Dropdown(value=None, choices=utils.get_available_characters(), label='Character', elem_id='character-menu', info='Used in chat and chat-instruct modes.', elem_classes='slim-dropdown') + ui.create_refresh_button(shared.gradio['character_menu'], lambda: None, lambda: {'choices': utils.get_available_characters()}, 'refresh-button', interactive=not mu) + shared.gradio['save_character'] = gr.Button('💾', elem_classes='refresh-button', interactive=not mu) + shared.gradio['delete_character'] = gr.Button('🗑️', elem_classes='refresh-button', interactive=not mu) + + shared.gradio['name1'] = gr.Textbox(value=shared.settings['name1'], lines=1, label='Your name') + shared.gradio['name2'] = gr.Textbox(value='', lines=1, label='Character\'s name') + shared.gradio['context'] = gr.Textbox(value='', lines=10, label='Context', elem_classes=['add_scrollbar']) + shared.gradio['greeting'] = gr.Textbox(value='', lines=5, label='Greeting', elem_classes=['add_scrollbar']) + + with gr.Column(scale=1): + shared.gradio['character_picture'] = gr.Image(label='Character picture', type='pil', interactive=not mu) + shared.gradio['your_picture'] = gr.Image(label='Your picture', type='pil', value=Image.open(Path('cache/pfp_me.png')) if Path('cache/pfp_me.png').exists() else None, interactive=not mu) + + with gr.Tab('Instruction template'): + with gr.Row(): + with gr.Column(): + with gr.Row(): + shared.gradio['instruction_template'] = gr.Dropdown(choices=utils.get_available_instruction_templates(), label='Saved instruction templates', value='Select template to load...', elem_classes='slim-dropdown') + ui.create_refresh_button(shared.gradio['instruction_template'], lambda: None, lambda: {'choices': utils.get_available_instruction_templates()}, 'refresh-button', interactive=not mu) + shared.gradio['load_template'] = gr.Button("Load", elem_classes='refresh-button') + shared.gradio['save_template'] = gr.Button('💾', elem_classes='refresh-button', interactive=not mu) + shared.gradio['delete_template'] = gr.Button('🗑️ ', elem_classes='refresh-button', interactive=not mu) + + with gr.Column(): + pass + + with gr.Row(): + with gr.Column(): + shared.gradio['custom_system_message'] = gr.Textbox(value=shared.settings['custom_system_message'], lines=2, label='Custom system message', info='If not empty, will be used instead of the default one.', elem_classes=['add_scrollbar']) + shared.gradio['instruction_template_str'] = gr.Textbox(value='', label='Instruction template', lines=24, info='Change this according to the model/LoRA that you are using. Used in instruct and chat-instruct modes.', elem_classes=['add_scrollbar', 'monospace']) + with gr.Row(): + shared.gradio['send_instruction_to_default'] = gr.Button('Send to default', elem_classes=['small-button']) + shared.gradio['send_instruction_to_notebook'] = gr.Button('Send to notebook', elem_classes=['small-button']) + shared.gradio['send_instruction_to_negative_prompt'] = gr.Button('Send to negative prompt', elem_classes=['small-button']) + + with gr.Column(): + shared.gradio['chat_template_str'] = gr.Textbox(value=shared.settings['chat_template_str'], label='Chat template', lines=22, elem_classes=['add_scrollbar', 'monospace']) + shared.gradio['chat-instruct_command'] = gr.Textbox(value=shared.settings['chat-instruct_command'], lines=4, label='Command for chat-instruct mode', info='<|character|> gets replaced by the bot name, and <|prompt|> gets replaced by the regular chat prompt.', elem_classes=['add_scrollbar']) + + with gr.Tab('Chat history'): + with gr.Row(): + with gr.Column(): + shared.gradio['save_chat_history'] = gr.Button(value='Save history') + + with gr.Column(): + shared.gradio['load_chat_history'] = gr.File(type='binary', file_types=['.json', '.txt'], label='Upload History JSON') + + with gr.Tab('Upload character'): + with gr.Tab('YAML or JSON'): + with gr.Row(): + shared.gradio['upload_json'] = gr.File(type='binary', file_types=['.json', '.yaml'], label='JSON or YAML File', interactive=not mu) + shared.gradio['upload_img_bot'] = gr.Image(type='pil', label='Profile Picture (optional)', interactive=not mu) + + shared.gradio['Submit character'] = gr.Button(value='Submit', interactive=False) + + with gr.Tab('TavernAI PNG'): + with gr.Row(): + with gr.Column(): + shared.gradio['upload_img_tavern'] = gr.Image(type='pil', label='TavernAI PNG File', elem_id='upload_img_tavern', interactive=not mu) + shared.gradio['tavern_json'] = gr.State() + with gr.Column(): + shared.gradio['tavern_name'] = gr.Textbox(value='', lines=1, label='Name', interactive=False) + shared.gradio['tavern_desc'] = gr.Textbox(value='', lines=4, max_lines=4, label='Description', interactive=False) + + shared.gradio['Submit tavern character'] = gr.Button(value='Submit', interactive=False) + + +def create_event_handlers(): + + # Obsolete variables, kept for compatibility with old extensions + shared.input_params = gradio(inputs) + shared.reload_inputs = gradio(reload_arr) + + shared.gradio['Generate'].click( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + lambda x: (x, ''), gradio('textbox'), gradio('Chat input', 'textbox'), show_progress=False).then( + chat.generate_chat_reply_wrapper, gradio(inputs), gradio('display', 'history'), show_progress=False).then( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + chat.save_history, gradio('history', 'unique_id', 'character_menu', 'mode'), None).then( + lambda: None, None, None, _js=f'() => {{{ui.audio_notification_js}}}') + + shared.gradio['textbox'].submit( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + lambda x: (x, ''), gradio('textbox'), gradio('Chat input', 'textbox'), show_progress=False).then( + chat.generate_chat_reply_wrapper, gradio(inputs), gradio('display', 'history'), show_progress=False).then( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + chat.save_history, gradio('history', 'unique_id', 'character_menu', 'mode'), None).then( + lambda: None, None, None, _js=f'() => {{{ui.audio_notification_js}}}') + + shared.gradio['Regenerate'].click( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + partial(chat.generate_chat_reply_wrapper, regenerate=True), gradio(inputs), gradio('display', 'history'), show_progress=False).then( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + chat.save_history, gradio('history', 'unique_id', 'character_menu', 'mode'), None).then( + lambda: None, None, None, _js=f'() => {{{ui.audio_notification_js}}}') + + shared.gradio['Continue'].click( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + partial(chat.generate_chat_reply_wrapper, _continue=True), gradio(inputs), gradio('display', 'history'), show_progress=False).then( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + chat.save_history, gradio('history', 'unique_id', 'character_menu', 'mode'), None).then( + lambda: None, None, None, _js=f'() => {{{ui.audio_notification_js}}}') + + shared.gradio['Impersonate'].click( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + lambda x: x, gradio('textbox'), gradio('Chat input'), show_progress=False).then( + chat.impersonate_wrapper, gradio(inputs), gradio('textbox', 'display'), show_progress=False).then( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + lambda: None, None, None, _js=f'() => {{{ui.audio_notification_js}}}') + + shared.gradio['Replace last reply'].click( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + chat.replace_last_reply, gradio('textbox', 'interface_state'), gradio('history')).then( + lambda: '', None, gradio('textbox'), show_progress=False).then( + chat.redraw_html, gradio(reload_arr), gradio('display')).then( + chat.save_history, gradio('history', 'unique_id', 'character_menu', 'mode'), None) + + shared.gradio['Send dummy message'].click( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + chat.send_dummy_message, gradio('textbox', 'interface_state'), gradio('history')).then( + lambda: '', None, gradio('textbox'), show_progress=False).then( + chat.redraw_html, gradio(reload_arr), gradio('display')).then( + chat.save_history, gradio('history', 'unique_id', 'character_menu', 'mode'), None) + + shared.gradio['Send dummy reply'].click( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + chat.send_dummy_reply, gradio('textbox', 'interface_state'), gradio('history')).then( + lambda: '', None, gradio('textbox'), show_progress=False).then( + chat.redraw_html, gradio(reload_arr), gradio('display')).then( + chat.save_history, gradio('history', 'unique_id', 'character_menu', 'mode'), None) + + shared.gradio['Remove last'].click( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + chat.remove_last_message, gradio('history'), gradio('textbox', 'history'), show_progress=False).then( + chat.redraw_html, gradio(reload_arr), gradio('display')).then( + chat.save_history, gradio('history', 'unique_id', 'character_menu', 'mode'), None) + + shared.gradio['Stop'].click( + stop_everything_event, None, None, queue=False).then( + chat.redraw_html, gradio(reload_arr), gradio('display')) + + if not shared.args.multi_user: + shared.gradio['unique_id'].select( + chat.load_history, gradio('unique_id', 'character_menu', 'mode'), gradio('history')).then( + chat.redraw_html, gradio(reload_arr), gradio('display')) + + shared.gradio['Start new chat'].click( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + chat.start_new_chat, gradio('interface_state'), gradio('history')).then( + chat.redraw_html, gradio(reload_arr), gradio('display')).then( + lambda x: gr.update(choices=(histories := chat.find_all_histories(x)), value=histories[0]), gradio('interface_state'), gradio('unique_id')) + + shared.gradio['delete_chat'].click(lambda: [gr.update(visible=True), gr.update(visible=False), gr.update(visible=True)], None, gradio(clear_arr)) + shared.gradio['delete_chat-cancel'].click(lambda: [gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)], None, gradio(clear_arr)) + shared.gradio['delete_chat-confirm'].click( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + chat.delete_history, gradio('unique_id', 'character_menu', 'mode'), None).then( + chat.load_latest_history, gradio('interface_state'), gradio('history')).then( + chat.redraw_html, gradio(reload_arr), gradio('display')).then( + lambda x: gr.update(choices=(histories := chat.find_all_histories(x)), value=histories[0]), gradio('interface_state'), gradio('unique_id')).then( + lambda: [gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)], None, gradio(clear_arr)) + + shared.gradio['rename_chat'].click( + lambda x: x, gradio('unique_id'), gradio('rename_to')).then( + lambda: [gr.update(visible=True)] * 3, None, gradio('rename_to', 'rename_to-confirm', 'rename_to-cancel'), show_progress=False) + + shared.gradio['rename_to-cancel'].click( + lambda: [gr.update(visible=False)] * 3, None, gradio('rename_to', 'rename_to-confirm', 'rename_to-cancel'), show_progress=False) + + shared.gradio['rename_to-confirm'].click( + chat.rename_history, gradio('unique_id', 'rename_to', 'character_menu', 'mode'), None).then( + lambda: [gr.update(visible=False)] * 3, None, gradio('rename_to', 'rename_to-confirm', 'rename_to-cancel'), show_progress=False).then( + lambda x, y: gr.update(choices=chat.find_all_histories(x), value=y), gradio('interface_state', 'rename_to'), gradio('unique_id')) + + shared.gradio['load_chat_history'].upload( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + chat.start_new_chat, gradio('interface_state'), gradio('history')).then( + chat.load_history_json, gradio('load_chat_history', 'history'), gradio('history')).then( + chat.redraw_html, gradio(reload_arr), gradio('display')).then( + lambda x: gr.update(choices=(histories := chat.find_all_histories(x)), value=histories[0]), gradio('interface_state'), gradio('unique_id')).then( + chat.save_history, gradio('history', 'unique_id', 'character_menu', 'mode'), None).then( + lambda: None, None, None, _js=f'() => {{{ui.switch_tabs_js}; switch_to_chat()}}') + + shared.gradio['character_menu'].change( + chat.load_character, gradio('character_menu', 'name1', 'name2'), gradio('name1', 'name2', 'character_picture', 'greeting', 'context')).success( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + chat.load_latest_history, gradio('interface_state'), gradio('history')).then( + chat.redraw_html, gradio(reload_arr), gradio('display')).then( + lambda x: gr.update(choices=(histories := chat.find_all_histories(x)), value=histories[0]), gradio('interface_state'), gradio('unique_id')).then( + lambda: None, None, None, _js=f'() => {{{ui.update_big_picture_js}; updateBigPicture()}}') + + shared.gradio['mode'].change( + lambda x: gr.update(visible=x != 'instruct'), gradio('mode'), gradio('chat_style'), show_progress=False).then( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + chat.load_latest_history, gradio('interface_state'), gradio('history')).then( + chat.redraw_html, gradio(reload_arr), gradio('display')).then( + lambda x: gr.update(choices=(histories := chat.find_all_histories(x)), value=histories[0]), gradio('interface_state'), gradio('unique_id')) + + shared.gradio['chat_style'].change(chat.redraw_html, gradio(reload_arr), gradio('display')) + shared.gradio['Copy last reply'].click(chat.send_last_reply_to_input, gradio('history'), gradio('textbox'), show_progress=False) + + # Save/delete a character + shared.gradio['save_character'].click( + lambda x: x, gradio('name2'), gradio('save_character_filename')).then( + lambda: gr.update(visible=True), None, gradio('character_saver')) + + shared.gradio['delete_character'].click(lambda: gr.update(visible=True), None, gradio('character_deleter')) + + shared.gradio['load_template'].click( + chat.load_instruction_template, gradio('instruction_template'), gradio('instruction_template_str')).then( + lambda: "Select template to load...", None, gradio('instruction_template')) + + shared.gradio['save_template'].click( + lambda: 'My Template.yaml', None, gradio('save_filename')).then( + lambda: 'instruction-templates/', None, gradio('save_root')).then( + chat.generate_instruction_template_yaml, gradio('instruction_template_str'), gradio('save_contents')).then( + lambda: gr.update(visible=True), None, gradio('file_saver')) + + shared.gradio['delete_template'].click( + lambda x: f'{x}.yaml', gradio('instruction_template'), gradio('delete_filename')).then( + lambda: 'instruction-templates/', None, gradio('delete_root')).then( + lambda: gr.update(visible=True), None, gradio('file_deleter')) + + shared.gradio['save_chat_history'].click( + lambda x: json.dumps(x, indent=4), gradio('history'), gradio('temporary_text')).then( + None, gradio('temporary_text', 'character_menu', 'mode'), None, _js=f'(hist, char, mode) => {{{ui.save_files_js}; saveHistory(hist, char, mode)}}') + + shared.gradio['Submit character'].click( + chat.upload_character, gradio('upload_json', 'upload_img_bot'), gradio('character_menu')).then( + lambda: None, None, None, _js=f'() => {{{ui.switch_tabs_js}; switch_to_character()}}') + + shared.gradio['Submit tavern character'].click( + chat.upload_tavern_character, gradio('upload_img_tavern', 'tavern_json'), gradio('character_menu')).then( + lambda: None, None, None, _js=f'() => {{{ui.switch_tabs_js}; switch_to_character()}}') + + shared.gradio['upload_json'].upload(lambda: gr.update(interactive=True), None, gradio('Submit character')) + shared.gradio['upload_json'].clear(lambda: gr.update(interactive=False), None, gradio('Submit character')) + shared.gradio['upload_img_tavern'].upload(chat.check_tavern_character, gradio('upload_img_tavern'), gradio('tavern_name', 'tavern_desc', 'tavern_json', 'Submit tavern character'), show_progress=False) + shared.gradio['upload_img_tavern'].clear(lambda: (None, None, None, gr.update(interactive=False)), None, gradio('tavern_name', 'tavern_desc', 'tavern_json', 'Submit tavern character'), show_progress=False) + shared.gradio['your_picture'].change( + chat.upload_your_profile_picture, gradio('your_picture'), None).then( + partial(chat.redraw_html, reset_cache=True), gradio(reload_arr), gradio('display')) + + shared.gradio['send_instruction_to_default'].click( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + lambda x: x.update({'mode': 'instruct', 'history': {'internal': [], 'visible': []}}), gradio('interface_state'), None).then( + partial(chat.generate_chat_prompt, 'Input'), gradio('interface_state'), gradio('textbox-default')).then( + lambda: None, None, None, _js=f'() => {{{ui.switch_tabs_js}; switch_to_default()}}') + + shared.gradio['send_instruction_to_notebook'].click( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + lambda x: x.update({'mode': 'instruct', 'history': {'internal': [], 'visible': []}}), gradio('interface_state'), None).then( + partial(chat.generate_chat_prompt, 'Input'), gradio('interface_state'), gradio('textbox-notebook')).then( + lambda: None, None, None, _js=f'() => {{{ui.switch_tabs_js}; switch_to_notebook()}}') + + shared.gradio['send_instruction_to_negative_prompt'].click( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + lambda x: x.update({'mode': 'instruct', 'history': {'internal': [], 'visible': []}}), gradio('interface_state'), None).then( + partial(chat.generate_chat_prompt, 'Input'), gradio('interface_state'), gradio('negative_prompt')).then( + lambda: None, None, None, _js=f'() => {{{ui.switch_tabs_js}; switch_to_generation_parameters()}}') + + shared.gradio['send-chat-to-default'].click( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + partial(chat.generate_chat_prompt, '', _continue=True), gradio('interface_state'), gradio('textbox-default')).then( + lambda: None, None, None, _js=f'() => {{{ui.switch_tabs_js}; switch_to_default()}}') + + shared.gradio['send-chat-to-notebook'].click( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + partial(chat.generate_chat_prompt, '', _continue=True), gradio('interface_state'), gradio('textbox-notebook')).then( + lambda: None, None, None, _js=f'() => {{{ui.switch_tabs_js}; switch_to_notebook()}}') + + shared.gradio['show_controls'].change(None, gradio('show_controls'), None, _js=f'(x) => {{{ui.show_controls_js}; toggle_controls(x)}}') diff --git a/python/llm/example/Text-Generation-WebUI/modules/ui_default.py b/python/llm/example/Text-Generation-WebUI/modules/ui_default.py new file mode 100644 index 00000000..8daf7ca7 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/ui_default.py @@ -0,0 +1,123 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/ui_default.py + + +import gradio as gr + +from modules import logits, shared, ui, utils +from modules.prompts import count_tokens, load_prompt +from modules.text_generation import ( + generate_reply_wrapper, + get_token_ids, + stop_everything_event +) +from modules.utils import gradio + +inputs = ('textbox-default', 'interface_state') +outputs = ('output_textbox', 'html-default') + + +def create_ui(): + mu = shared.args.multi_user + with gr.Tab('Default', elem_id='default-tab'): + shared.gradio['last_input-default'] = gr.State('') + with gr.Row(): + with gr.Column(): + with gr.Row(): + shared.gradio['textbox-default'] = gr.Textbox(value='', lines=27, label='Input', elem_classes=['textbox_default', 'add_scrollbar']) + shared.gradio['token-counter-default'] = gr.HTML(value="0", elem_classes=["token-counter", "default-token-counter"]) + + with gr.Row(): + shared.gradio['Generate-default'] = gr.Button('Generate', variant='primary') + shared.gradio['Stop-default'] = gr.Button('Stop', elem_id='stop') + shared.gradio['Continue-default'] = gr.Button('Continue') + + with gr.Row(): + shared.gradio['prompt_menu-default'] = gr.Dropdown(choices=utils.get_available_prompts(), value='None', label='Prompt', elem_classes='slim-dropdown') + ui.create_refresh_button(shared.gradio['prompt_menu-default'], lambda: None, lambda: {'choices': utils.get_available_prompts()}, 'refresh-button', interactive=not mu) + shared.gradio['save_prompt-default'] = gr.Button('💾', elem_classes='refresh-button', interactive=not mu) + shared.gradio['delete_prompt-default'] = gr.Button('🗑️', elem_classes='refresh-button', interactive=not mu) + + with gr.Column(): + with gr.Tab('Raw'): + shared.gradio['output_textbox'] = gr.Textbox(lines=27, label='Output', elem_id='textbox-default', elem_classes=['textbox_default_output', 'add_scrollbar']) + + with gr.Tab('Markdown'): + shared.gradio['markdown_render-default'] = gr.Button('Render') + shared.gradio['markdown-default'] = gr.Markdown() + + with gr.Tab('HTML'): + shared.gradio['html-default'] = gr.HTML() + + with gr.Tab('Logits'): + with gr.Row(): + with gr.Column(scale=10): + shared.gradio['get_logits-default'] = gr.Button('Get next token probabilities') + with gr.Column(scale=1): + shared.gradio['use_samplers-default'] = gr.Checkbox(label='Use samplers', value=True, elem_classes=['no-background']) + + with gr.Row(): + shared.gradio['logits-default'] = gr.Textbox(lines=23, label='Output', elem_classes=['textbox_logits', 'add_scrollbar']) + shared.gradio['logits-default-previous'] = gr.Textbox(lines=23, label='Previous output', elem_classes=['textbox_logits', 'add_scrollbar']) + + with gr.Tab('Tokens'): + shared.gradio['get_tokens-default'] = gr.Button('Get token IDs for the input') + shared.gradio['tokens-default'] = gr.Textbox(lines=23, label='Tokens', elem_classes=['textbox_logits', 'add_scrollbar', 'monospace']) + + +def create_event_handlers(): + shared.gradio['Generate-default'].click( + lambda x: x, gradio('textbox-default'), gradio('last_input-default')).then( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + generate_reply_wrapper, gradio(inputs), gradio(outputs), show_progress=False).then( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + lambda: None, None, None, _js=f'() => {{{ui.audio_notification_js}}}') + + shared.gradio['textbox-default'].submit( + lambda x: x, gradio('textbox-default'), gradio('last_input-default')).then( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + generate_reply_wrapper, gradio(inputs), gradio(outputs), show_progress=False).then( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + lambda: None, None, None, _js=f'() => {{{ui.audio_notification_js}}}') + + shared.gradio['markdown_render-default'].click(lambda x: x, gradio('output_textbox'), gradio('markdown-default'), queue=False) + shared.gradio['Continue-default'].click( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + generate_reply_wrapper, [shared.gradio['output_textbox']] + gradio(inputs)[1:], gradio(outputs), show_progress=False).then( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + lambda: None, None, None, _js=f'() => {{{ui.audio_notification_js}}}') + + shared.gradio['Stop-default'].click(stop_everything_event, None, None, queue=False) + shared.gradio['prompt_menu-default'].change(load_prompt, gradio('prompt_menu-default'), gradio('textbox-default'), show_progress=False) + shared.gradio['save_prompt-default'].click( + lambda x: x, gradio('textbox-default'), gradio('save_contents')).then( + lambda: 'prompts/', None, gradio('save_root')).then( + lambda: utils.current_time() + '.txt', None, gradio('save_filename')).then( + lambda: gr.update(visible=True), None, gradio('file_saver')) + + shared.gradio['delete_prompt-default'].click( + lambda: 'prompts/', None, gradio('delete_root')).then( + lambda x: x + '.txt', gradio('prompt_menu-default'), gradio('delete_filename')).then( + lambda: gr.update(visible=True), None, gradio('file_deleter')) + + shared.gradio['textbox-default'].change(lambda x: f"{count_tokens(x)}", gradio('textbox-default'), gradio('token-counter-default'), show_progress=False) + shared.gradio['get_logits-default'].click( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + logits.get_next_logits, gradio('textbox-default', 'interface_state', 'use_samplers-default', 'logits-default'), gradio('logits-default', 'logits-default-previous'), show_progress=False) + + shared.gradio['get_tokens-default'].click(get_token_ids, gradio('textbox-default'), gradio('tokens-default'), show_progress=False) diff --git a/python/llm/example/Text-Generation-WebUI/modules/ui_file_saving.py b/python/llm/example/Text-Generation-WebUI/modules/ui_file_saving.py new file mode 100644 index 00000000..1009290b --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/ui_file_saving.py @@ -0,0 +1,121 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/ui_file_saving.py + + +import gradio as gr + +from modules import chat, presets, shared, ui, utils +from modules.utils import gradio + + +def create_ui(): + mu = shared.args.multi_user + + # Text file saver + with gr.Group(visible=False, elem_classes='file-saver') as shared.gradio['file_saver']: + shared.gradio['save_filename'] = gr.Textbox(lines=1, label='File name') + shared.gradio['save_root'] = gr.Textbox(lines=1, label='File folder', info='For reference. Unchangeable.', interactive=False) + shared.gradio['save_contents'] = gr.Textbox(lines=10, label='File contents') + with gr.Row(): + shared.gradio['save_confirm'] = gr.Button('Save', elem_classes="small-button", variant='primary', interactive=not mu) + shared.gradio['save_cancel'] = gr.Button('Cancel', elem_classes="small-button") + + # Text file deleter + with gr.Group(visible=False, elem_classes='file-saver') as shared.gradio['file_deleter']: + shared.gradio['delete_filename'] = gr.Textbox(lines=1, label='File name') + shared.gradio['delete_root'] = gr.Textbox(lines=1, label='File folder', info='For reference. Unchangeable.', interactive=False) + with gr.Row(): + shared.gradio['delete_confirm'] = gr.Button('Delete', elem_classes="small-button", variant='stop', interactive=not mu) + shared.gradio['delete_cancel'] = gr.Button('Cancel', elem_classes="small-button") + + # Character saver/deleter + with gr.Group(visible=False, elem_classes='file-saver') as shared.gradio['character_saver']: + shared.gradio['save_character_filename'] = gr.Textbox(lines=1, label='File name', info='The character will be saved to your characters/ folder with this base filename.') + with gr.Row(): + shared.gradio['save_character_confirm'] = gr.Button('Save', elem_classes="small-button", variant='primary', interactive=not mu) + shared.gradio['save_character_cancel'] = gr.Button('Cancel', elem_classes="small-button") + + with gr.Group(visible=False, elem_classes='file-saver') as shared.gradio['character_deleter']: + gr.Markdown('Confirm the character deletion?') + with gr.Row(): + shared.gradio['delete_character_confirm'] = gr.Button('Delete', elem_classes="small-button", variant='stop', interactive=not mu) + shared.gradio['delete_character_cancel'] = gr.Button('Cancel', elem_classes="small-button") + + # Preset saver + with gr.Group(visible=False, elem_classes='file-saver') as shared.gradio['preset_saver']: + shared.gradio['save_preset_filename'] = gr.Textbox(lines=1, label='File name', info='The preset will be saved to your presets/ folder with this base filename.') + shared.gradio['save_preset_contents'] = gr.Textbox(lines=10, label='File contents') + with gr.Row(): + shared.gradio['save_preset_confirm'] = gr.Button('Save', elem_classes="small-button", variant='primary', interactive=not mu) + shared.gradio['save_preset_cancel'] = gr.Button('Cancel', elem_classes="small-button") + + +def create_event_handlers(): + shared.gradio['save_confirm'].click( + lambda x, y, z: utils.save_file(x + y, z), gradio('save_root', 'save_filename', 'save_contents'), None).then( + lambda: gr.update(visible=False), None, gradio('file_saver')) + + shared.gradio['delete_confirm'].click( + lambda x, y: utils.delete_file(x + y), gradio('delete_root', 'delete_filename'), None).then( + lambda: gr.update(visible=False), None, gradio('file_deleter')) + + shared.gradio['delete_cancel'].click(lambda: gr.update(visible=False), None, gradio('file_deleter')) + shared.gradio['save_cancel'].click(lambda: gr.update(visible=False), None, gradio('file_saver')) + + shared.gradio['save_character_confirm'].click( + chat.save_character, gradio('name2', 'greeting', 'context', 'character_picture', 'save_character_filename'), None).then( + lambda: gr.update(visible=False), None, gradio('character_saver')).then( + lambda x: gr.update(choices=utils.get_available_characters(), value=x), gradio('save_character_filename'), gradio('character_menu')) + + shared.gradio['delete_character_confirm'].click( + chat.delete_character, gradio('character_menu'), None).then( + lambda: gr.update(visible=False), None, gradio('character_deleter')).then( + lambda: gr.update(choices=(characters := utils.get_available_characters()), value=characters[0]), None, gradio('character_menu')) + + shared.gradio['save_character_cancel'].click(lambda: gr.update(visible=False), None, gradio('character_saver')) + shared.gradio['delete_character_cancel'].click(lambda: gr.update(visible=False), None, gradio('character_deleter')) + + shared.gradio['save_preset'].click( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + presets.generate_preset_yaml, gradio('interface_state'), gradio('save_preset_contents')).then( + lambda: 'My Preset', None, gradio('save_preset_filename')).then( + lambda: gr.update(visible=True), None, gradio('preset_saver')) + + shared.gradio['save_preset_confirm'].click( + lambda x, y: utils.save_file(f'presets/{x}.yaml', y), gradio('save_preset_filename', 'save_preset_contents'), None).then( + lambda: gr.update(visible=False), None, gradio('preset_saver')).then( + lambda x: gr.update(choices=utils.get_available_presets(), value=x), gradio('save_preset_filename'), gradio('preset_menu')) + + shared.gradio['save_preset_cancel'].click(lambda: gr.update(visible=False), None, gradio('preset_saver')) + + shared.gradio['delete_preset'].click( + lambda x: f'{x}.yaml', gradio('preset_menu'), gradio('delete_filename')).then( + lambda: 'presets/', None, gradio('delete_root')).then( + lambda: gr.update(visible=True), None, gradio('file_deleter')) + + shared.gradio['save_grammar'].click( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + lambda x: x, gradio('grammar_string'), gradio('save_contents')).then( + lambda: 'grammars/', None, gradio('save_root')).then( + lambda: 'My Fancy Grammar.gbnf', None, gradio('save_filename')).then( + lambda: gr.update(visible=True), None, gradio('file_saver')) + + shared.gradio['delete_grammar'].click( + lambda x: x, gradio('grammar_file'), gradio('delete_filename')).then( + lambda: 'grammars/', None, gradio('delete_root')).then( + lambda: gr.update(visible=True), None, gradio('file_deleter')) diff --git a/python/llm/example/Text-Generation-WebUI/modules/ui_model_menu.py b/python/llm/example/Text-Generation-WebUI/modules/ui_model_menu.py new file mode 100644 index 00000000..25aab074 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/ui_model_menu.py @@ -0,0 +1,306 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/ui_model_menu.py + + +import importlib +import math +import re +import traceback +from functools import partial +from pathlib import Path + +import gradio as gr +import psutil +import torch +from transformers import is_torch_xpu_available + +from modules import loaders, shared, ui, utils +from modules.logging_colors import logger +from modules.models import load_model, unload_model +from modules.models_settings import ( + apply_model_settings_to_state, + get_model_metadata, + save_model_settings, + update_model_parameters +) +from modules.utils import gradio + + +def create_ui(): + mu = shared.args.multi_user + + # Finding the default values for the GPU and CPU memories + total_mem = [] + if is_torch_xpu_available(): + for i in range(torch.xpu.device_count()): + total_mem.append(math.floor(torch.xpu.get_device_properties(i).total_memory / (1024 * 1024))) + else: + for i in range(torch.cuda.device_count()): + total_mem.append(math.floor(torch.cuda.get_device_properties(i).total_memory / (1024 * 1024))) + + default_gpu_mem = [] + if shared.args.gpu_memory is not None and len(shared.args.gpu_memory) > 0: + for i in shared.args.gpu_memory: + if 'mib' in i.lower(): + default_gpu_mem.append(int(re.sub('[a-zA-Z ]', '', i))) + else: + default_gpu_mem.append(int(re.sub('[a-zA-Z ]', '', i)) * 1000) + + while len(default_gpu_mem) < len(total_mem): + default_gpu_mem.append(0) + + total_cpu_mem = math.floor(psutil.virtual_memory().total / (1024 * 1024)) + if shared.args.cpu_memory is not None: + default_cpu_mem = re.sub('[a-zA-Z ]', '', shared.args.cpu_memory) + else: + default_cpu_mem = 0 + + with gr.Tab("Model", elem_id="model-tab"): + with gr.Row(): + with gr.Column(): + with gr.Row(): + with gr.Column(): + with gr.Row(): + shared.gradio['model_menu'] = gr.Dropdown(choices=utils.get_available_models(), value=lambda: shared.model_name, label='Model', elem_classes='slim-dropdown', interactive=not mu) + ui.create_refresh_button(shared.gradio['model_menu'], lambda: None, lambda: {'choices': utils.get_available_models()}, 'refresh-button', interactive=not mu) + shared.gradio['load_model'] = gr.Button("Load", visible=not shared.settings['autoload_model'], elem_classes='refresh-button', interactive=not mu) + shared.gradio['unload_model'] = gr.Button("Unload", elem_classes='refresh-button', interactive=not mu) + shared.gradio['reload_model'] = gr.Button("Reload", elem_classes='refresh-button', interactive=not mu) + shared.gradio['save_model_settings'] = gr.Button("Save settings", elem_classes='refresh-button', interactive=not mu) + + with gr.Column(): + with gr.Row(): + shared.gradio['lora_menu'] = gr.Dropdown(multiselect=True, choices=utils.get_available_loras(), value=shared.lora_names, label='LoRA(s)', elem_classes='slim-dropdown', interactive=not mu) + ui.create_refresh_button(shared.gradio['lora_menu'], lambda: None, lambda: {'choices': utils.get_available_loras(), 'value': shared.lora_names}, 'refresh-button', interactive=not mu) + shared.gradio['lora_menu_apply'] = gr.Button(value='Apply LoRAs', elem_classes='refresh-button', interactive=not mu) + + with gr.Row(): + with gr.Column(): + shared.gradio['loader'] = gr.Dropdown(label="Model loader", choices=loaders.loaders_and_params.keys(), value=None) + with gr.Box(): + with gr.Row(): + with gr.Column(): + for i in range(len(total_mem)): + shared.gradio[f'gpu_memory_{i}'] = gr.Slider(label=f"gpu-memory in MiB for device :{i}", maximum=total_mem[i], value=default_gpu_mem[i]) + + shared.gradio['cpu_memory'] = gr.Slider(label="cpu-memory in MiB", maximum=total_cpu_mem, value=default_cpu_mem) + shared.gradio['transformers_info'] = gr.Markdown('load-in-4bit params:') + shared.gradio['compute_dtype'] = gr.Dropdown(label="compute_dtype", choices=["bfloat16", "float16", "float32"], value=shared.args.compute_dtype) + shared.gradio['quant_type'] = gr.Dropdown(label="quant_type", choices=["nf4", "fp4"], value=shared.args.quant_type) + shared.gradio['hqq_backend'] = gr.Dropdown(label="hqq_backend", choices=["PYTORCH", "PYTORCH_COMPILE", "ATEN"], value=shared.args.hqq_backend) + + shared.gradio['n_gpu_layers'] = gr.Slider(label="n-gpu-layers", minimum=0, maximum=128, value=shared.args.n_gpu_layers) + shared.gradio['n_ctx'] = gr.Slider(minimum=0, maximum=shared.settings['truncation_length_max'], step=256, label="n_ctx", value=shared.args.n_ctx, info='Context length. Try lowering this if you run out of memory while loading the model.') + shared.gradio['threads'] = gr.Slider(label="threads", minimum=0, step=1, maximum=32, value=shared.args.threads) + shared.gradio['threads_batch'] = gr.Slider(label="threads_batch", minimum=0, step=1, maximum=32, value=shared.args.threads_batch) + shared.gradio['n_batch'] = gr.Slider(label="n_batch", minimum=1, maximum=2048, value=shared.args.n_batch) + + shared.gradio['wbits'] = gr.Dropdown(label="wbits", choices=["None", 1, 2, 3, 4, 8], value=shared.args.wbits if shared.args.wbits > 0 else "None") + shared.gradio['groupsize'] = gr.Dropdown(label="groupsize", choices=["None", 32, 64, 128, 1024], value=shared.args.groupsize if shared.args.groupsize > 0 else "None") + shared.gradio['model_type'] = gr.Dropdown(label="model_type", choices=["None"], value=shared.args.model_type or "None") + shared.gradio['pre_layer'] = gr.Slider(label="pre_layer", minimum=0, maximum=100, value=shared.args.pre_layer[0] if shared.args.pre_layer is not None else 0) + shared.gradio['autogptq_info'] = gr.Markdown('ExLlamav2_HF is recommended over AutoGPTQ for models derived from Llama.') + shared.gradio['gpu_split'] = gr.Textbox(label='gpu-split', info='Comma-separated list of VRAM (in GB) to use per GPU. Example: 20,7,7') + shared.gradio['max_seq_len'] = gr.Slider(label='max_seq_len', minimum=0, maximum=shared.settings['truncation_length_max'], step=256, info='Context length. Try lowering this if you run out of memory while loading the model.', value=shared.args.max_seq_len) + shared.gradio['alpha_value'] = gr.Slider(label='alpha_value', minimum=1, maximum=8, step=0.05, info='Positional embeddings alpha factor for NTK RoPE scaling. Recommended values (NTKv1): 1.75 for 1.5x context, 2.5 for 2x context. Use either this or compress_pos_emb, not both.', value=shared.args.alpha_value) + shared.gradio['rope_freq_base'] = gr.Slider(label='rope_freq_base', minimum=0, maximum=1000000, step=1000, info='If greater than 0, will be used instead of alpha_value. Those two are related by rope_freq_base = 10000 * alpha_value ^ (64 / 63)', value=shared.args.rope_freq_base) + shared.gradio['compress_pos_emb'] = gr.Slider(label='compress_pos_emb', minimum=1, maximum=8, step=1, info='Positional embeddings compression factor. Should be set to (context length) / (model\'s original context length). Equal to 1/rope_freq_scale.', value=shared.args.compress_pos_emb) + shared.gradio['quipsharp_info'] = gr.Markdown('QuIP# has to be installed manually at the moment.') + shared.gradio['device'] = gr.Dropdown(label="device", choices=["CPU", "GPU"], value=shared.args.device) + shared.gradio['load_in_4bit'] = gr.Checkbox(label="load-in-4bit", + value=shared.args.load_in_4bit, + info="Load linear's weights to symmetric int 4.\n\nTo enable this option, start the web UI with the --load-in-4bit flag.", + interactive=shared.args.load_in_4bit) + shared.gradio['load_in_low_bit'] = gr.Dropdown(label="load-in-low-bit", + choices=["sym_int4", "asym_int4", "sym_int5", "asym_int5", "sym_int8", + "nf3", "nf4", "fp4", "fp8", "fp8_e4m3", "fp8_e5m2", "fp16", "bf16"], + value=shared.args.load_in_low_bit, + info='Apply relevant low bit optimizations to the model.\n\nTo enable this option, start the web UI with the --load-in-low-bit flag.', + interactive=shared.args.load_in_4bit is False) + + with gr.Column(): + shared.gradio['tensorcores'] = gr.Checkbox(label="tensorcores", value=shared.args.tensorcores, info='Use llama-cpp-python compiled with tensor cores support. This increases performance on RTX cards. NVIDIA only.') + shared.gradio['no_offload_kqv'] = gr.Checkbox(label="no_offload_kqv", value=shared.args.no_offload_kqv, info='Do not offload the K, Q, V to the GPU. This saves VRAM but reduces the performance.') + shared.gradio['triton'] = gr.Checkbox(label="triton", value=shared.args.triton) + shared.gradio['no_inject_fused_attention'] = gr.Checkbox(label="no_inject_fused_attention", value=shared.args.no_inject_fused_attention, info='Disable fused attention. Fused attention improves inference performance but uses more VRAM. Fuses layers for AutoAWQ. Disable if running low on VRAM.') + shared.gradio['no_inject_fused_mlp'] = gr.Checkbox(label="no_inject_fused_mlp", value=shared.args.no_inject_fused_mlp, info='Affects Triton only. Disable fused MLP. Fused MLP improves performance but uses more VRAM. Disable if running low on VRAM.') + shared.gradio['no_use_cuda_fp16'] = gr.Checkbox(label="no_use_cuda_fp16", value=shared.args.no_use_cuda_fp16, info='This can make models faster on some systems.') + shared.gradio['desc_act'] = gr.Checkbox(label="desc_act", value=shared.args.desc_act, info='\'desc_act\', \'wbits\', and \'groupsize\' are used for old models without a quantize_config.json.') + shared.gradio['no_mul_mat_q'] = gr.Checkbox(label="no_mul_mat_q", value=shared.args.no_mul_mat_q, info='Disable the mulmat kernels.') + shared.gradio['no_mmap'] = gr.Checkbox(label="no-mmap", value=shared.args.no_mmap) + shared.gradio['mlock'] = gr.Checkbox(label="mlock", value=shared.args.mlock) + shared.gradio['numa'] = gr.Checkbox(label="numa", value=shared.args.numa, info='NUMA support can help on some systems with non-uniform memory access.') + shared.gradio['cpu'] = gr.Checkbox(label="cpu", value=shared.args.cpu) + shared.gradio['load_in_8bit'] = gr.Checkbox(label="load-in-8bit", value=shared.args.load_in_8bit) + shared.gradio['bf16'] = gr.Checkbox(label="bf16", value=shared.args.bf16) + shared.gradio['auto_devices'] = gr.Checkbox(label="auto-devices", value=shared.args.auto_devices) + shared.gradio['disk'] = gr.Checkbox(label="disk", value=shared.args.disk) + shared.gradio['optimize_model'] = gr.Checkbox(label="optimize-model", value=shared.args.optimize_model, info="Enable this option to further optimize the low-bit llm model.") + shared.gradio['cpu_embedding'] = gr.Checkbox(label="cpu-embedding", value=shared.args.cpu_embedding, info="Whether to replace the Embedding layer.") + shared.gradio['use_double_quant'] = gr.Checkbox(label="use_double_quant", value=shared.args.use_double_quant) + shared.gradio['tensor_split'] = gr.Textbox(label='tensor_split', info='Split the model across multiple GPUs, comma-separated list of proportions, e.g. 18,17') + shared.gradio['modules_to_not_convert'] = gr.Textbox(label="modules-to-not-convert", value=shared.args.modules_to_not_convert, info="modules (nn.Module) that are skipped when.") + shared.gradio['lightweight_bmm'] = gr.Checkbox(label="lightweight-bmm", value=shared.args.lightweight_bmm, info="Whether to replace the torch.bmm ops.") + shared.gradio['trust_remote_code'] = gr.Checkbox(label="trust-remote-code", value=shared.args.trust_remote_code, info='To enable this option, start the web UI with the --trust-remote-code flag. It is necessary for some models.') + shared.gradio['use_cache'] = gr.Checkbox(label="use-cache", value=shared.args.use_cache, info="Wether to use past_key_values to speed up model decoding.") + shared.gradio['cfg_cache'] = gr.Checkbox(label="cfg-cache", value=shared.args.cfg_cache, info='Create an additional cache for CFG negative prompts.') + shared.gradio['logits_all'] = gr.Checkbox(label="logits_all", value=shared.args.logits_all, info='Needs to be set for perplexity evaluation to work. Otherwise, ignore it, as it makes prompt processing slower.') + shared.gradio['use_flash_attention_2'] = gr.Checkbox(label="use_flash_attention_2", value=shared.args.use_flash_attention_2, info='Set use_flash_attention_2=True while loading the model.') + shared.gradio['disable_exllama'] = gr.Checkbox(label="disable_exllama", value=shared.args.disable_exllama, info='Disable ExLlama kernel.') + shared.gradio['disable_exllamav2'] = gr.Checkbox(label="disable_exllamav2", value=shared.args.disable_exllamav2, info='Disable ExLlamav2 kernel.') + shared.gradio['no_flash_attn'] = gr.Checkbox(label="no_flash_attn", value=shared.args.no_flash_attn, info='Force flash-attention to not be used.') + shared.gradio['cache_8bit'] = gr.Checkbox(label="cache_8bit", value=shared.args.cache_8bit, info='Use 8-bit cache to save VRAM.') + shared.gradio['no_use_fast'] = gr.Checkbox(label="no_use_fast", value=shared.args.no_use_fast, info='Set use_fast=False while loading the tokenizer.') + shared.gradio['num_experts_per_token'] = gr.Number(label="Number of experts per token", value=shared.args.num_experts_per_token, info='Only applies to MoE models like Mixtral.') + shared.gradio['gptq_for_llama_info'] = gr.Markdown('Legacy loader for compatibility with older GPUs. ExLlamav2_HF or AutoGPTQ are preferred for GPTQ models when supported.') + shared.gradio['exllamav2_info'] = gr.Markdown("ExLlamav2_HF is recommended over ExLlamav2 for better integration with extensions and more consistent sampling behavior across loaders.") + shared.gradio['llamacpp_HF_info'] = gr.Markdown('llamacpp_HF loads llama.cpp as a Transformers model. To use it, you need to download a tokenizer.\n\nOption 1 (recommended): place your .gguf in a subfolder of models/ along with these 4 files: special_tokens_map.json, tokenizer_config.json, tokenizer.json, tokenizer.model.\n\nOption 2: download `oobabooga/llama-tokenizer` under "Download model or LoRA". That\'s a default Llama tokenizer that will work for some (but not all) models.') + + with gr.Column(): + with gr.Row(): + shared.gradio['autoload_model'] = gr.Checkbox(value=shared.settings['autoload_model'], label='Autoload the model', info='Whether to load the model as soon as it is selected in the Model dropdown.', interactive=not mu) + + shared.gradio['custom_model_menu'] = gr.Textbox(label="Download model or LoRA", info="Enter the Hugging Face username/model path, for instance: facebook/galactica-125m. To specify a branch, add it at the end after a \":\" character like this: facebook/galactica-125m:main. To download a single file, enter its name in the second box.", interactive=not mu) + shared.gradio['download_specific_file'] = gr.Textbox(placeholder="File name (for GGUF models)", show_label=False, max_lines=1, interactive=not mu) + with gr.Row(): + shared.gradio['download_model_button'] = gr.Button("Download", variant='primary', interactive=not mu) + shared.gradio['get_file_list'] = gr.Button("Get file list", interactive=not mu) + + with gr.Row(): + shared.gradio['model_status'] = gr.Markdown('No model is loaded' if shared.model_name == 'None' else 'Ready') + + +def create_event_handlers(): + shared.gradio['loader'].change( + loaders.make_loader_params_visible, gradio('loader'), gradio(loaders.get_all_params())).then( + lambda value: gr.update(choices=loaders.get_model_types(value)), gradio('loader'), gradio('model_type')) + + # In this event handler, the interface state is read and updated + # with the model defaults (if any), and then the model is loaded + # unless "autoload_model" is unchecked + shared.gradio['model_menu'].change( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + apply_model_settings_to_state, gradio('model_menu', 'interface_state'), gradio('interface_state')).then( + ui.apply_interface_values, gradio('interface_state'), gradio(ui.list_interface_input_elements()), show_progress=False).then( + update_model_parameters, gradio('interface_state'), None).then( + load_model_wrapper, gradio('model_menu', 'loader', 'autoload_model'), gradio('model_status'), show_progress=False).success( + update_truncation_length, gradio('truncation_length', 'interface_state'), gradio('truncation_length')).then( + lambda x: x, gradio('loader'), gradio('filter_by_loader')) + + shared.gradio['load_model'].click( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + update_model_parameters, gradio('interface_state'), None).then( + partial(load_model_wrapper, autoload=True), gradio('model_menu', 'loader'), gradio('model_status'), show_progress=False).success( + update_truncation_length, gradio('truncation_length', 'interface_state'), gradio('truncation_length')).then( + lambda x: x, gradio('loader'), gradio('filter_by_loader')) + + shared.gradio['reload_model'].click( + unload_model, None, None).then( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + update_model_parameters, gradio('interface_state'), None).then( + partial(load_model_wrapper, autoload=True), gradio('model_menu', 'loader'), gradio('model_status'), show_progress=False).success( + update_truncation_length, gradio('truncation_length', 'interface_state'), gradio('truncation_length')).then( + lambda x: x, gradio('loader'), gradio('filter_by_loader')) + + shared.gradio['unload_model'].click( + unload_model, None, None).then( + lambda: "Model unloaded", None, gradio('model_status')) + + shared.gradio['save_model_settings'].click( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + save_model_settings, gradio('model_menu', 'interface_state'), gradio('model_status'), show_progress=False) + + shared.gradio['lora_menu_apply'].click(load_lora_wrapper, gradio('lora_menu'), gradio('model_status'), show_progress=False) + shared.gradio['download_model_button'].click(download_model_wrapper, gradio('custom_model_menu', 'download_specific_file'), gradio('model_status'), show_progress=True) + shared.gradio['get_file_list'].click(partial(download_model_wrapper, return_links=True), gradio('custom_model_menu', 'download_specific_file'), gradio('model_status'), show_progress=True) + shared.gradio['autoload_model'].change(lambda x: gr.update(visible=not x), gradio('autoload_model'), gradio('load_model')) + + +def load_model_wrapper(selected_model, loader, autoload=False): + if not autoload: + yield f"The settings for `{selected_model}` have been updated.\n\nClick on \"Load\" to load it." + return + + if selected_model == 'None': + yield "No model selected" + else: + try: + yield f"Loading `{selected_model}`..." + unload_model() + if selected_model != '': + shared.model, shared.tokenizer = load_model(selected_model, loader) + + if shared.model is not None: + output = f"Successfully loaded `{selected_model}`." + + settings = get_model_metadata(selected_model) + if 'instruction_template' in settings: + output += '\n\nIt seems to be an instruction-following model with template "{}". In the chat tab, instruct or chat-instruct modes should be used.'.format(settings['instruction_template']) + + yield output + else: + yield f"Failed to load `{selected_model}`." + except: + exc = traceback.format_exc() + logger.error('Failed to load the model.') + print(exc) + yield exc.replace('\n', '\n\n') + + +def load_lora_wrapper(selected_loras): + yield ("Applying the following LoRAs to {}:\n\n{}".format(shared.model_name, '\n'.join(selected_loras))) + #add_lora_to_model(selected_loras) + yield ("Successfuly applied the LoRAs") + + +def download_model_wrapper(repo_id, specific_file, progress=gr.Progress(), return_links=False, check=False): + try: + progress(0.0) + downloader = importlib.import_module("download-model").ModelDownloader() + model, branch = downloader.sanitize_model_and_branch_names(repo_id, None) + yield ("Getting the download links from Hugging Face") + links, sha256, is_lora, is_llamacpp = downloader.get_download_links_from_huggingface(model, branch, text_only=False, specific_file=specific_file) + if return_links: + yield '\n\n'.join([f"`{Path(link).name}`" for link in links]) + return + + yield ("Getting the output folder") + base_folder = shared.args.lora_dir if is_lora else shared.args.model_dir + output_folder = downloader.get_output_folder(model, branch, is_lora, is_llamacpp=is_llamacpp, base_folder=base_folder) + if check: + progress(0.5) + yield ("Checking previously downloaded files") + downloader.check_model_files(model, branch, links, sha256, output_folder) + progress(1.0) + else: + yield (f"Downloading file{'s' if len(links) > 1 else ''} to `{output_folder}/`") + downloader.download_model_files(model, branch, links, sha256, output_folder, progress_bar=progress, threads=4, is_llamacpp=is_llamacpp) + yield ("Done!") + except: + progress(1.0) + yield traceback.format_exc().replace('\n', '\n\n') + + +def update_truncation_length(current_length, state): + if 'loader' in state: + if state['loader'].lower().startswith('exllama'): + return state['max_seq_len'] + elif state['loader'] in ['llama.cpp', 'llamacpp_HF', 'ctransformers']: + return state['n_ctx'] + + return current_length diff --git a/python/llm/example/Text-Generation-WebUI/modules/ui_notebook.py b/python/llm/example/Text-Generation-WebUI/modules/ui_notebook.py new file mode 100644 index 00000000..1b0344be --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/ui_notebook.py @@ -0,0 +1,125 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/ui_notebook.py + + +import gradio as gr + +from modules import logits, shared, ui, utils +from modules.prompts import count_tokens, load_prompt +from modules.text_generation import ( + generate_reply_wrapper, + get_token_ids, + stop_everything_event +) +from modules.utils import gradio + +inputs = ('textbox-notebook', 'interface_state') +outputs = ('textbox-notebook', 'html-notebook') + + +def create_ui(): + mu = shared.args.multi_user + with gr.Tab('Notebook', elem_id='notebook-tab'): + shared.gradio['last_input-notebook'] = gr.State('') + with gr.Row(): + with gr.Column(scale=4): + with gr.Tab('Raw'): + with gr.Row(): + shared.gradio['textbox-notebook'] = gr.Textbox(value='', lines=27, elem_id='textbox-notebook', elem_classes=['textbox', 'add_scrollbar']) + shared.gradio['token-counter-notebook'] = gr.HTML(value="0", elem_classes=["token-counter"]) + + with gr.Tab('Markdown'): + shared.gradio['markdown_render-notebook'] = gr.Button('Render') + shared.gradio['markdown-notebook'] = gr.Markdown() + + with gr.Tab('HTML'): + shared.gradio['html-notebook'] = gr.HTML() + + with gr.Tab('Logits'): + with gr.Row(): + with gr.Column(scale=10): + shared.gradio['get_logits-notebook'] = gr.Button('Get next token probabilities') + with gr.Column(scale=1): + shared.gradio['use_samplers-notebook'] = gr.Checkbox(label='Use samplers', value=True, elem_classes=['no-background']) + + with gr.Row(): + shared.gradio['logits-notebook'] = gr.Textbox(lines=23, label='Output', elem_classes=['textbox_logits_notebook', 'add_scrollbar']) + shared.gradio['logits-notebook-previous'] = gr.Textbox(lines=23, label='Previous output', elem_classes=['textbox_logits_notebook', 'add_scrollbar']) + + with gr.Tab('Tokens'): + shared.gradio['get_tokens-notebook'] = gr.Button('Get token IDs for the input') + shared.gradio['tokens-notebook'] = gr.Textbox(lines=23, label='Tokens', elem_classes=['textbox_logits_notebook', 'add_scrollbar', 'monospace']) + + with gr.Row(): + shared.gradio['Generate-notebook'] = gr.Button('Generate', variant='primary', elem_classes='small-button') + shared.gradio['Stop-notebook'] = gr.Button('Stop', elem_classes='small-button', elem_id='stop') + shared.gradio['Undo'] = gr.Button('Undo', elem_classes='small-button') + shared.gradio['Regenerate-notebook'] = gr.Button('Regenerate', elem_classes='small-button') + + with gr.Column(scale=1): + gr.HTML('
      ') + with gr.Row(): + shared.gradio['prompt_menu-notebook'] = gr.Dropdown(choices=utils.get_available_prompts(), value='None', label='Prompt', elem_classes='slim-dropdown') + ui.create_refresh_button(shared.gradio['prompt_menu-notebook'], lambda: None, lambda: {'choices': utils.get_available_prompts()}, ['refresh-button', 'refresh-button-small'], interactive=not mu) + shared.gradio['save_prompt-notebook'] = gr.Button('💾', elem_classes=['refresh-button', 'refresh-button-small'], interactive=not mu) + shared.gradio['delete_prompt-notebook'] = gr.Button('🗑️', elem_classes=['refresh-button', 'refresh-button-small'], interactive=not mu) + + +def create_event_handlers(): + shared.gradio['Generate-notebook'].click( + lambda x: x, gradio('textbox-notebook'), gradio('last_input-notebook')).then( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + generate_reply_wrapper, gradio(inputs), gradio(outputs), show_progress=False).then( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + lambda: None, None, None, _js=f'() => {{{ui.audio_notification_js}}}') + + shared.gradio['textbox-notebook'].submit( + lambda x: x, gradio('textbox-notebook'), gradio('last_input-notebook')).then( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + generate_reply_wrapper, gradio(inputs), gradio(outputs), show_progress=False).then( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + lambda: None, None, None, _js=f'() => {{{ui.audio_notification_js}}}') + + shared.gradio['Undo'].click(lambda x: x, gradio('last_input-notebook'), gradio('textbox-notebook'), show_progress=False) + shared.gradio['markdown_render-notebook'].click(lambda x: x, gradio('textbox-notebook'), gradio('markdown-notebook'), queue=False) + shared.gradio['Regenerate-notebook'].click( + lambda x: x, gradio('last_input-notebook'), gradio('textbox-notebook'), show_progress=False).then( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + generate_reply_wrapper, gradio(inputs), gradio(outputs), show_progress=False).then( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + lambda: None, None, None, _js=f'() => {{{ui.audio_notification_js}}}') + + shared.gradio['Stop-notebook'].click(stop_everything_event, None, None, queue=False) + shared.gradio['prompt_menu-notebook'].change(load_prompt, gradio('prompt_menu-notebook'), gradio('textbox-notebook'), show_progress=False) + shared.gradio['save_prompt-notebook'].click( + lambda x: x, gradio('textbox-notebook'), gradio('save_contents')).then( + lambda: 'prompts/', None, gradio('save_root')).then( + lambda: utils.current_time() + '.txt', None, gradio('save_filename')).then( + lambda: gr.update(visible=True), None, gradio('file_saver')) + + shared.gradio['delete_prompt-notebook'].click( + lambda: 'prompts/', None, gradio('delete_root')).then( + lambda x: x + '.txt', gradio('prompt_menu-notebook'), gradio('delete_filename')).then( + lambda: gr.update(visible=True), None, gradio('file_deleter')) + + shared.gradio['textbox-notebook'].input(lambda x: f"{count_tokens(x)}", gradio('textbox-notebook'), gradio('token-counter-notebook'), show_progress=False) + shared.gradio['get_logits-notebook'].click( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + logits.get_next_logits, gradio('textbox-notebook', 'interface_state', 'use_samplers-notebook', 'logits-notebook'), gradio('logits-notebook', 'logits-notebook-previous'), show_progress=False) + + shared.gradio['get_tokens-notebook'].click(get_token_ids, gradio('textbox-notebook'), gradio('tokens-notebook'), show_progress=False) diff --git a/python/llm/example/Text-Generation-WebUI/modules/ui_parameters.py b/python/llm/example/Text-Generation-WebUI/modules/ui_parameters.py new file mode 100644 index 00000000..54bb2e0c --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/ui_parameters.py @@ -0,0 +1,135 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/ui_parameters.py + + +from pathlib import Path + +import gradio as gr + +from modules import loaders, presets, shared, ui, ui_chat, utils +from modules.utils import gradio + + +def create_ui(default_preset): + mu = shared.args.multi_user + generate_params = presets.load_preset(default_preset) + with gr.Tab("Parameters", elem_id="parameters"): + with gr.Tab("Generation"): + with gr.Row(): + with gr.Column(): + with gr.Row(): + shared.gradio['preset_menu'] = gr.Dropdown(choices=utils.get_available_presets(), value=default_preset, label='Preset', elem_classes='slim-dropdown') + ui.create_refresh_button(shared.gradio['preset_menu'], lambda: None, lambda: {'choices': utils.get_available_presets()}, 'refresh-button', interactive=not mu) + shared.gradio['save_preset'] = gr.Button('💾', elem_classes='refresh-button', interactive=not mu) + shared.gradio['delete_preset'] = gr.Button('🗑️', elem_classes='refresh-button', interactive=not mu) + shared.gradio['random_preset'] = gr.Button('🎲', elem_classes='refresh-button') + + with gr.Column(): + shared.gradio['filter_by_loader'] = gr.Dropdown(label="Filter by loader", choices=["All"] + list(loaders.loaders_and_params.keys()), value="All", elem_classes='slim-dropdown') + + with gr.Row(): + with gr.Column(): + with gr.Row(): + with gr.Column(): + shared.gradio['max_new_tokens'] = gr.Slider(minimum=shared.settings['max_new_tokens_min'], maximum=shared.settings['max_new_tokens_max'], step=1, label='max_new_tokens', value=shared.settings['max_new_tokens']) + shared.gradio['temperature'] = gr.Slider(0.01, 5, value=generate_params['temperature'], step=0.01, label='temperature') + shared.gradio['top_p'] = gr.Slider(0.0, 1.0, value=generate_params['top_p'], step=0.01, label='top_p') + shared.gradio['min_p'] = gr.Slider(0.0, 1.0, value=generate_params['min_p'], step=0.01, label='min_p') + shared.gradio['top_k'] = gr.Slider(0, 200, value=generate_params['top_k'], step=1, label='top_k') + shared.gradio['repetition_penalty'] = gr.Slider(1.0, 1.5, value=generate_params['repetition_penalty'], step=0.01, label='repetition_penalty') + shared.gradio['presence_penalty'] = gr.Slider(0, 2, value=generate_params['presence_penalty'], step=0.05, label='presence_penalty') + shared.gradio['frequency_penalty'] = gr.Slider(0, 2, value=generate_params['frequency_penalty'], step=0.05, label='frequency_penalty') + shared.gradio['repetition_penalty_range'] = gr.Slider(0, 4096, step=64, value=generate_params['repetition_penalty_range'], label='repetition_penalty_range') + shared.gradio['typical_p'] = gr.Slider(0.0, 1.0, value=generate_params['typical_p'], step=0.01, label='typical_p') + shared.gradio['tfs'] = gr.Slider(0.0, 1.0, value=generate_params['tfs'], step=0.01, label='tfs') + shared.gradio['top_a'] = gr.Slider(0.0, 1.0, value=generate_params['top_a'], step=0.01, label='top_a') + shared.gradio['epsilon_cutoff'] = gr.Slider(0, 9, value=generate_params['epsilon_cutoff'], step=0.01, label='epsilon_cutoff') + shared.gradio['eta_cutoff'] = gr.Slider(0, 20, value=generate_params['eta_cutoff'], step=0.01, label='eta_cutoff') + + with gr.Column(): + shared.gradio['guidance_scale'] = gr.Slider(-0.5, 2.5, step=0.05, value=generate_params['guidance_scale'], label='guidance_scale', info='For CFG. 1.5 is a good value.') + shared.gradio['negative_prompt'] = gr.Textbox(value=shared.settings['negative_prompt'], label='Negative prompt', lines=3, elem_classes=['add_scrollbar']) + shared.gradio['penalty_alpha'] = gr.Slider(0, 5, value=generate_params['penalty_alpha'], label='penalty_alpha', info='For Contrastive Search. do_sample must be unchecked.') + shared.gradio['mirostat_mode'] = gr.Slider(0, 2, step=1, value=generate_params['mirostat_mode'], label='mirostat_mode', info='mode=1 is for llama.cpp only.') + shared.gradio['mirostat_tau'] = gr.Slider(0, 10, step=0.01, value=generate_params['mirostat_tau'], label='mirostat_tau') + shared.gradio['mirostat_eta'] = gr.Slider(0, 1, step=0.01, value=generate_params['mirostat_eta'], label='mirostat_eta') + shared.gradio['dynamic_temperature_low'] = gr.Slider(0.01, 5, value=generate_params['dynamic_temperature_low'], step=0.01, label='dynamic_temperature_low', info='Only used when dynamic_temperature is checked.') + shared.gradio['dynamic_temperature'] = gr.Checkbox(value=generate_params['dynamic_temperature'], label='dynamic_temperature') + shared.gradio['temperature_last'] = gr.Checkbox(value=generate_params['temperature_last'], label='temperature_last', info='Makes temperature the last sampler instead of the first.') + shared.gradio['do_sample'] = gr.Checkbox(value=generate_params['do_sample'], label='do_sample') + shared.gradio['seed'] = gr.Number(value=shared.settings['seed'], label='Seed (-1 for random)') + with gr.Accordion('Other parameters', open=False): + shared.gradio['encoder_repetition_penalty'] = gr.Slider(0.8, 1.5, value=generate_params['encoder_repetition_penalty'], step=0.01, label='encoder_repetition_penalty') + shared.gradio['no_repeat_ngram_size'] = gr.Slider(0, 20, step=1, value=generate_params['no_repeat_ngram_size'], label='no_repeat_ngram_size') + shared.gradio['min_length'] = gr.Slider(0, 2000, step=1, value=generate_params['min_length'], label='min_length') + shared.gradio['num_beams'] = gr.Slider(1, 20, step=1, value=generate_params['num_beams'], label='num_beams', info='For Beam Search, along with length_penalty and early_stopping.') + shared.gradio['length_penalty'] = gr.Slider(-5, 5, value=generate_params['length_penalty'], label='length_penalty') + shared.gradio['early_stopping'] = gr.Checkbox(value=generate_params['early_stopping'], label='early_stopping') + + gr.Markdown("[Learn more](https://github.com/oobabooga/text-generation-webui/wiki/03-%E2%80%90-Parameters-Tab)") + + with gr.Column(): + with gr.Row(): + with gr.Column(): + shared.gradio['truncation_length'] = gr.Slider(value=get_truncation_length(), minimum=shared.settings['truncation_length_min'], maximum=shared.settings['truncation_length_max'], step=256, label='Truncate the prompt up to this length', info='The leftmost tokens are removed if the prompt exceeds this length. Most models require this to be at most 2048.') + shared.gradio['max_tokens_second'] = gr.Slider(value=shared.settings['max_tokens_second'], minimum=0, maximum=20, step=1, label='Maximum tokens/second', info='To make text readable in real time.') + shared.gradio['max_updates_second'] = gr.Slider(value=shared.settings['max_updates_second'], minimum=0, maximum=24, step=1, label='Maximum UI updates/second', info='Set this if you experience lag in the UI during streaming.') + + shared.gradio['custom_stopping_strings'] = gr.Textbox(lines=1, value=shared.settings["custom_stopping_strings"] or None, label='Custom stopping strings', info='In addition to the defaults. Written between "" and separated by commas.', placeholder='"\\n", "\\nYou:"') + shared.gradio['custom_token_bans'] = gr.Textbox(value=shared.settings['custom_token_bans'] or None, label='Custom token bans', info='Specific token IDs to ban from generating, comma-separated. The IDs can be found in the Default or Notebook tab.') + + with gr.Column(): + shared.gradio['auto_max_new_tokens'] = gr.Checkbox(value=shared.settings['auto_max_new_tokens'], label='auto_max_new_tokens', info='Expand max_new_tokens to the available context length.') + shared.gradio['ban_eos_token'] = gr.Checkbox(value=shared.settings['ban_eos_token'], label='Ban the eos_token', info='Forces the model to never end the generation prematurely.') + shared.gradio['add_bos_token'] = gr.Checkbox(value=shared.settings['add_bos_token'], label='Add the bos_token to the beginning of prompts', info='Disabling this can make the replies more creative.') + shared.gradio['skip_special_tokens'] = gr.Checkbox(value=shared.settings['skip_special_tokens'], label='Skip special tokens', info='Some specific models need this unset.') + shared.gradio['stream'] = gr.Checkbox(value=shared.settings['stream'], label='Activate text streaming') + + with gr.Row() as shared.gradio['grammar_file_row']: + shared.gradio['grammar_file'] = gr.Dropdown(value='None', choices=utils.get_available_grammars(), label='Load grammar from file (.gbnf)', elem_classes='slim-dropdown') + ui.create_refresh_button(shared.gradio['grammar_file'], lambda: None, lambda: {'choices': utils.get_available_grammars()}, 'refresh-button', interactive=not mu) + shared.gradio['save_grammar'] = gr.Button('💾', elem_classes='refresh-button', interactive=not mu) + shared.gradio['delete_grammar'] = gr.Button('🗑️ ', elem_classes='refresh-button', interactive=not mu) + + shared.gradio['grammar_string'] = gr.Textbox(value='', label='Grammar', lines=16, elem_classes=['add_scrollbar', 'monospace']) + + ui_chat.create_chat_settings_ui() + + +def create_event_handlers(): + shared.gradio['filter_by_loader'].change(loaders.blacklist_samplers, gradio('filter_by_loader'), gradio(loaders.list_all_samplers()), show_progress=False) + shared.gradio['preset_menu'].change(presets.load_preset_for_ui, gradio('preset_menu', 'interface_state'), gradio('interface_state') + gradio(presets.presets_params())) + shared.gradio['random_preset'].click(presets.random_preset, gradio('interface_state'), gradio('interface_state') + gradio(presets.presets_params())) + shared.gradio['grammar_file'].change(load_grammar, gradio('grammar_file'), gradio('grammar_string')) + + +def get_truncation_length(): + if 'max_seq_len' in shared.provided_arguments or shared.args.max_seq_len != shared.args_defaults.max_seq_len: + return shared.args.max_seq_len + elif 'n_ctx' in shared.provided_arguments or shared.args.n_ctx != shared.args_defaults.n_ctx: + return shared.args.n_ctx + else: + return shared.settings['truncation_length'] + + +def load_grammar(name): + p = Path(f'grammars/{name}') + if p.exists(): + return open(p, 'r').read() + else: + return '' diff --git a/python/llm/example/Text-Generation-WebUI/modules/ui_session.py b/python/llm/example/Text-Generation-WebUI/modules/ui_session.py new file mode 100644 index 00000000..22ba3a69 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/ui_session.py @@ -0,0 +1,89 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/ui_session.py + + +import gradio as gr + +from modules import shared, ui, utils +from modules.github import clone_or_pull_repository +from modules.utils import gradio + + +def create_ui(): + mu = shared.args.multi_user + with gr.Tab("Session", elem_id="session-tab"): + with gr.Row(): + with gr.Column(): + shared.gradio['reset_interface'] = gr.Button("Apply flags/extensions and restart", interactive=not mu) + with gr.Row(): + shared.gradio['toggle_dark_mode'] = gr.Button('Toggle 💡') + shared.gradio['save_settings'] = gr.Button('Save UI defaults to settings.yaml', interactive=not mu) + + with gr.Row(): + with gr.Column(): + shared.gradio['extensions_menu'] = gr.CheckboxGroup(choices=utils.get_available_extensions(), value=shared.args.extensions, label="Available extensions", info='Note that some of these extensions may require manually installing Python requirements through the command: pip install -r extensions/extension_name/requirements.txt', elem_classes='checkboxgroup-table') + + with gr.Column(): + shared.gradio['bool_menu'] = gr.CheckboxGroup(choices=get_boolean_arguments(), value=get_boolean_arguments(active=True), label="Boolean command-line flags", elem_classes='checkboxgroup-table') + + with gr.Column(): + extension_name = gr.Textbox(lines=1, label='Install or update an extension', info='Enter the GitHub URL below and press Enter. For a list of extensions, see: https://github.com/oobabooga/text-generation-webui-extensions ⚠️ WARNING ⚠️ : extensions can execute arbitrary code. Make sure to inspect their source code before activating them.', interactive=not mu) + extension_status = gr.Markdown() + + extension_name.submit(clone_or_pull_repository, extension_name, extension_status, show_progress=False) + + # Reset interface event + shared.gradio['reset_interface'].click( + set_interface_arguments, gradio('extensions_menu', 'bool_menu'), None).then( + lambda: None, None, None, _js='() => {document.body.innerHTML=\'

      Reloading...

      \'; setTimeout(function(){location.reload()},2500); return []}') + + shared.gradio['toggle_dark_mode'].click(lambda: None, None, None, _js='() => {document.getElementsByTagName("body")[0].classList.toggle("dark")}') + shared.gradio['save_settings'].click( + ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then( + ui.save_settings, gradio('interface_state', 'preset_menu', 'extensions_menu', 'show_controls'), gradio('save_contents')).then( + lambda: './', None, gradio('save_root')).then( + lambda: 'settings.yaml', None, gradio('save_filename')).then( + lambda: gr.update(visible=True), None, gradio('file_saver')) + + +def set_interface_arguments(extensions, bool_active): + shared.args.extensions = extensions + + bool_list = get_boolean_arguments() + + for k in bool_list: + setattr(shared.args, k, False) + for k in bool_active: + setattr(shared.args, k, True) + if k == 'api': + shared.add_extension('openai', last=True) + + shared.need_restart = True + + +def get_boolean_arguments(active=False): + exclude = shared.deprecated_args + + cmd_list = vars(shared.args) + bool_list = sorted([k for k in cmd_list if type(cmd_list[k]) is bool and k not in exclude + ui.list_model_elements()]) + bool_active = [k for k in bool_list if vars(shared.args)[k]] + + if active: + return bool_active + else: + return bool_list diff --git a/python/llm/example/Text-Generation-WebUI/modules/utils.py b/python/llm/example/Text-Generation-WebUI/modules/utils.py new file mode 100644 index 00000000..95897c7c --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/modules/utils.py @@ -0,0 +1,153 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/modules/utils.py + + +import os +import re +from datetime import datetime +from pathlib import Path + +from modules import github, shared +from modules.logging_colors import logger + + +# Helper function to get multiple values from shared.gradio +def gradio(*keys): + if len(keys) == 1 and type(keys[0]) in [list, tuple]: + keys = keys[0] + + return [shared.gradio[k] for k in keys] + + +def save_file(fname, contents): + if fname == '': + logger.error('File name is empty!') + return + + root_folder = Path(__file__).resolve().parent.parent + abs_path_str = os.path.abspath(fname) + rel_path_str = os.path.relpath(abs_path_str, root_folder) + rel_path = Path(rel_path_str) + if rel_path.parts[0] == '..': + logger.error(f'Invalid file path: {fname}') + return + + with open(abs_path_str, 'w', encoding='utf-8') as f: + f.write(contents) + + logger.info(f'Saved {abs_path_str}.') + + +def delete_file(fname): + if fname == '': + logger.error('File name is empty!') + return + + root_folder = Path(__file__).resolve().parent.parent + abs_path_str = os.path.abspath(fname) + rel_path_str = os.path.relpath(abs_path_str, root_folder) + rel_path = Path(rel_path_str) + if rel_path.parts[0] == '..': + logger.error(f'Invalid file path: {fname}') + return + + if rel_path.exists(): + rel_path.unlink() + logger.info(f'Deleted {fname}.') + + +def current_time(): + return f"{datetime.now().strftime('%Y-%m-%d-%H%M%S')}" + + +def atoi(text): + return int(text) if text.isdigit() else text.lower() + + +# Replace multiple string pairs in a string +def replace_all(text, dic): + for i, j in dic.items(): + text = text.replace(i, j) + + return text + + +def natural_keys(text): + return [atoi(c) for c in re.split(r'(\d+)', text)] + + +def get_available_models(): + model_list = [] + for item in list(Path(f'{shared.args.model_dir}/').glob('*')): + if not item.name.endswith(('.txt', '-np', '.pt', '.json', '.yaml', '.py')) and 'llama-tokenizer' not in item.name: + model_list.append(re.sub('.pth$', '', item.name)) + + return ['None'] + sorted(model_list, key=natural_keys) + + +def get_available_presets(): + return sorted(set((k.stem for k in Path('presets').glob('*.yaml'))), key=natural_keys) + + +def get_available_prompts(): + prompts = [] + files = set((k.stem for k in Path('prompts').glob('*.txt'))) + prompts += sorted([k for k in files if re.match('^[0-9]', k)], key=natural_keys, reverse=True) + prompts += sorted([k for k in files if re.match('^[^0-9]', k)], key=natural_keys) + prompts += ['None'] + return prompts + + +def get_available_characters(): + paths = (x for x in Path('characters').iterdir() if x.suffix in ('.json', '.yaml', '.yml')) + return sorted(set((k.stem for k in paths)), key=natural_keys) + + +def get_available_instruction_templates(): + path = "instruction-templates" + paths = [] + if os.path.exists(path): + paths = (x for x in Path(path).iterdir() if x.suffix in ('.json', '.yaml', '.yml')) + + return ['Select template to load...'] + sorted(set((k.stem for k in paths)), key=natural_keys) + + +def get_available_extensions(): + extensions = sorted(set(map(lambda x: x.parts[1], Path('extensions').glob('*/script.py'))), key=natural_keys) + extensions = [v for v in extensions if v not in github.new_extensions] + return extensions + + +def get_available_loras(): + return ['None'] + sorted([item.name for item in list(Path(shared.args.lora_dir).glob('*')) if not item.name.endswith(('.txt', '-np', '.pt', '.json'))], key=natural_keys) + + +def get_datasets(path: str, ext: str): + # include subdirectories for raw txt files to allow training from a subdirectory of txt files + if ext == "txt": + return ['None'] + sorted(set([k.stem for k in list(Path(path).glob('*.txt')) + list(Path(path).glob('*/')) if k.stem != 'put-trainer-datasets-here']), key=natural_keys) + + return ['None'] + sorted(set([k.stem for k in Path(path).glob(f'*.{ext}') if k.stem != 'put-trainer-datasets-here']), key=natural_keys) + + +def get_available_chat_styles(): + return sorted(set(('-'.join(k.stem.split('-')[1:]) for k in Path('css').glob('chat_style*.css'))), key=natural_keys) + + +def get_available_grammars(): + return ['None'] + sorted([item.name for item in list(Path('grammars').glob('*.gbnf'))], key=natural_keys) diff --git a/python/llm/example/Text-Generation-WebUI/presets/Asterism.yaml b/python/llm/example/Text-Generation-WebUI/presets/Asterism.yaml new file mode 100644 index 00000000..87b56e1f --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/presets/Asterism.yaml @@ -0,0 +1,4 @@ +temperature: 1.68 +top_p: 0.17 +repetition_penalty: 1.02 +top_k: 77 diff --git a/python/llm/example/Text-Generation-WebUI/presets/Big O.yaml b/python/llm/example/Text-Generation-WebUI/presets/Big O.yaml new file mode 100644 index 00000000..2ab18268 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/presets/Big O.yaml @@ -0,0 +1,6 @@ +temperature: 0.87 +top_p: 0.99 +typical_p: 0.68 +tfs: 0.68 +repetition_penalty: 1.01 +top_k: 85 diff --git a/python/llm/example/Text-Generation-WebUI/presets/Contrastive Search.yaml b/python/llm/example/Text-Generation-WebUI/presets/Contrastive Search.yaml new file mode 100644 index 00000000..d9a47a9f --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/presets/Contrastive Search.yaml @@ -0,0 +1,3 @@ +do_sample: false +top_k: 4 +penalty_alpha: 0.3 diff --git a/python/llm/example/Text-Generation-WebUI/presets/Debug-deterministic.yaml b/python/llm/example/Text-Generation-WebUI/presets/Debug-deterministic.yaml new file mode 100644 index 00000000..45d5953f --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/presets/Debug-deterministic.yaml @@ -0,0 +1,2 @@ +do_sample: false +top_k: 1 diff --git a/python/llm/example/Text-Generation-WebUI/presets/Divine Intellect.yaml b/python/llm/example/Text-Generation-WebUI/presets/Divine Intellect.yaml new file mode 100644 index 00000000..ac750e40 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/presets/Divine Intellect.yaml @@ -0,0 +1,4 @@ +temperature: 1.31 +top_p: 0.14 +repetition_penalty: 1.17 +top_k: 49 diff --git a/python/llm/example/Text-Generation-WebUI/presets/Dynamic Temperature.yaml b/python/llm/example/Text-Generation-WebUI/presets/Dynamic Temperature.yaml new file mode 100644 index 00000000..3ef48739 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/presets/Dynamic Temperature.yaml @@ -0,0 +1,5 @@ +dynamic_temperature: true +dynamic_temperature_low: 0.1 +temperature: 3 +temperature_last: true +min_p: 0.05 diff --git a/python/llm/example/Text-Generation-WebUI/presets/LLaMA-Precise.yaml b/python/llm/example/Text-Generation-WebUI/presets/LLaMA-Precise.yaml new file mode 100644 index 00000000..c5f9cae2 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/presets/LLaMA-Precise.yaml @@ -0,0 +1,4 @@ +temperature: 0.7 +top_p: 0.1 +repetition_penalty: 1.18 +top_k: 40 diff --git a/python/llm/example/Text-Generation-WebUI/presets/Midnight Enigma.yaml b/python/llm/example/Text-Generation-WebUI/presets/Midnight Enigma.yaml new file mode 100644 index 00000000..0bd1763c --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/presets/Midnight Enigma.yaml @@ -0,0 +1,4 @@ +temperature: 0.98 +top_p: 0.37 +repetition_penalty: 1.18 +top_k: 100 diff --git a/python/llm/example/Text-Generation-WebUI/presets/Mirostat.yaml b/python/llm/example/Text-Generation-WebUI/presets/Mirostat.yaml new file mode 100644 index 00000000..8bf97e80 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/presets/Mirostat.yaml @@ -0,0 +1,2 @@ +mirostat_mode: 2 +mirostat_tau: 8 diff --git a/python/llm/example/Text-Generation-WebUI/presets/Shortwave.yaml b/python/llm/example/Text-Generation-WebUI/presets/Shortwave.yaml new file mode 100644 index 00000000..a2528abd --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/presets/Shortwave.yaml @@ -0,0 +1,4 @@ +temperature: 1.53 +top_p: 0.64 +repetition_penalty: 1.07 +top_k: 33 diff --git a/python/llm/example/Text-Generation-WebUI/presets/Space Alien.yaml b/python/llm/example/Text-Generation-WebUI/presets/Space Alien.yaml new file mode 100644 index 00000000..4c9a2dd2 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/presets/Space Alien.yaml @@ -0,0 +1,4 @@ +temperature: 1.31 +top_p: 0.29 +repetition_penalty: 1.09 +top_k: 72 diff --git a/python/llm/example/Text-Generation-WebUI/presets/StarChat.yaml b/python/llm/example/Text-Generation-WebUI/presets/StarChat.yaml new file mode 100644 index 00000000..2d00898b --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/presets/StarChat.yaml @@ -0,0 +1,3 @@ +temperature: 0.2 +top_p: 0.95 +top_k: 50 diff --git a/python/llm/example/Text-Generation-WebUI/presets/Titanic.yaml b/python/llm/example/Text-Generation-WebUI/presets/Titanic.yaml new file mode 100644 index 00000000..38760c26 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/presets/Titanic.yaml @@ -0,0 +1,5 @@ +temperature: 1.01 +top_p: 0.21 +repetition_penalty: 1.21 +encoder_repetition_penalty: 1.07 +top_k: 91 diff --git a/python/llm/example/Text-Generation-WebUI/presets/Yara.yaml b/python/llm/example/Text-Generation-WebUI/presets/Yara.yaml new file mode 100644 index 00000000..87bb019e --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/presets/Yara.yaml @@ -0,0 +1,4 @@ +temperature: 0.82 +top_p: 0.21 +repetition_penalty: 1.19 +top_k: 72 diff --git a/python/llm/example/Text-Generation-WebUI/presets/simple-1.yaml b/python/llm/example/Text-Generation-WebUI/presets/simple-1.yaml new file mode 100644 index 00000000..30a10659 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/presets/simple-1.yaml @@ -0,0 +1,4 @@ +temperature: 0.7 +top_p: 0.9 +repetition_penalty: 1.15 +top_k: 20 diff --git a/python/llm/example/Text-Generation-WebUI/presets/tfs-with-top-a.yaml b/python/llm/example/Text-Generation-WebUI/presets/tfs-with-top-a.yaml new file mode 100644 index 00000000..f16127cd --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/presets/tfs-with-top-a.yaml @@ -0,0 +1,4 @@ +temperature: 0.7 +tfs: 0.95 +top_a: 0.2 +repetition_penalty: 1.15 diff --git a/python/llm/example/Text-Generation-WebUI/prompts/Alpaca-with-Input.txt b/python/llm/example/Text-Generation-WebUI/prompts/Alpaca-with-Input.txt new file mode 100644 index 00000000..56df0e28 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/prompts/Alpaca-with-Input.txt @@ -0,0 +1,10 @@ +Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request. + +### Instruction: +Instruction + +### Input: +Input + +### Response: + diff --git a/python/llm/example/Text-Generation-WebUI/prompts/GPT-4chan.txt b/python/llm/example/Text-Generation-WebUI/prompts/GPT-4chan.txt new file mode 100644 index 00000000..1bc8c7f4 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/prompts/GPT-4chan.txt @@ -0,0 +1,6 @@ +----- +--- 865467536 +Hello, AI frens! +How are you doing on this fine day? +--- 865467537 + diff --git a/python/llm/example/Text-Generation-WebUI/prompts/QA.txt b/python/llm/example/Text-Generation-WebUI/prompts/QA.txt new file mode 100644 index 00000000..32b0e235 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/prompts/QA.txt @@ -0,0 +1,4 @@ +Common sense questions and answers + +Question: +Factual answer: diff --git a/python/llm/example/Text-Generation-WebUI/readme_folder/image-1.png b/python/llm/example/Text-Generation-WebUI/readme_folder/image-1.png new file mode 100644 index 00000000..b758f5da Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/readme_folder/image-1.png differ diff --git a/python/llm/example/Text-Generation-WebUI/readme_folder/image-2.png b/python/llm/example/Text-Generation-WebUI/readme_folder/image-2.png new file mode 100644 index 00000000..bd3b9b1a Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/readme_folder/image-2.png differ diff --git a/python/llm/example/Text-Generation-WebUI/readme_folder/image-3.png b/python/llm/example/Text-Generation-WebUI/readme_folder/image-3.png new file mode 100644 index 00000000..4a4fafb1 Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/readme_folder/image-3.png differ diff --git a/python/llm/example/Text-Generation-WebUI/readme_folder/image-4.png b/python/llm/example/Text-Generation-WebUI/readme_folder/image-4.png new file mode 100644 index 00000000..21f12cb7 Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/readme_folder/image-4.png differ diff --git a/python/llm/example/Text-Generation-WebUI/readme_folder/image.png b/python/llm/example/Text-Generation-WebUI/readme_folder/image.png new file mode 100644 index 00000000..a4a9692b Binary files /dev/null and b/python/llm/example/Text-Generation-WebUI/readme_folder/image.png differ diff --git a/python/llm/example/Text-Generation-WebUI/requirements.txt b/python/llm/example/Text-Generation-WebUI/requirements.txt new file mode 100644 index 00000000..2733e6d6 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/requirements.txt @@ -0,0 +1,20 @@ +accelerate==0.21.* +colorama +datasets +einops +gradio==3.50.* +markdown +numpy==1.24.* +pandas +peft==0.7.* +Pillow>=9.5.0 +pyyaml +requests +rich +safetensors==0.4.1 +scipy +sentencepiece +tensorboard +transformers==4.36.* +tqdm +wandb diff --git a/python/llm/example/Text-Generation-WebUI/server.py b/python/llm/example/Text-Generation-WebUI/server.py new file mode 100644 index 00000000..328f7c97 --- /dev/null +++ b/python/llm/example/Text-Generation-WebUI/server.py @@ -0,0 +1,272 @@ +# +# Copyright 2016 The BigDL Authors. +# +# 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. + +# This file is adapted from +# https://github.com/oobabooga/text-generation-webui/blob/main/server.py + + +import os +import warnings + +from modules import shared + +import accelerate # This early import makes Intel GPUs happy + +from modules.block_requests import OpenMonkeyPatch, RequestBlocker +from modules.logging_colors import logger + +os.environ['GRADIO_ANALYTICS_ENABLED'] = 'False' +os.environ['BITSANDBYTES_NOWELCOME'] = '1' +warnings.filterwarnings('ignore', category=UserWarning, message='TypedStorage is deprecated') +warnings.filterwarnings('ignore', category=UserWarning, message='Using the update method is deprecated') +warnings.filterwarnings('ignore', category=UserWarning, message='Field "model_name" has conflict') +warnings.filterwarnings('ignore', category=UserWarning, message='The value passed into gr.Dropdown()') +warnings.filterwarnings('ignore', category=UserWarning, message='Field "model_names" has conflict') + +with RequestBlocker(): + import gradio as gr + +import matplotlib + +matplotlib.use('Agg') # This fixes LaTeX rendering on some systems + +import json +import os +import signal +import sys +import time +from functools import partial +from pathlib import Path +from threading import Lock + +import yaml + +import modules.extensions as extensions_module +from modules import chat, utils +from modules import ( + ui, + ui_chat, + ui_default, + ui_file_saving, + ui_model_menu, + ui_notebook, + ui_parameters, + ui_session, +) +from modules.extensions import apply_extensions +from modules.models import load_model +from modules.models_settings import ( + get_fallback_settings, + get_model_metadata, + update_model_parameters +) +from modules.shared import do_cmd_flags_warnings +from modules.utils import gradio + + +def signal_handler(sig, frame): + logger.info("Received Ctrl+C. Shutting down Text generation web UI gracefully.") + sys.exit(0) + +signal.signal(signal.SIGINT, signal_handler) + +def create_interface(): + + title = 'Text generation web UI' + + # Password authentication + auth = [] + if shared.args.gradio_auth: + auth.extend(x.strip() for x in shared.args.gradio_auth.strip('"').replace('\n', '').split(',') if x.strip()) + if shared.args.gradio_auth_path: + with open(shared.args.gradio_auth_path, 'r', encoding="utf8") as file: + auth.extend(x.strip() for line in file for x in line.split(',') if x.strip()) + auth = [tuple(cred.split(':')) for cred in auth] + + # Import the extensions and execute their setup() functions + if shared.args.extensions is not None and len(shared.args.extensions) > 0: + extensions_module.load_extensions() + + # Force some events to be triggered on page load + shared.persistent_interface_state.update({ + 'loader': shared.args.loader or 'BigDL-LLM', + 'mode': shared.settings['mode'], + 'character_menu': shared.args.character or shared.settings['character'], + 'instruction_template_str': shared.settings['instruction_template_str'], + 'prompt_menu-default': shared.settings['prompt-default'], + 'prompt_menu-notebook': shared.settings['prompt-notebook'], + 'filter_by_loader': shared.args.loader or 'All' + }) + + if Path("cache/pfp_character.png").exists(): + Path("cache/pfp_character.png").unlink() + + # css/js strings + css = ui.css + js = ui.js + css += apply_extensions('css') + js += apply_extensions('js') + + # Interface state elements + shared.input_elements = ui.list_interface_input_elements() + + with gr.Blocks(css=css, analytics_enabled=False, title=title, theme=ui.theme) as shared.gradio['interface']: + + # Interface state + shared.gradio['interface_state'] = gr.State({k: None for k in shared.input_elements}) + + # Audio notification + if Path("notification.mp3").exists(): + shared.gradio['audio_notification'] = gr.Audio(interactive=False, value="notification.mp3", elem_id="audio_notification", visible=False) + + # Floating menus for saving/deleting files + ui_file_saving.create_ui() + + # Temporary clipboard for saving files + shared.gradio['temporary_text'] = gr.Textbox(visible=False) + + # Text Generation tab + ui_chat.create_ui() + ui_default.create_ui() + ui_notebook.create_ui() + + ui_parameters.create_ui(shared.settings['preset']) # Parameters tab + ui_model_menu.create_ui() # Model tab + ui_session.create_ui() # Session tab + + # Generation events + ui_chat.create_event_handlers() + ui_default.create_event_handlers() + ui_notebook.create_event_handlers() + + # Other events + ui_file_saving.create_event_handlers() + ui_parameters.create_event_handlers() + ui_model_menu.create_event_handlers() + + # Interface launch events + if shared.settings['dark_theme']: + shared.gradio['interface'].load(lambda: None, None, None, _js="() => document.getElementsByTagName('body')[0].classList.add('dark')") + + shared.gradio['interface'].load(lambda: None, None, None, _js=f"() => {{{js}}}") + shared.gradio['interface'].load(None, gradio('show_controls'), None, _js=f'(x) => {{{ui.show_controls_js}; toggle_controls(x)}}') + shared.gradio['interface'].load(partial(ui.apply_interface_values, {}, use_persistent=True), None, gradio(ui.list_interface_input_elements()), show_progress=False) + shared.gradio['interface'].load(chat.redraw_html, gradio(ui_chat.reload_arr), gradio('display')) + + extensions_module.create_extensions_tabs() # Extensions tabs + extensions_module.create_extensions_block() # Extensions block + + # Launch the interface + shared.gradio['interface'].queue(concurrency_count=64) + with OpenMonkeyPatch(): + shared.gradio['interface'].launch( + prevent_thread_lock=True, + share=True, + server_name=None if not shared.args.listen else (shared.args.listen_host or '0.0.0.0'), + server_port=shared.args.listen_port, + inbrowser=shared.args.auto_launch, + auth=auth or None, + ssl_verify=False if (shared.args.ssl_keyfile or shared.args.ssl_certfile) else True, + ssl_keyfile=shared.args.ssl_keyfile, + ssl_certfile=shared.args.ssl_certfile + ) + + +if __name__ == "__main__": + + logger.info("Starting Text generation web UI") + do_cmd_flags_warnings() + + # Load custom settings + settings_file = None + if shared.args.settings is not None and Path(shared.args.settings).exists(): + settings_file = Path(shared.args.settings) + elif Path('settings.yaml').exists(): + settings_file = Path('settings.yaml') + elif Path('settings.json').exists(): + settings_file = Path('settings.json') + + if settings_file is not None: + logger.info(f"Loading settings from {settings_file}") + file_contents = open(settings_file, 'r', encoding='utf-8').read() + new_settings = json.loads(file_contents) if settings_file.suffix == "json" else yaml.safe_load(file_contents) + shared.settings.update(new_settings) + + # Fallback settings for models + shared.model_config['.*'] = get_fallback_settings() + shared.model_config.move_to_end('.*', last=False) # Move to the beginning + + # Activate the extensions listed on settings.yaml + extensions_module.available_extensions = utils.get_available_extensions() + for extension in shared.settings['default_extensions']: + shared.args.extensions = shared.args.extensions or [] + if extension not in shared.args.extensions: + shared.args.extensions.append(extension) + + available_models = utils.get_available_models() + + # Model defined through --model + if shared.args.model is not None: + shared.model_name = shared.args.model + + # Select the model from a command-line menu + elif shared.args.model_menu: + if len(available_models) == 0: + logger.error('No models are available! Please download at least one.') + sys.exit(0) + else: + print('The following models are available:\n') + for i, model in enumerate(available_models): + print(f'{i+1}. {model}') + + print(f'\nWhich one do you want to load? 1-{len(available_models)}\n') + i = int(input()) - 1 + print() + + shared.model_name = available_models[i] + + # If any model has been selected, load it + if shared.model_name != 'None': + p = Path(shared.model_name) + if p.exists(): + model_name = p.parts[-1] + shared.model_name = model_name + else: + model_name = shared.model_name + + model_settings = get_model_metadata(model_name) + update_model_parameters(model_settings, initial=True) # hijack the command-line arguments + + # Load the model + shared.model, shared.tokenizer = load_model(model_name) + + shared.generation_lock = Lock() + + if shared.args.nowebui: + # Start the API in standalone mode + shared.args.extensions = [x for x in shared.args.extensions if x != 'gallery'] + if shared.args.extensions is not None and len(shared.args.extensions) > 0: + extensions_module.load_extensions() + else: + # Launch the web UI + create_interface() + while True: + time.sleep(0.5) + if shared.need_restart: + shared.need_restart = False + time.sleep(0.5) + shared.gradio['interface'].close() + time.sleep(0.5) + create_interface()