├── README.md └── Z_Image_Turbo_4bit_jupyter.ipynb /README.md: -------------------------------------------------------------------------------- 1 | # ipynb 2 | -------------------------------------------------------------------------------- /Z_Image_Turbo_4bit_jupyter.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "id": "view-in-github", 7 | "colab_type": "text" 8 | }, 9 | "source": [ 10 | "\"Open" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": null, 16 | "metadata": { 17 | "id": "em60PSjRMU3A" 18 | }, 19 | "outputs": [], 20 | "source": [ 21 | "# 单元格 1: 安装依赖\n", 22 | "!pip install -U git+https://github.com/huggingface/diffusers git+https://github.com/Disty0/sdnq\n", 23 | "!pip install flask pyngrok" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": null, 29 | "metadata": { 30 | "id": "A0fTNKI4MU3D" 31 | }, 32 | "outputs": [], 33 | "source": [ 34 | "# 单元格 2: 加载模型(需要几分钟)\n", 35 | "import torch\n", 36 | "import diffusers\n", 37 | "from sdnq import SDNQConfig\n", 38 | "from sdnq.loader import apply_sdnq_options_to_model\n", 39 | "\n", 40 | "pipe = diffusers.ZImagePipeline.from_pretrained(\n", 41 | " \"Disty0/Z-Image-Turbo-SDNQ-uint4-svd-r32\",\n", 42 | " torch_dtype=torch.float32,\n", 43 | " device_map=\"cuda\"\n", 44 | ")\n", 45 | "pipe.transformer = apply_sdnq_options_to_model(pipe.transformer, use_quantized_matmul=True)\n", 46 | "pipe.text_encoder = apply_sdnq_options_to_model(pipe.text_encoder, use_quantized_matmul=True)\n", 47 | "\n", 48 | "print(\"模型加载完成!\")" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "metadata": { 55 | "id": "Q6nCR1UUMU3D" 56 | }, 57 | "outputs": [], 58 | "source": [ 59 | "# 单元格 3: 启动 API 服务\n", 60 | "# 运行后会显示一个 Gradio 地址,复制到本地脚本中使用\n", 61 | "import io\n", 62 | "import base64\n", 63 | "import random\n", 64 | "import gradio as gr\n", 65 | "import torch\n", 66 | "import PIL.Image\n", 67 | "\n", 68 | "# 图像生成函数,适配 Gradio 接口\n", 69 | "def sdnq_image_generator(prompt: str, width: int, height: int, seed: int) -> PIL.Image.Image:\n", 70 | " print(f\"收到请求: {prompt[:50]}...\")\n", 71 | "\n", 72 | " # 生成图片\n", 73 | " image = pipe(\n", 74 | " prompt=prompt,\n", 75 | " height=height,\n", 76 | " width=width,\n", 77 | " num_inference_steps=9,\n", 78 | " guidance_scale=0.0,\n", 79 | " generator=torch.manual_seed(seed),\n", 80 | " ).images[0]\n", 81 | "\n", 82 | " print(\"图片生成成功!\")\n", 83 | " return image\n", 84 | "\n", 85 | "# 创建 Gradio 接口\n", 86 | "iface = gr.Interface(\n", 87 | " fn=sdnq_image_generator,\n", 88 | " inputs=[\n", 89 | " gr.Textbox(label=\"Prompt\", placeholder=\"A vibrant, fantastical landscape...\"),\n", 90 | " gr.Slider(minimum=256, maximum=1024, step=64, value=1024, label=\"Width\"),\n", 91 | " gr.Slider(minimum=256, maximum=1024, step=64, value=1024, label=\"Height\"),\n", 92 | " gr.Number(label=\"Seed\", value=random.randint(0, 999999), precision=0)\n", 93 | " ],\n", 94 | " outputs=gr.Image(label=\"Generated Image\", type=\"pil\"),\n", 95 | " title=\"Z-Image-Turbo-SDNQ-uint4-svd-r32 Image Generator\",\n", 96 | " description=\"Generate high-quality images with Z-Image-Turbo-SDNQ-uint4-svd-r32 model.\"\n", 97 | ")\n", 98 | "\n", 99 | "# 启动 Gradio 接口,并使用 share=True 创建公共链接\n", 100 | "# Gradio 会自动处理外部访问,无需 ngrok\n", 101 | "print(\"正在启动 Gradio 界面...\")\n", 102 | "iface.launch(share=True, debug=True)\n", 103 | "print(\"Gradio 界面已启动!请查看上面的公共链接。\")" 104 | ] 105 | } 106 | ], 107 | "metadata": { 108 | "accelerator": "GPU", 109 | "colab": { 110 | "gpuType": "T4", 111 | "provenance": [], 112 | "include_colab_link": true 113 | }, 114 | "kernelspec": { 115 | "display_name": "Python 3", 116 | "name": "python3" 117 | }, 118 | "language_info": { 119 | "name": "python" 120 | } 121 | }, 122 | "nbformat": 4, 123 | "nbformat_minor": 0 124 | } --------------------------------------------------------------------------------