File size: 11,066 Bytes
27c79bc
 
 
 
 
 
760ff9a
27c79bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
760ff9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27c79bc
 
760ff9a
 
 
 
 
 
 
 
 
27c79bc
 
760ff9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27c79bc
 
760ff9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27c79bc
760ff9a
 
 
 
 
 
 
27c79bc
 
760ff9a
 
 
 
 
 
 
27c79bc
760ff9a
27c79bc
 
 
 
 
760ff9a
27c79bc
 
 
760ff9a
 
 
 
 
27c79bc
 
 
760ff9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27c79bc
760ff9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27c79bc
 
760ff9a
 
 
 
 
 
 
 
 
 
 
 
 
 
27c79bc
760ff9a
27c79bc
 
 
 
 
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
# this imports the code from files and modules
import gradio as gr
import utilities as u
import os
import ctypes
import store_helper as sh
import process_text


# 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
libc = ctypes.cdll.LoadLibrary("libc.so.6")
M_MMAP_THRESHOLD = -3

# Set malloc mmap threshold.
libc.mallopt(M_MMAP_THRESHOLD, 2**20)

# Declare accessible directories
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, "output"), 
                    os.path.join(base_dir, "dependencies"),
                    os.path.join(base_dir, "galleries")] 
gr.set_static_paths(paths=list_of_static_dir)

# Build gradio app
# Storing everything inside the Blocks to be accessible to the app

with gr.Blocks() as demo:
    # Functions and State Variables
    store= gr.State()
    store_name = gr.State()
    store_description = gr.State()
    store_reputation = gr.State()
    store_backstory = gr.State()
    store_sd_prompt = gr.State()
    store_location = gr.State()
    store_type = gr.State()
    store_size = gr.State()
    store_owner = gr.State()
    store_employees = gr.State()
    store_hours = gr.State()
    store_services = gr.State()
    store_specialties = gr.State()
    notable_customers = gr.State()
    store_quests = gr.State()
    store_rumors = gr.State()
    store_inventory = gr.State()
    store_inventory_dict = gr.State()

    specialties_description = gr.State()

    def check_and_process_contents(item):
        if item:
            if len(item) > 0:
                store_value = item
                # if type(store_value) == list:
                store_value = process_text.format_qualities(store_value)
        else : store_value = ""
        return store_value
    
    def gen_store_desc(user_description): 
        llm_output = sh.call_llm_and_cleanup(user_description)
        store_dict = sh.convert_to_dict(llm_output)
        # Key List for checking is speciufic keys are present before running parsing functions
        keys_list = list(llm_output)
        store_name_value = store_dict['store_name']
        store_description_value = store_dict['description']
        store_reputation_value = store_dict['reputation']
        store_backstory_value = store_dict['backstory']
        store_sd_prompt_value = store_dict['store_sd_prompt']
        store_type_value = store_dict['type']
        store_size_value = store_dict['size']
        store_location_value = check_and_process_contents(store_dict['location'])
        store_owner_value = check_and_process_contents(store_dict['owners'])
        store_employees_value = check_and_process_contents(store_dict['employees'])
        store_hours_value = store_dict['store_hours']
        store_services_value = check_and_process_contents(store_dict['services'])
        store_specialties_value = check_and_process_contents(store_dict['specialties'])
        notable_customers_value = check_and_process_contents(store_dict['notable_customers'])
        store_quests_value = check_and_process_contents(store_dict['related_quests'])
        store_rumors_value = check_and_process_contents(store_dict['rumors'])

        
        #Return each State variable twice, once to the variable and once to the textbox
        return [store_name_value, store_name_value,
                store_description_value, store_description_value,
                store_reputation_value, store_reputation_value,
                store_backstory_value, store_backstory_value,
                store_sd_prompt_value, store_sd_prompt_value,
                store_type_value, store_type_value,
                store_size_value, store_size_value,
                store_location_value, store_location_value,
                store_owner_value, store_owner_value,
                store_employees_value,store_employees_value,
                notable_customers_value, notable_customers_value,
                store_hours_value, store_hours_value,
                store_services_value, store_services_value,
                store_specialties_value, store_specialties_value,
                store_quests_value, store_quests_value,
                store_rumors_value, store_rumors_value
                ]
    
    def gen_store_inventory(store_name, store_type, store_size, store_owner, store_reputation): 
        inventory_description = f"{store_name}, {store_type} {store_size} {store_owner} {store_reputation}"
        inventory_dict = sh.call_llm_and_cleanup(inventory_description, inventory=True)
        inventory_dict = sh.convert_to_dict(inventory_dict)
        if inventory_dict['inventory']:
            store_inventory_value = inventory_dict['inventory']
            store_inventory_value = process_text.format_inventory(store_inventory_value)
        
        #Return each State variable twice, once to the variable and once to the textbox
        return [store_inventory_value,store_inventory_value
                ]
    
    def inventory_to_dict(inventory):
        inventory_dict = process_text.parse_text_to_inventory(inventory)
        inventory_dict = sh.convert_to_dict(inventory_dict)
        return inventory_dict, inventory_dict

#Function to dynamically render textbox if it has text.                
    def update_visibility(textbox):        
        if not textbox:
            return gr.update(visible=False)
        return gr.update(visible=True)
    with gr.Tab("Store"):
        user_store_dict = gr.Textbox(label = "Step 1 : What are the core qualities of the store?",
                                                lines = 1, placeholder=f"Ex : A trade shop with a female ogre merchant, basic trade goods and travel supplies, has a secret basement for smuggling.",
                                                elem_id= "custom-textbox")
        desc_gen = gr.Button(value = "Click to Generate Description")
        store_name_output = gr.Textbox(label = "Store Name", lines = 1, interactive = True, visible = False)
        store_name_output.change(fn=update_visibility,
                                                    inputs=[store_name_output],
                                                    outputs=[store_name_output])
        store_description_output = gr.Textbox(label = "Store Description", lines = 1, interactive = True, visible = True)
        store_description_output.change(fn=update_visibility,
                                                    inputs=[store_description_output],
                                                    outputs=[store_description_output])
        store_reputation_output = gr.Textbox(label = "Store Reputation", lines = 1, interactive = True, visible = True)
        store_backstory_output = gr.Textbox(label = "Store Backstory", lines = 1, interactive = True, visible = True)
        store_sd_prompt_output = gr.Textbox(label = "Store Image Prompt", lines = 1, interactive = True, visible = True)
        store_type_output = gr.Textbox(label = "Store Type", lines = 1, interactive = True, visible = True)
        store_size_output = gr.Textbox(label = "Store Size", lines = 1, interactive = True, visible = True)
        store_location_output = gr.Textbox(label = "Store Location", lines = 1, interactive = True, visible = True)
        store_owner_output = gr.Textbox(label = "Owners", lines = 1, interactive = True, visible = True)
        store_employees_output = gr.Textbox(label = "Employees", lines = 1, interactive = True, visible = True)
        notable_customers_output = gr.Textbox(label = "Notable Customers", lines = 1, interactive = True, visible = True)
        store_hours_output = gr.Textbox(label = "Hours", lines = 1, interactive = True, visible = True)
        store_services_output = gr.Textbox(label = "Services", lines = 1, interactive = True, visible = True)
        store_specialties_output = gr.Textbox(label = "Specialties", lines = 1, interactive = True, visible = True)
        store_quests_output = gr.Textbox(label = "Quests", lines = 1, interactive = True, visible = True)
        store_rumors_output = gr.Textbox(label = "Rumors", lines = 1, interactive = True, visible = True)


        desc_gen.click(fn = gen_store_desc, inputs = [user_store_dict],
                        outputs= [
                                    store_name, store_name_output,
                                    store_description, store_description_output,
                                    store_reputation, store_reputation_output,
                                    store_backstory, store_backstory_output,
                                    store_sd_prompt,store_sd_prompt_output,
                                    store_type, store_type_output,
                                    store_size, store_size_output,
                                    store_location, store_location_output,
                                    store_owner, store_owner_output,
                                    store_employees, store_employees_output,
                                    notable_customers, notable_customers_output,
                                    store_hours, store_hours_output,
                                    store_services,store_services_output,     
                                    store_specialties,store_specialties_output,
                                    store_quests, store_quests_output,
                                    store_rumors, store_rumors_output                                                               
                                    ])
        
        image_path_list= u.absolute_path("./folder_with_images")
    
    with gr.Tab("Inventory"):        
        inv_gen = gr.Button(value = "Click to Generate Inventory")
        store_inventory_output = gr.Textbox(label = 'Inventory', lines = 16, interactive=True, visible=True)
        store_inventory_output.change(fn=update_visibility,
                                                    inputs=[store_inventory_output],
                                                    outputs=[store_inventory_output])
        inv_gen.click(fn = gen_store_inventory, inputs = [store_name, store_type, store_size, store_owner, store_reputation],
                        outputs= [store_inventory, store_inventory_output                                    
                                    ])
        dict_gen = gr.Button(value = "Click to convert test to data object")
        store_inventory_dict_output = gr.Textbox(label = 'Inventory', lines = 16, interactive=True, visible=True)
        
        dict_gen.click(fn = inventory_to_dict, inputs = [store_inventory_output],
                        outputs= [store_inventory_dict, store_inventory_dict_output                                    
                                    ])
        

        image_path_list= u.absolute_path("./folder_with_images")
    if __name__ == "__main__":
        demo.launch(allowed_paths=list_of_static_dir)