Add episode 41
Davidbae6443795e2
diff --git a/README.md b/README.mdindex c924e51..d86d50b 100644--- a/README.md+++ b/README.md@@ -35,6 +35,7 @@ In this repo, you can find the n8n templates we created for the episodes of [AI- [Episode 38: Create AI reaction videos with Veo 3.1 and n8n](episode_38)- [Episode 39: Create infographics with Nano Banana Pro in n8n](episode_39)- [Episode 40: Flux.2[dev] with n8n](episode_40)+- [Episode 41: FREE z-image-turbo with n8n](episode_41)## serversdiff --git a/episode_41/README.md b/episode_41/README.mdnew file mode 100644index 0000000..3bfeb0b--- /dev/null+++ b/episode_41/README.md@@ -0,0 +1,19 @@+# Episode 41: FREE z-image-turbo with n8n++## [📚 Join our Skool community for support, premium content and more!](https://www.skool.com/ai-agents-az/about)++### Get the premium versions of the workflows and the exclusive content - with the hosted GPU media server++## Free n8n JSON workflow++- [modal server for z-image-turbo](modal_z_image_fast.py)+- [n8n workflow to test z-image-turbo](n8n_test_z_image_turbo.json)++## Additional resources++- [Join n8n](https://n8n.partnerlinks.io/fenoo5ekqs1g)+-++## Watch the video++[](https://www.youtube.com/watch?v=R8D1uBDfR1Q)diff --git a/episode_41/modal_z_image_fast.py b/episode_41/modal_z_image_fast.pynew file mode 100644index 0000000..e8780f2--- /dev/null+++ b/episode_41/modal_z_image_fast.py@@ -0,0 +1,228 @@+from io import BytesIO+from pathlib import Path+from typing import Optional+import base64++import modal++image = (+ modal.Image.from_registry(+ "nvidia/cuda:12.8.1-devel-ubuntu22.04",+ add_python="3.11",+ )+ .entrypoint([])+ .apt_install("git")+ .pip_install("uv")+ .run_commands(+ "uv pip install --system --compile-bytecode flask torch==2.7.1 --extra-index-url https://download.pytorch.org/whl/cu128"+ )+ .run_commands(+ "uv pip install --system --compile-bytecode git+https://github.com/huggingface/diffusers git+https://github.com/Disty0/sdnq"+ )+)++MODEL_NAME = "Disty0/Z-Image-Turbo-SDNQ-uint4-svd-r32"++CACHE_DIR = Path("/cache")+cache_volume = modal.Volume.from_name("hf-hub-cache", create_if_missing=True)+volumes = {CACHE_DIR: cache_volume}++image = image.env(+ {+ "HF_HOME": str(CACHE_DIR),+ }+)++app = modal.App("z-image-turbo-fastapi")++with image.imports():+ import torch+ import os+ import diffusers+ from sdnq import SDNQConfig+ from sdnq.loader import apply_sdnq_options_to_model+ from flask import Flask, request, jsonify+++ image=image,+ gpu="L40s",+ volumes=volumes,+ scaledown_window=120,+ timeout=10 * 60,+)+class ZImageTurboModel:+ @modal.enter()+ def enter(self):+ print(f"Downloading {MODEL_NAME} and applying SDNQ optimizations...")++ # Enable TensorFloat32 for better performance on modern GPUs+ torch.set_float32_matmul_precision("high")++ self.device = "cuda"++ # Load the Z-Image pipeline with SDNQ quantization+ self.pipe = diffusers.ZImagePipeline.from_pretrained(+ MODEL_NAME,+ torch_dtype=torch.float32,+ device_map="cuda",+ cache_dir=CACHE_DIR,+ )++ # Apply SDNQ optimizations+ self.pipe.transformer = apply_sdnq_options_to_model(+ self.pipe.transformer, use_quantized_matmul=True+ )+ self.pipe.text_encoder = apply_sdnq_options_to_model(+ self.pipe.text_encoder, use_quantized_matmul=True+ )++ print("Model loaded successfully!")++ @modal.method()+ def inference(+ self,+ prompt: str,+ width: int = 1024,+ height: int = 1024,+ num_inference_steps: int = 9,+ guidance_scale: float = 0.0,+ seed: Optional[int] = None,+ ) -> bytes:+ import random++ def adjust_dimensions_preserve_aspect(w, h, divisor=16, min_size=256):+ """Adjust dimensions to be divisible by divisor while preserving aspect ratio."""+ aspect_ratio = w / h++ # Start from width, go up and down to find first valid pair+ for offset in range(0, max(w, 1000), divisor):+ for test_w in [w + offset, w - offset]:+ if test_w < min_size:+ continue+ if test_w % divisor != 0:+ continue++ # Calculate height from aspect ratio+ test_h = round(test_w / aspect_ratio)+ if test_h < min_size:+ continue+ if test_h % divisor == 0:+ return int(test_w), int(test_h)++ # Fallback: just round both+ return (w // divisor) * divisor, (h // divisor) * divisor++ # Adjust dimensions while preserving aspect ratio+ width, height = adjust_dimensions_preserve_aspect(width, height)++ # Use provided seed or generate random one+ if seed is None:+ seed = random.randint(0, 2**32 - 1)++ generator = torch.manual_seed(seed)++ print(f"Generating image with prompt: {prompt}")+ print(f"Dimensions: {width}x{height}")++ image = self.pipe(+ prompt=prompt,+ height=height,+ width=width,+ num_inference_steps=num_inference_steps,+ guidance_scale=guidance_scale,+ generator=generator,+ ).images[0]++ byte_stream = BytesIO()+ image.save(byte_stream, format="PNG")+ image_bytes = byte_stream.getvalue()++ return image_bytes++[email protected](image=image, volumes=volumes, cpu="0.5", memory="2GiB")[email protected]_app()+def flask_app():+ web_app = Flask(__name__)++ @web_app.route("/")+ def health_check():+ return jsonify({"status": "alive"})++ @web_app.route("/generate", methods=["POST"])+ def generate_image():+ """+ Generate an image using Z-Image Turbo SDNQ model.++ - **prompt**: Text description of the image you want to generate+ - **width**: Desired image width in pixels (default: 1024)+ - **height**: Desired image height in pixels (default: 1024)+ - **steps**: Number of inference steps (default: 9)+ - **guidance_scale**: Guidance scale (default: 0.0)+ - **seed**: Seed for reproducible results (default: 42)+ """+ data = request.get_json(force=True)+ prompt = data.get("prompt")+ width = data.get("width", 1024)+ height = data.get("height", 1024)+ steps = data.get("steps", 9)+ guidance_scale = data.get("guidance_scale", 0.0)+ seed = data.get("seed") # None if not provided, will use random++ if not prompt:+ return jsonify({"error": "'prompt' parameter is required"}), 400++ def adjust_dimensions_preserve_aspect(w, h, divisor=16, min_size=256):+ """Adjust dimensions to be divisible by divisor while preserving aspect ratio."""+ aspect_ratio = w / h++ # Start from width, go up and down to find first valid pair+ for offset in range(0, max(w, 1000), divisor):+ for test_w in [w + offset, w - offset]:+ if test_w < min_size:+ continue+ if test_w % divisor != 0:+ continue++ # Calculate height from aspect ratio+ test_h = round(test_w / aspect_ratio)+ if test_h < min_size:+ continue+ if test_h % divisor == 0:+ return int(test_w), int(test_h)++ # Fallback: just round both+ return (w // divisor) * divisor, (h // divisor) * divisor++ try:+ # Adjust dimensions while preserving aspect ratio+ adjusted_width, adjusted_height = adjust_dimensions_preserve_aspect(+ width, height+ )++ model = ZImageTurboModel()+ result_bytes = model.inference.remote(+ prompt=prompt,+ width=adjusted_width,+ height=adjusted_height,+ num_inference_steps=steps,+ guidance_scale=guidance_scale,+ seed=seed,+ )++ # Encode as base64+ img_base64 = base64.b64encode(result_bytes).decode("utf-8")++ return jsonify(+ {+ "image": img_base64,+ "format": "png",+ }+ )++ except Exception as e:+ print(f"Error: {e}")+ return jsonify({"error": str(e)}), 500++ return web_appdiff --git a/episode_41/n8n_test_z_image_turbo.json b/episode_41/n8n_test_z_image_turbo.jsonnew file mode 100644index 0000000..d358618--- /dev/null+++ b/episode_41/n8n_test_z_image_turbo.json@@ -0,0 +1,104 @@+{+ "name": "Test z-image-turbo server",+ "nodes": [+ {+ "parameters": {},+ "type": "n8n-nodes-base.manualTrigger",+ "typeVersion": 1,+ "position": [+ -160,+ 0+ ],+ "id": "66b96ebe-9002-4ce0-9c49-0787813d0e8a",+ "name": "When clicking ‘Execute workflow’"+ },+ {+ "parameters": {+ "operation": "toBinary",+ "sourceProperty": "image",+ "options": {}+ },+ "type": "n8n-nodes-base.convertToFile",+ "typeVersion": 1.1,+ "position": [+ 288,+ 0+ ],+ "id": "b5fd232a-d4a9-4518-bfd6-c7823d61c5ba",+ "name": "Convert to File"+ },+ {+ "parameters": {+ "content": "# 📚 [Join our Skool community for video automations, support, premium content and more!](https://www.skool.com/ai-agents-az/about)\n\n## premium workflows\n## hosted GPU media server\n## voice cloning\n\n## Long-form video tools like \n### LoFi music video creator\n### Sleep video creator",+ "height": 452,+ "width": 636,+ "color": 6+ },+ "type": "n8n-nodes-base.stickyNote",+ "typeVersion": 1,+ "position": [+ -144,+ -528+ ],+ "id": "1b25b497-a1de-4f6a-a96a-1ee51716e1f0",+ "name": "Sticky Note17"+ },+ {+ "parameters": {+ "method": "POST",+ "url": "<<insert your modal.com url here>>/generate",+ "sendBody": true,+ "specifyBody": "json",+ "jsonBody": "={{ JSON.stringify({\n \"prompt\": \"a pirate cat\",\n \"width\": 1024,\n \"height\": 1024,\n \"steps\": 9\n}, null, 2) }}",+ "options": {+ "redirect": {+ "redirect": {}+ }+ }+ },+ "type": "n8n-nodes-base.httpRequest",+ "typeVersion": 4.3,+ "position": [+ 64,+ 0+ ],+ "id": "7991386d-7039-4e29-bd2e-9ec9a69e5437",+ "name": "Generate image"+ }+ ],+ "pinData": {},+ "connections": {+ "When clicking ‘Execute workflow’": {+ "main": [+ [+ {+ "node": "Generate image",+ "type": "main",+ "index": 0+ }+ ]+ ]+ },+ "Generate image": {+ "main": [+ [+ {+ "node": "Convert to File",+ "type": "main",+ "index": 0+ }+ ]+ ]+ }+ },+ "active": false,+ "settings": {+ "executionOrder": "v1"+ },+ "versionId": "f13c6dce-0c67-4d91-bb1d-f8add6ad5d1d",+ "meta": {+ "instanceId": "636db6b6ab21a21c6458e2137f340e33dc3e49f5a1c560c8d1e2372c227cf40e"+ },+ "id": "gBhuFNz3Ar84mpgf",+ "tags": []+}\ No newline at end of file