camenduru commited on
Commit
27c94f8
·
verified ·
1 Parent(s): a3e0673

Upload app.ipynb

Browse files
Files changed (1) hide show
  1. app.ipynb +106 -0
app.ipynb ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": null,
6
+ "id": "c00cc11f-9150-4f82-85a4-08142e9ad14f",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "%cd /content/ComfyUI\n",
11
+ "\n",
12
+ "import random\n",
13
+ "import torch\n",
14
+ "import numpy as np\n",
15
+ "from PIL import Image\n",
16
+ "import nodes\n",
17
+ "from nodes import NODE_CLASS_MAPPINGS\n",
18
+ "from comfy_extras import nodes_custom_sampler\n",
19
+ "from comfy import model_management\n",
20
+ "\n",
21
+ "DualCLIPLoader = NODE_CLASS_MAPPINGS[\"DualCLIPLoader\"]()\n",
22
+ "UNETLoader = NODE_CLASS_MAPPINGS[\"UNETLoader\"]()\n",
23
+ "RandomNoise = nodes_custom_sampler.NODE_CLASS_MAPPINGS[\"RandomNoise\"]()\n",
24
+ "BasicGuider = nodes_custom_sampler.NODE_CLASS_MAPPINGS[\"BasicGuider\"]()\n",
25
+ "KSamplerSelect = nodes_custom_sampler.NODE_CLASS_MAPPINGS[\"KSamplerSelect\"]()\n",
26
+ "BasicScheduler = nodes_custom_sampler.NODE_CLASS_MAPPINGS[\"BasicScheduler\"]()\n",
27
+ "SamplerCustomAdvanced = nodes_custom_sampler.NODE_CLASS_MAPPINGS[\"SamplerCustomAdvanced\"]()\n",
28
+ "VAELoader = NODE_CLASS_MAPPINGS[\"VAELoader\"]()\n",
29
+ "VAEDecode = NODE_CLASS_MAPPINGS[\"VAEDecode\"]()\n",
30
+ "EmptyLatentImage = NODE_CLASS_MAPPINGS[\"EmptyLatentImage\"]()\n",
31
+ "\n",
32
+ "with torch.inference_mode():\n",
33
+ " clip = DualCLIPLoader.load_clip(\"t5xxl_fp8_e4m3fn.safetensors\", \"clip_l.safetensors\", \"flux\")[0]\n",
34
+ " unet = UNETLoader.load_unet(\"flux1-dev-fp8.safetensors\", \"fp8_e4m3fn\")[0]\n",
35
+ " vae = VAELoader.load_vae(\"ae.sft\")[0]\n",
36
+ "\n",
37
+ "def closestNumber(n, m):\n",
38
+ " q = int(n / m)\n",
39
+ " n1 = m * q\n",
40
+ " if (n * m) > 0:\n",
41
+ " n2 = m * (q + 1)\n",
42
+ " else:\n",
43
+ " n2 = m * (q - 1)\n",
44
+ " if abs(n - n1) < abs(n - n2):\n",
45
+ " return n1\n",
46
+ " return n2"
47
+ ]
48
+ },
49
+ {
50
+ "cell_type": "code",
51
+ "execution_count": null,
52
+ "id": "371fe26c-4994-47a0-a966-627c7b2b0c82",
53
+ "metadata": {},
54
+ "outputs": [],
55
+ "source": [
56
+ "with torch.inference_mode():\n",
57
+ " positive_prompt = \"black forest toast spelling out the words 'FLUX DEV', tasty, food photography, dynamic shot\"\n",
58
+ " width = 1024\n",
59
+ " height = 1024\n",
60
+ " seed = 0\n",
61
+ " steps = 20\n",
62
+ " sampler_name = \"euler\"\n",
63
+ " scheduler = \"simple\"\n",
64
+ "\n",
65
+ " if seed == 0:\n",
66
+ " seed = random.randint(0, 18446744073709551615)\n",
67
+ " print(seed)\n",
68
+ "\n",
69
+ " cond, pooled = clip.encode_from_tokens(clip.tokenize(positive_prompt), return_pooled=True)\n",
70
+ " cond = [[cond, {\"pooled_output\": pooled}]]\n",
71
+ " noise = RandomNoise.get_noise(seed)[0] \n",
72
+ " guider = BasicGuider.get_guider(unet, cond)[0]\n",
73
+ " sampler = KSamplerSelect.get_sampler(sampler_name)[0]\n",
74
+ " sigmas = BasicScheduler.get_sigmas(unet, scheduler, steps, 1.0)[0]\n",
75
+ " latent_image = EmptyLatentImage.generate(closestNumber(width, 16), closestNumber(height, 16))[0]\n",
76
+ " sample, sample_denoised = SamplerCustomAdvanced.sample(noise, guider, sampler, sigmas, latent_image)\n",
77
+ " model_management.soft_empty_cache()\n",
78
+ " decoded = VAEDecode.decode(vae, sample)[0].detach()\n",
79
+ " Image.fromarray(np.array(decoded*255, dtype=np.uint8)[0]).save(\"/content/flux.png\")\n",
80
+ "\n",
81
+ "Image.fromarray(np.array(decoded*255, dtype=np.uint8)[0])"
82
+ ]
83
+ }
84
+ ],
85
+ "metadata": {
86
+ "kernelspec": {
87
+ "display_name": "Python 3 (ipykernel)",
88
+ "language": "python",
89
+ "name": "python3"
90
+ },
91
+ "language_info": {
92
+ "codemirror_mode": {
93
+ "name": "ipython",
94
+ "version": 3
95
+ },
96
+ "file_extension": ".py",
97
+ "mimetype": "text/x-python",
98
+ "name": "python",
99
+ "nbconvert_exporter": "python",
100
+ "pygments_lexer": "ipython3",
101
+ "version": "3.10.12"
102
+ }
103
+ },
104
+ "nbformat": 4,
105
+ "nbformat_minor": 5
106
+ }