File size: 16,929 Bytes
a6fa654
 
 
b37c702
a6fa654
 
 
 
 
 
 
 
 
 
 
75a5574
 
 
 
 
 
 
 
 
 
a6fa654
75a5574
 
 
 
 
 
 
 
 
 
 
a6fa654
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2518242
a6fa654
75a5574
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6fa654
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75a5574
a6fa654
75a5574
a6fa654
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4e19b9
 
6990ec5
 
 
 
 
 
f4e19b9
 
 
a6fa654
 
 
 
fe96c20
 
 
 
 
a6fa654
 
fe96c20
a6fa654
 
fe96c20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2d4258
fe96c20
 
 
 
a6fa654
 
 
 
 
7199bd5
a6fa654
1c791ce
a6fa654
 
 
 
 
 
 
 
 
 
75a5574
a6fa654
 
75a5574
a6fa654
 
 
 
 
 
 
 
 
 
 
75a5574
 
 
 
 
 
 
 
 
 
 
 
a6fa654
75a5574
a6fa654
75a5574
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6fa654
 
 
 
75a5574
a6fa654
3b509c3
a6fa654
 
 
 
 
 
 
 
fe96c20
a6fa654
fe96c20
a6fa654
 
 
fe96c20
 
a6fa654
1c791ce
f26612a
fe96c20
a6fa654
 
75a5574
a6fa654
 
 
 
 
75a5574
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6fa654
 
 
75a5574
 
 
 
 
 
 
 
 
 
a6fa654
b37c702
b516f91
b37c702
 
 
e220a4f
a6fa654
 
c4abe73
b37c702
a6fa654
 
 
 
 
 
 
 
 
 
 
 
 
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
import img2img
import card_generator as card
import utilities as u
import os
import user_input as useri
import gradio as gr
import template_builder as tb

initial_name = "A Crowbar"

with gr.Blocks() as demo:
    
    # Functions and State Variables
    # Build functions W/in the Gradio format, because it only allows modification within it's context
    # Define inputs to match what is called on click, and output of the function as a list that matches the list of outputs
    textbox_default_dict = {'Name':'', 
                            'Pet Species': '',
                            'Breed':'',
                            'Fur':'',
                            'Intelligence Level':'',
                            'Affection Level':'',
                            'Energy Level':'',
                            'Noise Level':'',
                            'Play Level':'',
                            'image Prompt':''
                            }
    # Text states
    pet_name_state = gr.State()
    pet_species_state = gr.State()
    pet_breed_state = gr.State()
    pet_fur_state = gr.State()
    pet_intelligence_level_state = gr.State()
    pet_affection_level_state = gr.State()
    pet_energy_level_state = gr.State()
    pet_noise_level_state = gr.State()
    pet_play_level_state = gr.State()
    image_prompt_var = gr.State('')

    selected_border_image = gr.State('./card_templates/Moonstone Border.png')
    num_image_to_generate = gr.State(4)
    generated_image_list = gr.State([])
    selected_generated_image = gr.State()
    selected_seed_image = gr.State()
    built_template = gr.State()  
    mimic = None

    def set_textbox_defaults(textbox_default_dict, key):
        item_name = textbox_default_dict[key]
        return item_name


    # Function called when user generates item info, then assign values of dictionary to variables, output once to State, twice to textbox
    def generate_text_update_textboxes(user_input):
        
        llm_output=useri.call_llm(user_input)
        pet_key = list(llm_output.keys())
        pet_name = llm_output[pet_key[0]]['Name']
        pet_species = llm_output[pet_key[0]]['Pet Species']
        pet_breed = llm_output[pet_key[0]]['Breed']            
        pet_fur = llm_output[pet_key[0]]['Fur']
        pet_intelligence_level = llm_output[pet_key[0]]['Intelligence Level']
        pet_affection_level = llm_output[pet_key[0]]['Affection Level']
        pet_energy_level = llm_output[pet_key[0]]['Energy Level']
        pet_noise_level = llm_output[pet_key[0]]['Noise Level']
        pet_play_level = llm_output[pet_key[0]]['Play Level']
        image_prompt = llm_output[pet_key[0]]['Image Prompt']
        return [pet_name, pet_name,
                    pet_species, pet_species,
                    pet_breed, pet_breed,
                    pet_fur, pet_fur,
                    pet_intelligence_level, pet_intelligence_level,
                    pet_affection_level, pet_affection_level,
                    pet_energy_level, pet_energy_level,
                    pet_noise_level, pet_noise_level,
                    pet_play_level, pet_play_level,
                    image_prompt, image_prompt]
    
    # Called on user selecting an image from the gallery, outputs the path of the image
    def assign_img_path(evt: gr.SelectData):          
        img_dict = evt.value
        print(img_dict)
        selected_image_path = img_dict['image']['url']
        print(selected_image_path)
        return selected_image_path   
    
     # Make a list of files in image_temp and delete them
    def delete_temp_images():
        image_list = u.directory_contents('./image_temp')
        u.delete_files(image_list)
        u.image_list.clear()
    
    # Called when pressing button to generate image, updates gallery by returning the list of image URLs
    def generate_image_update_gallery(num_img, sd_prompt,item_name, built_template):
        delete_temp_images()
        print(f"num_img is a {type(num_img)}")
        print(f"sd_prompt is a {type(sd_prompt)}")
        image_list = img2img.preview_and_generate_image(4,sd_prompt, built_template)
        return image_list
    
    def build_template(selected_border, selected_seed_image):
        image_list = tb.build_card_template(selected_border, selected_seed_image)
        return image_list, image_list
   

    # Beginning of UI Page
    # Beginning of UI Page
    gr.HTML(""" <div id="inner"> <header>
            <h1>Item Card Generator</h1>
            <p>
            With this AI driven tool you will build a collectible style card of a fantasy flavored item with details.
            </p>
            </div>""")
    markdown_instructions = """## How It Works

1. Your intitial text along with the prompt is sent to OpenAI's GPT-4o to generate a JSON.
2. This JSON will be parsed into textboxes to review and edit. 
3. The final text field is the image prompt, these generate like one sentence stories describing the scene of your pet.
4. **Image and Text Generation**: Now generate 4 blank cards in about 15 seconds.
5. Finally, click the add text button to add text to your favorite template.
3. **Result**: The final product is a beautifully crafted collectible pet card."""

    gr.Markdown(markdown_instructions)

    gr.HTML(""" <div id="inner"> <header>
            <h2><b>First:</b> Build a Card Template</h2>
                </div>""")
    with gr.Row():
        # Template Gallery instructions
        gr.HTML(""" <div id="inner"> <header>
                    <h3>1. Click a border from the 'Card Template Gallery'</h3> 
                    </div>""")
        gr.HTML(""" <div id="inner"> <header>
                <h3>2. Click a image from the Seed Image Gallery</h3><br>
                </div>""")
        gr.HTML(""" <div id="inner"> <header><h4> -Or- Upload your own seed image, by dropping it into the 'Generated Template Gallery' </h4><br>
                <h3>3. Click 'Generate Card Template'</h3><br>
            </div>""")
        
    with gr.Row():

    
        
        border_gallery = gr.Gallery(label = "Card Template Gallery", 
                                        scale = 2,
                                        value = u.index_image_paths("Drakosfire/CardGenerator", "seed_images/card_templates"),
                                        show_label = True,
                                        columns = [3], rows = [3],
                                        object_fit = "contain",
                                        height = "auto",
                                        elem_id = "Template Gallery")
        
        
        
        border_gallery.select(assign_img_path, outputs = selected_border_image)
        seed_image_gallery = gr.Gallery(label= " Image Seed Gallery",
                                        scale = 2,
                                        value = u.index_image_paths("Drakosfire/CardGenerator", "seed_images/item_seeds"),
                                        show_label = True,
                                        columns = [3], rows = [3],
                                        object_fit = "contain",
                                        height = None,
                                        elem_id = "Template Gallery",
                                        interactive=True)                 

    
    
    built_template_gallery = gr.Gallery(label= "Generated Template Gallery",
                                        scale = 1,
                                        value = None,
                                        show_label = True,
                                        columns = [1], rows = [1],
                                        object_fit = "contain",
                                        height = None,
                                        elem_id = "Template Gallery",
                                        interactive=True,
                                        type="filepath") 
    
    seed_image_gallery.select(assign_img_path, outputs = selected_seed_image)
    built_template_gallery.upload(u.receive_upload, inputs=built_template_gallery, outputs= selected_seed_image)
    build_card_template_button = gr.Button(value = "Generate Card Template")
    build_card_template_button.click(build_template, inputs = [selected_border_image, selected_seed_image], outputs = [built_template_gallery, built_template]) 
        
    gr.HTML(""" <div id="inner"> <header>
                    <h2><b>Second:</b> Generate Pet Text </h2>
                   </div>""")  
    gr.HTML(""" <div id="inner"> <header>
                        <h3>1. Use a name, a breed, some coloring and the animal type, Ex: 'Hermione the Stormy Grey English Shorthair Cat' then click 'Generate Text' </h3>
                        </div>""")
    with gr.Row():
        user_input =  gr.Textbox(label = 'Item', lines =1, placeholder= "Flaming Magical Sword", elem_id= "Item", scale =4)
        item_text_generate = gr.Button(value = "Generate item text", scale=1)

    gr.HTML(""" <div id="inner"> <header>
                <h3> 2. Review and Edit the text</h3>
                </div>""") 
    with gr.Row():
    # Build text boxes for the broken up item dictionary values         
        with gr.Column(scale = 1):
            pet_name_output = gr.Textbox(value = set_textbox_defaults(
                textbox_default_dict, 'Name'),label = 'Name', lines = 1, interactive=True, elem_id='Pet Name')
            
            pet_species_output = gr.Textbox(value = set_textbox_defaults(
                textbox_default_dict, 'Pet Species'),label = 'Type', lines = 1, interactive=True, elem_id='Pet Species')
            
            pet_breed_output = gr.Textbox(value = set_textbox_defaults(
                textbox_default_dict, 'Breed'),label = 'Breed : [Common, Uncommon, Rare, Very Rare, Legendary]',
                lines = 1, interactive=True, elem_id='Pet Breed')
            
            pet_fur_output = gr.Textbox(value = set_textbox_defaults(
                textbox_default_dict, 'Fur'),label = 'Fur Quality', lines = 1, interactive=True, elem_id='Pet Fur')
                        
    # Pass the user input and border template to the generator
        with gr.Column(scale = 1):    
            pet_affection_level_output = gr.Textbox(value = set_textbox_defaults(
                textbox_default_dict, 'Affection Level'),label = 'Affection Level', lines = 1, interactive=True, elem_id='Affection Level')
            
            pet_energy_level_output = gr.Textbox(value = set_textbox_defaults(
                textbox_default_dict, 'Energy Level'),label = 'Energy Level', lines = 1, interactive=True, elem_id='Energy Level')
            
            pet_noise_level_output = gr.Textbox(value = set_textbox_defaults(
                textbox_default_dict, 'Noise Level'),label = 'Noise Level', lines = 1, interactive=True, elem_id='Noise Level')
            
            pet_play_level_output = gr.Textbox(value = set_textbox_defaults(
                textbox_default_dict, 'Play Level'),label = 'Play Level', lines = 1, interactive=True, elem_id='Play Level')
            
            pet_intelligence_level_output = gr.Textbox(value = set_textbox_defaults(
                textbox_default_dict, 'Intelligence Level'),label = 'Intelligence Level',
                lines = 1, interactive=True, elem_id='Intelligence Level')
    
    gr.HTML(""" <div id="inner"> <header>
                <h3> 3. This text will be used to generate the card's image.</h3>
                </div>""") 
    image_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, 'image Prompt'), lines = 1, interactive=True, elem_id='image Prompt')         
    gr.HTML(""" <div id="inner"> <header>
                <h2> <b>Third:</b> Click 'Generate Cards' to generate 4 cards to choose from. \n First image from a cold boot takes about 2 minutes. \n After that it's 10 seconds per image. </h2>
                </div>""")          
    card_gen_button = gr.Button(value = "Generate Cards", elem_id="Generate Card Button")
           
    # No longer Row Context, in context of entire Block
    gr.HTML(""" <div id="inner"> <header>
                <h2> <b>Fourth:</b> Click your favorite card then add text, or click 'Generate Four Card Options' again.<br>
                 </h2>
                </div>""")       
    generate_final_item_card = gr.Button(value = "Add Text", elem_id = "Generate user card")
    with gr.Row():
        
        generate_gallery = gr.Gallery(label = "Generated Cards",
                                    value = [],
                                    show_label= True,
                                    scale= 1,
                                    columns =[4], rows = [1],
                                    object_fit= "fill",
                                    height = "auto",
                                    elem_id = "Generated Cards Gallery",
                                    allow_preview=True
                                    )
    
    card_gen_button.click(fn = generate_image_update_gallery, inputs =[num_image_to_generate,image_prompt_output,pet_name_output,built_template], outputs= generate_gallery)
    generate_gallery.select(assign_img_path, outputs = selected_generated_image)

        # Button logice calls function when button object is pressed, passing inputs and passing output to components
    llm_output = item_text_generate.click(generate_text_update_textboxes, 
                                                inputs = [user_input], 
                                                outputs= [pet_name_state, 
                                                        pet_name_output,
                                                        pet_species_state,
                                                        pet_species_output, 
                                                        pet_breed_state,
                                                        pet_breed_output,
                                                        pet_fur_state,
                                                        pet_fur_output,
                                                        pet_intelligence_level_state,
                                                        pet_intelligence_level_output,
                                                        pet_affection_level_state,
                                                        pet_affection_level_output,
                                                        pet_energy_level_state,
                                                        pet_energy_level_output,
                                                        pet_noise_level_state,
                                                        pet_noise_level_output,
                                                        pet_play_level_state,
                                                        pet_play_level_output,                                                     
                                                        image_prompt_var,
                                                        image_prompt_output])           
        
        
    generate_final_item_card.click(card.render_text_on_card, inputs = [selected_generated_image,
                                                                        pet_name_output, 
                                                                        pet_species_output, 
                                                                        pet_breed_output, 
                                                                        pet_fur_output,
                                                                        pet_intelligence_level_output, 
                                                                        pet_affection_level_output,
                                                                        pet_energy_level_output,
                                                                        pet_noise_level_output,
                                                                        pet_play_level_output
                                                                        ],
                                                                        outputs = generate_gallery )
    base_dir = os.path.dirname(os.path.abspath(__file__))  # Gets the directory where the script is located
    print(f"Base Directory :",base_dir)
    list_of_static_dir = [os.path.join(base_dir, "card_parts"), 
                        os.path.join(base_dir, "fonts"), 
                        os.path.join(base_dir, "image_temp")]
    gr.set_static_paths(paths=list_of_static_dir)
    
    
if __name__ == "__main__":
    demo.launch(allowed_paths=list_of_static_dir)