Spaces:
Running
Running
UI v2
Browse files- prompts.py +7 -0
- svg_editor_gradio.py +222 -209
prompts.py
CHANGED
@@ -10,6 +10,7 @@ Instructions:
|
|
10 |
Result SVG XML:
|
11 |
""",
|
12 |
"display": True,
|
|
|
13 |
"example": "Three dots",
|
14 |
"primer": '<?xml version="1.0" ?><svg xmlns="http://www.w3.org/2000/svg"',
|
15 |
},
|
@@ -27,6 +28,7 @@ Instructions:
|
|
27 |
Result SVG XML:
|
28 |
""",
|
29 |
"display": True,
|
|
|
30 |
"example": "Change all colors to red",
|
31 |
"primer": "",
|
32 |
},
|
@@ -41,6 +43,7 @@ SVG XML:
|
|
41 |
Description of the image:
|
42 |
""",
|
43 |
"display": False,
|
|
|
44 |
"example": "N/A",
|
45 |
"primer": "",
|
46 |
},
|
@@ -91,6 +94,7 @@ SVG XML in IBM design style:
|
|
91 |
|
92 |
""",
|
93 |
"display": False,
|
|
|
94 |
"example": "Three dots",
|
95 |
"primer": '<?xml version="1.0" ?><svg xmlns="http://www.w3.org/2000/svg"',
|
96 |
},
|
@@ -112,3 +116,6 @@ def get_prompt_example(promptid):
|
|
112 |
|
113 |
def get_prompt_primer(promptid):
|
114 |
return svg_prompts[promptid]["primer"]
|
|
|
|
|
|
|
|
10 |
Result SVG XML:
|
11 |
""",
|
12 |
"display": True,
|
13 |
+
"uploadmode" : False,
|
14 |
"example": "Three dots",
|
15 |
"primer": '<?xml version="1.0" ?><svg xmlns="http://www.w3.org/2000/svg"',
|
16 |
},
|
|
|
28 |
Result SVG XML:
|
29 |
""",
|
30 |
"display": True,
|
31 |
+
"uploadmode" : True,
|
32 |
"example": "Change all colors to red",
|
33 |
"primer": "",
|
34 |
},
|
|
|
43 |
Description of the image:
|
44 |
""",
|
45 |
"display": False,
|
46 |
+
"uploadmode" : True,
|
47 |
"example": "N/A",
|
48 |
"primer": "",
|
49 |
},
|
|
|
94 |
|
95 |
""",
|
96 |
"display": False,
|
97 |
+
"uploadmode" : False,
|
98 |
"example": "Three dots",
|
99 |
"primer": '<?xml version="1.0" ?><svg xmlns="http://www.w3.org/2000/svg"',
|
100 |
},
|
|
|
116 |
|
117 |
def get_prompt_primer(promptid):
|
118 |
return svg_prompts[promptid]["primer"]
|
119 |
+
|
120 |
+
def get_prompt_uploadmode(promptid):
|
121 |
+
return svg_prompts[promptid]["uploadmode"]
|
svg_editor_gradio.py
CHANGED
@@ -9,32 +9,33 @@ from prompts import (
|
|
9 |
get_prompt_template,
|
10 |
get_prompt_example,
|
11 |
get_prompt_primer,
|
|
|
12 |
)
|
13 |
from data_images import svg_three_dots
|
|
|
14 |
|
15 |
|
16 |
-
# Functions
|
17 |
def read_file(uploaded_file):
|
18 |
if uploaded_file:
|
19 |
svg_xml = open(uploaded_file, "r").read()
|
20 |
-
|
21 |
return (
|
22 |
-
|
23 |
svg_xml,
|
24 |
-
html_img_preview(
|
25 |
-
xml_string_to_html_img(svg_xml),
|
26 |
)
|
27 |
|
28 |
|
29 |
-
def
|
30 |
# print(f"encoded_string_box_change:image_data:{data_string}")
|
31 |
svg_xml = decode_b64_string_to_pretty_xml(data_string)
|
32 |
-
return svg_xml, html_img_preview(data_string)
|
33 |
|
34 |
|
35 |
-
def
|
36 |
data_string = xml_string_to_data_string(svg_xml)
|
37 |
-
return data_string, html_img_preview(data_string)
|
38 |
|
39 |
|
40 |
def xml_string_to_data_string(svg_xml: str):
|
@@ -44,38 +45,23 @@ def xml_string_to_data_string(svg_xml: str):
|
|
44 |
|
45 |
|
46 |
def html_img_preview(data_string):
|
47 |
-
return
|
48 |
|
49 |
|
50 |
-
|
51 |
-
data_string = xml_string_to_data_string(svg_xml)
|
52 |
-
return html_img_preview(data_string)
|
53 |
-
|
54 |
-
|
55 |
-
# def replace_color(svg_xml: str, color_from, color_to):
|
56 |
-
# print(
|
57 |
-
# f"replace_color:svg_xml:{svg_xml}, color_from:{color_from}, color_to:{color_to} "
|
58 |
-
# )
|
59 |
-
# if svg_xml and color_from and color_to:
|
60 |
-
# new_svg_xml = svg_xml.replace(color_from, color_to)
|
61 |
-
# data_string = xml_string_to_data_string(new_svg_xml)
|
62 |
-
# return data_string, new_svg_xml, html_img_preview(data_string)
|
63 |
-
|
64 |
-
|
65 |
-
# def switch_colors(color_from, color_to):
|
66 |
-
# print(f"switch_colors:color_from:{color_from}, color_to:{color_to} ")
|
67 |
-
# return color_to, color_from
|
68 |
-
|
69 |
|
70 |
# Functions - watsonx
|
71 |
|
72 |
|
73 |
-
def
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
79 |
|
80 |
|
81 |
def wx_models_dropdown(wx_engine_state):
|
@@ -86,8 +72,11 @@ def wx_models_dropdown(wx_engine_state):
|
|
86 |
if wx_engine is not None:
|
87 |
model_list = wx_engine.list_models()
|
88 |
default_value = (
|
89 |
-
|
90 |
)
|
|
|
|
|
|
|
91 |
return gr.Dropdown(
|
92 |
label="Model",
|
93 |
info=recommended_model + " recommended",
|
@@ -96,10 +85,30 @@ def wx_models_dropdown(wx_engine_state):
|
|
96 |
)
|
97 |
|
98 |
|
99 |
-
def
|
100 |
-
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
|
105 |
def prepare_prompt(
|
@@ -136,7 +145,8 @@ def wx_generate(
|
|
136 |
stop_sequences=["</svg>"],
|
137 |
)
|
138 |
print(f"wx_generate:wx_result:{wx_result}")
|
139 |
-
|
|
|
140 |
|
141 |
|
142 |
def wx_stream(
|
@@ -157,211 +167,214 @@ def wx_stream(
|
|
157 |
)
|
158 |
for chunk in wx_result_generator:
|
159 |
wx_result += chunk
|
160 |
-
|
|
|
161 |
print(f"wx_stream:wx_result:{wx_result}")
|
162 |
-
|
|
|
163 |
|
164 |
|
165 |
-
|
166 |
-
return xml_string_to_html_img(wx_result)
|
167 |
|
168 |
|
169 |
-
def
|
170 |
-
data_string = xml_string_to_data_string(
|
171 |
return (
|
172 |
data_string,
|
173 |
-
|
174 |
html_img_preview(data_string),
|
175 |
-
xml_string_to_html_img(svg_xml),
|
176 |
)
|
177 |
|
178 |
|
179 |
-
# APP
|
180 |
with gr.Blocks() as demo:
|
181 |
gr.Markdown("# SVG editor")
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
- Describe an SVG: Upload, paste or generate an SVG file, Select the prompt template 'Describe SVG' and click 'Submit' """
|
188 |
-
)
|
189 |
|
190 |
-
# load env variables
|
191 |
-
status, env_apiendpoint, env_apikey, env_projectid = load_credentials()
|
192 |
# init state - note gr.State() initial value must be deep-copyable - my wx_engine class is not
|
193 |
wx_engine_state = gr.State(None)
|
194 |
-
with gr.
|
195 |
-
#
|
196 |
-
|
197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
# Upload an .svg file
|
199 |
-
|
200 |
-
#
|
201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
202 |
label="Image string",
|
203 |
info="data:image/svg+xml;base64,...",
|
204 |
lines=7,
|
205 |
max_lines=7,
|
206 |
show_copy_button=True,
|
207 |
scale=3,
|
|
|
208 |
)
|
209 |
-
|
210 |
-
|
211 |
-
# Decoded string and preview
|
212 |
with gr.Row():
|
213 |
-
|
214 |
-
label="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
215 |
lines=7,
|
216 |
max_lines=7,
|
217 |
show_copy_button=True,
|
218 |
scale=3,
|
219 |
-
|
220 |
-
# decoded preview
|
221 |
-
decoded_svg_preview = gr.HTML(f"<img src='{svg_three_dots}'/>")
|
222 |
-
# with gr.Row(): # Color changer
|
223 |
-
# color_from_area = gr.ColorPicker(label="Search color:", value="#000000", scale=0)
|
224 |
-
# color_switch_btn = gr.Button("<->", scale=0)
|
225 |
-
# color_to_area = gr.ColorPicker(label="Replace color:", value="#FFFFFF", scale=0)
|
226 |
-
# color_replace_btn = gr.Button("Replace", scale=0)
|
227 |
-
|
228 |
-
with gr.Accordion("watsonx.ai:", open=True):
|
229 |
-
with gr.Row(): # watsonx
|
230 |
-
with gr.Column(scale=0):
|
231 |
-
with gr.Group():
|
232 |
-
# credentials
|
233 |
-
with gr.Accordion("Credentials:", open=True):
|
234 |
-
wx_creds_endpoint = gr.Textbox(
|
235 |
-
label="Endpoint", value=env_apiendpoint, max_lines=1
|
236 |
-
)
|
237 |
-
wx_creds_apikey = gr.Textbox(
|
238 |
-
label="API key", value=env_apikey, max_lines=1
|
239 |
-
)
|
240 |
-
wx_creds_projectid = gr.Textbox(
|
241 |
-
label="Project id", value=env_projectid, max_lines=1
|
242 |
-
)
|
243 |
-
wx_connect_btn = gr.Button("Connect")
|
244 |
-
# model
|
245 |
-
wx_models_drop = wx_models_dropdown(None)
|
246 |
-
wx_connect_btn.click(
|
247 |
-
fn=wx_connect,
|
248 |
-
inputs=[
|
249 |
-
wx_engine_state,
|
250 |
-
wx_creds_endpoint,
|
251 |
-
wx_creds_apikey,
|
252 |
-
wx_creds_projectid,
|
253 |
-
],
|
254 |
-
outputs=[wx_engine_state, wx_models_drop],
|
255 |
-
)
|
256 |
-
# prompt template
|
257 |
-
prompt_template_names = list_prompts()
|
258 |
-
wx_prompt_drop = gr.Dropdown(
|
259 |
-
label="Prompt template",
|
260 |
-
choices=prompt_template_names,
|
261 |
-
value=prompt_template_names[0],
|
262 |
-
)
|
263 |
-
wx_prompt_box = gr.Textbox(
|
264 |
-
info="Text",
|
265 |
-
show_label=False,
|
266 |
-
max_lines=5,
|
267 |
-
value=get_prompt_template(prompt_template_names[0]),
|
268 |
-
)
|
269 |
-
wx_primer_box = gr.Textbox(
|
270 |
-
info="Primer",
|
271 |
-
show_label=False,
|
272 |
-
max_lines=2,
|
273 |
-
value=get_prompt_primer(prompt_template_names[0]),
|
274 |
-
)
|
275 |
-
with gr.Column():
|
276 |
-
with gr.Row():
|
277 |
-
wx_instructions_box = gr.Textbox(
|
278 |
-
label="Instructions",
|
279 |
-
scale=3,
|
280 |
-
value=get_prompt_example(prompt_template_names[0]),
|
281 |
-
show_copy_button=True,
|
282 |
-
)
|
283 |
-
wx_submit_btn = gr.Button("↓Submit↓", scale=0)
|
284 |
-
wx_accept_btn = gr.Button("↑Accept↑", scale=0)
|
285 |
-
with gr.Row():
|
286 |
-
wx_result_box = gr.Textbox(
|
287 |
-
label="Result",
|
288 |
-
lines=7,
|
289 |
-
max_lines=7,
|
290 |
-
scale=3,
|
291 |
-
show_copy_button=True,
|
292 |
-
)
|
293 |
-
wx_svg_preview = gr.HTML(f"<img src='{svg_three_dots}'/>")
|
294 |
-
wx_status_box = gr.Markdown("Status")
|
295 |
-
|
296 |
-
wx_prompt_drop.input(
|
297 |
-
fn=wx_prompt_change,
|
298 |
-
inputs=wx_prompt_drop,
|
299 |
-
outputs=[wx_prompt_box, wx_instructions_box, wx_primer_box],
|
300 |
-
)
|
301 |
-
wx_submit_btn.click(
|
302 |
-
# fn=wx_generate,
|
303 |
-
fn=wx_stream,
|
304 |
-
inputs=[
|
305 |
-
wx_engine_state,
|
306 |
-
wx_models_drop,
|
307 |
-
wx_prompt_box,
|
308 |
-
wx_instructions_box,
|
309 |
-
wx_primer_box,
|
310 |
-
xml_string_box,
|
311 |
-
],
|
312 |
-
outputs=[wx_status_box, wx_result_box, wx_svg_preview],
|
313 |
-
api_name="wx_generate",
|
314 |
-
)
|
315 |
-
wx_result_box.input(
|
316 |
-
fn=wx_result_box_change,
|
317 |
-
inputs=[wx_result_box],
|
318 |
-
outputs=[wx_svg_preview],
|
319 |
-
)
|
320 |
-
wx_accept_btn.click(
|
321 |
-
fn=wx_accept,
|
322 |
-
inputs=[wx_result_box],
|
323 |
-
outputs=[
|
324 |
-
encoded_string_box,
|
325 |
-
xml_string_box,
|
326 |
-
encoded_svg_preview,
|
327 |
-
decoded_svg_preview,
|
328 |
-
],
|
329 |
)
|
330 |
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
336 |
)
|
337 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
338 |
fn=read_file,
|
339 |
-
inputs=
|
340 |
outputs=[
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
decoded_svg_preview,
|
345 |
],
|
346 |
)
|
347 |
-
|
348 |
-
fn=
|
349 |
-
inputs=[
|
350 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
351 |
)
|
352 |
-
# color_switch_btn.click(
|
353 |
-
# fn=switch_colors,
|
354 |
-
# inputs=[color_from_area, color_to_area],
|
355 |
-
# outputs=[color_from_area, color_to_area],
|
356 |
-
# api_name="color_switch",
|
357 |
-
# )
|
358 |
-
# color_replace_btn.click(
|
359 |
-
# fn=replace_color,
|
360 |
-
# inputs=[xml_string_box, color_from_area, color_to_area],
|
361 |
-
# outputs=[encoded_string_box, xml_string_box, svg_preview],
|
362 |
-
# api_name="color_replace",
|
363 |
-
# )
|
364 |
-
|
365 |
|
366 |
# Main
|
367 |
if __name__ == "__main__":
|
|
|
9 |
get_prompt_template,
|
10 |
get_prompt_example,
|
11 |
get_prompt_primer,
|
12 |
+
get_prompt_uploadmode,
|
13 |
)
|
14 |
from data_images import svg_three_dots
|
15 |
+
from ibm_watsonx_ai.wml_client_error import WMLClientError
|
16 |
|
17 |
|
18 |
+
# Functions - input
|
19 |
def read_file(uploaded_file):
|
20 |
if uploaded_file:
|
21 |
svg_xml = open(uploaded_file, "r").read()
|
22 |
+
encoded_data_string = xml_string_to_data_string(svg_xml)
|
23 |
return (
|
24 |
+
encoded_data_string,
|
25 |
svg_xml,
|
26 |
+
html_img_preview(encoded_data_string),
|
|
|
27 |
)
|
28 |
|
29 |
|
30 |
+
def input_encoded_string_box_change(data_string: str):
|
31 |
# print(f"encoded_string_box_change:image_data:{data_string}")
|
32 |
svg_xml = decode_b64_string_to_pretty_xml(data_string)
|
33 |
+
return svg_xml, html_img_preview(data_string)
|
34 |
|
35 |
|
36 |
+
def input_xml_string_box_change(svg_xml: str):
|
37 |
data_string = xml_string_to_data_string(svg_xml)
|
38 |
+
return data_string, html_img_preview(data_string)
|
39 |
|
40 |
|
41 |
def xml_string_to_data_string(svg_xml: str):
|
|
|
45 |
|
46 |
|
47 |
def html_img_preview(data_string):
|
48 |
+
return f'<img src="{data_string}" style="display: block; margin-left: auto; margin-right: auto;"/>'
|
49 |
|
50 |
|
51 |
+
image_placeholder = html_img_preview(svg_three_dots)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
# Functions - watsonx
|
54 |
|
55 |
|
56 |
+
def wx_prompt_drop_change(prompt_template_name):
|
57 |
+
show_upload = get_prompt_uploadmode(prompt_template_name)
|
58 |
+
return {
|
59 |
+
wx_prompt_box: get_prompt_template(prompt_template_name),
|
60 |
+
wx_instructions_box: get_prompt_example(prompt_template_name),
|
61 |
+
wx_primer_box: get_prompt_primer(prompt_template_name),
|
62 |
+
upload_row: gr.Row(visible=show_upload),
|
63 |
+
upload_accordeon: gr.Accordion(visible=show_upload),
|
64 |
+
}
|
65 |
|
66 |
|
67 |
def wx_models_dropdown(wx_engine_state):
|
|
|
72 |
if wx_engine is not None:
|
73 |
model_list = wx_engine.list_models()
|
74 |
default_value = (
|
75 |
+
recommended_model if recommended_model in model_list else model_list[0]
|
76 |
)
|
77 |
+
# print(f"wx_models_dropdown:model_list:{model_list}")
|
78 |
+
# print(f"wx_models_dropdown:default_value:{default_value}")
|
79 |
+
|
80 |
return gr.Dropdown(
|
81 |
label="Model",
|
82 |
info=recommended_model + " recommended",
|
|
|
85 |
)
|
86 |
|
87 |
|
88 |
+
def wx_connect_click(wx_engine_state, apiendpoint, apikey, projectid):
|
89 |
+
# if apiendpoint is not None and apikey is not None and projectid is not None:
|
90 |
+
try:
|
91 |
+
wx_engine_state = wxEngine(apiendpoint, apikey, projectid)
|
92 |
+
msg = "watsonx.ai sucessfully activated"
|
93 |
+
print(msg)
|
94 |
+
return (
|
95 |
+
wx_engine_state,
|
96 |
+
wx_models_dropdown(wx_engine_state),
|
97 |
+
gr.Accordion(open=False),
|
98 |
+
gr.Button(interactive=True),
|
99 |
+
gr.Textbox(msg),
|
100 |
+
)
|
101 |
+
except WMLClientError as ex:
|
102 |
+
template = "Exception {0} occurred: {1!r}"
|
103 |
+
msg = template.format(type(ex).__name__, ex.args)
|
104 |
+
print(msg)
|
105 |
+
return (
|
106 |
+
wx_engine_state,
|
107 |
+
[],
|
108 |
+
gr.Accordion(open=True),
|
109 |
+
gr.Button(interactive=False),
|
110 |
+
gr.Textbox(msg),
|
111 |
+
)
|
112 |
|
113 |
|
114 |
def prepare_prompt(
|
|
|
145 |
stop_sequences=["</svg>"],
|
146 |
)
|
147 |
print(f"wx_generate:wx_result:{wx_result}")
|
148 |
+
data_string = xml_string_to_data_string(wx_result)
|
149 |
+
return wx_status, data_string, wx_result, html_img_preview(data_string)
|
150 |
|
151 |
|
152 |
def wx_stream(
|
|
|
167 |
)
|
168 |
for chunk in wx_result_generator:
|
169 |
wx_result += chunk
|
170 |
+
status = f"Processing.{'.'*int(randrange(3))}"
|
171 |
+
yield status, status, wx_result, None
|
172 |
print(f"wx_stream:wx_result:{wx_result}")
|
173 |
+
data_string = xml_string_to_data_string(wx_result)
|
174 |
+
yield wx_status, data_string, wx_result, html_img_preview(data_string)
|
175 |
|
176 |
|
177 |
+
# Functions - output
|
|
|
178 |
|
179 |
|
180 |
+
def output_xml_string_box_change(xml_string):
|
181 |
+
data_string = xml_string_to_data_string(xml_string)
|
182 |
return (
|
183 |
data_string,
|
184 |
+
xml_string,
|
185 |
html_img_preview(data_string),
|
|
|
186 |
)
|
187 |
|
188 |
|
189 |
+
# APP layout
|
190 |
with gr.Blocks() as demo:
|
191 |
gr.Markdown("# SVG editor")
|
192 |
+
gr.Markdown(
|
193 |
+
"""### Get started:
|
194 |
+
- Create a new SVG: Enter a description in the 'Instructions' box and click 'Submit'
|
195 |
+
- Modify an existing SVG: Upload an SVG file, or paste an image string or SVG XML, then Select the prompt template 'Modify SVG', enter a change instruction in the 'Instructions' box and click 'Submit'"""
|
196 |
+
)
|
|
|
|
|
197 |
|
|
|
|
|
198 |
# init state - note gr.State() initial value must be deep-copyable - my wx_engine class is not
|
199 |
wx_engine_state = gr.State(None)
|
200 |
+
with gr.Row(): # main UI
|
201 |
+
with gr.Column(scale=0): # watsonx setup
|
202 |
+
# prompt template selection
|
203 |
+
prompt_template_names = list_prompts()
|
204 |
+
default_prompt_template_name = prompt_template_names[0]
|
205 |
+
wx_prompt_drop = gr.Dropdown(
|
206 |
+
label="Action",
|
207 |
+
choices=prompt_template_names,
|
208 |
+
value=default_prompt_template_name,
|
209 |
+
)
|
210 |
+
# credentials
|
211 |
+
# load env variables
|
212 |
+
status_unused, env_apiendpoint, env_apikey, env_projectid = (
|
213 |
+
load_credentials()
|
214 |
+
)
|
215 |
+
with gr.Accordion("Credentials", open=True) as credentials_accordeon:
|
216 |
+
wx_creds_endpoint = gr.Textbox(
|
217 |
+
label="Endpoint", value=env_apiendpoint, max_lines=1
|
218 |
+
)
|
219 |
+
wx_creds_apikey = gr.Textbox(
|
220 |
+
label="API key", value=env_apikey, max_lines=1
|
221 |
+
)
|
222 |
+
wx_creds_projectid = gr.Textbox(
|
223 |
+
label="Project id", value=env_projectid, max_lines=1
|
224 |
+
)
|
225 |
+
wx_connect_btn = gr.Button("Connect")
|
226 |
+
# model
|
227 |
+
wx_models_drop = wx_models_dropdown(None)
|
228 |
+
# prompt text and primer
|
229 |
+
wx_prompt_box = gr.Textbox(
|
230 |
+
info="Text",
|
231 |
+
show_label=False,
|
232 |
+
max_lines=5,
|
233 |
+
value=get_prompt_template(prompt_template_names[0]),
|
234 |
+
)
|
235 |
+
wx_primer_box = gr.Textbox(
|
236 |
+
info="Primer",
|
237 |
+
show_label=False,
|
238 |
+
max_lines=2,
|
239 |
+
value=get_prompt_primer(prompt_template_names[0]),
|
240 |
+
)
|
241 |
+
with gr.Column(): # main pane
|
242 |
+
# Upload
|
243 |
+
with gr.Row(
|
244 |
+
visible=get_prompt_uploadmode(default_prompt_template_name)
|
245 |
+
) as upload_row:
|
246 |
# Upload an .svg file
|
247 |
+
input_file = gr.File(scale=0, label="Upload an SVG file")
|
248 |
+
# original preview
|
249 |
+
input_svg_preview = gr.HTML(image_placeholder)
|
250 |
+
# decoded SVG XML
|
251 |
+
input_xml_string_box = gr.Textbox(
|
252 |
+
label="Input SVG XML",
|
253 |
+
lines=7,
|
254 |
+
max_lines=7,
|
255 |
+
show_copy_button=True,
|
256 |
+
scale=3,
|
257 |
+
)
|
258 |
+
with gr.Accordion(
|
259 |
+
label="Input encoded string",
|
260 |
+
open=False,
|
261 |
+
visible=get_prompt_uploadmode(default_prompt_template_name),
|
262 |
+
) as upload_accordeon:
|
263 |
+
# Encoded image string
|
264 |
+
input_encoded_string_box = gr.Textbox(
|
265 |
label="Image string",
|
266 |
info="data:image/svg+xml;base64,...",
|
267 |
lines=7,
|
268 |
max_lines=7,
|
269 |
show_copy_button=True,
|
270 |
scale=3,
|
271 |
+
container=False,
|
272 |
)
|
273 |
+
|
274 |
+
# modification
|
|
|
275 |
with gr.Row():
|
276 |
+
wx_instructions_box = gr.Textbox(
|
277 |
+
label="Instructions",
|
278 |
+
scale=3,
|
279 |
+
value=get_prompt_example(prompt_template_names[0]),
|
280 |
+
show_copy_button=True,
|
281 |
+
)
|
282 |
+
wx_generate_btn = gr.Button("↓Generate↓", scale=0, interactive=False)
|
283 |
+
output_svg_preview = gr.HTML(image_placeholder)
|
284 |
+
output_xml_string_box = gr.Textbox(
|
285 |
+
label="Result SVG XML",
|
286 |
+
lines=7,
|
287 |
+
max_lines=7,
|
288 |
+
scale=3,
|
289 |
+
show_copy_button=True,
|
290 |
+
)
|
291 |
+
with gr.Accordion(label="Result encoded string", open=False):
|
292 |
+
output_encoded_string_box = gr.Textbox(
|
293 |
+
label="Image string",
|
294 |
+
info="data:image/svg+xml;base64,...",
|
295 |
lines=7,
|
296 |
max_lines=7,
|
297 |
show_copy_button=True,
|
298 |
scale=3,
|
299 |
+
container=False,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
300 |
)
|
301 |
|
302 |
+
wx_status_box = gr.Markdown("Status")
|
303 |
+
|
304 |
+
# Map controls to functions
|
305 |
+
wx_prompt_drop.input(
|
306 |
+
fn=wx_prompt_drop_change,
|
307 |
+
inputs=wx_prompt_drop,
|
308 |
+
outputs=[
|
309 |
+
wx_prompt_box,
|
310 |
+
wx_instructions_box,
|
311 |
+
wx_primer_box,
|
312 |
+
upload_row,
|
313 |
+
upload_accordeon,
|
314 |
+
],
|
315 |
)
|
316 |
+
wx_connect_btn.click(
|
317 |
+
fn=wx_connect_click,
|
318 |
+
inputs=[
|
319 |
+
wx_engine_state,
|
320 |
+
wx_creds_endpoint,
|
321 |
+
wx_creds_apikey,
|
322 |
+
wx_creds_projectid,
|
323 |
+
],
|
324 |
+
outputs=[
|
325 |
+
wx_engine_state,
|
326 |
+
wx_models_drop,
|
327 |
+
credentials_accordeon,
|
328 |
+
wx_generate_btn,
|
329 |
+
wx_status_box,
|
330 |
+
],
|
331 |
+
)
|
332 |
+
input_file.upload(
|
333 |
fn=read_file,
|
334 |
+
inputs=input_file,
|
335 |
outputs=[
|
336 |
+
input_encoded_string_box,
|
337 |
+
input_xml_string_box,
|
338 |
+
input_svg_preview,
|
|
|
339 |
],
|
340 |
)
|
341 |
+
input_encoded_string_box.input(
|
342 |
+
fn=input_encoded_string_box_change,
|
343 |
+
inputs=[input_encoded_string_box],
|
344 |
+
outputs=[input_xml_string_box, input_svg_preview],
|
345 |
+
)
|
346 |
+
input_xml_string_box.input(
|
347 |
+
fn=input_xml_string_box_change,
|
348 |
+
inputs=[input_xml_string_box],
|
349 |
+
outputs=[input_encoded_string_box, input_svg_preview],
|
350 |
+
)
|
351 |
+
wx_generate_btn.click(
|
352 |
+
fn=wx_stream,
|
353 |
+
inputs=[
|
354 |
+
wx_engine_state,
|
355 |
+
wx_models_drop,
|
356 |
+
wx_prompt_box,
|
357 |
+
wx_instructions_box,
|
358 |
+
wx_primer_box,
|
359 |
+
input_xml_string_box,
|
360 |
+
],
|
361 |
+
outputs=[
|
362 |
+
wx_status_box,
|
363 |
+
output_encoded_string_box,
|
364 |
+
output_xml_string_box,
|
365 |
+
output_svg_preview,
|
366 |
+
],
|
367 |
+
api_name="wx_generate",
|
368 |
+
)
|
369 |
+
output_xml_string_box.input(
|
370 |
+
fn=output_xml_string_box_change,
|
371 |
+
inputs=[output_xml_string_box],
|
372 |
+
outputs=[
|
373 |
+
output_encoded_string_box,
|
374 |
+
output_xml_string_box,
|
375 |
+
output_svg_preview,
|
376 |
+
],
|
377 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
378 |
|
379 |
# Main
|
380 |
if __name__ == "__main__":
|