drakosfire commited on
Commit
876b664
1 Parent(s): 147f212

stop tracking symlink

Browse files
.dockerignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ .get
2
+ output
.gitattributes ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ *.png filter=lfs diff=lfs merge=lfs -text
37
+ models/starling-lm-7b-alpha.Q8_0.gguf filter=lfs diff=lfs merge=lfs -text
38
+ cuda_12.4.0_550.54.14_linux.run filter=lfs diff=lfs merge=lfs -text
.gitignore CHANGED
@@ -1,7 +1,6 @@
1
  output/
2
  image_temp/
3
  MerchantBotCLI/
4
- models/
5
  seed_images/
6
  cuda_12.4.0_550.54.14_linux.run
7
- /media/drakosfire/Shared/models
 
1
  output/
2
  image_temp/
3
  MerchantBotCLI/
4
+ models
5
  seed_images/
6
  cuda_12.4.0_550.54.14_linux.run
 
Dockerfile ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Stage 1: Build Cuda toolkit
2
+ FROM ubuntu:22.04 as cuda-setup
3
+
4
+
5
+ ARG DEBIAN_FRONTEND=noninteractive
6
+
7
+ # Install necessary libraries including libxml2
8
+ RUN apt-get update && \
9
+ apt-get install -y gcc libxml2 && \
10
+ apt-get clean && \
11
+ rm -rf /var/lib/apt/lists/*
12
+
13
+ COPY cuda_12.4.0_550.54.14_linux.run .
14
+
15
+ # Install wget, download cuda-toolkit and run
16
+ RUN chmod +x cuda_12.4.0_550.54.14_linux.run && \
17
+ ./cuda_12.4.0_550.54.14_linux.run --silent --toolkit --override
18
+
19
+ # Second Stage: Copy necessary CUDA directories install flash-attn
20
+ FROM ubuntu:22.04 as base-layer
21
+
22
+ # Copy the CUDA toolkit from the first stage
23
+ COPY --from=cuda-setup /usr/local/cuda-12.4 /usr/local/cuda-12.4
24
+
25
+ # Set environment variables to enable CUDA commands
26
+ ENV PATH=/usr/local/cuda-12.4/bin:${PATH}
27
+ ENV LD_LIBRARY_PATH=/usr/local/cuda-12.4/lib64:${LD_LIBRARY_PATH}
28
+
29
+ # Install Python, pip, and virtualenv
30
+ RUN apt-get update && \
31
+ apt-get install -y python3 python3-pip python3-venv git && \
32
+ apt-get clean && \
33
+ rm -rf /var/lib/apt/lists/*
34
+
35
+ # Create a virtual environment and install dependencies
36
+ RUN python3 -m venv /venv
37
+ ENV PATH="/venv/bin:$PATH"
38
+ FROM ubuntu:22.04 as cuda-setup
39
+
40
+
41
+ ARG DEBIAN_FRONTEND=noninteractive
42
+
43
+ # Install necessary libraries including libxml2
44
+ RUN apt-get update && \
45
+ apt-get install -y gcc libxml2 && \
46
+ apt-get clean && \
47
+ rm -rf /var/lib/apt/lists/*
48
+
49
+ COPY cuda_12.4.0_550.54.14_linux.run .
50
+
51
+ # Install wget, download cuda-toolkit and run
52
+ RUN chmod +x cuda_12.4.0_550.54.14_linux.run && \
53
+ ./cuda_12.4.0_550.54.14_linux.run --silent --toolkit --override
54
+
55
+ # Second Stage: Copy necessary CUDA directories install flash-attn
56
+ FROM ubuntu:22.04 as base-layer
57
+
58
+ # Copy the CUDA toolkit from the first stage
59
+ COPY --from=cuda-setup /usr/local/cuda-12.4 /usr/local/cuda-12.4
60
+
61
+ # Set environment variables to enable CUDA commands
62
+ ENV PATH=/usr/local/cuda-12.4/bin:${PATH}
63
+ ENV LD_LIBRARY_PATH=/usr/local/cuda-12.4/lib64:${LD_LIBRARY_PATH}
64
+
65
+ # Install Python, pip, and virtualenv
66
+ RUN apt-get update && \
67
+ apt-get install -y python3 python3-pip python3-venv git && \
68
+ apt-get clean && \
69
+ rm -rf /var/lib/apt/lists/*
70
+
71
+ # Create a virtual environment and install dependencies
72
+ RUN python3 -m venv /venv
73
+ ENV PATH="/venv/bin:$PATH"
74
+
75
+ # Llama.cpp requires the ENV variable be set to signal the CUDA build and be built with the CMAKE variables from pip for python use
76
+ ENV LLAMA_CUBLAS=1
77
+ RUN pip install --no-cache-dir torch packaging wheel && \
78
+ pip install flash-attn && \
79
+ RUN pip install --no-cache-dir torch packaging wheel && \
80
+ pip install flash-attn && \
81
+ pip install gradio && \
82
+ CMAKE_ARGS="-DLLAMA_CUBLAS=on" pip install llama_cpp_python==0.2.55 && \
83
+ CMAKE_ARGS="-DLLAMA_CUBLAS=on" pip install llama_cpp_python==0.2.55 && \
84
+ pip install pillow && \
85
+ pip install diffusers && \
86
+ pip install accelerate && \
87
+ pip install transformers && \
88
+ pip install peft && \
89
+ pip install pip install PyGithub
90
+
91
+
92
+ FROM ubuntu:22.04 as final-layer
93
+
94
+ COPY --from=base-layer /usr/local/cuda-12.4 /usr/local/cuda-12.4
95
+ COPY --from=base-layer /venv /venv
96
+
97
+ ENV PATH=/usr/local/cuda-12.4/bin:/venv/bin:${PATH}
98
+ ENV LD_LIBRARY_PATH=/usr/local/cuda-12.4/lib64:${LD_LIBRARY_PATH}
99
+ ENV LLAMA_CPP_LIB=/venv/lib/python3.10/site-packages/llama_cpp/libllama.so
100
+ ENV VIRTUAL_ENV=/venv
101
+
102
+ # Install Python and create a user
103
+ RUN apt-get update && apt-get install -y python3 python3-venv && apt-get clean && rm -rf /var/lib/apt/lists/* && \
104
+ useradd -m -u 1000 user
105
+
106
+
107
+ # Install Python and create a user
108
+ RUN apt-get update && apt-get install -y python3 python3-venv && apt-get clean && rm -rf /var/lib/apt/lists/* && \
109
+ useradd -m -u 1000 user
110
+
111
+ ENV PATH="$VIRTUAL_ENV/bin:$PATH"
112
+ # Set working directory and user
113
+ COPY . /home/user/app
114
+ # Set working directory and user
115
+ COPY . /home/user/app
116
+ WORKDIR /home/user/app
117
+ RUN chown -R user:user /home/user/app/ && \
118
+ mkdir -p /home/user/app/output && \
119
+ chown -R user:user /home/user/app/image_temp && \
120
+ chown -R user:user /home/user/app/output
121
+
122
+ USER user
123
+
124
+ # Set the entrypoint
125
+ EXPOSE 8000
126
+
127
+ ENTRYPOINT ["python", "main.py"]
LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Drakosfire
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
README.md ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ title: Collectible Card Generator
4
+ short_description: Use LLM and SD to make custom collectible cards
5
+ app_file: main.py
6
+ sdk: docker
7
+ app_port: 8000
8
+ ---
__pycache__/card_generator.cpython-310.pyc ADDED
Binary file (3.31 kB). View file
 
__pycache__/image_gen.cpython-310.pyc ADDED
Binary file (1.74 kB). View file
 
__pycache__/img2img.cpython-310.pyc ADDED
Binary file (2.48 kB). View file
 
__pycache__/inventory.cpython-310.pyc ADDED
Binary file (3.77 kB). View file
 
__pycache__/item_dict_gen.cpython-310.pyc ADDED
Binary file (18.4 kB). View file
 
__pycache__/main.cpython-310.pyc ADDED
Binary file (7.27 kB). View file
 
__pycache__/render_card_text.cpython-310.pyc ADDED
Binary file (2.03 kB). View file
 
__pycache__/template_builder.cpython-310.pyc ADDED
Binary file (1.16 kB). View file
 
__pycache__/user_input.cpython-310.pyc ADDED
Binary file (2.63 kB). View file
 
__pycache__/utilities.cpython-310.pyc ADDED
Binary file (1.74 kB). View file
 
card_generator.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import render_card_text as rend
2
+ from PIL import Image, ImageFilter
3
+ import utilities as u
4
+ import ast
5
+
6
+
7
+ def save_image(image,item_key):
8
+ image.save(f"{item_key['Name']}.png")
9
+
10
+
11
+ # Import Inventory
12
+ #shop_inventory = inv.inventory
13
+ #purchased_item_key = shop_inventory['Shortsword']
14
+ #border_path = './card_templates/Shining Sunset Border.png'
15
+
16
+ sticker_path_dictionary = {'Default': './card_parts/Sizzek Sticker.png','Common': './card_parts/Common.png', 'Uncommon': './card_parts/Uncommon.png','Rare': './card_parts/Rare.png','Very Rare':'./card_parts/Very Rare.png','Legendary':'./card_parts/Legendary.png'}
17
+ blank_overlay_path = "./card_parts/white-fill-title-detail-value-transparent.png"
18
+ value_overlay_path = "./card_parts/Value_box_transparent.png"
19
+ test_item = {'Name': 'Pustulent Raspberry', 'Type': 'Fruit', 'Value': '1 cp', 'Properties': ['Unusual Appearance', 'Rare Taste'], 'Weight': '0.2 lb', 'Description': 'This small fruit has a pustulent appearance, with bumps and irregular shapes covering its surface. Its vibrant colors and strange texture make it an oddity among other fruits.', 'Quote': 'A fruit that defies expectations, as sweet and sour as life itself.', 'SD Prompt': 'A small fruit with vibrant colors and irregular shapes, bumps covering its surface.'}
20
+
21
+
22
+
23
+ # Function that takes in an image url and a dictionary and uses the values to print onto a card.
24
+ def paste_image_and_resize(base_image,sticker_path, x_position, y_position,img_width, img_height, purchased_item_key = None):
25
+
26
+ # Check for if item has a Rarity string that is in the dictionary of sticker paths
27
+ if purchased_item_key:
28
+ if sticker_path[purchased_item_key]:
29
+ sticker_path = sticker_path[purchased_item_key]
30
+ else: sticker_path = sticker_path['Default']
31
+
32
+ # Load the image to paste
33
+ image_to_paste = Image.open(sticker_path)
34
+
35
+ # Define the new size (scale) for the image you're pasting
36
+
37
+ new_size = (img_width, img_height)
38
+
39
+ # Resize the image to the new size
40
+ image_to_paste_resized = image_to_paste.resize(new_size)
41
+
42
+ # Specify the top-left corner where the resized image will be pasted
43
+ paste_position = (x_position, y_position) # Replace x and y with the coordinates
44
+
45
+ # Paste the resized image onto the base image
46
+ base_image.paste(image_to_paste_resized, paste_position, image_to_paste_resized)
47
+
48
+ def render_text_on_card(image_path, item_name,
49
+ item_type,
50
+ item_rarity,
51
+ item_value,
52
+ item_properties,
53
+ item_damage,
54
+ item_weight,
55
+ item_description,
56
+ item_quote) :
57
+ # Card Properties
58
+ image_list = []
59
+ item_properties = ast.literal_eval(item_properties)
60
+ item_properties = '\n'.join(item_properties)
61
+ output_image_path = f"./{item_name}.png"
62
+ print(f"Saving image to {output_image_path}")
63
+ font_path = "./fonts/Balgruf.ttf"
64
+ italics_font_path = './fonts/BalgrufItalic.ttf'
65
+ initial_font_size = 50
66
+
67
+ # Title Properties
68
+ title_center_position = (395, 55)
69
+ title_area_width = 600 # Maximum width of the text box
70
+ title_area_height = 60 # Maximum height of the text box
71
+
72
+ # Type box properties
73
+ type_center_position = (384, 545)
74
+ type_area_width = 600
75
+ type_area_height = 45
76
+ type_text = item_type
77
+ if len(item_weight) >= 1:
78
+ type_text = type_text + ' '+ item_weight
79
+
80
+ if len(item_damage) >= 1 :
81
+ type_text = type_text + ' '+ item_damage
82
+
83
+ # Description box properties
84
+ description_position = (105, 630)
85
+ description_area_width = 590
86
+ description_area_height = 215
87
+
88
+ # Value box properties (This is good, do not change unless underlying textbox layout is changing)
89
+ value_position = (660,905)
90
+ value_area_width = 125
91
+ value_area_height = 50
92
+
93
+ # Quote test properties
94
+ quote_position = (110,885)
95
+ quote_area_width = 470
96
+ quote_area_height = 60
97
+
98
+ # open image and render text
99
+ image = u.open_image_from_url(image_path)
100
+ image = rend.render_text_with_dynamic_spacing(image, item_name, title_center_position, title_area_width, title_area_height,font_path,initial_font_size)
101
+ image = rend.render_text_with_dynamic_spacing(image,type_text , type_center_position, type_area_width, type_area_height,font_path,initial_font_size)
102
+ image = rend.render_text_with_dynamic_spacing(image, item_description + '\n\n' + item_properties, description_position, description_area_width, description_area_height,font_path,initial_font_size, description = True)
103
+ paste_image_and_resize(image, value_overlay_path,x_position= 0,y_position=0, img_width= 768, img_height= 1024)
104
+ image = rend.render_text_with_dynamic_spacing(image, item_value, value_position, value_area_width, value_area_height,font_path,initial_font_size)
105
+ image = rend.render_text_with_dynamic_spacing(image, item_quote, quote_position, quote_area_width, quote_area_height,italics_font_path,initial_font_size, quote = True)
106
+ #Paste Sizzek Sticker
107
+ paste_image_and_resize(image, sticker_path_dictionary,x_position= 0,y_position=909, img_width= 115, img_height= 115, purchased_item_key= item_rarity)
108
+
109
+ # Add blur, gives it a less artificial look, put into list and return the list since gallery requires lists
110
+ image = image.filter(ImageFilter.GaussianBlur(.5))
111
+ image_list.append(image)
112
+
113
+ image = image.save(f"./output/{item_name}.png")
114
+
115
+
116
+
117
+ return image_list
118
+
119
+
120
+ #render_text_on_card('./card_templates/Shining Sunset Border.png',test_item )
121
+
122
+
123
+
124
+
card_parts/Common.png ADDED

Git LFS Details

  • SHA256: 34c5e1d977377ef156a2f146dbd6a49a870bbe4c5fc06948e9026f0b8340c029
  • Pointer size: 132 Bytes
  • Size of remote file: 1.35 MB
card_parts/Legendary.png ADDED

Git LFS Details

  • SHA256: e35e4bdbbcff19e9c0c625460128c4a13c5b9ed5d4e663c35a4357f951be0bc1
  • Pointer size: 132 Bytes
  • Size of remote file: 1.4 MB
card_parts/Rare.png ADDED

Git LFS Details

  • SHA256: 7d95030a73ca3985e5c61044459ba99f624ff667507ea67042822efbd1d2b421
  • Pointer size: 132 Bytes
  • Size of remote file: 1.3 MB
card_parts/Sizzek Sticker.png ADDED

Git LFS Details

  • SHA256: 863b193d8f637c343a83d2b8942fe5c9715888843bef3e595f3a927789e0917a
  • Pointer size: 131 Bytes
  • Size of remote file: 963 kB
card_parts/Uncommon.png ADDED

Git LFS Details

  • SHA256: 7e772c8dc34ac80ab87cc1b6b1f220c3ecfb15db844d76a913e16190841cc9a3
  • Pointer size: 132 Bytes
  • Size of remote file: 1.33 MB
card_parts/Value_box_transparent.png ADDED

Git LFS Details

  • SHA256: ea2ffa23ddcc6e77ee5d56f6963920dfa74bf33b4925c7afbaa077b0e61acae3
  • Pointer size: 130 Bytes
  • Size of remote file: 27.8 kB
card_parts/Very Rare.png ADDED

Git LFS Details

  • SHA256: 64c7765ee4b0e872a7b71b97cb398d8e4d57d5e53e9e07a6d15ac29d405e1ac4
  • Pointer size: 132 Bytes
  • Size of remote file: 1.23 MB
card_parts/white-fill-title-detail-value-transparent.png ADDED

Git LFS Details

  • SHA256: e3fb9d83290603ac2c8e4a07030d2696c6f144dadc371e11ea0c4e097cba9045
  • Pointer size: 130 Bytes
  • Size of remote file: 19.3 kB
fonts/Balgruf.ttf ADDED
Binary file (85.4 kB). View file
 
fonts/BalgrufItalic.ttf ADDED
Binary file (79.4 kB). View file
 
fonts/Goudy Medieval Medieval.ttf ADDED
Binary file (53.6 kB). View file
 
fonts/balgruf-font/Balgruf.ttf ADDED
Binary file (85.4 kB). View file
 
fonts/balgruf-font/BalgrufItalic.ttf ADDED
Binary file (79.4 kB). View file
 
fonts/balgruf-font/info.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ license: SIL Open Font License (OFL)
2
+ link: https://www.fontspace.com/balgruf-font-f59539
fonts/balgruf-font/misc/Balgruf-31cd.woff2 ADDED
Binary file (31.7 kB). View file
 
fonts/balgruf-font/misc/Balgruf-d256.woff ADDED
Binary file (39.8 kB). View file
 
fonts/balgruf-font/misc/Balgruf_Italic-52d6.woff ADDED
Binary file (38.8 kB). View file
 
fonts/balgruf-font/misc/Balgruf_Italic-e184.woff2 ADDED
Binary file (31.1 kB). View file
 
img2img.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import (StableDiffusionXLImg2ImgPipeline, AutoencoderKL)
2
+ from diffusers.utils import load_image
3
+ import torch
4
+ import time
5
+ import utilities as u
6
+ import card_generator as card
7
+ from PIL import Image
8
+
9
+ pipe = None
10
+ start_time = time.time()
11
+ torch.backends.cuda.matmul.allow_tf32 = True
12
+ model_path = ("./models/stable-diffusion/card-generator-v1.safetensors")
13
+ lora_path = "./models/stable-diffusion/Loras/blank-card-template-5.safetensors"
14
+ detail_lora_path = "./models/stable-diffusion/Loras/add-detail-xl.safetensors"
15
+ mimic_lora_path = "./models/stable-diffusion/Loras/EnvyMimicXL01.safetensors"
16
+ temp_image_path = "./image_temp/"
17
+ card_pre_prompt = " blank magic card,high resolution, detailed intricate high quality border, textbox, high quality detailed magnum opus drawing of a "
18
+ negative_prompts = "text, words, numbers, letters"
19
+ image_list = []
20
+
21
+
22
+ def load_img_gen(prompt, item, mimic = None):
23
+ prompt = card_pre_prompt + item + ' ' + prompt
24
+ print(prompt)
25
+ # image_path = f"{user_input_template}"
26
+ # init_image = load_image(image_path).convert("RGB")
27
+
28
+ pipe = StableDiffusionXLImg2ImgPipeline.from_single_file(model_path,
29
+ custom_pipeline="low_stable_diffusion",
30
+ torch_dtype=torch.float16,
31
+ variant="fp16").to("cuda")
32
+ # Load LoRAs for controlling image
33
+ #pipe.load_lora_weights(lora_path, weight_name = "blank-card-template-5.safetensors",adapter_name = 'blank-card-template')
34
+ pipe.load_lora_weights(detail_lora_path, weight_name = "add-detail-xl.safetensors", adapter_name = "add-detail-xl")
35
+
36
+ # If mimic keyword has been detected, load the mimic LoRA and set adapter values
37
+ if mimic:
38
+ pipe.load_lora_weights(mimic_lora_path, weight_name = "EnvyMimicXL01.safetensors", adapter_name = "EnvyMimicXL")
39
+ pipe.set_adapters(['blank-card-template', "add-detail-xl", "EnvyMimicXL"], adapter_weights = [0.9,0.9,1.0])
40
+ else :
41
+ pipe.set_adapters([ "add-detail-xl"], adapter_weights = [0.9])
42
+ pipe.enable_vae_slicing()
43
+ return pipe, prompt
44
+
45
+ def preview_and_generate_image(x,pipe, prompt, user_input_template, item):
46
+ img_start = time.time()
47
+ image = pipe(prompt=prompt,
48
+ strength = .9,
49
+ guidance_scale = 5,
50
+ image= user_input_template,
51
+ negative_promt = negative_prompts,
52
+ num_inference_steps=40,
53
+ height = 1024, width = 768).images[0]
54
+
55
+ image = image.save(temp_image_path+str(x) + f"{item}.png")
56
+ output_image_path = temp_image_path+str(x) + f"{item}.png"
57
+ img_time = time.time() - img_start
58
+ img_its = 50/img_time
59
+ print(f"image gen time = {img_time} and {img_its} it/s")
60
+
61
+ # Delete the image variable to keep VRAM open to load the LLM
62
+ del image
63
+ print(f"Memory after del {torch.cuda.memory_allocated()}")
64
+ print(image_list)
65
+ total_time = time.time() - start_time
66
+ print(total_time)
67
+
68
+ return output_image_path
69
+
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+
item_dict_gen.py ADDED
@@ -0,0 +1,368 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from llama_cpp import Llama
2
+ import ast
3
+ import gc
4
+ import torch
5
+
6
+ model_path = "./models/starling-lm-7b-alpha.Q8_0.gguf"
7
+
8
+ def load_llm(user_input):
9
+ llm = Llama(
10
+ model_path=model_path,
11
+ n_ctx=8192, # The max sequence length to use - note that longer sequence lengths require much more resources
12
+ n_threads=8, # The number of CPU threads to use, tailor to your system and the resulting performance
13
+ n_gpu_layers=32 # The number of layers to offload to GPU, if you have GPU acceleration available
14
+ )
15
+ return llm(
16
+ f"GPT4 User: {prompt_instructions} the item is {user_input}: <|end_of_turn|>GPT4 Assistant:", # Prompt
17
+ max_tokens=768, # Generate up to 512 tokens
18
+ stop=["</s>"], # Example stop token - not necessarily correct for this specific model! Please check before using.
19
+ echo=False # Whether to echo the prompt
20
+ )
21
+
22
+ def call_llm_and_cleanup(user_input):
23
+ # Call the LLM and store its output
24
+ llm_output = load_llm(user_input)
25
+ print(llm_output['choices'][0]['text'])
26
+ gc.collect()
27
+ if torch.cuda.is_available():
28
+ torch.cuda.empty_cache() # Clear VRAM allocated by PyTorch
29
+
30
+ # llm_output is still available for use here
31
+
32
+ return llm_output
33
+
34
+ def convert_to_dict(string):
35
+ # Evaluate if string is dictionary literal
36
+ try:
37
+ result = ast.literal_eval(string)
38
+ if isinstance(result, dict):
39
+ print("Item dictionary is valid")
40
+ return result
41
+ # If not, modify by attempting to add brackets to where they tend to fail to generate.
42
+ else:
43
+ modified_string = '{' + string
44
+ if isinstance(modified_string, dict):
45
+ return modified_string
46
+ modified_string = string + '}'
47
+ if isinstance(modified_string, dict):
48
+ return modified_string
49
+ modified_string = '{' + string + '}'
50
+ if isinstance(modified_string, dict):
51
+ return modified_string
52
+ except (ValueError, SyntaxError) :
53
+ print("Dictionary not valid")
54
+ return None
55
+
56
+
57
+ # Instructions past 4 are not time tested and may need to be removed.
58
+ ### Meta prompted :
59
+ prompt_instructions = """ **Purpose**: Generate a structured inventory entry for a specific item as a hashmap. Follow the format provided in the examples below.
60
+
61
+ **Instructions**:
62
+ 1. Replace `{item}` with the name of the user item, DO NOT CHANGE THE USER ITEM NAME enclosed in single quotes (e.g., `'Magic Wand'`).
63
+ 2. Ensure your request is formatted as a hashmap.
64
+ 3. Weapons MUST have a key 'Damage'
65
+ 4. The description should be brief and puncy, or concise and thoughtful.
66
+ 5. The quote and SD Prompt MUST be inside double quotations ie " ".
67
+ 6. The quote is from the perspective of someone commenting on the impact of the {item} on their life
68
+ 7. Value should be assigned as an integer of copper pieces (cp), silver pieces (sp), electrum pieces (ep), gold pieces (gp), and platinum pieces (pp).
69
+ 8. Use this table for reference on value :
70
+ 1 cp 1 lb. of wheat
71
+ 2 cp 1 lb. of flour or one chicken
72
+ 5 cp 1 lb. of salt
73
+ 1 sp 1 lb. of iron or 1 sq. yd. of canvas
74
+ 5 sp 1 lb. of copper or 1 sq. yd. of cotton cloth
75
+ 1 gp 1 lb. of ginger or one goat
76
+ 2 gp 1 lb. of cinnamon or pepper, or one sheep
77
+ 3 gp 1 lb. of cloves or one pig
78
+ 5 gp 1 lb. of silver or 1 sq. yd. of linen
79
+ 10 gp 1 sq. yd. of silk or one cow
80
+ 15 gp 1 lb. of saffron or one ox
81
+ 50 gp 1 lb. of gold
82
+ 500 gp 1 lb. of platinum
83
+
84
+
85
+ 300 gp +1 Melee or Ranged Weapon
86
+ 2000 gp +2 Melee or Ranged Weapon
87
+ 10000 gp +3 Melee or Ranged Weapon
88
+ 300 gp +1 Armor Uncommon
89
+ 2000 gp +2 Armor Rare
90
+ 10000 gp +3 Armor Very Rare
91
+ 300 gp +1 Shield Uncommon
92
+ 2000 gp +2 Shield Rare
93
+ 10000 gp +3 Shield Very Rare
94
+
95
+ 9. Examples of Magical Scroll Value:
96
+ Common: 50-100 gp
97
+ Uncommon: 101-500 gp
98
+ Rare: 501-5000 gp
99
+ Very rare: 5001-50000 gp
100
+ Legendary: 50001+ gp
101
+
102
+ A scroll's rarity depends on the spell's level:
103
+ Cantrip-1: Common
104
+ 2-3: Uncommon
105
+ 4-5: Rare
106
+ 6-8: Very rare
107
+ 9: Legendary
108
+
109
+ 10. Explanation of Mimics:
110
+ Mimics are shapeshifting predators able to take on the form of inanimate objects to lure creatures to their doom. In dungeons, these cunning creatures most often take the form of doors and chests, having learned that such forms attract a steady stream of prey.
111
+ Imitative Predators. Mimics can alter their outward texture to resemble wood, stone, and other basic materials, and they have evolved to assume the appearance of objects that other creatures are likely to come into contact with. A mimic in its altered form is nearly unrecognizable until potential prey blunders into its reach, whereupon the monster sprouts pseudopods and attacks.
112
+ When it changes shape, a mimic excretes an adhesive that helps it seize prey and weapons that touch it. The adhesive is absorbed when the mimic assumes its amorphous form and on parts the mimic uses to move itself.
113
+ Cunning Hunters. Mimics live and hunt alone, though they occasionally share their feeding grounds with other creatures. Although most mimics have only predatory intelligence, a rare few evolve greater cunning and the ability to carry on simple conversations in Common or Undercommon. Such mimics might allow safe passage through their domains or provide useful information in exchange for food.
114
+
115
+ 11.
116
+ **Format Example**:
117
+ - **Dictionary Structure**:
118
+
119
+ {"{item}": {
120
+ 'Name': "{item name}",
121
+ 'Type': '{item type}',
122
+ 'Rarity': '{item rarity},
123
+ 'Value': '{item value}',
124
+ 'Properties': ["{property1}", "{property2}", ...],
125
+ 'Damage': '{damage formula} , '{damage type}',
126
+ 'Weight': '{weight}',
127
+ 'Description': "{item description}",
128
+ 'Quote': "{item quote}",
129
+ 'SD Prompt': "{special description for the item}"
130
+ } }
131
+
132
+ - **Input Placeholder**:
133
+ - "{item}": Replace with the item name, ensuring it's wrapped in single quotes.
134
+
135
+ **Output Examples**:
136
+ 1. Cloak of Whispering Shadows Entry:
137
+
138
+ {"Cloak of Whispering Shadows": {
139
+ 'Name': 'Cloak of Whispering Shadows',
140
+ 'Type': 'Cloak',
141
+ 'Rarity': 'Very Rare',
142
+ 'Value': '7500 gp',
143
+ 'Properties': ["Grants invisibility in dim light or darkness","Allows communication with shadows for gathering information"],
144
+ 'Weight': '1 lb',
145
+ 'Description': "A cloak woven from the essence of twilight, blending its wearer into the shadows. Whispers of the past and present linger in its folds, offering secrets to those who listen.",
146
+ 'Quote': "In the embrace of night, secrets surface in the silent whispers of the dark.",
147
+ 'SD Prompt': " Cloak of deep indigo almost black, swirling patterns that shift and move with every step. As it drapes over one's shoulders, an eerie connection forms between the wearer and darkness itself."
148
+ } }
149
+
150
+ 2. Health Potion Entry:
151
+
152
+ {"Health Potion": {
153
+ 'Name' : "Health Portion",
154
+ 'Type' : 'Potion',
155
+ 'Rarity' : 'Common',
156
+ 'Value': '50 gp',
157
+ 'Properties': ["Quafable", "Restores 1d4 + 2 HP upon consumption"],
158
+ 'Weight': '0.5 lb',
159
+ 'Description': "Contained within this small vial is a crimson liquid that sparkles when shaken, a life-saving elixir for those who brave the unknown.",
160
+ 'Quote': "To the weary, a drop of hope; to the fallen, a chance to stand once more.",
161
+ 'SD Prompt' : " a small, delicate vial containing a sparkling crimson liquid. Emit a soft glow, suggesting its restorative properties. The vial is set against a dark, ambiguous background."
162
+ } }
163
+
164
+ 3. Wooden Shield Entry:
165
+
166
+ {"Wooden Shield": {
167
+ 'Name' : "Wooden Shield",
168
+ 'Type' : 'Armor, Shield',
169
+ 'Rarity': 'Common',
170
+ 'Value': '10 gp',
171
+ 'Properties': ["+2 AC"],
172
+ 'Weight': '6 lb',
173
+ 'Description': "Sturdy and reliable, this wooden shield is a simple yet effective defense against the blows of adversaries.",
174
+ 'Quote': "In the rhythm of battle, it dances - a barrier between life and defeat.",
175
+ 'SD Prompt': " a sturdy wooden shield, a symbol of defense, with a simple yet solid design. The shield, has visible grain patterns and a few battle scars. It stands as a steadfast protector, embodying the essence of a warrior's resilience in the face of adversity."
176
+ } }
177
+
178
+ 4. Helmet of Perception Entry:
179
+
180
+ {"Helmet of Perception": {
181
+ 'Name' : "Helmet of Perception",
182
+ 'Type' : 'Magical Item (armor, helmet)',
183
+ 'Rarity': 'Very Rare',
184
+ 'Value': '3000 gp',
185
+ 'Properties': ["+ 1 to AC", "Grants the wearer advantage on perception checks", "+5 to passive perception"],
186
+ 'Weight': '3 lb',
187
+ 'Description': "Forged from mystic metals and enchanted with ancient spells, this helmet offers protection beyond the physical realm.",
188
+ 'Quote': "A crown not of royalty, but of unyielding vigilance, warding off the unseen threats that lurk in the shadows.",
189
+ 'SD Prompt': " a mystical helmet crafted from enchanted metals, glowing with subtle runes. imbued with spells, radiates a mystical aura, symbolizing enhanced perception and vigilance,elegant,formidable"
190
+ } }
191
+
192
+ 5. Longbow Entry:
193
+
194
+ {"Longbow": {
195
+ 'Name': "Longbow",
196
+ 'Type': 'Ranged Weapon (martial, longbow)',
197
+ 'Rarity': 'Common',
198
+ 'Value': '50 gp',
199
+ 'Properties': ["2-handed", "Range 150/600", "Loading"],
200
+ 'Damage': '1d8 + Dex, piercing',
201
+ 'Weight': '6 lb',
202
+ 'Description': "With a sleek and elegant design, this longbow is crafted for speed and precision, capable of striking down foes from a distance.",
203
+ 'Quote': "From the shadows it emerges, a silent whisper of steel that pierces the veil of darkness, bringing justice to those who dare to trespass.",
204
+ 'SD Prompt' : "a longbow with intricate carvings and stone inlay with a black string"
205
+ } }
206
+
207
+
208
+ 6. Mace Entry:
209
+
210
+ {"Mace": {
211
+ 'Name': "Mace",
212
+ 'Type': 'Melee Weapon (martial, bludgeoning)',
213
+ 'Rarity': 'Common',
214
+ 'Value': '25 gp',
215
+ 'Properties': ["Bludgeoning", "One-handed"],
216
+ 'Damage': '1d6 + str, bludgeoning',
217
+ 'Weight': '6 lb',
218
+ 'Description': "This mace is a fearsome sight, its head a heavy and menacing ball of metal designed to crush bone and break spirits.",
219
+ 'Quote': "With each swing, it sings a melody of pain and retribution, an anthem of justice to those who wield it.",
220
+ 'SD Prompt': "a menacing metal spike ball mace, designed for bludgeoning, with a heavy, intimidating head, embodying a tool for bone-crushing and spirit-breaking."
221
+ } }
222
+
223
+ 7. Flying Carpet Entry:
224
+
225
+ {"Flying Carpet": {
226
+ 'Name': "Flying Carpet",
227
+ 'Type': 'Magical Item (transportation)',
228
+ 'Rarity': 'Very Rare',
229
+ 'Value': '3000 gp',
230
+ 'Properties': ["Flying", "Personal Flight", "Up to 2 passengers", Speed : 60 ft],
231
+ 'Weight': '50 lb',
232
+ 'Description': "This enchanted carpet whisks its riders through the skies, providing a swift and comfortable mode of transport across great distances.",
233
+ 'Quote': "Soar above the mundane, and embrace the winds of adventure with this magical gift from the heavens.",
234
+ 'SD Prompt': "a vibrant, intricately patterned flying carpet soaring high in the sky, with clouds and a clear blue backdrop, emphasizing its magical essence and freedom of flight"
235
+ } }
236
+
237
+ 8. Tome of Endless Stories Entry:
238
+
239
+ {"Tome of Endless Stories": {
240
+ 'Name': "Tome of Endless Stories",
241
+ 'Type': 'Book',
242
+ 'Rarity': 'Uncommon'
243
+ 'Value': '500 gp',
244
+ 'Properties': [
245
+ "Generates a new story or piece of lore each day",
246
+ "Reading a story grants insight or a hint towards solving a problem or puzzle"
247
+ ],
248
+ 'Weight': '3 lbs',
249
+ 'Description': "An ancient tome bound in leather that shifts colors like the sunset. Its pages are never-ending, filled with tales from worlds both known and undiscovered.",
250
+ 'Quote': "Within its pages lie the keys to a thousand worlds, each story a doorway to infinite possibilities.",
251
+ 'SD Prompt': "leather-bound with gold and silver inlay, pages appear aged but are incredibly durable, magical glyphs shimmer softly on the cover."
252
+ } }
253
+
254
+ 9. Ring of Miniature Summoning Entry:
255
+
256
+ {"Ring of Miniature Summoning": {
257
+ 'Name': "Ring of Miniature Summoning",
258
+ 'Type': 'Ring',
259
+ 'Rarity': 'Rare',
260
+ 'Value': '1500 gp',
261
+ 'Properties': ["Summons a miniature beast ally once per day", "Beast follows commands and lasts for 1 hour", "Choice of beast changes with each dawn"],
262
+ 'Weight': '0 lb',
263
+ 'Description': "A delicate ring with a gem that shifts colors. When activated, it brings forth a small, loyal beast companion from the ether.",
264
+ 'Quote': "Not all companions walk beside us. Some are summoned from the depths of magic, small in size but vast in heart.",
265
+ 'SD Prompt': "gemstone with changing colors, essence of companionship and versatility."
266
+ } }
267
+
268
+
269
+ 10. Spoon of Tasting Entry:
270
+
271
+ {"Spoon of Tasting": {
272
+ 'Name': "Spoon of Tasting",
273
+ 'Type': 'Spoon',
274
+ 'Rarity': 'Uncommon',
275
+ 'Value': '200 gp',
276
+ 'Properties': ["When used to taste any dish, it can instantly tell you all the ingredients", "Provides exaggerated compliments or critiques about the dish"],
277
+ 'Weight': '0.2 lb',
278
+ 'Description': "A culinary critic’s dream or nightmare. This spoon doesn’t hold back its opinions on any dish it tastes.",
279
+ 'Quote': "A spoonful of sugar helps the criticism go down.",
280
+ 'SD Prompt': "Looks like an ordinary spoon, but with a mouth that speaks more than you’d expect."
281
+ } }
282
+
283
+ 11. Infinite Scroll Entry:
284
+
285
+ {"Infinite Scroll": {
286
+ 'Name': "Infinite Scroll",
287
+ 'Type': 'Magical Scroll',
288
+ 'Rarity': 'Legendary',
289
+ 'Value': '25000',
290
+ 'Properties': [
291
+ "Endlessly Extends with New Knowledge","Reveals Content Based on Reader’s Need or Desire","Cannot be Fully Transcribed"],
292
+ 'Weight': '0.5 lb',
293
+ 'Description': "This scroll appears to be a standard parchment at first glance. However, as one begins to read, it unrolls to reveal an ever-expanding tapestry of knowledge, lore, and spells that seems to have no end.",
294
+ 'Quote': "In the pursuit of knowledge, the horizon is ever receding. So too is the content of this scroll, an endless journey within a parchment’s bounds.",
295
+ 'SD Prompt': "A seemingly ordinary scroll that extends indefinitely"
296
+ } }
297
+
298
+ 12. Mimic Treasure Chest Entry:
299
+
300
+ {"Mimic Treasure Chest": {
301
+ 'Name': "Mimic Treasure Chest",
302
+ 'Type': 'Trap',
303
+ 'Rarity': 'Rare',
304
+ 'Value': '1000 gp', # Increased value reflects its dangerous and rare nature
305
+ 'Properties': ["Deceptively inviting","Springs to life when interacted with","Capable of attacking unwary adventurers"],
306
+ 'Weight': '50 lb', # Mimics are heavy due to their monstrous nature
307
+ 'Description': "This enticing treasure chest is a deadly Mimic, luring adventurers with the promise of riches only to unleash its monstrous true form upon those who dare to approach, turning their greed into a fight for survival.",
308
+ 'SD Prompt': "A seemingly ordinary treasure chest that glimmers with promise. Upon closer inspection, sinister, almost living edges move with malice, revealing its true nature as a Mimic, ready to unleash fury on the unwary."
309
+ } }
310
+
311
+ 13. Hammer of Thunderbolts Entry:
312
+
313
+ {'Hammer of Thunderbolts': {
314
+ 'Name': 'Hammer of Thunderbolts',
315
+ 'Type': 'Melee Weapon (maul, bludgeoning)',
316
+ 'Rarity': 'Legendary',
317
+ 'Value': '16000',
318
+ 'Damage': '2d6 + 1 (martial, bludgeoning)',
319
+ 'Properties': ["requires attunement","Giant's Bane","must be wearing a belt of giant strength and gauntlets of ogre power","Str +4","Can excees 20 but not 30","20 against giant, DC 17 save against death","5 charges, expend 1 to make a range attack 20/60","ranged attack releases thunderclap on hit, DC 17 save against stunned 30 ft","regain 1d4+1 charges at dawn"],
320
+ 'Weight': 15 lb',
321
+ 'Description': "God-forged and storm-bound, a supreme force, its rune-etched head blazing with power. More than a weapon, it's a symbol of nature's fury, capable of reshaping landscapes and commanding elements with every strike.",
322
+ 'Quote': "When the skies rage and the earth trembles, know that the Hammer of Thunderbolts has found its mark. It is not merely a weapon, but the embodiment of the storm\'s wrath wielded by those deemed worthy.",
323
+ 'SD Prompt': "It radiates with electric energy, its rune-etched head and storm-weathered leather grip symbolizing its dominion over storms. In its grasp, it pulses with the potential to summon the heavens' fury, embodying the tempest's raw power."
324
+ } }
325
+
326
+ 14. Shadow Lamp Entry:
327
+
328
+ {'Shadow Lamp': {
329
+ 'Name': 'Shadow Lamp',
330
+ 'Type': 'Magical Item',
331
+ 'Rarity': 'Uncommon',
332
+ 'Value': '500 gp',
333
+ 'Properties': ["Provides dim light in a 20-foot radius", "Invisibility to darkness-based senses", "Can cast Darkness spell once per day"],
334
+ 'Weight': '1 lb',
335
+ 'Description': "A small lamp carved from obsidian and powered by a mysterious force, it casts an eerie glow that illuminates its surroundings while making the wielder invisible to those relying on darkness-based senses.",
336
+ 'Quote': "In the heart of shadow lies an unseen light, casting away darkness and revealing what was once unseen.",
337
+ 'SD Prompt': "Glass lantern filled with inky swirling shadows, black gaseous clouds flow out, blackness flows from it, spooky, sneaky"
338
+ } }
339
+
340
+ 15. Dark Mirror:
341
+
342
+ {'Dark Mirror': {
343
+ 'Name': 'Dark Mirror',
344
+ 'Type': 'Magical Item',
345
+ 'Rarity': 'Rare',
346
+ 'Value': '600 gp',
347
+ 'Properties': ["Reflects only darkness when viewed from one side", "Grants invisibility to its reflection", "Can be used to cast Disguise Self spell once per day"],
348
+ 'Weight': '2 lb',
349
+ 'Description': "An ordinary-looking mirror with a dark, almost sinister tint. It reflects only darkness and distorted images when viewed from one side, making it an ideal tool for spies and those seeking to hide their true identity.",
350
+ 'Quote': "A glass that hides what lies within, a surface that reflects only darkness and deceit.",
351
+ 'SD Prompt': "Dark and mysterious black surfaced mirror with an obsidian flowing center with a tint of malice, its surface reflecting nothing but black and distorted images, swirling with tendrils, spooky, ethereal"
352
+ } }
353
+
354
+ 16. Moon-Touched Greatsword Entry:
355
+
356
+ {'Moon-Touched Greatsword':{
357
+ 'Name': 'Moontouched Greatsword',
358
+ 'Type': 'Melee Weapon (greatsword, slashing)',
359
+ 'Rarity': 'Very Rare',
360
+ 'Value': '8000 gp',
361
+ 'Damage': '2d6 + Str slashing',
362
+ 'Properties': ["Adds +2 to attack and damage rolls while wielder is under the effects of Moonbeam or Daylight spells", "Requires attunement"],
363
+ 'Weight': '6 lb',
364
+ 'Description': "Forged from lunar metal and imbued with celestial magic, this greatsword gleams like a silver crescent moon, its edge sharp enough to cut through the darkest shadows.",
365
+ 'Quote': "With each swing, it sings a melody of light that pierces the veil of darkness, a beacon of hope and justice.",
366
+ 'SD Prompt': "A silver greatsword with a crescent moon-shaped blade that reflects a soft glow, reminiscent of the moon's radiance. The hilt is wrapped in silvery leather, and the metal seems to shimmer and change with the light, reflecting the lunar cycles."
367
+ } }
368
+ """
main.py ADDED
@@ -0,0 +1,303 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import img2img
3
+ import card_generator as card
4
+ import utilities as u
5
+ import ctypes
6
+ import user_input as useri
7
+ import gradio as gr
8
+ import template_builder as tb
9
+
10
+ # This is a fix for the way that python doesn't release system memory back to the OS and it was leading to locking up the system
11
+ libc = ctypes.cdll.LoadLibrary("libc.so.6")
12
+ M_MMAP_THRESHOLD = -3
13
+
14
+ # Set malloc mmap threshold.
15
+ libc.mallopt(M_MMAP_THRESHOLD, 2**20)
16
+ initial_name = "A Crowbar"
17
+
18
+ with gr.Blocks() as demo:
19
+
20
+ # Functions and State Variables
21
+ # Build functions W/in the Gradio format, because it only allows modification within it's context
22
+ # Define inputs to match what is called on click, and output of the function as a list that matches the list of outputs
23
+ textbox_default_dict = {'Name':'', \
24
+ 'Type': '',
25
+ 'Rarity':'',
26
+ 'Value':'',
27
+ 'Properties':'',
28
+ 'Damage':'',
29
+ 'Weight':'',
30
+ 'Description':'',
31
+ 'Quote':'',
32
+ 'SD Prompt':''
33
+ }
34
+
35
+ item_name_var = gr.State()
36
+ item_type_var = gr.State()
37
+ item_rarity_var = gr.State()
38
+ item_value_var = gr.State()
39
+ item_properties_var = gr.State()
40
+ item_damage_var = gr.State()
41
+ item_weight_var = gr.State()
42
+ item_description_var = gr.State()
43
+ item_quote_var = gr.State()
44
+ item_sd_prompt_var = gr.State('')
45
+
46
+ selected_border_image = gr.State('./card_templates/Moonstone Border.png')
47
+ num_image_to_generate = gr.State(4)
48
+ generated_image_list = gr.State([])
49
+ selected_generated_image = gr.State()
50
+ selected_seed_image = gr.State()
51
+ built_template = gr.State()
52
+ mimic = None
53
+
54
+ def set_textbox_defaults(textbox_default_dict, key):
55
+ item_name = textbox_default_dict[key]
56
+ return item_name
57
+
58
+ # Function called when user generates item info, then assign values of dictionary to variables, output once to State, twice to textbox
59
+ def generate_text_update_textboxes(user_input):
60
+ u.reclaim_mem()
61
+
62
+ llm_output=useri.call_llm(user_input)
63
+ item_key = list(llm_output.keys())
64
+
65
+ item_key_values = list(llm_output[item_key[0]].keys())
66
+ item_name = llm_output[item_key[0]]['Name']
67
+ item_type = llm_output[item_key[0]]['Type']
68
+ item_rarity = llm_output[item_key[0]]['Rarity']
69
+ item_value = llm_output[item_key[0]]['Value']
70
+ item_properties = llm_output[item_key[0]]['Properties']
71
+
72
+ if 'Damage' in item_key_values:
73
+ item_damage = llm_output[item_key[0]]['Damage']
74
+ else: item_damage = ''
75
+
76
+ item_weight = llm_output[item_key[0]]['Weight']
77
+ item_description = llm_output[item_key[0]]['Description']
78
+ item_quote = llm_output[item_key[0]]['Quote']
79
+ sd_prompt = llm_output[item_key[0]]['SD Prompt']
80
+
81
+ return [item_name, item_name,
82
+ item_type, item_type,
83
+ item_rarity, item_rarity,
84
+ item_value, item_value,
85
+ item_properties, item_properties,
86
+ item_damage, item_damage,
87
+ item_weight, item_weight,
88
+ item_description, item_description,
89
+ item_quote, item_quote,
90
+ sd_prompt, sd_prompt]
91
+
92
+ # Called on user selecting an image from the gallery, outputs the path of the image
93
+ def assign_img_path(evt: gr.SelectData):
94
+ img_dict = evt.value
95
+ print(img_dict)
96
+ selected_image_path = img_dict['image']['url']
97
+ print(selected_image_path)
98
+ return selected_image_path
99
+
100
+ # Make a list of files in image_temp and delete them
101
+ def delete_temp_images():
102
+ image_list = u.directory_contents('./image_temp')
103
+ u.delete_files(image_list)
104
+ img2img.image_list.clear()
105
+
106
+ # Called when pressing button to generate image, updates gallery by returning the list of image URLs
107
+ def generate_image_update_gallery(num_img, sd_prompt,item_name, built_template):
108
+ delete_temp_images()
109
+ print(type(built_template))
110
+ image_list = []
111
+ img_gen, prompt = img2img.load_img_gen(sd_prompt, item_name)
112
+ for x in range(num_img):
113
+ preview = img2img.preview_and_generate_image(x,img_gen, prompt, built_template, item_name)
114
+ image_list.append(preview)
115
+ yield image_list
116
+ #generate_gallery.change(image_list)
117
+ del preview
118
+ u.reclaim_mem()
119
+
120
+ #generated_image_list = img2img.generate_image(num_img,sd_prompt,item_name,selected_border)
121
+ return image_list
122
+
123
+ def build_template(selected_border, selected_seed_image):
124
+ image_list = tb.build_card_template(selected_border, selected_seed_image)
125
+ return image_list, image_list
126
+
127
+
128
+ # Beginning of UI Page
129
+ gr.HTML(""" <div id="inner"> <header>
130
+ <h1>Item Card Generator</h1>
131
+ <p>
132
+ With this AI driven tool you will build a collectible style card of a fantasy flavored item with details.
133
+ </p>
134
+ </div>""")
135
+
136
+ gr.HTML(""" <div id="inner"> <header>
137
+ <h2><b>First:</b> Build a Card Template</h2>
138
+ </div>""")
139
+ with gr.Row():
140
+ with gr.Column():
141
+
142
+ # Template Gallery instructions
143
+ gr.HTML(""" <div id="inner"> <header>
144
+ <h3>1. Click a border from the 'Card Template Gallery'</h3>
145
+ </div>""")
146
+
147
+ border_gallery = gr.Gallery(label = "Card Template Gallery",
148
+ scale = 2,
149
+ value = useri.index_image_paths("Drakosfire/CardGenerator", "seed_images/card_templates"),
150
+ show_label = True,
151
+ columns = [3], rows = [3],
152
+ object_fit = "contain",
153
+ height = "auto",
154
+ elem_id = "Template Gallery")
155
+ with gr.Column():
156
+ gr.HTML(""" <div id="inner"> <header>
157
+ <h3>2. Click a image from the Seed Image Gallery</h3><br>
158
+ </div>""")
159
+
160
+ border_gallery.select(assign_img_path, outputs = selected_border_image)
161
+ seed_image_gallery = gr.Gallery(label= " Image Seed Gallery",
162
+ scale = 2,
163
+ value = useri.index_image_paths("Drakosfire/CardGenerator", "seed_images/item_seeds"),
164
+ show_label = True,
165
+ columns = [3], rows = [3],
166
+ object_fit = "contain",
167
+ height = "auto",
168
+ elem_id = "Template Gallery",
169
+ interactive=True)
170
+
171
+ gr.HTML(""" <div id="inner"> <header><h4> -Or- Upload your own seed image, by dropping it into the 'Generated Template Gallery' </h4><br>
172
+ <h3>3. Click 'Generate Card Template'</h3><br>
173
+ </div>""")
174
+
175
+ built_template_gallery = gr.Gallery(label= "Generated Template Gallery",
176
+ scale = 1,
177
+ value = None,
178
+ show_label = True,
179
+ columns = [4], rows = [4],
180
+ object_fit = "contain",
181
+ height = "auto",
182
+ elem_id = "Template Gallery",
183
+ interactive=True)
184
+
185
+ seed_image_gallery.select(assign_img_path, outputs = selected_seed_image)
186
+ built_template_gallery.upload(u.receive_upload, inputs=built_template_gallery, outputs= selected_seed_image)
187
+ build_card_template_button = gr.Button(value = "Generate Card Template")
188
+ build_card_template_button.click(build_template, inputs = [selected_border_image, selected_seed_image], outputs = [built_template_gallery, built_template])
189
+
190
+ gr.HTML(""" <div id="inner"> <header>
191
+ <h2><b>Second:</b> Generate Item Text </h2>
192
+ </div>""")
193
+ gr.HTML(""" <div id="inner"> <header>
194
+ <h3>1. Use a few words to describe the item then click 'Generate Text' </h3>
195
+ </div>""")
196
+ with gr.Row():
197
+ user_input = gr.Textbox(label = 'Item', lines =1, placeholder= "Flaming Magical Sword", elem_id= "Item", scale =4)
198
+ item_text_generate = gr.Button(value = "Generate item text", scale=1)
199
+
200
+ gr.HTML(""" <div id="inner"> <header>
201
+ <h3> 2. Review and Edit the text</h3>
202
+ </div>""")
203
+ with gr.Row():
204
+
205
+ # Build text boxes for the broken up item dictionary values
206
+ with gr.Column(scale = 1):
207
+ item_name_output = gr.Textbox(value = set_textbox_defaults(textbox_default_dict, 'Name'),label = 'Name', lines = 1, interactive=True, elem_id='Item Name')
208
+ item_type_output = gr.Textbox(value = set_textbox_defaults(textbox_default_dict, 'Type'),label = 'Type', lines = 1, interactive=True, elem_id='Item Type')
209
+ item_rarity_output = gr.Textbox(value = set_textbox_defaults(textbox_default_dict, 'Rarity'),label = 'Rarity : [Common, Uncommon, Rare, Very Rare, Legendary]', lines = 1, interactive=True, elem_id='Item Rarity')
210
+ item_value_output = gr.Textbox(value = set_textbox_defaults(textbox_default_dict, 'Value'),label = 'Value', lines = 1, interactive=True, elem_id='Item Value')
211
+
212
+ # Pass the user input and border template to the generator
213
+ with gr.Column(scale = 1):
214
+ item_damage_output = gr.Textbox(value = set_textbox_defaults(textbox_default_dict, 'Damage'),label = 'Damage', lines = 1, interactive=True, elem_id='Item Damage')
215
+ item_weight_output = gr.Textbox(value = set_textbox_defaults(textbox_default_dict, 'Weight'),label = 'Weight', lines = 1, interactive=True, elem_id='Item Weight')
216
+ item_description_output = gr.Textbox(value = set_textbox_defaults(textbox_default_dict, 'Description'),label = 'Description', lines = 1, interactive=True, elem_id='Item Description')
217
+ item_quote_output = gr.Textbox(value = set_textbox_defaults(textbox_default_dict, 'Quote'),label = 'Quote', lines = 1, interactive=True, elem_id='Item quote')
218
+ item_properties_output = gr.Textbox(value = set_textbox_defaults(textbox_default_dict, 'Properties'),label = 'Properties : [List of comma seperated values]', lines = 1, interactive=True, elem_id='Item Properties')
219
+
220
+ gr.HTML(""" <div id="inner"> <header>
221
+ <h3> 3. This text will be used to generate the card's image.</h3>
222
+ </div>""")
223
+ item_sd_prompt_output = gr.Textbox(label = 'Putting words or phrases in parenthesis adds weight. Example: (Flaming Magical :1.0) Sword.', value = set_textbox_defaults(textbox_default_dict, 'SD Prompt'), lines = 1, interactive=True, elem_id='SD Prompt')
224
+
225
+ gr.HTML(""" <div id="inner"> <header>
226
+ <h2> <b>Third:</b> Click 'Generate Cards' to generate 4 cards to choose from. </h2>
227
+ </div>""")
228
+ card_gen_button = gr.Button(value = "Generate Cards", elem_id="Generate Card Button")
229
+
230
+ # No longer Row Context, in context of entire Block
231
+ gr.HTML(""" <div id="inner"> <header>
232
+ <h2> <b>Fourth:</b> Click your favorite card then add text, or click 'Generate Four Card Options' again.<br>
233
+ </h2>
234
+ </div>""")
235
+
236
+ with gr.Row():
237
+ generate_gallery = gr.Gallery(label = "Generated Cards",
238
+ value = [],
239
+ show_label= True,
240
+ scale= 5,
241
+ columns =[2], rows = [2],
242
+ object_fit= "fill",
243
+ height = "768",
244
+ elem_id = "Generated Cards Gallery"
245
+ )
246
+ generate_final_item_card = gr.Button(value = "Add Text", elem_id = "Generate user card")
247
+
248
+ card_gen_button.click(fn = generate_image_update_gallery, inputs =[num_image_to_generate,item_sd_prompt_output,item_name_output,built_template], outputs= generate_gallery)
249
+ generate_gallery.select(assign_img_path, outputs = selected_generated_image)
250
+
251
+ # Button logice calls function when button object is pressed, passing inputs and passing output to components
252
+ llm_output = item_text_generate.click(generate_text_update_textboxes,
253
+ inputs = [user_input],
254
+ outputs= [item_name_var,
255
+ item_name_output,
256
+ item_type_var,
257
+ item_type_output,
258
+ item_rarity_var,
259
+ item_rarity_output,
260
+ item_value_var,
261
+ item_value_output,
262
+ item_properties_var,
263
+ item_properties_output,
264
+ item_damage_var,
265
+ item_damage_output,
266
+ item_weight_var,
267
+ item_weight_output,
268
+ item_description_var,
269
+ item_description_output,
270
+ item_quote_var,
271
+ item_quote_output,
272
+ item_sd_prompt_var,
273
+ item_sd_prompt_output])
274
+
275
+ generate_final_item_card.click(card.render_text_on_card, inputs = [selected_generated_image,
276
+ item_name_output,
277
+ item_type_output,
278
+ item_rarity_output,
279
+ item_value_output,
280
+ item_properties_output,
281
+ item_damage_output,
282
+ item_weight_output,
283
+ item_description_output,
284
+ item_quote_output
285
+ ],
286
+ outputs = generate_gallery )
287
+
288
+ if __name__ == '__main__':
289
+ demo.launch(server_name = "0.0.0.0", server_port = 8000, share = False, allowed_paths = ["/media/drakosfire/Shared/","/media/drakosfire/Shared/MerchantBot/card_templates"])
290
+
291
+
292
+
293
+
294
+
295
+
296
+
297
+
298
+
299
+
300
+
301
+
302
+
303
+
render_card_text.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image, ImageDraw, ImageFont
2
+
3
+ # Function for managing longer bodies of text and breaking into a list of lines to be printed based on input arguments
4
+ def split_text_into_lines(text, font, max_width, draw):
5
+ blocks = text.split('\n')
6
+ lines = []
7
+ for block in blocks:
8
+ words = block.split()
9
+ current_line = ''
10
+
11
+ for word in words:
12
+ # Check width with new word added
13
+ test_line = f"{current_line} {word}".strip()
14
+ test_width = draw.textlength(text = test_line, font=font)
15
+ if test_width <= max_width:
16
+ current_line = test_line
17
+ else:
18
+ #If the line with the new word exceeds the max width, start a new line
19
+ lines.append(current_line)
20
+ current_line = word
21
+ # add the last line
22
+ lines.append(current_line)
23
+ return lines
24
+ # Function for calculating the height of the text at the current font setting
25
+
26
+
27
+ def adjust_font_size_lines_and_spacing(text, font_path, initial_font_size, max_width, area_height, image) :
28
+ font_size = initial_font_size
29
+ optimal_font_size = font_size
30
+ optimal_lines = []
31
+ line_spacing_factor = 1.2 # multiple of font size that will get added between each line
32
+
33
+ while font_size > 10: # Set minimum font size
34
+ font = ImageFont.truetype(font_path, font_size)
35
+ draw = ImageDraw.Draw(image)
36
+ # Fitting text into box dimensions
37
+ lines = split_text_into_lines(text, font, max_width, draw)
38
+ # Calculate total height with dynamic line spacing
39
+ single_line_height = draw.textbbox((0, 0), "Ay", font=font)[3] - draw.textbbox((0, 0), "Ay", font=font)[1] # Height of 'Ay'
40
+ line_spacing = int(single_line_height * line_spacing_factor) - single_line_height
41
+ total_text_height = len(lines) * single_line_height + (len(lines) - 1) * line_spacing # Estimate total height of all lines by multiplying number of lines by font height plus number of lines -1 times line spacing
42
+
43
+ if total_text_height <= area_height :
44
+ optimal_font_size = font_size
45
+ optimal_lines = lines
46
+ break # Exit loop font fits in contraints
47
+
48
+ else:
49
+ font_size -= 1 # Reduce font by 1 to check if it fits
50
+
51
+ return optimal_font_size, optimal_lines, line_spacing
52
+ # Function that takes in an image,text and properties for textfrom card_generator
53
+ def render_text_with_dynamic_spacing(image, text, center_position, max_width, area_height,font_path, initial_font_size,description = None, quote = None):
54
+
55
+
56
+ optimal_font_size, optimal_lines, line_spacing = adjust_font_size_lines_and_spacing(
57
+ text, font_path, initial_font_size, max_width, area_height, image)
58
+ # create an object to draw on
59
+
60
+ font = ImageFont.truetype(font_path, optimal_font_size)
61
+ draw = ImageDraw.Draw(image)
62
+
63
+ # Shadow settings
64
+ shadow_offset = (1, 1) # X and Y offset for shadow
65
+ shadow_color = 'grey' # Shadow color
66
+
67
+ # Unsure about the following line, not sure if I want y_offset to be dynamic
68
+ y_offset = center_position[1]
69
+
70
+ if description or quote :
71
+ for line in optimal_lines:
72
+ line_width = draw.textlength(text = line, font=font)
73
+ x = center_position[0]
74
+ # Draw Shadow first
75
+ shadow_position = (x + shadow_offset[0], y_offset + shadow_offset[1])
76
+ draw.text(shadow_position, line, font=font, fill=shadow_color)
77
+ #Draw text
78
+ draw.text((x, y_offset), line, font=font, fill = 'black', align = "left" )
79
+ y_offset += optimal_font_size + line_spacing # Move to next line
80
+ return image
81
+
82
+ for line in optimal_lines:
83
+ line_width = draw.textlength(text = line, font=font)
84
+ x = center_position[0] - (line_width / 2)
85
+ # Draw Shadow first
86
+ shadow_position = (x + shadow_offset[0], y_offset + shadow_offset[1])
87
+ draw.text(shadow_position, line, font=font, fill=shadow_color)
88
+ #Draw text
89
+ draw.text((x, y_offset), line, font=font, fill = 'black', align = "left" )
90
+ y_offset += optimal_font_size + line_spacing # Move to next line
91
+ return image
92
+
93
+ # Function to put the description objects together, this will be the complicated bit, I think iterate through keys excluding title, type and cost
94
+
95
+
96
+
97
+
template_builder.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image, ImageDraw, ImageFont
2
+ import utilities as u
3
+
4
+ # Function to initialize image canvas
5
+
6
+ # Function to scale the seed image
7
+
8
+ # Paste seed image onto blank canvas at specific location
9
+
10
+ # Paste the selected border on top
11
+
12
+ # Save and return
13
+
14
+ # Function that takes in an image url and a dictionary and uses the values to print onto a card.
15
+
16
+ # Seed Image starting x,y
17
+ seed_x = 56
18
+ seed_y = 128
19
+ seed_width = 657
20
+ seed_height = 422
21
+
22
+ def paste_image_and_resize(base_image,sticker, x_position, y_position,img_width, img_height):
23
+
24
+ # Load the image to paste
25
+ # image_to_paste = Image.open(sticker_path)
26
+
27
+ # Define the new size (scale) for the image you're pasting
28
+
29
+ new_size = (img_width, img_height)
30
+
31
+ # Resize the image to the new size
32
+ sticker = sticker.resize(new_size)
33
+
34
+ # Specify the top-left corner where the resized image will be pasted
35
+ paste_position = (x_position, y_position) # Replace x and y with the coordinates
36
+
37
+ # Paste the resized image onto the base image
38
+ base_image.paste(sticker, paste_position)
39
+
40
+ return base_image
41
+
42
+ def build_card_template(selected_border, selected_seed_image):
43
+ print(selected_seed_image)
44
+ print(type(selected_seed_image))
45
+ selected_border = u.open_image_from_url(selected_border)
46
+ if type(selected_seed_image) == str:
47
+ print(f"String : {selected_seed_image}")
48
+ selected_seed_image = u.open_image_from_url(selected_seed_image)
49
+
50
+ mask = selected_border.split()[3]
51
+
52
+ image_list = []
53
+
54
+ # Image size parameters
55
+ width = 768
56
+ height = 1024
57
+
58
+ # Set canvas as transparent
59
+
60
+ background_color = (0,0,0,0)
61
+
62
+ #initialize canvas
63
+ canvas = Image.new('RGB', (width, height), background_color)
64
+
65
+ canvas = paste_image_and_resize(canvas, selected_seed_image,seed_x,seed_y, seed_width, seed_height)
66
+
67
+ canvas.paste(selected_border,(0,0), mask = mask)
68
+
69
+ image_list.append(canvas)
70
+
71
+ return image_list
72
+
73
+
user_input.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import item_dict_gen as igen
2
+ import img2img
3
+ import card_generator as card
4
+ import utilities as u
5
+ import sys
6
+ import tempfile
7
+ from PIL import Image
8
+ from github import Github
9
+
10
+ image_path = str
11
+ end_phrase = """<|end_of_turn|>"""
12
+ # Indexing the contents of Card templates and temp images
13
+ card_template_path = "./card_templates/"
14
+ temp_image_path = "./image_temp"
15
+
16
+ def index_image_paths(repo_name,directory_path):
17
+ g = Github() # No token needed for public repos
18
+ repo = g.get_repo(repo_name)
19
+ contents = repo.get_contents(directory_path)
20
+
21
+ files = []
22
+ for content_file in contents:
23
+ if content_file.type == "file":
24
+ files.append(content_file.download_url) # Or content_file.path for just the path
25
+
26
+ return files
27
+
28
+ user_pick_template_prompt = "Pick a template number from this list : "
29
+ user_pick_image_prompt = "Select an image : "
30
+
31
+ # Check if the user wants to exit the chatbot
32
+
33
+ def user_exit_question(user_input):
34
+ if user_input.lower() in ['exit', 'quit']:
35
+ print("Chatbot session ended.")
36
+ sys.exit()
37
+ # Process the list of files in the card_template directory and print with corresponding numbers to index
38
+ def process_list_for_user_response(list_of_items):
39
+ x = 0
40
+ for item in list_of_items:
41
+ print(f"{x} : {item}")
42
+ x += 1
43
+
44
+ def user_pick_item(user_prompt,list_of_items):
45
+ process_list_for_user_response(list_of_items)
46
+ user_input = input(user_prompt)
47
+ # Check if the user wants to exit the chatbot
48
+ user_exit_question(user_input)
49
+ return list_of_items[int(user_input)]
50
+
51
+ def call_llm(user_input):
52
+ # Process the query and get the response
53
+ llm_call = igen.call_llm_and_cleanup(user_input)
54
+ response = llm_call['choices'][0]['text']
55
+
56
+ # Find the index of the phrase
57
+ index = response.find(end_phrase)
58
+ print(f"index = {index}")
59
+ if index != -1:
60
+ # Slice the string from the end of the phrase onwards
61
+ response = response[index + len(end_phrase):]
62
+ else:
63
+ # Phrase not found, optional handling
64
+ response = response
65
+
66
+ response = response.replace("GPT4 Assistant: ", "")
67
+ print(response)
68
+ response = igen.convert_to_dict(response)
69
+ if not response:
70
+ response = call_llm(user_input)
71
+ del llm_call
72
+ return response
73
+
74
+ def prompt_user_input():
75
+ mimic = None
76
+ while True:
77
+ user_input_item = input("Provide an item : ")
78
+ user_exit_question(user_input_item)
79
+
80
+ if 'mimic' in user_input_item.lower():
81
+ mimic = True
82
+
83
+ #user_input_template = input(f"Pick a template number from this list : {process_list_for_user_response(list_of_card_templates)}")
84
+
85
+ user_input_template = user_pick_item(user_pick_template_prompt,list_of_card_templates)
86
+ response = call_llm(user_input_item)
87
+ print(response[u.keys_list(response,0)])
88
+ output_dict = response[u.keys_list(response,0)]
89
+ u.reclaim_mem()
90
+ item_name = response[u.keys_list(response,0)]['Name']
91
+ sd_prompt = response[u.keys_list(response,0)]['SD Prompt']
92
+ image_path = img2img.generate_image(4,sd_prompt,item_name,user_input_template, mimic)
93
+ user_card_image = user_pick_item(user_pick_image_prompt, image_path)
94
+
95
+ print(image_path)
96
+
97
+ card.render_text_on_card(user_card_image, output_dict)
98
+ u.delete_files(img2img.image_list)
99
+
100
+
101
+
102
+
103
+
utilities.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Create a list of hashmap key values .
2
+ import torch
3
+ import time
4
+ import gc
5
+ from io import BytesIO
6
+ import requests
7
+ import os
8
+ from PIL import Image
9
+ from pathlib import Path
10
+ # Utility Functions to be called from all modules
11
+
12
+ # Function to return a list of keys of a nested dictionary using it's key value (item or creature)
13
+ def keys_list(dict, index):
14
+ keys_list=list(dict.keys())
15
+ return keys_list[index]
16
+
17
+ # Function to clear model from VRAM to make space for other model
18
+ def reclaim_mem():
19
+
20
+ print(f"Memory before del {torch.cuda.memory_allocated()}")
21
+ torch.cuda.ipc_collect()
22
+ gc.collect()
23
+ torch.cuda.empty_cache()
24
+ time.sleep(0.01)
25
+ print(f"Memory after del {torch.cuda.memory_allocated()}")
26
+
27
+ #def del_object(object):
28
+ # del object
29
+ # gc.collect()
30
+
31
+ # Create a list of a directory if directory exists
32
+ def directory_contents(directory_path):
33
+ if os.path.isdir(directory_path) :
34
+ contents = os.listdir(directory_path)
35
+ return contents
36
+ else : pass
37
+
38
+ # Delete a list of file
39
+ def delete_files(file_paths):
40
+
41
+ for file_path in file_paths:
42
+ try:
43
+ os.remove(f"./image_temp/{file_path}")
44
+ print(f"Remove : ./image_temp/{file_path}")
45
+ except OSError as e:
46
+ print(f"Error: {file_path} : {e.strerror}")
47
+ file_paths.clear()
48
+
49
+
50
+ def open_image_from_url(image_url):
51
+ response = requests.get(image_url)
52
+ image_data = BytesIO(response.content)
53
+ image = Image.open(image_data)
54
+ return image
55
+
56
+ def receive_upload(image_file):
57
+
58
+ image = Image.open(image_file[0][0])
59
+
60
+ print(image)
61
+ return image
62
+