nightfury commited on
Commit
d5a3b78
1 Parent(s): 1d9912b

Create new file

Browse files
Files changed (1) hide show
  1. css_and_js.py +92 -0
css_and_js.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from os import path
2
+ import json
3
+
4
+
5
+ def readTextFile(*args):
6
+ dir = path.dirname(__file__)
7
+ entry = path.join(dir, *args)
8
+ with open(entry, "r", encoding="utf8") as f:
9
+ data = f.read()
10
+ return data
11
+
12
+
13
+ def css(opt):
14
+ styling = readTextFile("css", "styles.css")
15
+ # TODO: @altryne restore this before merge
16
+ if not opt.no_progressbar_hiding:
17
+ styling += readTextFile("css", "no_progress_bar.css")
18
+ return styling
19
+
20
+
21
+ def js(opt):
22
+ data = readTextFile("js", "index.js")
23
+ data = "(z) => {" + data + "; return z ?? [] }"
24
+ return data
25
+
26
+
27
+ # TODO : @altryne fix this to the new JS format
28
+ js_copy_txt2img_output = "(x) => {navigator.clipboard.writeText(document.querySelector('gradio-app').shadowRoot.querySelector('#highlight .textfield').textContent.replace(/\s+/g,' ').replace(/: /g,':'))}"
29
+
30
+
31
+
32
+ js_parse_prompt ="""
33
+ (txt2img_prompt, txt2img_width, txt2img_height, txt2img_steps, txt2img_seed, txt2img_batch_count, txt2img_cfg) => {
34
+
35
+ const prompt_input = document.querySelector('gradio-app').shadowRoot.querySelector('#prompt_input [data-testid="textbox"]');
36
+ const multiline = document.querySelector('gradio-app').shadowRoot.querySelector('#submit_on_enter label:nth-child(2)')
37
+ if (prompt_input.scrollWidth > prompt_input.clientWidth + 10 ) {
38
+ multiline.click();
39
+ }
40
+
41
+
42
+ let height_match = /(?:-h|-H|--height|height)[ :]?(?<height>\d+) /.exec(txt2img_prompt);
43
+ if (height_match) {
44
+ txt2img_height = Math.round(height_match.groups.height / 64) * 64;
45
+ txt2img_prompt = txt2img_prompt.replace(height_match[0], '');
46
+ }
47
+ let width_match = /(?:-w|-W|--width|width)[ :]?(?<width>\d+) /.exec(txt2img_prompt);
48
+ if (width_match) {
49
+ txt2img_width = Math.round(width_match.groups.width / 64) * 64;
50
+ txt2img_prompt = txt2img_prompt.replace(width_match[0], '');
51
+ }
52
+ let steps_match = /(?:-s|--steps|steps)[ :]?(?<steps>\d+) /.exec(txt2img_prompt);
53
+ if (steps_match) {
54
+ txt2img_steps = steps_match.groups.steps.trim();
55
+ txt2img_prompt = txt2img_prompt.replace(steps_match[0], '');
56
+ }
57
+ let seed_match = /(?:-S|--seed|seed)[ :]?(?<seed>\d+) /.exec(txt2img_prompt);
58
+ if (seed_match) {
59
+ txt2img_seed = seed_match.groups.seed;
60
+ txt2img_prompt = txt2img_prompt.replace(seed_match[0], '');
61
+ }
62
+ let batch_count_match = /(?:-n|-N|--number|number)[ :]?(?<batch_count>\d+) /.exec(txt2img_prompt);
63
+ if (batch_count_match) {
64
+ txt2img_batch_count = batch_count_match.groups.batch_count;
65
+ txt2img_prompt = txt2img_prompt.replace(batch_count_match[0], '');
66
+ }
67
+ let cfg_scale_match = /(?:-c|-C|--cfg-scale|cfg_scale|cfg)[ :]?(?<cfgscale>\d\.?\d+?) /.exec(txt2img_prompt);
68
+ if (cfg_scale_match) {
69
+ txt2img_cfg = parseFloat(cfg_scale_match.groups.cfgscale).toFixed(1);
70
+ txt2img_prompt = txt2img_prompt.replace(cfg_scale_match[0], '');
71
+ }
72
+ let sampler_match = /(?:-A|--sampler|sampler)[ :]?(?<sampler>\w+) /.exec(txt2img_prompt);
73
+ if (sampler_match) {
74
+
75
+ txt2img_prompt = txt2img_prompt.replace(sampler_match[0], '');
76
+ }
77
+
78
+ return [txt2img_prompt, parseInt(txt2img_width), parseInt(txt2img_height), parseInt(txt2img_steps), txt2img_seed, parseInt(txt2img_batch_count), parseFloat(txt2img_cfg)];
79
+ }
80
+ """
81
+
82
+
83
+ # Wrap the typical SD method call into async closure for ease of use
84
+ # Supplies the js function with a params object
85
+ # That includes all the passed arguments and input from Gradio: x
86
+ # ATTENTION: x is an array of values of all components passed to your
87
+ # python event handler
88
+ # Example call in Gradio component's event handler (pass the result to _js arg):
89
+ # _js=call_JS("myJsMethod", arg1="string", arg2=100, arg3=[])
90
+ def call_JS(sd_method, **kwargs):
91
+ param_str = json.dumps(kwargs)
92
+ return f"async (...x) => {{ return await SD.{sd_method}({{ x, ...{param_str} }}) ?? []; }}"