<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>AI/ML on Robin Su</title><link>https://robinsujob.github.io/categories/ai/ml/</link><description>Recent content in AI/ML on Robin Su</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Wed, 12 Feb 2025 00:00:00 +0800</lastBuildDate><atom:link href="https://robinsujob.github.io/categories/ai/ml/index.xml" rel="self" type="application/rss+xml"/><item><title>Deploy GGUF Models on Amazon SageMaker with llama.cpp</title><link>https://robinsujob.github.io/p/sagemaker-gguf-model-inference/</link><pubDate>Wed, 12 Feb 2025 00:00:00 +0800</pubDate><guid>https://robinsujob.github.io/p/sagemaker-gguf-model-inference/</guid><description>&lt;img src="https://robinsujob.github.io/" alt="Featured image of post Deploy GGUF Models on Amazon SageMaker with llama.cpp" /&gt;&lt;p&gt;With the rapid development of artificial intelligence, LLM models have demonstrated powerful capabilities in natural language processing, machine translation, and intelligent assistants. These models are typically developed using frameworks like PyTorch, with pre-trained results stored in specific binary formats such as PyTorch&amp;rsquo;s .pt files. However, as model sizes continue to grow, traditional file formats face challenges in storage, transmission, and loading — large file sizes, slow loading speeds, and poor cross-platform compatibility.&lt;/p&gt;
&lt;p&gt;To address these challenges, &lt;a class="link" href="https://github.com/ggerganov/ggml/blob/master/docs/gguf.md" target="_blank" rel="noopener"
 &gt;GGUF&lt;/a&gt; (GPT-Generated Unified Format) was created. GGUF is a binary file format designed specifically for LLM models, proposed by Georgi Gerganov, the founder of the llama.cpp open-source project. It aims to improve the storage and exchange efficiency of large models through optimized data structures, compact binary encoding, and memory mapping. GGUF format is now widely used in deploying and sharing various large models, especially popular in open-source communities like Hugging Face.&lt;/p&gt;
&lt;p&gt;Its core features enable developers to manage and use LLM models more efficiently while reducing resource consumption and improving performance. Through conversion tools, original model pre-training results can be easily converted to GGUF format for a more efficient experience.&lt;/p&gt;
&lt;p&gt;In this article, you will learn how to deploy LLM models in GGUF file format using Amazon SageMaker AI real-time inference endpoints.&lt;/p&gt;
&lt;h2 id="amazon-sagemaker-ai-model-deployment-options"&gt;Amazon SageMaker AI Model Deployment Options
&lt;/h2&gt;&lt;p&gt;&lt;a class="link" href="https://aws.amazon.com/cn/sagemaker-ai/" target="_blank" rel="noopener"
 &gt;Amazon SageMaker AI&lt;/a&gt; is a fully managed service that brings together a broad set of tools to enable high-performance, low-cost machine learning (ML) for any use case. With SageMaker AI, you can build, train, and deploy ML models at scale using notebooks, debuggers, profilers, pipelines, MLOps, and more. It supports built-in algorithms and pre-built Docker images for common ML frameworks (TensorFlow, PyTorch, ONNX, XGBoost), and provides specialized Deep Learning Containers (DLC), libraries, and tools for model parallelism and Large Model Inference (LMI).&lt;/p&gt;
&lt;p&gt;If the pre-built Docker images don&amp;rsquo;t meet your needs, you can build your own Docker container (BYOC – Bring Your Own Container) for inference. To be compatible with SageMaker AI, your container must:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Have a web server listening on port 8080.&lt;/li&gt;
&lt;li&gt;Accept POST requests to /invocations and /ping endpoints. Requests must return within 60 seconds with a maximum size of 6 MB.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To deploy and run GGUF models in SageMaker AI, you need to build a custom Docker container. This approach integrates the llama.cpp project to run GGUF models for effective deployment and inference on the SageMaker AI platform.&lt;/p&gt;
&lt;h2 id="llamacpp-and-gguf"&gt;llama.cpp and GGUF
&lt;/h2&gt;&lt;p&gt;&lt;a class="link" href="https://github.com/ggerganov/llama.cpp" target="_blank" rel="noopener"
 &gt;llama.cpp&lt;/a&gt; is a C/C++ LLM inference project designed for local or cloud-based LLM inference with minimal setup and optimal performance across various hardware. Key advantages include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Pure C/C++ implementation with no dependencies&lt;/li&gt;
&lt;li&gt;First-class support for Apple silicon, optimized via ARM NEON, Accelerate, and Metal frameworks&lt;/li&gt;
&lt;li&gt;AVX, AVX2, AVX512, and AMX support for x86 architectures&lt;/li&gt;
&lt;li&gt;Multiple quantization options (from 5-bit to 8-bit) for faster inference and lower memory usage&lt;/li&gt;
&lt;li&gt;Multiple GPU backend support including NVIDIA (CUDA), AMD (HIP), and Moore Threads MTT (MUSA)&lt;/li&gt;
&lt;li&gt;CPU+GPU hybrid inference, enabling partial acceleration for models larger than VRAM capacity&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To run in llama.cpp, models must be stored in GGUF format. You can run GGUF models through llama.cpp containers, leveraging its full capabilities. When running GGUF models in SageMaker, combining them with llama.cpp maximizes its efficient inference capabilities and multi-hardware architecture support.&lt;/p&gt;
&lt;h2 id="hosting-gguf-models-in-sagemaker-ai-using-byoc"&gt;Hosting GGUF Models in SageMaker AI Using BYOC
&lt;/h2&gt;&lt;p&gt;The following section details how to combine GGUF with llama.cpp using Amazon SageMaker AI. We&amp;rsquo;ll demonstrate how to build a dedicated GGUF Docker container for SageMaker AI and apply it to model inference.&lt;/p&gt;
&lt;p&gt;Using the &lt;a class="link" href="https://docs.aws.amazon.com/sagemaker/latest/dg/adapt-inference-container.html" target="_blank" rel="noopener"
 &gt;Bring Your Own Container (BYOC)&lt;/a&gt; approach, Amazon SageMaker AI provides great flexibility. Regardless of programming language, runtime, framework, or dependencies, you can integrate virtually any model and code into the SageMaker AI ecosystem.&lt;/p&gt;
&lt;p&gt;This article uses &lt;a class="link" href="https://aws.amazon.com/sagemaker-ai/notebooks/" target="_blank" rel="noopener"
 &gt;Amazon SageMaker AI Notebook&lt;/a&gt; to build and deploy the GGUF model runtime framework. The solution architecture is shown below.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Solution Architecture" class="gallery-image" data-flex-basis="344px" data-flex-grow="143" height="680" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai1.jpg" srcset="https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai1_hu_77d54a03e10f814.jpg 800w, https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai1.jpg 976w" width="976"&gt;&lt;/p&gt;
&lt;p&gt;The main steps are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;From HuggingFace, download a GGUF model and upload it to S3. This blog uses Llama 3 8B GGUF as an example.&lt;/li&gt;
&lt;li&gt;Prepare the key BYOC files in the Notebook: Dockerfile, main.py, requirements.txt, serve, server.sh.&lt;/li&gt;
&lt;li&gt;Create a custom Docker image and push it to Amazon ECR.&lt;/li&gt;
&lt;li&gt;Create an Amazon SageMaker Model in the Notebook and deploy it to an inference endpoint.&lt;/li&gt;
&lt;li&gt;During endpoint startup, download the GGUF model from S3 to the specified location in the container.&lt;/li&gt;
&lt;li&gt;Test the endpoint by invoking inference with the SageMaker SDK.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="1-create-amazon-sagemaker-ai-notebook"&gt;1. Create Amazon SageMaker AI Notebook
&lt;/h3&gt;&lt;p&gt;You can create a SageMaker AI Notebook instance in the AWS console, selecting an appropriate instance type and storage. Note that disk space of 30GB or more is recommended, primarily for container building and model storage.&lt;/p&gt;
&lt;p&gt;When selecting or creating an IAM role, ensure the role has permissions to push images to Amazon ECR.&lt;/p&gt;
&lt;p&gt;If the permissions are missing, add the following inline policy:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Notebook Creation" class="gallery-image" data-flex-basis="242px" data-flex-grow="101" height="1703" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai2.jpg" srcset="https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai2_hu_f3ed2e041fdb6436.jpg 800w, https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai2_hu_37b333e052ca44e0.jpg 1600w, https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai2.jpg 1722w" width="1722"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="IAM Permissions" class="gallery-image" data-flex-basis="292px" data-flex-grow="121" height="1249" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai3.jpg" srcset="https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai3_hu_8314466b84e5192e.jpg 800w, https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai3.jpg 1521w" width="1521"&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-json" data-lang="json"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;Effect&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;Allow&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;Action&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;ecr:CompleteLayerUpload&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;ecr:UploadLayerPart&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;ecr:InitiateLayerUpload&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;ecr:BatchCheckLayerAvailability&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;ecr:PutImage&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;ecr:BatchGetImage&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;],&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;Resource&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;arn:aws:ecr:*:*:*&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3 id="2-build-byoc-code"&gt;2. Build BYOC Code
&lt;/h3&gt;&lt;p&gt;The complete code is available in the &lt;a class="link" href="https://github.com/aws-samples/deploy-gguf-model-to-sagemaker" target="_blank" rel="noopener"
 &gt;aws-samples repository&lt;/a&gt;. This article covers key code explanations only.&lt;/p&gt;
&lt;p&gt;To run GGUF models in SageMaker, prepare the code with the following file structure:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;workspace
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;|-- Dockerfile
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;|-- main.py
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;|-- requirements.txt
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;|-- serve
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;|-- server.sh
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;The runtime trigger logic and HTTP requests are shown below.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Runtime Logic" class="gallery-image" data-flex-basis="287px" data-flex-grow="119" height="401" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai4.png" width="481"&gt;&lt;/p&gt;
&lt;h4 id="21-dockerfile"&gt;2.1 Dockerfile
&lt;/h4&gt;&lt;p&gt;The Dockerfile defines a container environment based on the llama.cpp CUDA version, designed specifically for running GGUF models on SageMaker.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;span class="lnt"&gt;14
&lt;/span&gt;&lt;span class="lnt"&gt;15
&lt;/span&gt;&lt;span class="lnt"&gt;16
&lt;/span&gt;&lt;span class="lnt"&gt;17
&lt;/span&gt;&lt;span class="lnt"&gt;18
&lt;/span&gt;&lt;span class="lnt"&gt;19
&lt;/span&gt;&lt;span class="lnt"&gt;20
&lt;/span&gt;&lt;span class="lnt"&gt;21
&lt;/span&gt;&lt;span class="lnt"&gt;22
&lt;/span&gt;&lt;span class="lnt"&gt;23
&lt;/span&gt;&lt;span class="lnt"&gt;24
&lt;/span&gt;&lt;span class="lnt"&gt;25
&lt;/span&gt;&lt;span class="lnt"&gt;26
&lt;/span&gt;&lt;span class="lnt"&gt;27
&lt;/span&gt;&lt;span class="lnt"&gt;28
&lt;/span&gt;&lt;span class="lnt"&gt;29
&lt;/span&gt;&lt;span class="lnt"&gt;30
&lt;/span&gt;&lt;span class="lnt"&gt;31
&lt;/span&gt;&lt;span class="lnt"&gt;32
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-dockerfile" data-lang="dockerfile"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;ghcr.io/ggerganov/llama.cpp:full-cuda&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;ENV&lt;/span&gt; &lt;span class="nv"&gt;PYTHONUNBUFFERED&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;ENV&lt;/span&gt; &lt;span class="nv"&gt;MODELPATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/app/llm_model.bin&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;ENV&lt;/span&gt; &lt;span class="nv"&gt;PATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$PATH&lt;/span&gt;:/app&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;ENV&lt;/span&gt; &lt;span class="nv"&gt;BUCKET&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&amp;#34;&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;ENV&lt;/span&gt; &lt;span class="nv"&gt;BUCKET_KEY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&amp;#34;&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;/app&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;RUN&lt;/span&gt; apt-get update &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; apt-get install -y &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; unzip &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; libcurl4-openssl-dev &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; python3 &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; python3-pip &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; python3-dev &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; git &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; psmisc &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; pciutils&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;COPY&lt;/span&gt; requirements.txt ./requirements.txt&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;COPY&lt;/span&gt; main.py /app/&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;COPY&lt;/span&gt; serve /app/&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;COPY&lt;/span&gt; server.sh /app/&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;RUN&lt;/span&gt; chmod u+x serve&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;RUN&lt;/span&gt; chmod u+x server.sh&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;RUN&lt;/span&gt; pip3 install -r requirements.txt&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;RUN&lt;/span&gt; &lt;span class="nb"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;PATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/app:&lt;span class="nv"&gt;$PATH&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;ENTRYPOINT&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;/bin/bash&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;8080&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;When building the Dockerfile, pay attention to these key environment variables:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;MODELPATH&lt;/strong&gt;: Specifies the model filename used at startup.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;BUCKET&lt;/strong&gt; and &lt;strong&gt;BUCKET_KEY&lt;/strong&gt;: Define the Amazon S3 bucket name and object key for the model file.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id="22-mainpy"&gt;2.2 main.py
&lt;/h4&gt;&lt;p&gt;This file implements an HTTP API server for interacting with llama.cpp. Key modifications for SageMaker deployment include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Port configuration: main.py defaults to port 8080, connecting to llama.cpp on port 8081&lt;/li&gt;
&lt;li&gt;Route changes: /v1/completions changed to /invocations, added /ping health check&lt;/li&gt;
&lt;li&gt;Model loading: Added update_model function for automatic S3 model download&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;span class="lnt"&gt;14
&lt;/span&gt;&lt;span class="lnt"&gt;15
&lt;/span&gt;&lt;span class="lnt"&gt;16
&lt;/span&gt;&lt;span class="lnt"&gt;17
&lt;/span&gt;&lt;span class="lnt"&gt;18
&lt;/span&gt;&lt;span class="lnt"&gt;19
&lt;/span&gt;&lt;span class="lnt"&gt;20
&lt;/span&gt;&lt;span class="lnt"&gt;21
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;update_model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;s3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;boto3&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;s3&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;s3&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;download_file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;MODELPATH&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;subprocess&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;/app/server.sh&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;MODELPATH&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)])&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="ne"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;traceback&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;format_exc&lt;/span&gt;&lt;span class="p"&gt;()))&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;False&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nd"&gt;@app.route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;/ping&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;methods&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;GET&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;ping&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nd"&gt;@app.route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;/invocations&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;methods&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;POST&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;completion&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="o"&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;is_present&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;configure&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;update_model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;configure&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;bucket&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;configure&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;key&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="o"&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h4 id="23-sagemaker-inference-entry-point-serve-file"&gt;2.3 SageMaker Inference Entry Point: serve File
&lt;/h4&gt;&lt;p&gt;SageMaker inference nodes execute &lt;code&gt;docker run image serve&lt;/code&gt; by default when starting containers.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#!/bin/sh
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;serve&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;uvicorn &lt;span class="s1"&gt;&amp;#39;main:asgi_app&amp;#39;&lt;/span&gt; --host 0.0.0.0 --port &lt;span class="m"&gt;8080&lt;/span&gt; --workers &lt;span class="m"&gt;8&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h4 id="24-llamacpp-startup-script-serversh"&gt;2.4 llama.cpp Startup Script: server.sh
&lt;/h4&gt;&lt;p&gt;server.sh starts the llama.cpp service. See the &lt;a class="link" href="https://github.com/ggerganov/llama.cpp/blob/master/examples/server/README.md" target="_blank" rel="noopener"
 &gt;llama.cpp documentation&lt;/a&gt; for available runtime parameters.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;span class="lnt"&gt;14
&lt;/span&gt;&lt;span class="lnt"&gt;15
&lt;/span&gt;&lt;span class="lnt"&gt;16
&lt;/span&gt;&lt;span class="lnt"&gt;17
&lt;/span&gt;&lt;span class="lnt"&gt;18
&lt;/span&gt;&lt;span class="lnt"&gt;19
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#!/bin/sh
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;server.sh&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;args: &lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;GPU Layer: &lt;/span&gt;&lt;span class="nv"&gt;$GPU_LAYERS&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt; lspci &lt;span class="p"&gt;|&lt;/span&gt; grep -i nvidia &lt;span class="p"&gt;&amp;amp;&lt;/span&gt;&amp;gt; /dev/null&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;NVIDIA GPU is available.&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nv"&gt;NGL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="nv"&gt;$GPU_LAYERS&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nv"&gt;CPU_PER_SLOT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;else&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;No NVIDIA GPU found.&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nv"&gt;NGL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nv"&gt;CPU_PER_SLOT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="m"&gt;4&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;killall llama-server
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;/app/llama-server -m &lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt; -c &lt;span class="m"&gt;2048&lt;/span&gt; -t &lt;span class="k"&gt;$(&lt;/span&gt;nproc --all&lt;span class="k"&gt;)&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --host 0.0.0.0 --port &lt;span class="m"&gt;8081&lt;/span&gt; -cb &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; -np &lt;span class="k"&gt;$(($(&lt;/span&gt;nproc --all&lt;span class="k"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nv"&gt;$CPU_PER_SLOT&lt;/span&gt;&lt;span class="k"&gt;))&lt;/span&gt; -ngl &lt;span class="nv"&gt;$NGL&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3 id="3-model-deployment-and-testing"&gt;3. Model Deployment and Testing
&lt;/h3&gt;&lt;p&gt;During model deployment, specify the ECR image, instance type, and corresponding environment variables.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;span class="lnt"&gt;14
&lt;/span&gt;&lt;span class="lnt"&gt;15
&lt;/span&gt;&lt;span class="lnt"&gt;16
&lt;/span&gt;&lt;span class="lnt"&gt;17
&lt;/span&gt;&lt;span class="lnt"&gt;18
&lt;/span&gt;&lt;span class="lnt"&gt;19
&lt;/span&gt;&lt;span class="lnt"&gt;20
&lt;/span&gt;&lt;span class="lnt"&gt;21
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;container_uri&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;ECR_REPOSITORY_URI&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;:&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;IMAGE_TAG&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;instance_type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;ml.g5.2xlarge&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;endpoint_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sagemaker&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;utils&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name_from_base&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;llama-cpp-gguf-byoc&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sagemaker&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;image_uri&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;container_uri&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;role&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;iam_role&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;endpoint_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;MODELPATH&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;/app/&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;MODEL_NAME&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;BUCKET&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;S3_BUCKET_NAME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;BUCKET_KEY&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MODEL_NAME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;GPU_LAYERS&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;32&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deploy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;instance_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;instance_type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;initial_instance_count&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;endpoint_name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;endpoint_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;During inference, you can pass parameters to get model output. Standard invocation:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;span class="lnt"&gt;14
&lt;/span&gt;&lt;span class="lnt"&gt;15
&lt;/span&gt;&lt;span class="lnt"&gt;16
&lt;/span&gt;&lt;span class="lnt"&gt;17
&lt;/span&gt;&lt;span class="lnt"&gt;18
&lt;/span&gt;&lt;span class="lnt"&gt;19
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;invoke_sagemaker_endpoint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;endpoint_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;llama_args&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sagemaker_runtime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;invoke_endpoint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;EndpointName&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;endpoint_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;Body&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;llama_args&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;ContentType&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;application/json&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;response_body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;Body&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;read&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;response_body&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;llama_args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;prompt&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;What are the most popular tourist attractions in Beijing?&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;max_tokens&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;512&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;temperature&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;repeat_penalty&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;frequency_penalty&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;1.1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;top_p&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;inference&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;invoke_sagemaker_endpoint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;endpoint_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;llama_args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inference&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;choices&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;text&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Streaming output:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;span class="lnt"&gt;14
&lt;/span&gt;&lt;span class="lnt"&gt;15
&lt;/span&gt;&lt;span class="lnt"&gt;16
&lt;/span&gt;&lt;span class="lnt"&gt;17
&lt;/span&gt;&lt;span class="lnt"&gt;18
&lt;/span&gt;&lt;span class="lnt"&gt;19
&lt;/span&gt;&lt;span class="lnt"&gt;20
&lt;/span&gt;&lt;span class="lnt"&gt;21
&lt;/span&gt;&lt;span class="lnt"&gt;22
&lt;/span&gt;&lt;span class="lnt"&gt;23
&lt;/span&gt;&lt;span class="lnt"&gt;24
&lt;/span&gt;&lt;span class="lnt"&gt;25
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;invoke_sagemaker_streaming_endpoint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;endpoint_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;llama_args&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sagemaker_runtime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;invoke_endpoint_with_response_stream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;EndpointName&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;endpoint_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;Body&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;llama_args&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;ContentType&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;application/json&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;event_stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;Body&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;event_stream&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;itm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;PayloadPart&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;Bytes&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;:]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;itm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;strict&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;choices&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;text&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;except&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;llama_args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;prompt&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;What are the most popular tourist attractions in Beijing?&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;max_tokens&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;512&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;temperature&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;repeat_penalty&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;frequency_penalty&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;1.1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;top_p&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;stream&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;invoke_sagemaker_streaming_endpoint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;endpoint_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;llama_args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;You can pass S3 bucket name and object key to the SageMaker AI inference instance for dynamic model replacement at runtime:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;configure&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;bucket&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;S3_BUCKET_NAME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;key&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MODEL_NAME&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sagemaker_runtime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;invoke_endpoint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;EndpointName&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;endpoint_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;ContentType&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;application/json&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;Body&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;response: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;The inference results after deployment are shown below.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Inference Results" class="gallery-image" data-flex-basis="483px" data-flex-grow="201" height="1648" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai5.jpg" srcset="https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai5_hu_8c8e6cd9d8315636.jpg 800w, https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai5_hu_96443b5ae743182f.jpg 1600w, https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai5_hu_31cf501a671b939e.jpg 2400w, https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai5.jpg 3322w" width="3322"&gt;&lt;/p&gt;
&lt;p&gt;After testing, if you no longer need the inference endpoint, delete the Endpoint resources to avoid unnecessary charges.&lt;/p&gt;
&lt;h2 id="summary"&gt;Summary
&lt;/h2&gt;&lt;p&gt;This article demonstrated how to deploy GGUF format models on Amazon SageMaker AI inference endpoints using the Bring Your Own Container (BYOC) approach via SageMaker AI Notebook. After successful deployment, you can seamlessly integrate the model with other AWS services and embed it into your applications to build feature-rich AI applications.&lt;/p&gt;
&lt;h2 id="references"&gt;References
&lt;/h2&gt;&lt;ol&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/ggerganov/ggml/blob/master/docs/gguf.md" target="_blank" rel="noopener"
 &gt;GGUF Specification&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/ggerganov/llama.cpp" target="_blank" rel="noopener"
 &gt;llama.cpp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://aws.amazon.com/cn/sagemaker/" target="_blank" rel="noopener"
 &gt;Amazon SageMaker&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://docs.aws.amazon.com/sagemaker/latest/dg/adapt-inference-container.html" target="_blank" rel="noopener"
 &gt;Adapting Your Own Inference Container&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://sagemaker-examples.readthedocs.io/en/latest/advanced_functionality/scikit_bring_your_own/scikit_bring_your_own.html" target="_blank" rel="noopener"
 &gt;SageMaker BYOC Examples&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/ggerganov/llama.cpp/blob/gguf-python/examples/server/api_like_OAI.py" target="_blank" rel="noopener"
 &gt;llama.cpp API Example&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/aws-samples/genai-llm-cpu-sagemaker/tree/main/docker" target="_blank" rel="noopener"
 &gt;GenAI LLM CPU SageMaker&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://community.aws/content/2eazHYzSfcY9flCGKsuGjpwqq1B" target="_blank" rel="noopener"
 &gt;Community AWS Article&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;hr&gt;

 &lt;blockquote&gt;
 &lt;p&gt;Original post: &lt;a class="link" href="https://aws.amazon.com/cn/blogs/china/practice-running-gguf-format-model-inference-using-sagemaker-ai/" target="_blank" rel="noopener"
 &gt;Practice Running GGUF Format Model Inference Using SageMaker AI&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;GitHub repo: &lt;a class="link" href="https://github.com/aws-samples/deploy-gguf-model-to-sagemaker" target="_blank" rel="noopener"
 &gt;aws-samples/deploy-gguf-model-to-sagemaker&lt;/a&gt;&lt;/p&gt;

 &lt;/blockquote&gt;</description></item><item><title>WordPress GenAI Plus - AI Content Generation Plugin</title><link>https://robinsujob.github.io/p/wordpress-genai-plus/</link><pubDate>Mon, 29 Apr 2024 00:00:00 +0800</pubDate><guid>https://robinsujob.github.io/p/wordpress-genai-plus/</guid><description>&lt;p&gt;Generative AI (GenAI) has risen rapidly since 2022, sweeping the globe and demonstrating enormous potential across multiple domains — text-to-image, text-to-text, image-to-image, and more. Models like Stable Diffusion and DALL-E can generate realistic images from natural-language descriptions, opening new possibilities for creative design and visual arts. Meanwhile, large language models such as Claude and GPT enable natural, fluent conversational interactions, empowering content creation and Q&amp;amp;A applications.&lt;/p&gt;
&lt;p&gt;WordPress is a widely used open-source content management system (CMS) ideal for creating and managing all types of websites — blogs, corporate sites, personal pages, and online stores. First released in 2003, it was designed to give users an easy-to-use platform for publishing content, even without a technical background.&lt;/p&gt;
&lt;p&gt;Now, leveraging the powerful AI capabilities of AWS, you can experience an intelligent AI writing assistant directly inside WordPress to accelerate your content creation. The newly launched WordPress GenAI Plus solution seamlessly integrates Claude 3 (via Amazon Bedrock) and Stable Diffusion into the WordPress editor. Simply provide a prompt or topic direction, and the AI will automatically generate article content and images. You can iterate as many times as needed until you achieve the ideal result.&lt;/p&gt;
&lt;p&gt;The main features provided by WordPress GenAI Plus writing assistant include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Text Generation&lt;/strong&gt;: Generate articles based on user prompts&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Image Generation&lt;/strong&gt;: Generate images based on user prompts&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Translation&lt;/strong&gt;: Currently supports Chinese, English, French, Japanese, and Korean as target languages; source language is unrestricted&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Grammar Check&lt;/strong&gt;: Provides grammar correction suggestions for selected English paragraphs (English only)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This solution supports one-click deployment via an Amazon CloudFormation template. By deploying the stack, users get the complete infrastructure — WordPress site, database, CDN, and AI capabilities — greatly simplifying setup and usage.&lt;/p&gt;
&lt;h2 id="architecture-overview"&gt;Architecture Overview
&lt;/h2&gt;&lt;p&gt;The WordPress GenAI Plus solution can be deployed in any AWS global region. It offers both a two-tier architecture (separating web and database layers) and a traditional LNMP (Linux, Nginx, MySQL, PHP) all-in-one architecture. The two-tier architecture is shown below:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Two-tier architecture" class="gallery-image" data-flex-basis="297px" data-flex-grow="124" height="2451" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img1.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img1_hu_ef986cc92121e38c.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img1_hu_1f38168aeec108de.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img1_hu_9cb7cba0cd6786a8.jpg 2400w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img1.jpg 3042w" width="3042"&gt;&lt;/p&gt;
&lt;p&gt;Architecture highlights: Two-tier (web + data), WordPress on public subnet, RDS on private subnet, multi-AZ option, CloudFront CDN, Lambda + Bedrock integration for GenAI capabilities. EC2, RDS, and Lambda can be deployed in any region; Bedrock is available in us-east-1 or us-west-2.&lt;/p&gt;
&lt;h2 id="deployment-process"&gt;Deployment Process
&lt;/h2&gt;&lt;h3 id="model-access-preparation"&gt;Model Access Preparation
&lt;/h3&gt;&lt;p&gt;Before getting started, you need to request model access permissions in Amazon Bedrock.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Select the Bedrock service in the AWS global console&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Select Bedrock service" class="gallery-image" data-flex-basis="423px" data-flex-grow="176" height="1632" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img2.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img2_hu_ff7adb1de6e28a7c.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img2_hu_ed60cc0f95369656.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img2_hu_eb0db7bf6f95213e.jpg 2400w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img2.jpg 2880w" width="2880"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Select the model deployment region (us-east-1 or us-west-2) in the top-right corner&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Select region" class="gallery-image" data-flex-basis="424px" data-flex-grow="176" height="1628" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img3.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img3_hu_c6dbf5acc69a53a.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img3_hu_690ac4e1519353c2.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img3_hu_7ffe6cbb83d97648.jpg 2400w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img3.jpg 2880w" width="2880"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;In the left sidebar, select &amp;ldquo;Model access&amp;rdquo;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Model access" class="gallery-image" data-flex-basis="426px" data-flex-grow="177" height="1622" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img4.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img4_hu_eb5eae4fa56907c.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img4_hu_2084e03a8b9945a9.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img4_hu_8bbe8fe229be6d5f.jpg 2400w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img4.jpg 2880w" width="2880"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Click the &amp;ldquo;Manage model access&amp;rdquo; button&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Manage model access" class="gallery-image" data-flex-basis="602px" data-flex-grow="250" height="1148" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img5.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img5_hu_4c7e1fcce0773894.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img5_hu_914817e58e4a3a46.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img5_hu_9c6dc810c37010b5.jpg 2400w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img5.jpg 2880w" width="2880"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Check the boxes for SDXL1.0, Claude 3 Sonnet, and Claude 3 Haiku, then click &amp;ldquo;Save changes&amp;rdquo;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Select models" class="gallery-image" data-flex-basis="462px" data-flex-grow="192" height="1368" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img6.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img6_hu_78eff3ca9133ee4f.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img6_hu_d3ad62343717f74c.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img6_hu_cc484afaeb398a9b.jpg 2400w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img6.jpg 2634w" width="2634"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Wait for permission to be granted (status changes to &amp;ldquo;Access granted&amp;rdquo;)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="create-key-pair"&gt;Create Key Pair
&lt;/h3&gt;&lt;p&gt;A key pair allows you to securely connect to your Amazon EC2 instance via SSH. Before deployment, make sure you have created a key pair.&lt;/p&gt;
&lt;p&gt;If you haven&amp;rsquo;t created one yet, &lt;a class="link" href="https://console.aws.amazon.com/ec2/home?#CreateKeyPair:" target="_blank" rel="noopener"
 &gt;click here to create a key pair&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Create key pair" class="gallery-image" data-flex-basis="421px" data-flex-grow="175" height="1617" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img7.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img7_hu_cad5c45fa3e0a90d.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img7_hu_f6cce1f57c16990f.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img7_hu_94281de5e7298609.jpg 2400w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img7.jpg 2842w" width="2842"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Confirm the deployment region, enter a key pair name, click &amp;ldquo;Create key pair&amp;rdquo; and save it securely.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="create-cloudformation-stack"&gt;Create CloudFormation Stack
&lt;/h3&gt;&lt;p&gt;Click the link above to enter the stack deployment page.&lt;/p&gt;
&lt;p&gt;&lt;a class="link" href="https://console.aws.amazon.com/cloudformation/home?#stacks/create/template?templateURL=https://aws-cn-getting-started.s3.us-west-2.amazonaws.com/wordpress-plus/WordpressTwoLayer.template.json" target="_blank" rel="noopener"
 &gt;Click to deploy&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Confirm the deployment region in the top-right corner (must match your key pair region), then specify the following parameters:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Custom stack name&lt;/li&gt;
&lt;li&gt;EC2 instance type&lt;/li&gt;
&lt;li&gt;EC2 disk size&lt;/li&gt;
&lt;li&gt;EC2 key pair&lt;/li&gt;
&lt;li&gt;Database username&lt;/li&gt;
&lt;li&gt;Database password (min 8 chars, no /, quotes, or @)&lt;/li&gt;
&lt;li&gt;Database instance type&lt;/li&gt;
&lt;li&gt;Database storage size&lt;/li&gt;
&lt;li&gt;Multi-AZ deployment option&lt;/li&gt;
&lt;li&gt;Bedrock deployment region&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Click Next.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Parameter configuration 1" class="gallery-image" data-flex-basis="437px" data-flex-grow="182" height="1482" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img8.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img8_hu_7d2b2c9cef5c41c6.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img8_hu_421e9f77cd018aa8.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img8_hu_439213e3bdf1cd13.jpg 2400w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img8.jpg 2701w" width="2701"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="Parameter configuration 2" class="gallery-image" data-flex-basis="340px" data-flex-grow="141" height="1481" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img9.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img9_hu_6288bdd7fec4c900.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img9_hu_b4fcc6939852e6fa.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img9.jpg 2103w" width="2103"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;On step 3 &amp;ldquo;Configure stack options&amp;rdquo;, click Next&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Configure stack options" class="gallery-image" data-flex-basis="468px" data-flex-grow="195" height="1267" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img10.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img10_hu_95518abbf47e5ca7.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img10_hu_fb67f51952fa9f00.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img10_hu_2115bc5a21c35a03.jpg 2400w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img10.jpg 2471w" width="2471"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;On step 4, check the IAM acknowledgments and CAPABILITY_AUTO_EXPAND, then click Submit&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Confirm and submit" class="gallery-image" data-flex-basis="1388px" data-flex-grow="578" height="334" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img11.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img11_hu_3d90c68d964ed45.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img11_hu_ff83e94cfa74f7ac.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img11.jpg 1933w" width="1933"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Wait about 10 minutes for the stack status to become CREATE_COMPLETE&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Creating" class="gallery-image" data-flex-basis="286px" data-flex-grow="119" height="946" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img12.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img12_hu_81f94874089ae4dd.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img12.jpg 1129w" width="1129"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="Creation complete" class="gallery-image" data-flex-basis="1107px" data-flex-grow="461" height="273" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img13.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img13_hu_3781969cab94306a.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img13.jpg 1260w" width="1260"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Check the Outputs tab for GenAI Function URL, WordPress install URL, admin URL, and site URL&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Outputs" class="gallery-image" data-flex-basis="496px" data-flex-grow="206" height="537" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img14.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img14_hu_353ab9d564899a99.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img14.jpg 1111w" width="1111"&gt;&lt;/p&gt;
&lt;h2 id="start-using-wordpress-ai-capabilities"&gt;Start Using WordPress AI Capabilities
&lt;/h2&gt;&lt;h3 id="initialize-wordpress"&gt;Initialize WordPress
&lt;/h3&gt;&lt;p&gt;After deployment, find the InstallWordPress field in the CloudFormation stack outputs and open the URL to configure WordPress.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Install WordPress" class="gallery-image" data-flex-basis="746px" data-flex-grow="310" height="926" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img15.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img15_hu_bf06d4c5c6828201.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img15_hu_481036577f33f7d8.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img15_hu_2d7b049f3e10d176.jpg 2400w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img15.jpg 2879w" width="2879"&gt;&lt;/p&gt;
&lt;p&gt;In the one-click deployment, database information is pre-configured. You only need to set up your site title, username, and password, then click &amp;ldquo;Install WordPress&amp;rdquo; and log into the admin panel.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Configure site info" class="gallery-image" data-flex-basis="223px" data-flex-grow="93" height="1758" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img16.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img16_hu_86e385ae7faafc9c.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img16_hu_641f58c03ebd0476.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img16.jpg 1639w" width="1639"&gt;&lt;/p&gt;
&lt;h3 id="enable-the-genai-plugin"&gt;Enable the GenAI Plugin
&lt;/h3&gt;&lt;p&gt;In the WordPress admin panel, click &amp;ldquo;Plugins&amp;rdquo;, find &amp;ldquo;Amazon AI writing assistant&amp;rdquo; and click Activate.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Enable plugin" class="gallery-image" data-flex-basis="400px" data-flex-grow="166" height="1083" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img17.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img17_hu_efdbd2773c46eb06.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img17_hu_e62b2ca4d64d37f7.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img17.jpg 1805w" width="1805"&gt;&lt;/p&gt;
&lt;p&gt;The Lambda URL and Region are pre-configured. Verify and click Save.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Plugin configuration" class="gallery-image" data-flex-basis="456px" data-flex-grow="190" height="1388" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img18.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img18_hu_56798583b4336e7f.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img18_hu_a787804102fa7bcf.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img18_hu_f4020940aeba884f.jpg 2400w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img18.jpg 2641w" width="2641"&gt;&lt;/p&gt;
&lt;h3 id="experience-genai-capabilities"&gt;Experience GenAI Capabilities
&lt;/h3&gt;&lt;p&gt;In the WordPress admin, click &amp;ldquo;New → Post&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;&lt;img alt="New post" class="gallery-image" data-flex-basis="457px" data-flex-grow="190" height="586" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img19.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img19_hu_824c0100d2ea8ab7.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img19.jpg 1118w" width="1118"&gt;&lt;/p&gt;
&lt;p&gt;In the editor body, type &lt;code&gt;/ask&lt;/code&gt; to trigger the Ask AI plugin. Click it to start using the AI features.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Ask AI plugin" class="gallery-image" data-flex-basis="658px" data-flex-grow="274" height="426" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img20.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img20_hu_71405a2822eef0f3.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img20.jpg 1168w" width="1168"&gt;&lt;/p&gt;
&lt;p&gt;You can enter text-to-text or text-to-image prompts in Chinese or English in the popup input box.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Text generation example" class="gallery-image" data-flex-basis="766px" data-flex-grow="319" height="432" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img21.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img21_hu_e2be7a30c6459c1b.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img21.jpg 1380w" width="1380"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="Image generation example" class="gallery-image" data-flex-basis="778px" data-flex-grow="324" height="401" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img22.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img22_hu_21aebfa7b3e7210c.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img22.jpg 1300w" width="1300"&gt;&lt;/p&gt;
&lt;p&gt;See the demo below:&lt;/p&gt;
&lt;p&gt;&lt;img alt="GenAI demo" class="gallery-image" data-flex-basis="368px" data-flex-grow="153" height="521" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/gif1.gif" width="800"&gt;&lt;/p&gt;
&lt;p&gt;The GenAI plugin also supports translation and grammar checking:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Translation&lt;/strong&gt;: Enter text in any language, click the pen icon in the floating toolbar → Translate Text. Target language is set in the plugin configuration page. Supported: ZH_CN, EN_US, FR, JA, KO.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Translation demo" class="gallery-image" data-flex-basis="417px" data-flex-grow="173" height="345" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/gif2.gif" width="600"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="Translation config" class="gallery-image" data-flex-basis="428px" data-flex-grow="178" height="1400" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img25.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img25_hu_6826f283d45e92c0.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img25_hu_c1733b4c18dafc5e.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img25_hu_6e5b049473cfecbd.jpg 2400w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img25.jpg 2500w" width="2500"&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Grammar Check&lt;/strong&gt;: For text you want to correct, click the pen icon in the floating toolbar → Correct Text.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Grammar check demo" class="gallery-image" data-flex-basis="417px" data-flex-grow="173" height="345" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/gif3.gif" width="600"&gt;&lt;/p&gt;
&lt;h2 id="alternative-architecture"&gt;Alternative Architecture
&lt;/h2&gt;&lt;p&gt;You can configure your own domain name in Amazon CloudFront, use Certificate Manager to request a free public certificate for HTTPS, and update the site address in the WordPress admin panel.&lt;/p&gt;
&lt;p&gt;This solution also supports a traditional LNMP all-in-one WordPress architecture:&lt;/p&gt;
&lt;p&gt;&lt;img alt="LNMP architecture" class="gallery-image" data-flex-basis="709px" data-flex-grow="295" height="1259" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img27.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img27_hu_1a9e40034b81b345.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img27_hu_959d415c4d7de88c.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img27_hu_b44b6ce634ac4395.jpg 2400w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img27.jpg 3721w" width="3721"&gt;&lt;/p&gt;
&lt;p&gt;Deploy the LNMP architecture: &lt;a class="link" href="https://console.aws.amazon.com/cloudformation/home?#stacks/create/template?templateURL=https://aws-cn-getting-started.s3.us-west-2.amazonaws.com/wordpress-plus/WordpressInOne.template.json" target="_blank" rel="noopener"
 &gt;Click to deploy&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="summary"&gt;Summary
&lt;/h2&gt;&lt;p&gt;Through this tutorial, you can deploy WordPress GenAI Plus on AWS with one click, integrating the power of generative AI into the WordPress editor for a brand-new AI-powered content creation experience.&lt;/p&gt;
&lt;p&gt;Related Resources:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://aws.amazon.com/cn/campaigns/aigc" target="_blank" rel="noopener"
 &gt;AWS AIGC&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;

 &lt;blockquote&gt;
 &lt;p&gt;Original article: &lt;a class="link" href="https://aws.amazon.com/cn/blogs/china/wordpress-genai-plus-accelerate-your-website-content-creation-with-generative-ai/" target="_blank" rel="noopener"
 &gt;WordPress GenAI Plus: Accelerate Your Website Content Creation with Generative AI&lt;/a&gt;&lt;/p&gt;

 &lt;/blockquote&gt;</description></item><item><title>Art QR Code with ControlNet and Stable Diffusion</title><link>https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/</link><pubDate>Fri, 01 Sep 2023 00:00:00 +0800</pubDate><guid>https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/</guid><description>&lt;img src="https://robinsujob.github.io/" alt="Featured image of post Art QR Code with ControlNet and Stable Diffusion" /&gt;&lt;h2 id="background"&gt;Background
&lt;/h2&gt;&lt;p&gt;Over the past few months, there has been growing interest in combining Stable Diffusion with ControlNet to create artistic QR codes. This post was originally developed as part of an AWS workshop demonstrating how to deploy and use Stable Diffusion on AWS infrastructure.&lt;/p&gt;
&lt;p&gt;This post shares practical tips for working with Stable Diffusion and walks through the best practices for generating artistic QR codes.&lt;/p&gt;
&lt;p&gt;We will use QR codes as ControlNet input, integrating QR code data points into artistic images while keeping them scannable by QR code readers. With this technology, you can transform any QR code into a unique piece of art, expressing and conveying information in an entirely new way. Here are some examples:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Sample 1" class="gallery-image" data-flex-basis="240px" data-flex-grow="100" height="2000" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/sample1.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/sample1_hu_55ce0a96d66b54da.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/sample1_hu_76a851db4e696861.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/sample1.jpg 2000w" width="2000"&gt;
&lt;img alt="Sample 2" class="gallery-image" data-flex-basis="240px" data-flex-grow="100" height="2000" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/sample2.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/sample2_hu_58726bd2cf5fe70d.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/sample2_hu_4c6be33718f82540.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/sample2.jpg 2000w" width="2000"&gt;
&lt;img alt="Sample 3" class="gallery-image" data-flex-basis="240px" data-flex-grow="100" height="2000" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/sample3.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/sample3_hu_aa92c1059d5785be.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/sample3_hu_52c82c010083f7a4.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/sample3.jpg 2000w" width="2000"&gt;&lt;/p&gt;
&lt;h2 id="stable-diffusion-practical-tips"&gt;Stable Diffusion Practical Tips
&lt;/h2&gt;&lt;p&gt;As the ancient Chinese saying goes: &amp;ldquo;The beginning is always the hardest&amp;rdquo; and &amp;ldquo;Seek breadth while perfecting the details.&amp;rdquo; These correspond to the two most common challenges when working with Stable Diffusion: first, how to choose the right prompt strategy to generate images that meet expectations; and second, how to fine-tune image details so the final output meets production-quality requirements.&lt;/p&gt;
&lt;p&gt;Based on our experience helping users work with Stable Diffusion, we have compiled the following best practices for reference.&lt;/p&gt;
&lt;h3 id="prompt-engineering"&gt;Prompt Engineering
&lt;/h3&gt;&lt;p&gt;As Stable Diffusion versions continue to evolve and AI&amp;rsquo;s understanding of semantics gets closer to &amp;ldquo;common sense,&amp;rdquo; the requirements for effective prompts are becoming increasingly demanding. Many common prompt misconceptions can sometimes have adverse effects on image generation.&lt;/p&gt;
&lt;h4 id="basic-concepts"&gt;Basic Concepts
&lt;/h4&gt;&lt;p&gt;Prompts are divided into &lt;strong&gt;positive prompts&lt;/strong&gt; (what you want) and &lt;strong&gt;negative prompts&lt;/strong&gt; (what you don&amp;rsquo;t want), telling the AI what to include and what to exclude.&lt;/p&gt;
&lt;h4 id="common-misconceptions"&gt;Common Misconceptions
&lt;/h4&gt;&lt;ul&gt;
&lt;li&gt;Precision over quantity — describing a scene with the shortest words is more effective than natural language.&lt;/li&gt;
&lt;li&gt;Quality-improving descriptors should not be mindlessly stacked; more is not better.&lt;/li&gt;
&lt;li&gt;Common starters like &amp;ldquo;masterpiece&amp;rdquo; and &amp;ldquo;best quality&amp;rdquo; often become dead weight in prompts. These words were meaningful in the NovelAI era, when they were heavily used during model training for image evaluation. However, after continuous model refinement by authors on Civitai, these prompts barely show their intended effect in modern models.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id="adjusting-prompt-weights"&gt;Adjusting Prompt Weights
&lt;/h4&gt;&lt;ul&gt;
&lt;li&gt;The default weight of each token is 1, decreasing from left to right.&lt;/li&gt;
&lt;li&gt;Prompt weights significantly affect the generated image.&lt;/li&gt;
&lt;li&gt;Use parentheses + colon + number to specify weight, e.g., &lt;code&gt;(one girl:1.5)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id="mind-the-prompt-order"&gt;Mind the Prompt Order
&lt;/h4&gt;&lt;ul&gt;
&lt;li&gt;Landscape tags first = smaller characters; character tags first = larger characters or half-body shots.&lt;/li&gt;
&lt;li&gt;Choose the correct order and syntax for prompts to more effectively render the desired scene.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id="emojis-in-prompts"&gt;Emojis in Prompts
&lt;/h4&gt;&lt;ul&gt;
&lt;li&gt;Prompts support emojis, and they work with good expressiveness.&lt;/li&gt;
&lt;li&gt;For specific facial expressions or actions, adding emojis can achieve the desired effect.&lt;/li&gt;
&lt;li&gt;To prevent semantic drift, prefer emojis and minimize unnecessary complex syntax.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id="recommended-camera-angle-prompts"&gt;Recommended Camera Angle Prompts
&lt;/h4&gt;&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Parameter&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;extreme closeup&lt;/td&gt;
 &lt;td&gt;Face close-up&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;close up&lt;/td&gt;
 &lt;td&gt;Head&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;medium close up&lt;/td&gt;
 &lt;td&gt;ID photo&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;medium shot&lt;/td&gt;
 &lt;td&gt;Half body&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;cowboy shot&lt;/td&gt;
 &lt;td&gt;No legs&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;medium full shot&lt;/td&gt;
 &lt;td&gt;No feet&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;full shot&lt;/td&gt;
 &lt;td&gt;Full body&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;h3 id="image-optimization"&gt;Image Optimization
&lt;/h3&gt;&lt;p&gt;Often we generate an image that&amp;rsquo;s not quite satisfactory and want to further optimize it, but don&amp;rsquo;t know where to start. Here are some best practices for parameter tuning:&lt;/p&gt;
&lt;h4 id="which-parameters-to-adjust"&gt;Which Parameters to Adjust
&lt;/h4&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CFG Scale&lt;/strong&gt;: The correlation between the image and the prompt. Higher values mean greater prompt influence and closer adherence to the prompt.
&lt;ul&gt;
&lt;li&gt;CFG 2–6: Creative but may be too distorted, not following the prompt. Fun for short prompts.&lt;/li&gt;
&lt;li&gt;CFG 7–10: Recommended for most prompts.&lt;/li&gt;
&lt;li&gt;CFG 10–15: Use when the prompt is detailed and clear.&lt;/li&gt;
&lt;li&gt;CFG 16–20: Not recommended unless the prompt is very detailed.&lt;/li&gt;
&lt;li&gt;CFG &amp;gt;20: Almost unusable.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sampling Steps&lt;/strong&gt;: More steps mean smaller, more precise adjustments per step, but proportionally increase generation time. For most samplers, more iterations produce better results, but beyond 50 steps, improvements become negligible.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sampling Method&lt;/strong&gt;: Different methods have different optimal step counts; comprehensive comparison is needed.
&lt;ul&gt;
&lt;li&gt;Euler a: Creative; different step counts produce different images. An efficient method for quickly testing prompt effects.&lt;/li&gt;
&lt;li&gt;DPM2 a Karras: Good for realistic models, hard to control after 30 steps.&lt;/li&gt;
&lt;li&gt;DPM++ 2M Karras: Excellent at high step counts, more details.&lt;/li&gt;
&lt;li&gt;DDIM: Fast convergence but low efficiency; good for inpainting.&lt;/li&gt;
&lt;li&gt;Different model and sampler combinations produce different results; the above is for reference only. When choosing sampling methods, it&amp;rsquo;s best to use X/Y/Z plots for comparison.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Seed&lt;/strong&gt;: The random seed often has a huge impact on composition and is the main source of randomness in SD. With the same seed and all other parameters fixed, you can reproduce (nearly) identical images. Once you find a suitable composition, fix the seed and fine-tune details — this is the recommended approach.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id="how-to-find-optimal-parameters"&gt;How to Find Optimal Parameters
&lt;/h4&gt;&lt;p&gt;Use X/Y/Z plots to clearly compare results under different parameters, quickly locate suitable parameter ranges, and perform further generation control.&lt;/p&gt;
&lt;p&gt;&lt;img alt="X/Y/Z Plot Comparison" class="gallery-image" data-flex-basis="353px" data-flex-grow="147" height="1358" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image4.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image4_hu_77502e0855ea324.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image4_hu_f3805d232f87f8e3.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image4.jpg 2000w" width="2000"&gt;&lt;/p&gt;
&lt;h4 id="image-size-optimization"&gt;Image Size Optimization
&lt;/h4&gt;&lt;ul&gt;
&lt;li&gt;Image quality is not directly tied to image size.&lt;/li&gt;
&lt;li&gt;However, size does affect the subject and content to some extent.&lt;/li&gt;
&lt;li&gt;When the output size is too wide, multiple subjects may appear.&lt;/li&gt;
&lt;li&gt;Sizes above 1024 may produce undesirable results, and GPU memory pressure becomes significant. Small resolution + upscaling is recommended.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id="optimizing-multi-character-and-wide-format-generation"&gt;Optimizing Multi-Character and Wide-Format Generation
&lt;/h4&gt;&lt;ul&gt;
&lt;li&gt;Using txt2img alone cannot effectively specify individual character features in multi-character scenarios.&lt;/li&gt;
&lt;li&gt;The recommended approach is to create a draft + img2img or ControlNet.&lt;/li&gt;
&lt;li&gt;For wide-format single-character generation, draft a sketch with color blocking to determine the main subject, or use ControlNet&amp;rsquo;s OpenPose for character skeleton.&lt;/li&gt;
&lt;li&gt;For multi-character scenes, use ControlNet&amp;rsquo;s OpenPose to specify character count; this also works for three-view drawings of the same character.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id="hand-repair"&gt;Hand Repair
&lt;/h4&gt;&lt;p&gt;Send the image to img2img inpaint with similar prompts, placing &amp;ldquo;hand&amp;rdquo; prompts at the front. Set denoising strength based on how much you want hand features to change (below 0.25 if you just want completeness), keeping steps and CFG the same as txt2img. For more precise hand control, find a satisfactory hand reference image and use ControlNet&amp;rsquo;s Canny or OpenPose_hands preprocessor + model combined with inpaint.&lt;/p&gt;
&lt;h4 id="face-repair"&gt;Face Repair
&lt;/h4&gt;&lt;p&gt;When drawing images with small character subjects, facial distortion frequently occurs. Especially in the artistic QR code generation process described later, faces often break due to QR code data points. For facial inpainting, the &lt;strong&gt;ADetailer&lt;/strong&gt; plugin is recommended. It uses the YOLO algorithm for object detection — we configure it to detect character faces and provide prompts and models for facial inpainting. The plugin performs localized inpainting at detected face locations to complete the repair. ADetailer can handle both face and hand detection and repair, and can also reference LoRA models for localized inpainting.&lt;/p&gt;
&lt;p&gt;&lt;img alt="ADetailer Face Repair" class="gallery-image" data-flex-basis="445px" data-flex-grow="185" height="1078" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image5.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image5_hu_6367fc31bcade539.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image5_hu_5676d0dce49e6a81.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image5.jpg 2000w" width="2000"&gt;&lt;/p&gt;
&lt;h2 id="generating-artistic-qr-codes-with-controlnet"&gt;Generating Artistic QR Codes with ControlNet
&lt;/h2&gt;&lt;h3 id="step-1-optimize-the-qr-code"&gt;Step 1: Optimize the QR Code
&lt;/h3&gt;&lt;p&gt;A QR code is a pattern of black and white geometric shapes distributed in 2D space that records symbolic data. There are multiple encoding methods; we use the most universal and fundamental one: QR Code.&lt;/p&gt;
&lt;p&gt;The input QR code is one of the most important factors when generating artistic QR codes with SD. We mainly care about two characteristics:&lt;/p&gt;
&lt;p&gt;&lt;img alt="QR Code Structure" class="gallery-image" data-flex-basis="463px" data-flex-grow="193" height="1035" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image6.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image6_hu_b52fdd4bc2347dd6.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image6_hu_4f11b8a8789a497c.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image6.jpg 2000w" width="2000"&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1. Information contained in the QR code&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Regardless of the encoding method, the more character information a QR code carries, the more complex its visual black-and-white structure becomes. Complex structures heavily constrain artistic creativity during generation. Therefore, we first need to simplify the content length.&lt;/p&gt;
&lt;p&gt;For the most common use case — web links — shorten the URL first. There are many URL shortening tools available; choose freely. Note that within mainland China, use platforms with registered domain names, otherwise links may be blocked by WeChat, browsers, etc.&lt;/p&gt;
&lt;p&gt;&lt;img alt="URL Length Comparison" class="gallery-image" data-flex-basis="434px" data-flex-grow="180" height="1105" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image7.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image7_hu_994cc7c8be691b7a.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image7_hu_5ecf3e0fd8a69b85.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image7.jpg 2000w" width="2000"&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2. How the QR code is presented&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;With technological development, QR codes no longer only support black and white square patterns; both positioning points and modules support diverse presentation styles.&lt;/p&gt;
&lt;p&gt;In practice, try different module styles to achieve the desired generation effect. The following images show how different QR code styles affect the final result:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Different Module Styles" class="gallery-image" data-flex-basis="665px" data-flex-grow="277" height="721" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image8.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image8_hu_f5428857b68a719e.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image8_hu_e03ceb67ae15f93d.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image8.jpg 2000w" width="2000"&gt;
&lt;img alt="Different QR Code Style Comparison" class="gallery-image" data-flex-basis="474px" data-flex-grow="197" height="1012" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image9.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image9_hu_19fbc491720a35dd.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image9_hu_299366d2f08403a0.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image9.jpg 2000w" width="2000"&gt;&lt;/p&gt;
&lt;p&gt;Generation parameters:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;span class="lnt"&gt;7
&lt;/span&gt;&lt;span class="lnt"&gt;8
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Prompt: mountain, green grassland, sky, cloud, bird, blue sky, no human, day, wide shot, flying, border, outdoors, white bird, scenery
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Negative prompt: easynegative
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Steps: 40, Sampler: DPM++ 2M Karras, CFG scale: 6, Seed: 3943213078, Size: 872x872,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Model hash: 876b4c7ba5, Model: cetusMix_Whalefall2, Clip skip: 2,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;ControlNet: &amp;#34;preprocessor: none, model: control_v1p_sd15_qrcode_monster [a6e58995],
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;weight: 1.35-1.5, starting/ending: (0.05, 1), resize mode: Resize and Fill,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;pixel perfect: True, control mode: Balanced, preprocessor params: (512, 64, 64)&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Version: v1.3.
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3 id="step-2-create-the-base-qr-code"&gt;Step 2: Create the Base QR Code
&lt;/h3&gt;&lt;p&gt;After understanding the key points above, use a QR code generation tool to create a base QR code for SD input. There are many web-based generators available; choose freely. For convenience, here is a useful WebUI plugin:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Anthony&amp;rsquo;s QR Toolkit&lt;/strong&gt;: Integrated QR code generation and optimization tool for WebUI
&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/antfu/sd-webui-qrcode-toolkit" target="_blank" rel="noopener"
 &gt;https://github.com/antfu/sd-webui-qrcode-toolkit&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="QR Toolkit Configuration" class="gallery-image" data-flex-basis="352px" data-flex-grow="146" height="1363" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image10.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image10_hu_7dd3f5c4533a8b1f.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image10_hu_a9e7cdd31e40fc49.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image10.jpg 2000w" width="2000"&gt;&lt;/p&gt;
&lt;p&gt;After creating the QR code, click &amp;ldquo;Download&amp;rdquo; to save it locally, or click &amp;ldquo;Send to ControlNet&amp;rdquo; to pass it directly for the next step.&lt;/p&gt;
&lt;h3 id="step-3-determine-the-art-style"&gt;Step 3: Determine the Art Style
&lt;/h3&gt;&lt;p&gt;The core of creating art with Stable Diffusion is choosing the right model + prompts. Before creating artistic QR codes, we recommend generating an image without ControlNet first to test the style.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Test Generation Result" class="gallery-image" data-flex-basis="506px" data-flex-grow="210" height="948" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image11.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image11_hu_67bbdab4025fc3f5.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image11_hu_c83f3b551e0924d7.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image11.jpg 2000w" width="2000"&gt;&lt;/p&gt;
&lt;p&gt;Generation parameters:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Prompt: mountain, green grassland, sky, cloud, bird, blue sky, no human, day, wide shot, flying, border, outdoors, white bird, scenery
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Negative prompt: easynegative
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 4078355702,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Face restoration: CodeFormer, Size: 512x512,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Model hash: 876b4c7ba5, Model: cetusMix_Whalefall2, Clip skip: 2, Version: v1.3.2
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3 id="step-4-import-qr-code-into-controlnet"&gt;Step 4: Import QR Code into ControlNet
&lt;/h3&gt;&lt;p&gt;After confirming the image style, upload the unprocessed QR code to ControlNet. Pay attention to these configuration options:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&amp;ldquo;Enable&amp;rdquo; button&lt;/strong&gt;: Check to ensure ControlNet is active during generation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Model selector&lt;/strong&gt;: Select &lt;code&gt;control_v1p_sd15_qrcode_monster&lt;/code&gt; to strengthen QR code control&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Control weight&lt;/strong&gt;: For the qrcode_monster model, we recommend setting it between 1.1–1.6&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Guidance start/end timing&lt;/strong&gt;: Start timing recommended between 0–0.1, end timing recommended at 1&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="ControlNet Configuration" class="gallery-image" data-flex-basis="240px" data-flex-grow="100" height="1997" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image12.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image12_hu_25f078fb5a3943d7.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image12_hu_f4879ef6b64e3a2e.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image12.jpg 2000w" width="2000"&gt;&lt;/p&gt;
&lt;p&gt;In the txt2img configuration, adjust two key values:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Sampling steps&lt;/strong&gt;: Recommended 30–50 (the default of 20 is insufficient for generating high-quality QR code images)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Width/Height&lt;/strong&gt;: Match the original QR code aspect ratio from ControlNet&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="txt2img Parameter Adjustment" class="gallery-image" data-flex-basis="251px" data-flex-grow="104" height="1909" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image13.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image13_hu_ed705bdb20a2cff1.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image13_hu_e4b0ba433aa581d9.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image13.jpg 2000w" width="2000"&gt;&lt;/p&gt;
&lt;p&gt;After all parameters are configured, click generate. Here we can see a good result — the QR code passes scanning tests on mobile phones.&lt;/p&gt;
&lt;p&gt;If the generated QR code doesn&amp;rsquo;t meet expectations, try fine-tuning the following parameters and increase the total batch count, iterating to approach the desired result:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Prompts&lt;/li&gt;
&lt;li&gt;Sampling method&lt;/li&gt;
&lt;li&gt;ControlNet control weight&lt;/li&gt;
&lt;li&gt;ControlNet guidance start/end timing&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Generation Result" class="gallery-image" data-flex-basis="259px" data-flex-grow="108" height="1849" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image14.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image14_hu_748dcd2b923a108f.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image14_hu_2ed397a762cf9367.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image14.jpg 2000w" width="2000"&gt;
&lt;img alt="Parameter Fine-tuning" class="gallery-image" data-flex-basis="418px" data-flex-grow="174" height="1146" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image15.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image15_hu_ad317a8ce958a578.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image15_hu_e79e5ee60f7554ba.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image15.jpg 2000w" width="2000"&gt;&lt;/p&gt;
&lt;p&gt;When necessary, use the X/Y/Z Plot in &amp;ldquo;Scripts&amp;rdquo; to compare QR code generation effects under different parameters. Here we compared ControlNet&amp;rsquo;s control weight and guidance start timing:&lt;/p&gt;
&lt;p&gt;&lt;img alt="X/Y/Z Plot Comparison" class="gallery-image" data-flex-basis="740px" data-flex-grow="308" height="648" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image16.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image16_hu_30abd11948c76d54.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image16_hu_ee7d7898db9411be.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image16.jpg 2000w" width="2000"&gt;&lt;/p&gt;
&lt;h2 id="enhancing-qr-codes-with-openpose"&gt;Enhancing QR Codes with OpenPose
&lt;/h2&gt;&lt;p&gt;Building on the QR code techniques above, we can take it further by combining &lt;strong&gt;OpenPose&lt;/strong&gt; with QR codes to create even more dynamic and artistic results. OpenPose generates skeleton maps of human poses, which serve as an additional ControlNet input to control character poses in the generated image.&lt;/p&gt;
&lt;h3 id="example-showcase"&gt;Example Showcase
&lt;/h3&gt;&lt;p&gt;Here are some example results combining OpenPose pose control with QR code generation:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th style="text-align: center"&gt;&lt;/th&gt;
 &lt;th style="text-align: center"&gt;&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td style="text-align: center"&gt;&lt;img alt="OpenPose + QR Code" class="gallery-image" data-flex-basis="400px" data-flex-grow="166" height="540" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/1-open-pose-code.png" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/1-open-pose-code_hu_1827644c6b490e79.png 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/1-open-pose-code.png 900w" width="900"&gt;&lt;/td&gt;
 &lt;td style="text-align: center"&gt;&lt;img alt="Girl with Car" class="gallery-image" data-flex-basis="401px" data-flex-grow="167" height="536" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/2-girl-with-car.png" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/2-girl-with-car_hu_d779f7fe9c1958a9.png 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/2-girl-with-car.png 896w" width="896"&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The prompt used for the example above:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;(school girl:1.6, walking), (a car, side view, to left), (residential area, cherry trees)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Negative prompt: (worst quality:1.6),(low quality:1.6), (inaccurate limb:1.2),bad composition, inaccurate eyes, easynegative
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Steps: 50, Sampler: Euler a, CFG scale: 7, Seed: 3408317133, Size: 900x540, Model: cetusMix_Whalefall2
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3 id="environment-setup-enable-multiple-controlnet-units"&gt;Environment Setup: Enable Multiple ControlNet Units
&lt;/h3&gt;&lt;p&gt;To use OpenPose and QR Code ControlNet simultaneously, you need at least &lt;strong&gt;2 ControlNet Units&lt;/strong&gt; enabled:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Go to &lt;strong&gt;Settings&lt;/strong&gt; → &lt;strong&gt;ControlNet&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Set the &lt;strong&gt;Multi ControlNet&lt;/strong&gt; unit count to &lt;strong&gt;2&lt;/strong&gt; (or more)&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Apply Settings&lt;/strong&gt;, then &lt;strong&gt;Reload UI&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img alt="Settings" class="gallery-image" data-flex-basis="753px" data-flex-grow="313" height="801" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/3-setting.png" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/3-setting_hu_b5e6a63896ebda6.png 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/3-setting_hu_8b6d41a6102ea411.png 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/3-setting_hu_ecc7edb38c7f398b.png 2400w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/3-setting.png 2514w" width="2514"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="Set Multi ControlNet Units" class="gallery-image" data-flex-basis="910px" data-flex-grow="379" height="855" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/4-set-multi-unit.png" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/4-set-multi-unit_hu_b5d1b2f4387cd450.png 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/4-set-multi-unit_hu_26545b7bcc39a476.png 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/4-set-multi-unit_hu_1bdf0964a8df41e6.png 2400w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/4-set-multi-unit.png 3245w" width="3245"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="Reload UI" class="gallery-image" data-flex-basis="534px" data-flex-grow="222" height="1486" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/5-reload-ui.png" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/5-reload-ui_hu_3b3ab1cb73bf46f3.png 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/5-reload-ui_hu_e849a8fb6bba1f41.png 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/5-reload-ui_hu_f3f3f518f7878675.png 2400w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/5-reload-ui.png 3310w" width="3310"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="Verify Result" class="gallery-image" data-flex-basis="376px" data-flex-grow="156" height="1030" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/6-check-result.png" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/6-check-result_hu_1ab84bcffc2484d0.png 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/6-check-result_hu_85b1c58013b0518d.png 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/6-check-result.png 1616w" width="1616"&gt;&lt;/p&gt;
&lt;h3 id="import-skeleton-data-and-enable-openpose"&gt;Import Skeleton Data and Enable OpenPose
&lt;/h3&gt;&lt;ol&gt;
&lt;li&gt;Open the &lt;strong&gt;OpenPose Editor&lt;/strong&gt; extension&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Load JSON&lt;/strong&gt; to import your prepared skeleton pose data&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Send to txt2img&lt;/strong&gt; to transfer the pose to ControlNet&lt;/li&gt;
&lt;li&gt;In ControlNet &lt;strong&gt;Unit 0&lt;/strong&gt;, enable ControlNet and select the model &lt;strong&gt;control_v11p_sd15_openpose&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img alt="Import Pose Data" class="gallery-image" data-flex-basis="547px" data-flex-grow="227" height="1448" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/7-import-pose.png" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/7-import-pose_hu_b2a0db80c07e8fdd.png 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/7-import-pose_hu_536c91b4faef4e8e.png 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/7-import-pose_hu_e0f0e9ff4141a491.png 2400w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/7-import-pose.png 3301w" width="3301"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="Enable OpenPose ControlNet" class="gallery-image" data-flex-basis="220px" data-flex-grow="91" height="1735" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/8-enable-openpose.png" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/8-enable-openpose_hu_f0fb744ed7c07583.png 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/8-enable-openpose.png 1595w" width="1595"&gt;&lt;/p&gt;
&lt;h3 id="import-qr-code-and-configure-generation"&gt;Import QR Code and Configure Generation
&lt;/h3&gt;&lt;ol&gt;
&lt;li&gt;Switch to &lt;strong&gt;ControlNet Unit 1&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Upload your QR code image&lt;/li&gt;
&lt;li&gt;Select the model &lt;strong&gt;control_v1p_sd15_qrcode_monster&lt;/strong&gt; with a &lt;strong&gt;Control Weight of 1.7&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Set &lt;strong&gt;Sampling Steps&lt;/strong&gt; to 30–50 for better quality&lt;/li&gt;
&lt;li&gt;Adjust &lt;strong&gt;Width/Height&lt;/strong&gt; to match your desired output resolution&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img alt="Enable QR Code Monster" class="gallery-image" data-flex-basis="233px" data-flex-grow="97" height="1716" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/9-enable-qr-monster.png" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/9-enable-qr-monster_hu_48432b44047ff4a8.png 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/9-enable-qr-monster_hu_5ed8c8f73f0276dc.png 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/9-enable-qr-monster.png 1668w" width="1668"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="Set Width and Height" class="gallery-image" data-flex-basis="228px" data-flex-grow="95" height="1737" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/10-set-hw.png" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/10-set-hw_hu_e0393aac628b989a.png 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/10-set-hw_hu_f56c4be88d52317e.png 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/10-set-hw.png 1656w" width="1656"&gt;&lt;/p&gt;
&lt;h3 id="prompts-and-generation"&gt;Prompts and Generation
&lt;/h3&gt;&lt;p&gt;Set your prompts for the final generation:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Positive prompt:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;(1girl:1.6, side lying sleep, on the garden, sunflowers), wooden floor
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Negative prompt:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;extra hands, extra fingers, extra legs, fewer fingers, (low quality, worst quality:1.4), (bad anatomy), (inaccurate limb:1.2),bad composition, inaccurate eyes, extra digit,fewer digits,(extra arms:1.2), signature, easynegative
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;img alt="Set Prompts" class="gallery-image" data-flex-basis="1108px" data-flex-grow="461" height="719" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/11-set-prompt.png" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/11-set-prompt_hu_140869ed3fe0cac5.png 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/11-set-prompt_hu_811ca3a0bd1bbe33.png 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/11-set-prompt_hu_c793fa54f7ad3c77.png 2400w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/11-set-prompt.png 3320w" width="3320"&gt;&lt;/p&gt;
&lt;h3 id="results"&gt;Results
&lt;/h3&gt;&lt;p&gt;Here is the final generated result combining OpenPose and QR code:&lt;/p&gt;
&lt;p&gt;&lt;img alt="OpenPose + QR Code Result" class="gallery-image" data-flex-basis="453px" data-flex-grow="189" height="1739" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/12-openpose-qrcode.png" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/12-openpose-qrcode_hu_1dda73f82d03b7f1.png 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/12-openpose-qrcode_hu_559208d515c67a7d.png 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/12-openpose-qrcode_hu_a9a88a4430cb2aa3.png 2400w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/12-openpose-qrcode.png 3288w" width="3288"&gt;&lt;/p&gt;
&lt;p&gt;By cleverly offsetting the QR code position and applying composition techniques, the viewer&amp;rsquo;s focus is effectively shifted from the QR code to the character itself, creating a more natural and harmonious overall image.&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th style="text-align: center"&gt;&lt;img class="gallery-image" data-flex-basis="355px" data-flex-grow="148" height="675" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/13-lying-sleep-openpose.png" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/13-lying-sleep-openpose_hu_aea52875040ebca6.png 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/13-lying-sleep-openpose.png 1000w" width="1000"&gt;&lt;/th&gt;
 &lt;th style="text-align: center"&gt;&lt;img class="gallery-image" data-flex-basis="357px" data-flex-grow="148" height="672" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/14-openpose.png" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/14-openpose_hu_3e8ab8bf2aeed12d.png 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/14-openpose.png 1000w" width="1000"&gt;&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;h3 id="fine-tuning-tips"&gt;Fine-Tuning Tips
&lt;/h3&gt;&lt;p&gt;To achieve the best results when combining OpenPose with QR codes, consider adjusting:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Prompts&lt;/strong&gt;: Experiment with different scene descriptions and character poses&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sampling method&lt;/strong&gt;: Try different samplers (Euler a, DPM++ 2M Karras, etc.)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Skeleton structure and position&lt;/strong&gt;: Adjust the pose skeleton to avoid overlapping with QR code critical areas&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Control weight&lt;/strong&gt;: Balance between OpenPose (pose accuracy) and QR code (scannability) weights&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Guidance start/end timing&lt;/strong&gt;: Fine-tune when each ControlNet kicks in during the denoising process&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="appendix-controlnet-qrcode-model-selection"&gt;Appendix: ControlNet QRCode Model Selection
&lt;/h2&gt;&lt;p&gt;To date, &lt;strong&gt;QRCode Monster&lt;/strong&gt; is the model with the highest QR code control success rate and best image fusion effect. It can be downloaded from HuggingFace:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://huggingface.co/monster-labs/control_v1p_sd15_qrcode_monster" target="_blank" rel="noopener"
 &gt;QRCode Monster - HuggingFace&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Another option is &lt;strong&gt;QR Pattern v2.0&lt;/strong&gt;, best used with IoC Lab&amp;rsquo;s Brightness model as an auxiliary to improve local contrast. However, based on our testing, this model introduces more interference that may significantly alter the image style. Downloads:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://civitai.com/models/90940/controlnet-qr-pattern-qr-codes" target="_blank" rel="noopener"
 &gt;QR Pattern v2.0 - Civitai&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://huggingface.co/ioclab/ioc-controlnet" target="_blank" rel="noopener"
 &gt;IoC Lab ControlNet - HuggingFace&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="references"&gt;References
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://catalog.us-east-1.prod.workshops.aws/workshops/facdf921-2eea-4638-bc01-522e1eef3dc5" target="_blank" rel="noopener"
 &gt;AIGC Stable Diffusion Workshop&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://antfu.me/posts/ai-qrcode-101" target="_blank" rel="noopener"
 &gt;QR Toolkit by Anthony Fu&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://huggingface.co/monster-labs/control_v1p_sd15_qrcode_monster" target="_blank" rel="noopener"
 &gt;QRCode Monster - HuggingFace&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://huggingface.co/ioclab/ioc-controlnet" target="_blank" rel="noopener"
 &gt;IoC Lab ControlNet - HuggingFace&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;

 &lt;blockquote&gt;
 &lt;p&gt;Original post: &lt;a class="link" href="https://aws.amazon.com/cn/blogs/china/art-qr-code-generation-with-controlnet-ai-painting-solution-based-on-stable-diffusion/" target="_blank" rel="noopener"
 &gt;Art QR Code Generation with ControlNet - AI Painting Solution Based on Stable Diffusion&lt;/a&gt;&lt;/p&gt;

 &lt;/blockquote&gt;</description></item></channel></rss>