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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import replicate
|
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 |
+
|
59 |
+
# Function called when user generates item info, then assign values of dictionary to variables, output once to State, twice to textbox
|
60 |
+
def generate_text_update_textboxes(user_input):
|
61 |
+
u.reclaim_mem()
|
62 |
+
|
63 |
+
|
64 |
+
llm_output=useri.call_llm(user_input)
|
65 |
+
item_key = list(llm_output.keys())
|
66 |
+
|
67 |
+
item_key_values = list(llm_output[item_key[0]].keys())
|
68 |
+
item_name = llm_output[item_key[0]]['Name']
|
69 |
+
item_type = llm_output[item_key[0]]['Type']
|
70 |
+
item_rarity = llm_output[item_key[0]]['Rarity']
|
71 |
+
item_value = llm_output[item_key[0]]['Value']
|
72 |
+
item_properties = llm_output[item_key[0]]['Properties']
|
73 |
+
|
74 |
+
if 'Damage' in item_key_values:
|
75 |
+
item_damage = llm_output[item_key[0]]['Damage']
|
76 |
+
else: item_damage = ''
|
77 |
+
|
78 |
+
|
79 |
+
item_weight = llm_output[item_key[0]]['Weight']
|
80 |
+
item_description = llm_output[item_key[0]]['Description']
|
81 |
+
item_quote = llm_output[item_key[0]]['Quote']
|
82 |
+
item_quote = llm_output[item_key[0]]['Quote']
|
83 |
+
sd_prompt = llm_output[item_key[0]]['SD Prompt']
|
84 |
+
|
85 |
+
|
86 |
+
return [item_name, item_name,
|
87 |
+
item_type, item_type,
|
88 |
+
item_rarity, item_rarity,
|
89 |
+
item_value, item_value,
|
90 |
+
item_properties, item_properties,
|
91 |
+
item_damage, item_damage,
|
92 |
+
item_weight, item_weight,
|
93 |
+
item_description, item_description,
|
94 |
+
item_quote, item_quote,
|
95 |
+
sd_prompt, sd_prompt]
|
96 |
+
|
97 |
+
# Called on user selecting an image from the gallery, outputs the path of the image
|
98 |
+
def assign_img_path(evt: gr.SelectData):
|
99 |
+
img_dict = evt.value
|
100 |
+
print(img_dict)
|
101 |
+
selected_image_path = img_dict['image']['url']
|
102 |
+
print(selected_image_path)
|
103 |
+
return selected_image_path
|
104 |
+
|
105 |
+
# Make a list of files in image_temp and delete them
|
106 |
+
def delete_temp_images():
|
107 |
+
image_list = u.directory_contents('./image_temp')
|
108 |
+
u.delete_files(image_list)
|
109 |
+
u.image_list.clear()
|
110 |
+
|
111 |
+
# Called when pressing button to generate image, updates gallery by returning the list of image URLs
|
112 |
+
def generate_image_update_gallery(num_img, sd_prompt,item_name, built_template):
|
113 |
+
delete_temp_images()
|
114 |
+
print(f"sd_prompt is a {type(sd_prompt)}")
|
115 |
+
image_list = []
|
116 |
+
|
117 |
+
|
118 |
+
for x in range(num_img):
|
119 |
+
preview = img2img.preview_and_generate_image(x,sd_prompt, built_template, item_name)
|
120 |
+
image_list.append(preview)
|
121 |
+
yield image_list
|
122 |
+
|
123 |
+
del preview
|
124 |
+
u.reclaim_mem()
|
125 |
+
|
126 |
+
#generated_image_list = img2img.generate_image(num_img,sd_prompt,item_name,selected_border)
|
127 |
+
return image_list
|
128 |
+
|
129 |
+
def build_template(selected_border, selected_seed_image):
|
130 |
+
image_list = tb.build_card_template(selected_border, selected_seed_image)
|
131 |
+
return image_list, image_list
|
132 |
+
|
133 |
+
|
134 |
+
# Beginning of UI Page
|
135 |
+
# Beginning of UI Page
|
136 |
+
gr.HTML(""" <div id="inner"> <header>
|
137 |
+
<h1>Item Card Generator</h1>
|
138 |
+
<p>
|
139 |
+
With this AI driven tool you will build a collectible style card of a fantasy flavored item with details.
|
140 |
+
</p>
|
141 |
+
</div>""")
|
142 |
+
|
143 |
+
|
144 |
+
gr.HTML(""" <div id="inner"> <header>
|
145 |
+
<h2><b>First:</b> Build a Card Template</h2>
|
146 |
+
</div>""")
|
147 |
+
with gr.Row():
|
148 |
+
with gr.Column():
|
149 |
+
|
150 |
+
# Template Gallery instructions
|
151 |
+
gr.HTML(""" <div id="inner"> <header>
|
152 |
+
<h3>1. Click a border from the 'Card Template Gallery'</h3>
|
153 |
+
</div>""")
|
154 |
+
|
155 |
+
border_gallery = gr.Gallery(label = "Card Template Gallery",
|
156 |
+
scale = 2,
|
157 |
+
value = useri.index_image_paths("Drakosfire/CardGenerator", "seed_images/card_templates"),
|
158 |
+
show_label = True,
|
159 |
+
columns = [3], rows = [3],
|
160 |
+
object_fit = "contain",
|
161 |
+
height = "auto",
|
162 |
+
elem_id = "Template Gallery")
|
163 |
+
|
164 |
+
gr.HTML(""" <div id="inner"> <header>
|
165 |
+
<h3>2. Click a image from the Seed Image Gallery</h3><br>
|
166 |
+
</div>""")
|
167 |
+
|
168 |
+
border_gallery.select(assign_img_path, outputs = selected_border_image)
|
169 |
+
seed_image_gallery = gr.Gallery(label= " Image Seed Gallery",
|
170 |
+
scale = 2,
|
171 |
+
value = useri.index_image_paths("Drakosfire/CardGenerator", "seed_images/item_seeds"),
|
172 |
+
show_label = True,
|
173 |
+
columns = [3], rows = [3],
|
174 |
+
object_fit = "contain",
|
175 |
+
height = "auto",
|
176 |
+
elem_id = "Template Gallery",
|
177 |
+
interactive=True)
|
178 |
+
|
179 |
+
gr.HTML(""" <div id="inner"> <header><h4> -Or- Upload your own seed image, by dropping it into the 'Generated Template Gallery' </h4><br>
|
180 |
+
<h3>3. Click 'Generate Card Template'</h3><br>
|
181 |
+
</div>""")
|
182 |
+
|
183 |
+
built_template_gallery = gr.Gallery(label= "Generated Template Gallery",
|
184 |
+
scale = 1,
|
185 |
+
value = None,
|
186 |
+
show_label = True,
|
187 |
+
columns = [4], rows = [4],
|
188 |
+
object_fit = "contain",
|
189 |
+
height = "auto",
|
190 |
+
elem_id = "Template Gallery",
|
191 |
+
interactive=True,
|
192 |
+
type="filepath")
|
193 |
+
|
194 |
+
seed_image_gallery.select(assign_img_path, outputs = selected_seed_image)
|
195 |
+
built_template_gallery.upload(u.receive_upload, inputs=built_template_gallery, outputs= selected_seed_image)
|
196 |
+
build_card_template_button = gr.Button(value = "Generate Card Template")
|
197 |
+
build_card_template_button.click(build_template, inputs = [selected_border_image, selected_seed_image], outputs = [built_template_gallery, built_template])
|
198 |
+
|
199 |
+
gr.HTML(""" <div id="inner"> <header>
|
200 |
+
<h2><b>Second:</b> Generate Item Text </h2>
|
201 |
+
</div>""")
|
202 |
+
gr.HTML(""" <div id="inner"> <header>
|
203 |
+
<h3>1. Use a few words to describe the item then click 'Generate Text' </h3>
|
204 |
+
</div>""")
|
205 |
+
with gr.Row():
|
206 |
+
user_input = gr.Textbox(label = 'Item', lines =1, placeholder= "Flaming Magical Sword", elem_id= "Item", scale =4)
|
207 |
+
item_text_generate = gr.Button(value = "Generate item text", scale=1)
|
208 |
+
|
209 |
+
gr.HTML(""" <div id="inner"> <header>
|
210 |
+
<h3> 2. Review and Edit the text</h3>
|
211 |
+
</div>""")
|
212 |
+
with gr.Row():
|
213 |
+
|
214 |
+
# Build text boxes for the broken up item dictionary values
|
215 |
+
|
216 |
+
# Build text boxes for the broken up item dictionary values
|
217 |
+
with gr.Column(scale = 1):
|
218 |
+
item_name_output = gr.Textbox(value = set_textbox_defaults(textbox_default_dict, 'Name'),label = 'Name', lines = 1, interactive=True, elem_id='Item Name')
|
219 |
+
item_type_output = gr.Textbox(value = set_textbox_defaults(textbox_default_dict, 'Type'),label = 'Type', lines = 1, interactive=True, elem_id='Item Type')
|
220 |
+
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')
|
221 |
+
item_value_output = gr.Textbox(value = set_textbox_defaults(textbox_default_dict, 'Value'),label = 'Value', lines = 1, interactive=True, elem_id='Item Value')
|
222 |
+
|
223 |
+
# Pass the user input and border template to the generator
|
224 |
+
with gr.Column(scale = 1):
|
225 |
+
item_damage_output = gr.Textbox(value = set_textbox_defaults(textbox_default_dict, 'Damage'),label = 'Damage', lines = 1, interactive=True, elem_id='Item Damage')
|
226 |
+
item_weight_output = gr.Textbox(value = set_textbox_defaults(textbox_default_dict, 'Weight'),label = 'Weight', lines = 1, interactive=True, elem_id='Item Weight')
|
227 |
+
item_description_output = gr.Textbox(value = set_textbox_defaults(textbox_default_dict, 'Description'),label = 'Description', lines = 1, interactive=True, elem_id='Item Description')
|
228 |
+
item_quote_output = gr.Textbox(value = set_textbox_defaults(textbox_default_dict, 'Quote'),label = 'Quote', lines = 1, interactive=True, elem_id='Item quote')
|
229 |
+
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')
|
230 |
+
|
231 |
+
|
232 |
+
gr.HTML(""" <div id="inner"> <header>
|
233 |
+
<h3> 3. This text will be used to generate the card's image.</h3>
|
234 |
+
</div>""")
|
235 |
+
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')
|
236 |
+
|
237 |
+
gr.HTML(""" <div id="inner"> <header>
|
238 |
+
<h2> <b>Third:</b> Click 'Generate Cards' to generate 4 cards to choose from. </h2>
|
239 |
+
</div>""")
|
240 |
+
card_gen_button = gr.Button(value = "Generate Cards", elem_id="Generate Card Button")
|
241 |
+
|
242 |
+
# No longer Row Context, in context of entire Block
|
243 |
+
gr.HTML(""" <div id="inner"> <header>
|
244 |
+
<h2> <b>Fourth:</b> Click your favorite card then add text, or click 'Generate Four Card Options' again.<br>
|
245 |
+
</h2>
|
246 |
+
</div>""")
|
247 |
+
|
248 |
+
with gr.Row():
|
249 |
+
generate_gallery = gr.Gallery(label = "Generated Cards",
|
250 |
+
value = [],
|
251 |
+
show_label= True,
|
252 |
+
scale= 5,
|
253 |
+
columns =[2], rows = [2],
|
254 |
+
object_fit= "fill",
|
255 |
+
height = "768",
|
256 |
+
elem_id = "Generated Cards Gallery"
|
257 |
+
)
|
258 |
+
generate_final_item_card = gr.Button(value = "Add Text", elem_id = "Generate user card")
|
259 |
+
|
260 |
+
|
261 |
+
card_gen_button.click(fn = generate_image_update_gallery, inputs =[num_image_to_generate,item_sd_prompt_output,item_name_output,built_template_gallery], outputs= generate_gallery)
|
262 |
+
generate_gallery.select(assign_img_path, outputs = selected_generated_image)
|
263 |
+
|
264 |
+
# Button logice calls function when button object is pressed, passing inputs and passing output to components
|
265 |
+
llm_output = item_text_generate.click(generate_text_update_textboxes,
|
266 |
+
inputs = [user_input],
|
267 |
+
outputs= [item_name_var,
|
268 |
+
item_name_output,
|
269 |
+
item_type_var,
|
270 |
+
item_type_output,
|
271 |
+
item_rarity_var,
|
272 |
+
item_rarity_output,
|
273 |
+
item_value_var,
|
274 |
+
item_value_output,
|
275 |
+
item_properties_var,
|
276 |
+
item_properties_output,
|
277 |
+
item_damage_var,
|
278 |
+
item_damage_output,
|
279 |
+
item_weight_var,
|
280 |
+
item_weight_output,
|
281 |
+
item_description_var,
|
282 |
+
item_description_output,
|
283 |
+
item_quote_var,
|
284 |
+
item_quote_output,
|
285 |
+
item_sd_prompt_var,
|
286 |
+
item_sd_prompt_output])
|
287 |
+
|
288 |
+
|
289 |
+
generate_final_item_card.click(card.render_text_on_card, inputs = [selected_generated_image,
|
290 |
+
item_name_output,
|
291 |
+
item_type_output,
|
292 |
+
item_rarity_output,
|
293 |
+
item_value_output,
|
294 |
+
item_properties_output,
|
295 |
+
item_damage_output,
|
296 |
+
item_weight_output,
|
297 |
+
item_description_output,
|
298 |
+
item_quote_output
|
299 |
+
],
|
300 |
+
outputs = generate_gallery )
|
301 |
+
|
302 |
+
|
303 |
+
if __name__ == '__main__':
|
304 |
+
demo.launch(server_name = "0.0.0.0", server_port = 8000, share = False, allowed_paths = ["/media/drakosfire/Shared/","/media/drakosfire/Shared/MerchantBot/card_templates"])
|
305 |
+
|
306 |
+
|
307 |
+
|
308 |
+
|
309 |
+
|
310 |
+
|
311 |
+
|
312 |
+
|
313 |
+
|
314 |
+
|
315 |
+
|
316 |
+
|
317 |
+
|
318 |
+
|