new base
Browse filesFiles changed (37)
- .gitignore+3-1
- app.pyAdded+14
- app_init.pyAdded+145
- build-run.shAdded+12
- config.pyAdded+58
- device.pyAdded+12
- frontend/.eslintignoreAdded+13
- frontend/.eslintrc.cjsAdded+30
- frontend/.gitignoreAdded+10
- frontend/.npmrcAdded+1
- frontend/.prettierignoreAdded+13
- frontend/.prettierrcAdded+19
- frontend/README.mdAdded+38
- frontend/package-lock.jsonAdded
- frontend/package.jsonAdded+36
- frontend/postcss.config.jsAdded+6
- frontend/src/app.cssAdded+3
- frontend/src/app.d.tsAdded+12
- frontend/src/app.htmlAdded+12
- frontend/src/lib/index.tsAdded+1
- frontend/src/lib/types.tsAdded
- frontend/src/routes/+layout.svelteAdded+5
- frontend/src/routes/+page.svelteAdded+160
- frontend/src/routes/+page.tsAdded+1
- frontend/static/favicon.pngAdded
- frontend/svelte.config.jsAdded+19
- frontend/tailwind.config.jsAdded+8
- frontend/tsconfig.jsonAdded+17
- frontend/vite.config.tsAdded+6
- pipelines/__init__.pyAdded
- pipelines/controlnet.pyAdded+90
- pipelines/txt2img.pyAdded+85
- pipelines/txt2imglora.pyAdded+93
- requirements.txt+2-2
- run.pyAdded+5
- user_queue.pyAdded+18
- util.pyAdded+16
12__pycache__/venv/1234__pycache__/venv/public/*.pem
1234567891011121314from fastapi import FastAPI
from config import argsfrom device import device, torch_dtypefrom app_init import init_appfrom user_queue import user_queue_mapfrom util import get_pipeline_class
app = FastAPI()
pipeline_class = get_pipeline_class(args.pipeline)pipeline = pipeline_class(args, device, torch_dtype)init_app(app, user_queue_map, args, pipeline)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145from fastapi import FastAPI, WebSocket, HTTPException, WebSocketDisconnectfrom fastapi.responses import StreamingResponse, JSONResponsefrom fastapi.middleware.cors import CORSMiddlewarefrom fastapi.staticfiles import StaticFiles
import loggingimport tracebackfrom config import Argsfrom user_queue import UserQueueDictimport uuidimport asyncioimport timefrom PIL import Imageimport io
def init_app(app: FastAPI, user_queue_map: UserQueueDict, args: Args, pipeline): app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) print("Init app", app)
@app.websocket("/ws") async def websocket_endpoint(websocket: WebSocket): await websocket.accept() if args.max_queue_size > 0 and len(user_queue_map) >= args.max_queue_size: print("Server is full") await websocket.send_json({"status": "error", "message": "Server is full"}) await websocket.close() return
try: uid = uuid.uuid4() print(f"New user connected: {uid}") await websocket.send_json( {"status": "success", "message": "Connected", "userId": uid} ) user_queue_map[uid] = {"queue": asyncio.Queue()} await websocket.send_json( {"status": "start", "message": "Start Streaming", "userId": uid} ) await handle_websocket_data(websocket, uid) except WebSocketDisconnect as e: logging.error(f"WebSocket Error: {e}, {uid}") traceback.print_exc() finally: print(f"User disconnected: {uid}") queue_value = user_queue_map.pop(uid, None) queue = queue_value.get("queue", None) if queue: while not queue.empty(): try: queue.get_nowait() except asyncio.QueueEmpty: continue
@app.get("/queue_size") async def get_queue_size(): queue_size = len(user_queue_map) return JSONResponse({"queue_size": queue_size})
@app.get("/stream/{user_id}") async def stream(user_id: uuid.UUID): uid = user_id try: user_queue = user_queue_map[uid] queue = user_queue["queue"]
async def generate(): last_prompt: str = None while True: data = await queue.get() input_image = data["image"] params = data["params"] if input_image is None: continue
image = pipeline.predict( input_image, params, ) if image is None: continue frame_data = io.BytesIO() image.save(frame_data, format="JPEG") frame_data = frame_data.getvalue() if frame_data is not None and len(frame_data) > 0: yield b"--frame\r\nContent-Type: image/jpeg\r\n\r\n" + frame_data + b"\r\n"
await asyncio.sleep(1.0 / 120.0)
return StreamingResponse( generate(), media_type="multipart/x-mixed-replace;boundary=frame" ) except Exception as e: logging.error(f"Streaming Error: {e}, {user_queue_map}") traceback.print_exc() return HTTPException(status_code=404, detail="User not found")
async def handle_websocket_data(websocket: WebSocket, user_id: uuid.UUID): uid = user_id user_queue = user_queue_map[uid] queue = user_queue["queue"] if not queue: return HTTPException(status_code=404, detail="User not found") last_time = time.time() try: while True: data = await websocket.receive_bytes() params = await websocket.receive_json() params = pipeline.InputParams(**params) pil_image = Image.open(io.BytesIO(data))
while not queue.empty(): try: queue.get_nowait() except asyncio.QueueEmpty: continue await queue.put({"image": pil_image, "params": params}) if args.timeout > 0 and time.time() - last_time > args.timeout: await websocket.send_json( { "status": "timeout", "message": "Your session has ended", "userId": uid, } ) await websocket.close() return
except Exception as e: logging.error(f"Error: {e}") traceback.print_exc()
# route to setup frontend @app.get("/settings") async def settings(): params = pipeline.InputParams() return JSONResponse({"settings": params.dict()})
app.mount("/", StaticFiles(directory="public", html=True), name="public")123456789101112#!/bin/bashcd frontendnpm installnpm run buildif [ $? -eq 0 ]; then echo -e "\033[1;32m\nfrontend build success \033[0m"else echo -e "\033[1;31m\nfrontend build failed\n\033[0m" >&2 exit 1ficd ../python run.py --reload
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758from typing import NamedTupleimport argparseimport os
class Args(NamedTuple): host: str port: int reload: bool mode: str max_queue_size: int timeout: float safety_checker: bool torch_compile: bool use_taesd: bool pipeline: str
MAX_QUEUE_SIZE = int(os.environ.get("MAX_QUEUE_SIZE", 0))TIMEOUT = float(os.environ.get("TIMEOUT", 0))SAFETY_CHECKER = os.environ.get("SAFETY_CHECKER", None) == "True"TORCH_COMPILE = os.environ.get("TORCH_COMPILE", None) == "True"USE_TAESD = os.environ.get("USE_TAESD", None) == "True"default_host = os.getenv("HOST", "0.0.0.0")default_port = int(os.getenv("PORT", "7860"))default_mode = os.getenv("MODE", "default")
parser = argparse.ArgumentParser(description="Run the app")parser.add_argument("--host", type=str, default=default_host, help="Host address")parser.add_argument("--port", type=int, default=default_port, help="Port number")parser.add_argument("--reload", action="store_true", help="Reload code on change")parser.add_argument( "--mode", type=str, default=default_mode, help="App Inferece Mode: txt2img, img2img")parser.add_argument( "--max_queue_size", type=int, default=MAX_QUEUE_SIZE, help="Max Queue Size")parser.add_argument("--timeout", type=float, default=TIMEOUT, help="Timeout")parser.add_argument( "--safety_checker", type=bool, default=SAFETY_CHECKER, help="Safety Checker")parser.add_argument( "--torch_compile", type=bool, default=TORCH_COMPILE, help="Torch Compile")parser.add_argument( "--use_taesd", type=bool, default=USE_TAESD, help="Use Tiny Autoencoder",)parser.add_argument( "--pipeline", type=str, default="txt2img", help="Pipeline to use",)
args = Args(**vars(parser.parse_args()))123456789101112import torch
# check if MPS is available OSX only M1/M2/M3 chipsmps_available = hasattr(torch.backends, "mps") and torch.backends.mps.is_available()xpu_available = hasattr(torch, "xpu") and torch.xpu.is_available()device = torch.device( "cuda" if torch.cuda.is_available() else "xpu" if xpu_available else "cpu")torch_dtype = torch.float16if mps_available: device = torch.device("mps") torch_dtype = torch.float3212345678910111213.DS_Storenode_modules/build/.svelte-kit/package.env.env.*!.env.example
# Ignore files for PNPM, NPM and YARNpnpm-lock.yamlpackage-lock.jsonyarn.lock123456789101112131415161718192021222324252627282930module.exports = { root: true, extends: [ 'eslint:recommended', 'plugin:@typescript-eslint/recommended', 'plugin:svelte/recommended', 'prettier' ], parser: '@typescript-eslint/parser', plugins: ['@typescript-eslint'], parserOptions: { sourceType: 'module', ecmaVersion: 2020, extraFileExtensions: ['.svelte'] }, env: { browser: true, es2017: true, node: true }, overrides: [ { files: ['*.svelte'], parser: 'svelte-eslint-parser', parserOptions: { parser: '@typescript-eslint/parser' } } ]};12345678910.DS_Storenode_modules/build/.svelte-kit/package.env.env.*!.env.examplevite.config.js.timestamp-*vite.config.ts.timestamp-*1engine-strict=true12345678910111213.DS_Storenode_modules/build/.svelte-kit/package.env.env.*!.env.example
# Ignore files for PNPM, NPM and YARNpnpm-lock.yamlpackage-lock.jsonyarn.lock12345678910111213141516171819{ "useTabs": false, "singleQuote": true, "trailingComma": "none", "printWidth": 100, "plugins": [ "prettier-plugin-svelte", "prettier-plugin-organize-imports", "prettier-plugin-tailwindcss" ], "overrides": [ { "files": "*.svelte", "options": { "parser": "svelte" } } ]}1234567891011121314151617181920212223242526272829303132333435363738# create-svelte
Everything you need to build a Svelte project, powered by [`create-svelte`](https://github.com/sveltejs/kit/tree/master/packages/create-svelte).
## Creating a project
If you're seeing this, you've probably already done this step. Congrats!
```bash# create a new project in the current directorynpm create svelte@latest
# create a new project in my-appnpm create svelte@latest my-app```
## Developing
Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:
```bashnpm run dev
# or start the server and open the app in a new browser tabnpm run dev -- --open```
## Building
To create a production version of your app:
```bashnpm run build```
You can preview the production build with `npm run preview`.
> To deploy your app, you may need to install an [adapter](https://kit.svelte.dev/docs/adapters) for your target environment.123456789101112131415161718192021222324252627282930313233343536{ "name": "frontend", "version": "0.0.1", "private": true, "scripts": { "dev": "vite dev", "build": "vite build", "preview": "vite preview", "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json", "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch", "lint": "prettier --check . && eslint .", "format": "prettier --write ." }, "devDependencies": { "@sveltejs/adapter-auto": "^2.0.0", "@sveltejs/kit": "^1.20.4", "@typescript-eslint/eslint-plugin": "^6.0.0", "@typescript-eslint/parser": "^6.0.0", "autoprefixer": "^10.4.16", "eslint": "^8.28.0", "eslint-config-prettier": "^9.0.0", "eslint-plugin-svelte": "^2.30.0", "postcss": "^8.4.31", "prettier": "^3.1.0", "prettier-plugin-organize-imports": "^3.2.4", "prettier-plugin-svelte": "^3.1.0", "prettier-plugin-tailwindcss": "^0.5.7", "svelte": "^4.0.5", "svelte-check": "^3.4.3", "tailwindcss": "^3.3.5", "tslib": "^2.4.1", "typescript": "^5.0.0", "vite": "^4.4.2" }, "type": "module"}123456export default { plugins: { tailwindcss: {}, autoprefixer: {} }};123@tailwind base;@tailwind components;@tailwind utilities;123456789101112// See https://kit.svelte.dev/docs/types#app// for information about these interfacesdeclare global { namespace App { // interface Error {} // interface Locals {} // interface PageData {} // interface Platform {} }}
export {};123456789101112<!doctype html><html lang="en"> <head> <meta charset="utf-8" /> <link rel="icon" href="%sveltekit.assets%/favicon.png" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> %sveltekit.head% </head> <body data-sveltekit-preload-data="hover"> <div style="display: contents">%sveltekit.body%</div> </body></html>1// place files you want to import through the `$lib` alias in this folder.12345<script> import '../app.css';</script>
<slot />123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160<script lang="ts"> import { onMount } from 'svelte'; import { PUBLIC_BASE_URL } from '$env/static/public';
onMount(() => { getSettings(); }); async function getSettings() { const settings = await fetch(`${PUBLIC_BASE_URL}/settings`).then((r) => r.json()); console.log(settings); }</script>
<div class="fixed right-2 top-2 max-w-xs rounded-lg p-4 text-center text-sm font-bold" id="error" /><main class="container mx-auto flex max-w-4xl flex-col gap-4 px-4 py-4"> <article class="mx-auto max-w-xl text-center"> <h1 class="text-3xl font-bold">Real-Time Latent Consistency Model</h1> <h2 class="mb-4 text-2xl font-bold">Image to Image</h2> <p class="text-sm"> This demo showcases <a href="https://huggingface.co/SimianLuo/LCM_Dreamshaper_v7" target="_blank" class="text-blue-500 underline hover:no-underline">LCM</a > Image to Image pipeline using <a href="https://github.com/huggingface/diffusers/tree/main/examples/community#latent-consistency-pipeline" target="_blank" class="text-blue-500 underline hover:no-underline">Diffusers</a > with a MJPEG stream server. </p> <p class="text-sm"> There are <span id="queue_size" class="font-bold">0</span> user(s) sharing the same GPU, affecting real-time performance. Maximum queue size is 4. <a href="https://huggingface.co/spaces/radames/Real-Time-Latent-Consistency-Model?duplicate=true" target="_blank" class="text-blue-500 underline hover:no-underline">Duplicate</a > and run it on your own GPU. </p> </article> <div> <h2 class="font-medium">Prompt</h2> <p class="text-sm text-gray-500"> Change the prompt to generate different images, accepts <a href="https://github.com/damian0815/compel/blob/main/doc/syntax.md" target="_blank" class="text-blue-500 underline hover:no-underline">Compel</a > syntax. </p> <div class="text-normal flex items-center rounded-md border border-gray-700 px-1 py-1"> <textarea type="text" id="prompt" class="mx-1 w-full px-3 py-2 font-light outline-none dark:text-black" title="Prompt, this is an example, feel free to modify" placeholder="Add your prompt here..." >Portrait of The Terminator with , glare pose, detailed, intricate, full of colour, cinematic lighting, trending on artstation, 8k, hyperrealistic, focused, extreme details, unreal engine 5, cinematic, masterpiece</textarea > </div> </div> <div class=""> <details> <summary class="cursor-pointer font-medium">Advanced Options</summary> <div class="grid max-w-md grid-cols-3 items-center gap-3 py-3"> <label class="text-sm font-medium" for="guidance-scale">Guidance Scale </label> <input type="range" id="guidance-scale" name="guidance-scale" min="1" max="30" step="0.001" value="8.0" oninput="this.nextElementSibling.value = Number(this.value).toFixed(2)" /> <output class="w-[50px] rounded-md border border-gray-700 px-1 py-1 text-center text-xs font-light" > 8.0</output > <label class="text-sm font-medium" for="strength">Strength</label> <input type="range" id="strength" name="strength" min="0.20" max="1" step="0.001" value="0.50" oninput="this.nextElementSibling.value = Number(this.value).toFixed(2)" /> <output class="w-[50px] rounded-md border border-gray-700 px-1 py-1 text-center text-xs font-light" > 0.5</output > <label class="text-sm font-medium" for="seed">Seed</label> <input type="number" id="seed" name="seed" value="299792458" class="rounded-md border border-gray-700 p-2 text-right font-light dark:text-black" /> <button onclick="document.querySelector('#seed').value = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)" class="button" > Rand </button> </div> </details> </div> <div class="flex gap-3"> <button id="start" class="button"> Start </button> <button id="stop" class="button"> Stop </button> <button id="snap" disabled class="button ml-auto"> Snapshot </button> </div> <div class="relative overflow-hidden rounded-lg border border-slate-300"> <img id="player" class="aspect-square w-full rounded-lg" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=" /> <div class="absolute left-0 top-0 aspect-square w-1/4"> <video id="webcam" class="relative z-10 aspect-square w-full object-cover" playsinline autoplay muted loop /> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 448" width="100" class="absolute top-0 z-0 w-full p-4 opacity-20" > <path fill="currentColor" d="M224 256a128 128 0 1 0 0-256 128 128 0 1 0 0 256zm-45.7 48A178.3 178.3 0 0 0 0 482.3 29.7 29.7 0 0 0 29.7 512h388.6a29.7 29.7 0 0 0 29.7-29.7c0-98.5-79.8-178.3-178.3-178.3h-91.4z" /> </svg> </div> </div></main>
<style lang="postcss"> :global(html) { @apply text-black dark:bg-gray-900 dark:text-white; } .button { @apply rounded bg-gray-700 p-2 font-normal text-white hover:bg-gray-800 disabled:cursor-not-allowed disabled:bg-gray-300 dark:disabled:bg-gray-700 dark:disabled:text-black; }</style>1export const prerender = true12345678910111213141516171819import adapter from '@sveltejs/adapter-static';import { vitePreprocess } from '@sveltejs/kit/vite';
/** @type {import('@sveltejs/kit').Config} */const config = { preprocess: vitePreprocess(),
kit: { adapter: adapter({ pages: '../public', assets: '../public', fallback: undefined, precompress: false, strict: true }) }};
export default config;12345678/** @type {import('tailwindcss').Config} */export default { content: ['./src/**/*.{html,js,svelte,ts}'], theme: { extend: {} }, plugins: []};1234567891011121314151617{ "extends": "./.svelte-kit/tsconfig.json", "compilerOptions": { "allowJs": true, "checkJs": true, "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "resolveJsonModule": true, "skipLibCheck": true, "sourceMap": true, "strict": true } // Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias // // If you want to overwrite includes/excludes, make sure to copy over the relevant includes/excludes // from the referenced tsconfig.json - TypeScript does not merge them in}123456import { sveltekit } from '@sveltejs/kit/vite';import { defineConfig } from 'vite';
export default defineConfig({ plugins: [sveltekit()]});123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990from diffusers import DiffusionPipeline, AutoencoderTinyfrom latent_consistency_controlnet import LatentConsistencyModelPipeline_controlnet
from compel import Compelimport torch
try: import intel_extension_for_pytorch as ipex # type: ignoreexcept: pass
import psutilfrom config import Argsfrom pydantic import BaseModelfrom PIL import Imagefrom typing import Callable
base_model = "SimianLuo/LCM_Dreamshaper_v7"WIDTH = 512HEIGHT = 512
class Pipeline: class InputParams(BaseModel): seed: int = 2159232 prompt: str guidance_scale: float = 8.0 strength: float = 0.5 steps: int = 4 lcm_steps: int = 50 width: int = WIDTH height: int = HEIGHT
@staticmethod def create_pipeline( args: Args, device: torch.device, torch_dtype: torch.dtype ) -> Callable[["Pipeline.InputParams"], Image.Image]: if args.safety_checker: pipe = DiffusionPipeline.from_pretrained(base_model) else: pipe = DiffusionPipeline.from_pretrained(base_model, safety_checker=None) if args.use_taesd: pipe.vae = AutoencoderTiny.from_pretrained( "madebyollin/taesd", torch_dtype=torch_dtype, use_safetensors=True )
pipe.set_progress_bar_config(disable=True) pipe.to(device=device, dtype=torch_dtype) pipe.unet.to(memory_format=torch.channels_last)
# check if computer has less than 64GB of RAM using sys or os if psutil.virtual_memory().total < 64 * 1024**3: pipe.enable_attention_slicing()
if args.torch_compile: pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True) pipe.vae = torch.compile(pipe.vae, mode="reduce-overhead", fullgraph=True)
pipe(prompt="warmup", num_inference_steps=1, guidance_scale=8.0)
compel_proc = Compel( tokenizer=pipe.tokenizer, text_encoder=pipe.text_encoder, truncate_long_prompts=False, )
def predict(params: "Pipeline.InputParams") -> Image.Image: generator = torch.manual_seed(params.seed) prompt_embeds = compel_proc(params.prompt) # Can be set to 1~50 steps. LCM support fast inference even <= 4 steps. Recommend: 1~8 steps. results = pipe( prompt_embeds=prompt_embeds, generator=generator, num_inference_steps=params.steps, guidance_scale=params.guidance_scale, width=params.width, height=params.height, original_inference_steps=params.lcm_steps, output_type="pil", ) nsfw_content_detected = ( results.nsfw_content_detected[0] if "nsfw_content_detected" in results else False ) if nsfw_content_detected: return None return results.images[0]
return predict12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485from diffusers import DiffusionPipeline, AutoencoderTinyfrom compel import Compelimport torch
try: import intel_extension_for_pytorch as ipex # type: ignoreexcept: pass
import psutilfrom config import Argsfrom pydantic import BaseModelfrom PIL import Imagefrom typing import Callable
base_model = "SimianLuo/LCM_Dreamshaper_v7"taesd_model = "madebyollin/taesd"
class Pipeline: class InputParams(BaseModel): seed: int = 2159232 prompt: str = "" guidance_scale: float = 8.0 strength: float = 0.5 steps: int = 4 width: int = 512 height: int = 512
def __init__(self, args: Args, device: torch.device, torch_dtype: torch.dtype): if args.safety_checker: self.pipe = DiffusionPipeline.from_pretrained(base_model) else: self.pipe = DiffusionPipeline.from_pretrained( base_model, safety_checker=None ) if args.use_taesd: self.pipe.vae = AutoencoderTiny.from_pretrained( taesd_model, torch_dtype=torch_dtype, use_safetensors=True )
self.pipe.set_progress_bar_config(disable=True) self.pipe.to(device=device, dtype=torch_dtype) self.pipe.unet.to(memory_format=torch.channels_last)
# check if computer has less than 64GB of RAM using sys or os if psutil.virtual_memory().total < 64 * 1024**3: self.pipe.enable_attention_slicing()
if args.torch_compile: self.pipe.unet = torch.compile( self.pipe.unet, mode="reduce-overhead", fullgraph=True ) self.pipe.vae = torch.compile( self.pipe.vae, mode="reduce-overhead", fullgraph=True )
self.pipe(prompt="warmup", num_inference_steps=1, guidance_scale=8.0)
self.compel_proc = Compel( tokenizer=self.pipe.tokenizer, text_encoder=self.pipe.text_encoder, truncate_long_prompts=False, )
def predict(self, params: "Pipeline.InputParams") -> Image.Image: generator = torch.manual_seed(params.seed) prompt_embeds = self.compel_proc(params.prompt) results = self.pipe( prompt_embeds=prompt_embeds, generator=generator, num_inference_steps=params.steps, guidance_scale=params.guidance_scale, width=params.width, height=params.height, output_type="pil", ) nsfw_content_detected = ( results.nsfw_content_detected[0] if "nsfw_content_detected" in results else False ) if nsfw_content_detected: return None return results.images[0]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293from diffusers import DiffusionPipeline, AutoencoderTinyfrom compel import Compelimport torch
try: import intel_extension_for_pytorch as ipex # type: ignoreexcept: pass
import psutilfrom config import Argsfrom pydantic import BaseModelfrom PIL import Imagefrom typing import Callable
base_model = "SimianLuo/LCM_Dreamshaper_v7"WIDTH = 512HEIGHT = 512
model_id = "wavymulder/Analog-Diffusion"lcm_lora_id = "latent-consistency/lcm-lora-sdv1-5"
class Pipeline: class InputParams(BaseModel): seed: int = 2159232 prompt: str guidance_scale: float = 8.0 strength: float = 0.5 steps: int = 4 lcm_steps: int = 50 width: int = WIDTH height: int = HEIGHT
@staticmethod def create_pipeline( args: Args, device: torch.device, torch_dtype: torch.dtype ) -> Callable[["Pipeline.InputParams"], Image.Image]: if args.safety_checker: pipe = DiffusionPipeline.from_pretrained(base_model) else: pipe = DiffusionPipeline.from_pretrained(base_model, safety_checker=None) if args.use_taesd: pipe.vae = AutoencoderTiny.from_pretrained( "madebyollin/taesd", torch_dtype=torch_dtype, use_safetensors=True )
pipe.set_progress_bar_config(disable=True) pipe.to(device=device, dtype=torch_dtype) pipe.unet.to(memory_format=torch.channels_last)
# Load LCM LoRA pipe.load_lora_weights(lcm_lora_id, adapter_name="lcm") # check if computer has less than 64GB of RAM using sys or os if psutil.virtual_memory().total < 64 * 1024**3: pipe.enable_attention_slicing()
if args.torch_compile: pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True) pipe.vae = torch.compile(pipe.vae, mode="reduce-overhead", fullgraph=True)
pipe(prompt="warmup", num_inference_steps=1, guidance_scale=8.0)
compel_proc = Compel( tokenizer=pipe.tokenizer, text_encoder=pipe.text_encoder, truncate_long_prompts=False, )
def predict(params: "Pipeline.InputParams") -> Image.Image: generator = torch.manual_seed(params.seed) prompt_embeds = compel_proc(params.prompt) # Can be set to 1~50 steps. LCM support fast inference even <= 4 steps. Recommend: 1~8 steps. results = pipe( prompt_embeds=prompt_embeds, generator=generator, num_inference_steps=params.steps, guidance_scale=params.guidance_scale, width=params.width, height=params.height, original_inference_steps=params.lcm_steps, output_type="pil", ) nsfw_content_detected = ( results.nsfw_content_detected[0] if "nsfw_content_detected" in results else False ) if nsfw_content_detected: return None return results.images[0]
return predict2 unmodified lines3456789102 unmodified linesgradio==3.50.2--extra-index-url https://download.pytorch.org/whl/cu121;torch==2.1.0fastapi==0.104.0uvicorn==0.23.2Pillow==10.1.0accelerate==0.24.0compel==2.0.22 unmodified lines3456789102 unmodified linesgradio==3.50.2--extra-index-url https://download.pytorch.org/whl/cu121;torch==2.1.0fastapi==0.104.1uvicorn==0.24.0.post1Pillow==10.1.0accelerate==0.24.0compel==2.0.2
12345if __name__ == "__main__": import uvicorn from config import args
uvicorn.run("app:app", host=args.host, port=args.port, reload=args.reload)123456789101112131415161718from typing import Dict, Unionfrom uuid import UUIDfrom asyncio import Queuefrom PIL import Imagefrom typing import Tuple, Unionfrom uuid import UUIDfrom asyncio import Queuefrom PIL import Image
UserId = UUID
InputParams = dict
QueueContent = Dict[str, Union[Image.Image, InputParams]]
UserQueueDict = Dict[UserId, Queue[QueueContent]]
user_queue_map: UserQueueDict = {}12345678910111213141516from importlib import import_modulefrom types import ModuleType
def get_pipeline_class(pipeline_name: str) -> ModuleType: try: module = import_module(f"pipelines.{pipeline_name}") except ModuleNotFoundError: raise ValueError(f"Pipeline {pipeline_name} module not found")
pipeline_class = getattr(module, "Pipeline", None)
if pipeline_class is None: raise ValueError(f"'Pipeline' class not found in module '{module_name}'.")
return pipeline_class.gitignore+3-1
app.pyAdded+14
app_init.pyAdded+145
build-run.shAdded+12
config.pyAdded+58
device.pyAdded+12
frontend/.eslintignoreAdded+13
frontend/.eslintrc.cjsAdded+30
frontend/.gitignoreAdded+10
frontend/.npmrcAdded+1
frontend/.prettierignoreAdded+13
frontend/.prettierrcAdded+19
frontend/README.mdAdded+38
frontend/package-lock.jsonAdded
frontend/package.jsonAdded+36
frontend/postcss.config.jsAdded+6
frontend/src/app.cssAdded+3
frontend/src/app.d.tsAdded+12
frontend/src/app.htmlAdded+12
frontend/src/lib/index.tsAdded+1
frontend/src/lib/types.tsAdded
frontend/src/routes/+layout.svelteAdded+5
frontend/src/routes/+page.svelteAdded+160
frontend/src/routes/+page.tsAdded+1
frontend/static/favicon.pngAdded
frontend/svelte.config.jsAdded+19
frontend/tailwind.config.jsAdded+8
frontend/tsconfig.jsonAdded+17
frontend/vite.config.tsAdded+6
pipelines/__init__.pyAdded
pipelines/controlnet.pyAdded+90
pipelines/txt2img.pyAdded+85
pipelines/txt2imglora.pyAdded+93
requirements.txt+2-2
run.pyAdded+5
user_queue.pyAdded+18
util.pyAdded+16