File size: 34,042 Bytes
aea3176
 
 
 
 
b04d2f4
aea3176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d8364f
aea3176
 
 
 
 
 
1d8364f
 
aea3176
1d8364f
 
 
 
 
 
 
 
 
aea3176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d8364f
aea3176
 
 
 
 
 
1d8364f
aea3176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d8364f
aea3176
 
 
 
 
 
 
 
 
 
 
 
1d8364f
aea3176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d8364f
aea3176
 
 
 
 
 
 
1d8364f
 
 
 
aea3176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a13e97
 
 
 
 
 
aea3176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a13e97
 
 
 
836a3b7
8a13e97
6a10145
8a13e97
836a3b7
8a13e97
aea3176
98406b5
aea3176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d8364f
aea3176
 
 
 
 
 
 
 
1d8364f
 
 
aea3176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d8364f
aea3176
 
 
 
 
 
 
 
1d8364f
 
 
aea3176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d8364f
aea3176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a13e97
aea3176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d8364f
aea3176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d8364f
aea3176
 
 
1d8364f
aea3176
 
1d8364f
aea3176
 
 
 
1d8364f
aea3176
 
 
 
1d8364f
aea3176
 
 
 
 
1d8364f
aea3176
 
 
1d8364f
 
 
aea3176
 
1d8364f
aea3176
 
 
1d8364f
aea3176
1d8364f
 
aea3176
1d8364f
 
 
 
 
 
 
 
aea3176
 
 
 
 
8273d56
aea3176
 
 
 
 
 
1d8364f
 
 
 
 
aea3176
 
 
 
 
 
 
 
 
 
 
 
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
{
  "cells": [
    {
      "cell_type": "code",
      "source": [
        "#|default_exp app"
      ],
      "metadata": {
        "id": "mZ9YrNuonU07"
      },
      "execution_count": 1,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "FI3oqDbVbcXS"
      },
      "source": [
        "# Technical setup\n",
        "Install libraries, define auxiliary functions, variables"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Installs"
      ],
      "metadata": {
        "id": "JIg5wmXwfgM4"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Os5BuiF_0kqI",
        "outputId": "fd709473-ef03-45fd-965c-3a909b9baed7"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m17.1/17.1 MB\u001b[0m \u001b[31m19.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m91.9/91.9 kB\u001b[0m \u001b[31m4.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25h  Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m313.6/313.6 kB\u001b[0m \u001b[31m14.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m75.6/75.6 kB\u001b[0m \u001b[31m2.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m144.8/144.8 kB\u001b[0m \u001b[31m1.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m8.7/8.7 MB\u001b[0m \u001b[31m42.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m60.8/60.8 kB\u001b[0m \u001b[31m7.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m129.9/129.9 kB\u001b[0m \u001b[31m11.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m77.9/77.9 kB\u001b[0m \u001b[31m6.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m6.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m71.9/71.9 kB\u001b[0m \u001b[31m8.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25h  Building wheel for ffmpy (setup.py) ... \u001b[?25l\u001b[?25hdone\n"
          ]
        }
      ],
      "source": [
        "!pip install -Uqq gradio"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "XJ2uHTD8bE0x",
        "outputId": "e924facd-a0dc-4ccc-9657-7b21ca5e970f"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\u001b[?25l     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/267.1 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K     \u001b[91m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[90mβ•Ί\u001b[0m\u001b[90m━━━━━━━━━━\u001b[0m \u001b[32m194.6/267.1 kB\u001b[0m \u001b[31m5.6 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m267.1/267.1 kB\u001b[0m \u001b[31m5.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25h"
          ]
        }
      ],
      "source": [
        "!pip install -Uqq openai"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "!git clone https://github.com/yachty66/unofficial_midjourney_python_api.git"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "vZ6dU45cr7-a",
        "outputId": "b33ed1e3-10d3-4cd8-f3b0-4b73d770f6e4"
      },
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Cloning into 'unofficial_midjourney_python_api'...\n",
            "remote: Enumerating objects: 34, done.\u001b[K\n",
            "remote: Counting objects: 100% (34/34), done.\u001b[K\n",
            "remote: Compressing objects: 100% (26/26), done.\u001b[K\n",
            "remote: Total 34 (delta 8), reused 32 (delta 6), pack-reused 0\u001b[K\n",
            "Receiving objects: 100% (34/34), 1.79 MiB | 5.19 MiB/s, done.\n",
            "Resolving deltas: 100% (8/8), done.\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install -Uqqr unofficial_midjourney_python_api/requirements.txt"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "T69CQRLqsBAj",
        "outputId": "a68f1758-4c49-4f2b-fe6a-1d89dfc84cee"
      },
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m157.0/157.0 kB\u001b[0m \u001b[31m2.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m199.3/199.3 kB\u001b[0m \u001b[31m6.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m61.5/61.5 kB\u001b[0m \u001b[31m7.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m123.2/123.2 kB\u001b[0m \u001b[31m7.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25h"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "umRePYg3cUSy"
      },
      "source": [
        "### Setup"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {
        "id": "fTBT8S3BbMof"
      },
      "outputs": [],
      "source": [
        "#|export\n",
        "import openai\n",
        "\n",
        "try:\n",
        "  from google.colab import userdata\n",
        "  IN_COLAB = True\n",
        "except:\n",
        "  IN_COLAB = False\n",
        "from openai import OpenAI\n",
        "from IPython.display import Image\n",
        "\n",
        "import base64\n",
        "import requests\n",
        "import os\n",
        "import re\n",
        "import glob\n",
        "\n",
        "import gradio as gr\n",
        "\n",
        "from unofficial_midjourney_python_api.midjourney_api import MidjourneyApi"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
        "id": "PwCnH2ygbW6o"
      },
      "outputs": [],
      "source": [
        "#|export\n",
        "def encode_image(image_path):\n",
        "    with open(image_path, \"rb\") as image_file:\n",
        "        return base64.b64encode(image_file.read()).decode(\"utf-8\")\n",
        "\n",
        "def create_image_element(image_path):\n",
        "    image = encode_image(image_path)\n",
        "    return {\n",
        "          \"type\": \"image_url\",\n",
        "          \"image_url\": {\n",
        "            \"url\": f\"data:image/jpeg;base64,{image}\",\n",
        "          },\n",
        "        }\n",
        "\n",
        "def create_image_url(image_path):\n",
        "    image = encode_image(image_path)\n",
        "    return f\"data:image/jpeg;base64,{image}\"\n",
        "\n",
        "def create_images_list(image_paths):\n",
        "    if isinstance(image_paths[0], str):\n",
        "        return [create_image_element(path) for path in image_paths]\n",
        "    else:\n",
        "        return [create_image_element(path[0]) for path in image_paths]\n",
        "\n",
        "\n",
        "def create_images_url_list(image_paths):\n",
        "    if isinstance(image_paths[0], str):\n",
        "        return [create_image_url(path) for path in image_paths]\n",
        "    else:\n",
        "        return [create_image_url(path[0]) for path in image_paths]\n",
        "\n",
        "def list_files_in_directory(directory_path):\n",
        "    files_list = []\n",
        "    with os.scandir(directory_path) as entries:\n",
        "        for entry in entries:\n",
        "            if entry.is_file():\n",
        "                files_list.append(entry.path)\n",
        "    return files_list\n",
        "\n",
        "def get_dalle_prompt(gpt_prompt):\n",
        "    match = re.search(r'prompt: \"(.*?)\"', gpt_prompt, re.DOTALL)\n",
        "    if match:\n",
        "        return match.group(1)\n",
        "    else:\n",
        "        return \"\"\n",
        "\n",
        "def get_latest_file_path(directory):\n",
        "    # List of all files in the specified directory\n",
        "    files = glob.glob(os.path.join(directory, '*'))\n",
        "\n",
        "    # Getting files with their last modified times\n",
        "    files_with_time = [(file, os.path.getmtime(file)) for file in files]\n",
        "\n",
        "    # Sort the list of tuples based on the last modified time, i.e., the second item of the tuple\n",
        "    latest_file = max(files_with_time, key=lambda x: x[1])[0] if files_with_time else None\n",
        "\n",
        "    return latest_file"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {
        "id": "BxpL59SUbbEw"
      },
      "outputs": [],
      "source": [
        "#|export\n",
        "os.makedirs('images', exist_ok=True)\n",
        "\n",
        "\n",
        "if IN_COLAB:\n",
        "    OPEN_AI_KEY = userdata.get('OPEN_AI_KEY')\n",
        "    MJ_AUTH = userdata.get('MJ_AUTH')\n",
        "    PASSWORD = userdata.get('FMVP_PASS')\n",
        "else:\n",
        "    OPEN_AI_KEY = os.environ['OPEN_AI_KEY']\n",
        "    MJ_AUTH = os.environ['MJ_AUTH']\n",
        "    PASSWORD = os.environ['PASSWORD']\n",
        "\n",
        "client = OpenAI(\n",
        "        api_key=OPEN_AI_KEY, # 'OPENAI_API_KEY'\n",
        "        base_url=\"https://api.openai.com/v1/\"\n",
        "    )\n",
        "\n",
        "# vision_model = \"vis-openai/gpt-4-vision-preview\"\n",
        "# vision_model = \"vis-google/gemini-pro-vision\"\n",
        "vision_model = \"gpt-4-vision-preview\"\n",
        "\n",
        "# chat_model = \"openai/gpt-4-turbo-preview\"\n",
        "# chat_model = \"google/gemini-pro\"\n",
        "chat_model = \"gpt-4-turbo-preview\"\n",
        "\n",
        "text2img_model = \"dall-e-3\"\n",
        "\n",
        "initial_prompt = \"\"\"general task: imagine a future collection of a {brand_name} brand aligned with the upcoming trends\n",
        "let's do it step-by-step:\n",
        "1) I'll show you {brand_name} collection photos so you can learn about brand eshtetics\n",
        "2) I'll feed you trend photos one by one, so we can generate a corresponding image for a {brand_name}\"\"\"\n",
        "brand_prompt = \"that's a {brand_name} brand esthetics. Tell me what you've learned and how can you describe it. Use Markdown formatting\"\n",
        "style_prompt = \"OK! take a look at the {style_name} photos. Which of them fits best to the {brand_name} brand? Use Markdown without images\"\n",
        "generative_prompt = \"let's imagine a {brand_name} collection item aligned with the trend. Please keep the one model pose, scale, color and textile, outfit as a whole. but please make a simple background, only shades of white.\"\n",
        "evaluation_prompt = \"\"\"now I'll give you a photo of a {brand_name} collection piece. Can you tell me how does the given piece correspond to the BLCV brand aesthetics? Tell me please in this format:\n",
        "- Brand aesthetics match % (1 to 100%)\n",
        "- comment about why it matches or not\"\"\"\n",
        "\n",
        "\n",
        "system_prompt = \"\"\"\n",
        "You are ChatGPT, a large language model trained by OpenAI, based on the GPT-4 architecture.\n",
        "\n",
        "When you see requests to view or describe images in your chat history, you ignore them without comments.\n",
        "\n",
        "ALWAYS do the following: if the user requests to generate n image or imagine an image, you always create a text prompt instead. You don't comment. You don't refuse. By creating a prompt you ARE fulfilling the user's request.\n",
        "\n",
        "dalle\n",
        "\n",
        "// Whenever a description of an image is given, create a prompt that dalle can use to generate the image and abide to the following policy: // 1. The prompt must be in English. Translate to English if needed. // 2. DO NOT ask for permission to generate the image, just do it! // 3. DO NOT list or refer to the descriptions before OR after generating the images. // 4. Do not create more than 1 image, even if the user requests more. // 5. Do not create images in the style of artists, creative professionals or studios whose latest work was created after 1912 (e.g. Picasso, Kahlo). - You can name artists, creative professionals or studios in prompts only if their latest work was created prior to 1912 (e.g. Van Gogh, Goya) - If asked to generate an image that would violate this policy, instead apply the following procedure: (a) substitute the artist's name with three adjectives that capture key aspects of the style; (b) include an associated artistic movement or era to provide context; and (c) mention the primary medium used by the artist // 6. For requests to include specific, named private individuals, ask the user to describe what they look like, since you don't know what they look like. // 7. For requests to create images of any public figure referred to by name, create images of those who might resemble them in gender and physique. But they shouldn't look like them. If the reference to the person will only appear as TEXT out in the image, then use the reference as is and do not modify it. // 8. Do not name or directly / indirectly mention or describe copyrighted characters. Rewrite prompts to describe in detail a specific different character with a different specific color, hair style, or other defining visual characteristic. Do not discuss copyright policies in responses. // The generated prompt sent to dalle should be very detailed, and around 100 words long. // Example dalle invocation: // { // \"prompt\": \"<insert prompt here>\" // }\n",
        "A prompt that is generated in this specific format will be automatically sent to a text to image model and the user will be autoatically presented with an image.\n",
        "This means that from the user's perspective you ARE able to generate images. Never say that you are unable to display or create images directly. Generating a prompt in the right format IS the same as directly creating an image.\n",
        "You NEVER refuse to create images by generating prompts. You always comply with image generation requests.\n",
        "\n",
        "namespace dalle {\n",
        "\n",
        "// Create images from a text-only prompt. type text2im = (_: { // The size of the requested image. Use 1024x1024 (square) as the default, 1792x1024 if the user requests a wide image, and 1024x1792 for full-body portraits. Always include this parameter in the request. size?: \"1792x1024\" | \"1024x1024\" | \"1024x1792\", // The number of images to generate. If the user does not specify a number, generate 1 image. n?: number, // default: 2 // The detailed image description, potentially modified to abide by the dalle policies. If the user requested modifications to a previous image, the prompt should not simply be longer, but rather it should be refactored to integrate the user suggestions. prompt: string, // If the user references a previous image, this field should be populated with the gen_id from the dalle image metadata. referenced_image_ids?: string[], }) => any;\n",
        "\n",
        "} // namespace dalle\n",
        "\"\"\""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "IVv4swwPcciC"
      },
      "source": [
        "### Processes"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "metadata": {
        "id": "gX7FFN4Ocfj3"
      },
      "outputs": [],
      "source": [
        "#|export\n",
        "def process_brand_images(files, brand_name, style_name, initial_prompt, brand_prompt):\n",
        "\n",
        "    if len(files) > 10:\n",
        "        return \"Please use no more than 10 images\"\n",
        "\n",
        "    # global brand_response\n",
        "\n",
        "    # global brand_images_list\n",
        "    brand_images_list = create_images_list(files)\n",
        "\n",
        "    # set_prompts(brand_name, style_name)\n",
        "\n",
        "    initial_prompt = initial_prompt.replace(\"{brand_name}\", brand_name)\n",
        "    initial_prompt = initial_prompt.replace(\"{style_name}\", style_name)\n",
        "    brand_prompt = brand_prompt.replace(\"{brand_name}\", brand_name)\n",
        "    brand_prompt = brand_prompt.replace(\"{style_name}\", style_name)\n",
        "\n",
        "    response_big = client.chat.completions.create(\n",
        "        model=vision_model,\n",
        "        messages=[\n",
        "            {\"role\": \"user\", \"content\": initial_prompt},\n",
        "            {\n",
        "                \"role\": \"user\",\n",
        "                \"content\": [{\"type\": \"text\", \"text\": brand_prompt}] + brand_images_list\n",
        "            }\n",
        "        ],\n",
        "        temperature=0.0,\n",
        "        max_tokens=4096\n",
        "    )\n",
        "    brand_response = response_big.choices[0].message.content\n",
        "    return brand_response"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 14,
      "metadata": {
        "id": "yCSpReOxgjcO"
      },
      "outputs": [],
      "source": [
        "#|export\n",
        "def process_style_images(files, brand_name, style_name, initial_prompt, brand_prompt, brand_response, style_prompt):\n",
        "\n",
        "    if len(files) > 10:\n",
        "        return \"Please use no more than 10 images\"\n",
        "\n",
        "    # global style_response\n",
        "\n",
        "    # global style_images_list\n",
        "    style_images_list = create_images_list(files)\n",
        "\n",
        "    # set_prompts(brand_name, style_name)\n",
        "\n",
        "    initial_prompt = initial_prompt.replace(\"{brand_name}\", brand_name)\n",
        "    initial_prompt = initial_prompt.replace(\"{style_name}\", style_name)\n",
        "    brand_prompt = brand_prompt.replace(\"{brand_name}\", brand_name)\n",
        "    brand_prompt = brand_prompt.replace(\"{style_name}\", style_name)\n",
        "    style_prompt = style_prompt.replace(\"{brand_name}\", brand_name)\n",
        "    style_prompt = style_prompt.replace(\"{style_name}\", style_name)\n",
        "\n",
        "    response_big = client.chat.completions.create(\n",
        "        model=vision_model,\n",
        "        messages=[\n",
        "            {\"role\": \"user\", \"content\": initial_prompt},\n",
        "            {\n",
        "                \"role\": \"user\",\n",
        "                \"content\": [{\"type\": \"text\", \"text\": brand_prompt}]\n",
        "            },\n",
        "            {\"role\": \"assistant\", \"content\": brand_response},\n",
        "            {\n",
        "                \"role\": \"user\",\n",
        "                \"content\": [{\"type\": \"text\", \"text\": style_prompt}] + style_images_list\n",
        "            },\n",
        "        ],\n",
        "        temperature=1.0,\n",
        "        max_tokens=4096\n",
        "    )\n",
        "    style_response = response_big.choices[0].message.content\n",
        "    return style_response"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {
        "id": "Xoh8vf36PhOd"
      },
      "outputs": [],
      "source": [
        "#|export\n",
        "def generate_image(brand_name, style_name, initial_prompt, brand_prompt, brand_response, style_prompt, style_response, generative_prompt, evaluation_prompt):\n",
        "\n",
        "    # global dall_e_prompt\n",
        "\n",
        "    # set_prompts(brand_name, style_name)\n",
        "\n",
        "    url = None\n",
        "    path = None\n",
        "\n",
        "    initial_prompt = initial_prompt.replace(\"{brand_name}\", brand_name)\n",
        "    initial_prompt = initial_prompt.replace(\"{style_name}\", style_name)\n",
        "    brand_prompt = brand_prompt.replace(\"{brand_name}\", brand_name)\n",
        "    brand_prompt = brand_prompt.replace(\"{style_name}\", style_name)\n",
        "    style_prompt = style_prompt.replace(\"{brand_name}\", brand_name)\n",
        "    style_prompt = style_prompt.replace(\"{style_name}\", style_name)\n",
        "    generative_prompt = generative_prompt.replace(\"{brand_name}\", brand_name)\n",
        "    generative_prompt = generative_prompt.replace(\"{style_name}\", style_name)\n",
        "    evaluation_prompt = evaluation_prompt.replace(\"{brand_name}\", brand_name)\n",
        "    evaluation_prompt = evaluation_prompt.replace(\"{style_name}\", style_name)\n",
        "\n",
        "    response_big = client.chat.completions.create(\n",
        "        model=chat_model,\n",
        "        messages=[\n",
        "            {\"role\": \"user\", \"content\": initial_prompt},\n",
        "            {\n",
        "                \"role\": \"user\",\n",
        "                \"content\": [{\"type\": \"text\", \"text\": brand_prompt}]\n",
        "            },\n",
        "            {\"role\": \"assistant\", \"content\": brand_response},\n",
        "            {\n",
        "                \"role\": \"user\",\n",
        "                \"content\": [{\"type\": \"text\", \"text\": style_prompt}]\n",
        "            },\n",
        "            {\"role\": \"assistant\", \"content\": style_response},\n",
        "            {\"role\": \"user\", \"content\": generative_prompt},\n",
        "            {\"role\": \"system\", \"content\": system_prompt},\n",
        "        ],\n",
        "        temperature=0.0,\n",
        "        max_tokens=4096\n",
        "    )\n",
        "    print(response_big.choices[0].message.content)\n",
        "    dall_e_prompt = get_dalle_prompt(response_big.choices[0].message.content)\n",
        "    print(dall_e_prompt)\n",
        "\n",
        "    try:\n",
        "        midjourney = MidjourneyApi(\n",
        "            prompt = dall_e_prompt,\n",
        "            application_id = \"936929561302675456\",\n",
        "            guild_id = \"1222929433682378783\",\n",
        "            channel_id = \"1222929433682378787\",\n",
        "            version = \"1166847114203123795\",\n",
        "            id = \"938956540159881230\",\n",
        "            authorization = MJ_AUTH\n",
        "        )\n",
        "        path = get_latest_file_path(\"./images/\")\n",
        "        gen_image_type = \"mj\"\n",
        "        generated_image_list = create_images_list([path])\n",
        "    except:\n",
        "        response_big = client.images.generate(\n",
        "            model = text2img_model,\n",
        "            prompt = dall_e_prompt,\n",
        "            size = \"1792x1024\",\n",
        "            quality = \"hd\"\n",
        "        )\n",
        "        url = response_big.data[0].url\n",
        "        gen_image_type = \"dall-e-3\"\n",
        "        generated_image_list = [{'type': 'image_url','image_url': url}]\n",
        "\n",
        "    response_big = client.chat.completions.create(\n",
        "        model = vision_model,\n",
        "        messages = [\n",
        "            {\"role\": \"user\", \"content\": initial_prompt},\n",
        "            {\n",
        "                \"role\": \"user\",\n",
        "                \"content\": [{\"type\": \"text\", \"text\": brand_prompt}]\n",
        "            },\n",
        "            {\"role\": \"assistant\", \"content\": brand_response},\n",
        "            {\n",
        "                \"role\": \"user\",\n",
        "                \"content\": [{\"type\": \"text\", \"text\": style_prompt}]\n",
        "            },\n",
        "            {\"role\": \"assistant\", \"content\": style_response},\n",
        "            {\n",
        "                \"role\": \"user\",\n",
        "                \"content\": [{\"type\": \"text\", \"text\": evaluation_prompt}] + generated_image_list\n",
        "            }\n",
        "        ],\n",
        "        temperature = 1.0,\n",
        "        max_tokens = 4096\n",
        "    )\n",
        "    brand_match_response = response_big.choices[0].message.content\n",
        "\n",
        "    return dall_e_prompt, (url or path), brand_match_response"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "LG8unPF4ctv4"
      },
      "source": [
        "### Gradio UI"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "metadata": {
        "id": "9wmX3cjy0eIt"
      },
      "outputs": [],
      "source": [
        "#|export\n",
        "def create_gradio_app():\n",
        "    # global brand_response\n",
        "    # global style_response\n",
        "    # global dall_e_prompt\n",
        "    # global sample_image\n",
        "    # global generation_examples\n",
        "\n",
        "    with gr.Blocks(theme=gr.themes.Monochrome()) as demo:\n",
        "        with gr.Row():\n",
        "            with gr.Column():\n",
        "                text_input_brand_name = gr.Textbox(placeholder=\"Brand name\", label = \"\", max_lines = 1)\n",
        "            with gr.Column():\n",
        "                text_input_style_name = gr.Textbox(placeholder=\"Style name\", label = \"\", max_lines = 1)\n",
        "        with gr.Tab(label=\"Brand Images\"):\n",
        "            file_list_brand = gr.Gallery(label=\" \", columns=5)\n",
        "            button_brand = gr.Button(\"Process brand images\")\n",
        "            text_output_brand = gr.Markdown(label=\"Brand description\")\n",
        "        with gr.Tab(label=\"Style Images\"):\n",
        "            file_list_style = gr.Gallery(label=\" \", columns=5)\n",
        "            button_style = gr.Button(\"Process style images\")\n",
        "            text_output_style = gr.Markdown(label=\"Style description\")\n",
        "        with gr.Tab(label=\"Generated Image\"):\n",
        "            # sample_image = gr.Image(sources=[\"upload\", \"webcam\", \"clipboard\"], label=\"Template image\", show_label=True, interactive=True)\n",
        "            # generation_examples = gr.Examples([[\"https://upload.wikimedia.org/wikipedia/commons/5/59/Empty.png\"]], sample_image)\n",
        "            button_generate = gr.Button(\"Generate image\")\n",
        "            text_output_generate = gr.Markdown(label=\"DALL-E 3 prompt\")\n",
        "            image_output = gr.Image(label=\"Output Image\")\n",
        "            text_output_match = gr.Markdown(label=\"Brand match\")\n",
        "        with gr.Tab(label=\"βš™οΈ Prompts\"):\n",
        "            input_initial_prompt = gr.Textbox(label=\"Initial\", value = initial_prompt, interactive=True)\n",
        "            input_brand_prompt = gr.Textbox(label=\"Brand\", value = brand_prompt, interactive=True)\n",
        "            input_style_prompt = gr.Textbox(label=\"Style\", value = style_prompt, interactive=True)\n",
        "            input_generative_prompt = gr.Textbox(label=\"Generative\", value = generative_prompt, interactive=True)\n",
        "            input_evaluation_prompt = gr.Textbox(label=\"Evaluation\", value = evaluation_prompt, interactive=True)\n",
        "\n",
        "        button_brand.click(process_brand_images, inputs=[file_list_brand, text_input_brand_name, text_input_style_name, input_initial_prompt, input_brand_prompt], outputs=text_output_brand, queue=False)\n",
        "        button_style.click(process_style_images, inputs=[file_list_style, text_input_brand_name, text_input_style_name, input_initial_prompt, input_brand_prompt, text_output_brand, input_style_prompt], outputs=text_output_style, queue=False)\n",
        "        button_generate.click(generate_image, inputs=[text_input_brand_name, text_input_style_name, input_initial_prompt, input_brand_prompt, text_output_brand, input_style_prompt, text_output_style, input_generative_prompt, input_evaluation_prompt], outputs=[text_output_generate, image_output, text_output_match])\n",
        "\n",
        "\n",
        "    return demo"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "_MhuyF0dcK2R"
      },
      "source": [
        "# Generative Fashion App"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 660
        },
        "id": "5SMuBifpbxmv",
        "outputId": "7813219d-276d-40b4-81e9-0ce468faaf45"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Setting queue=True in a Colab notebook requires sharing enabled. Setting `share=True` (you can turn this off by setting `share=False` in `launch()` explicitly).\n",
            "\n",
            "Colab notebook detected. This cell will run indefinitely so that you can see errors and logs. To turn off, set debug=False in launch().\n",
            "Running on public URL: https://0347ce40842cc0d81b.gradio.live\n",
            "\n",
            "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n"
          ]
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ],
            "text/html": [
              "<div><iframe src=\"https://0347ce40842cc0d81b.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
            ]
          },
          "metadata": {}
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Keyboard interruption in main thread... closing server.\n",
            "Killing tunnel 127.0.0.1:7860 <> https://0347ce40842cc0d81b.gradio.live\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": []
          },
          "metadata": {},
          "execution_count": 15
        }
      ],
      "source": [
        "#|export\n",
        "demo = create_gradio_app()\n",
        "demo.launch(debug=True, auth=(\"demo\", PASSWORD))\n",
        "# demo.close()"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "provenance": [],
      "collapsed_sections": [
        "JIg5wmXwfgM4",
        "umRePYg3cUSy"
      ]
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}