File size: 3,678 Bytes
6ed1db6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.chdir(\"..\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from diffusers.pipelines import FluxPipeline\n",
    "from src.condition import Condition\n",
    "from PIL import Image\n",
    "\n",
    "from src.generate import generate, seed_everything"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pipe = FluxPipeline.from_pretrained(\n",
    "    \"black-forest-labs/FLUX.1-dev\", torch_dtype=torch.bfloat16\n",
    ")\n",
    "pipe = pipe.to(\"cuda\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pipe.load_lora_weights(\n",
    "    \"Yuanshi/OminiControl\",\n",
    "    weight_name=f\"experimental/fill.safetensors\",\n",
    "    adapter_name=\"fill\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "image = Image.open(\"assets/monalisa.jpg\").convert(\"RGB\").resize((512, 512))\n",
    "\n",
    "masked_image = image.copy()\n",
    "masked_image.paste((0, 0, 0), (128, 100, 384, 220))\n",
    "\n",
    "condition = Condition(\"fill\", masked_image)\n",
    "\n",
    "seed_everything()\n",
    "result_img = generate(\n",
    "    pipe,\n",
    "    prompt=\"The Mona Lisa is wearing a white VR headset with 'Omini' written on it.\",\n",
    "    conditions=[condition],\n",
    ").images[0]\n",
    "\n",
    "concat_image = Image.new(\"RGB\", (1536, 512))\n",
    "concat_image.paste(image, (0, 0))\n",
    "concat_image.paste(condition.condition, (512, 0))\n",
    "concat_image.paste(result_img, (1024, 0))\n",
    "concat_image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "image = Image.open(\"assets/book.jpg\").convert(\"RGB\").resize((512, 512))\n",
    "\n",
    "w, h, min_dim = image.size + (min(image.size),)\n",
    "image = image.crop(\n",
    "    ((w - min_dim) // 2, (h - min_dim) // 2, (w + min_dim) // 2, (h + min_dim) // 2)\n",
    ").resize((512, 512))\n",
    "\n",
    "\n",
    "masked_image = image.copy()\n",
    "masked_image.paste((0, 0, 0), (150, 150, 350, 250))\n",
    "masked_image.paste((0, 0, 0), (200, 380, 320, 420))\n",
    "\n",
    "condition = Condition(\"fill\", masked_image)\n",
    "\n",
    "seed_everything()\n",
    "result_img = generate(\n",
    "    pipe,\n",
    "    prompt=\"A yellow book with the word 'OMINI' in large font on the cover. The text 'for FLUX' appears at the bottom.\",\n",
    "    conditions=[condition],\n",
    ").images[0]\n",
    "\n",
    "concat_image = Image.new(\"RGB\", (1536, 512))\n",
    "concat_image.paste(image, (0, 0))\n",
    "concat_image.paste(condition.condition, (512, 0))\n",
    "concat_image.paste(result_img, (1024, 0))\n",
    "concat_image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}