m3g4p0p commited on
Commit
86fc953
·
1 Parent(s): ff5b825

add color pickers

Browse files
Files changed (1) hide show
  1. myapp/app.py +33 -4
myapp/app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from functools import partial
2
  from pathlib import Path
3
  from tempfile import NamedTemporaryFile
@@ -33,13 +34,18 @@ with gr.Blocks() as demo:
33
  prompt = gr.TextArea("A psychedelic vulture", label="Prompt")
34
  model = gr.Radio(MODELS, value=MODELS[0], label="Model")
35
  button = gr.Button("Generate")
 
36
  with gr.Column():
 
37
  background = gr.Image(visible=False, type="filepath")
38
  scale = gr.Slider(3, 15, 9, step=1, label="Scale")
39
- output = gr.Image()
 
 
 
40
 
41
  def generate_background(data: dict[Component, Any]):
42
- if prompt not in data:
43
  return gr.skip(), gr.skip()
44
 
45
  return client.text_to_image(data[prompt], model=data[model]), None
@@ -48,6 +54,15 @@ with gr.Blocks() as demo:
48
  if data.get(background) is None:
49
  return None
50
 
 
 
 
 
 
 
 
 
 
51
  qr_code = segno.make(data[text], error="h")
52
  suffix = Path(data[background]).suffix
53
 
@@ -57,6 +72,8 @@ with gr.Blocks() as demo:
57
  target=temp_file,
58
  background=data[background],
59
  scale=data[scale],
 
 
60
  )
61
 
62
  return Image.open(temp_file.name)
@@ -75,9 +92,21 @@ with gr.Blocks() as demo:
75
  )
76
 
77
  gr.on(
78
- [text.submit, background.change, scale.change],
 
 
 
 
 
 
79
  generate_output,
80
- inputs={text, background, scale},
 
 
 
 
 
 
81
  outputs=output,
82
  )
83
 
 
1
+ import re
2
  from functools import partial
3
  from pathlib import Path
4
  from tempfile import NamedTemporaryFile
 
34
  prompt = gr.TextArea("A psychedelic vulture", label="Prompt")
35
  model = gr.Radio(MODELS, value=MODELS[0], label="Model")
36
  button = gr.Button("Generate")
37
+
38
  with gr.Column():
39
+ output = gr.Image()
40
  background = gr.Image(visible=False, type="filepath")
41
  scale = gr.Slider(3, 15, 9, step=1, label="Scale")
42
+
43
+ with gr.Row():
44
+ color_dark = gr.ColorPicker("#000000", label="Dark")
45
+ color_light = gr.ColorPicker("#FFFFFF", label="Light")
46
 
47
  def generate_background(data: dict[Component, Any]):
48
+ if not data.get(prompt):
49
  return gr.skip(), gr.skip()
50
 
51
  return client.text_to_image(data[prompt], model=data[model]), None
 
54
  if data.get(background) is None:
55
  return None
56
 
57
+ def to_hex_format(value: str):
58
+ if value.startswith("#"):
59
+ return value
60
+
61
+ matches = re.findall(r"\d+(?:\.\d+)?", value)
62
+ r, g, b = map(int, map(float, matches[:3]))
63
+
64
+ return f"#{r:x}{g:x}{b:x}".upper()
65
+
66
  qr_code = segno.make(data[text], error="h")
67
  suffix = Path(data[background]).suffix
68
 
 
72
  target=temp_file,
73
  background=data[background],
74
  scale=data[scale],
75
+ light=to_hex_format(data[color_light]),
76
+ dark=to_hex_format(data[color_dark]),
77
  )
78
 
79
  return Image.open(temp_file.name)
 
92
  )
93
 
94
  gr.on(
95
+ [
96
+ text.submit,
97
+ background.change,
98
+ scale.change,
99
+ color_light.change,
100
+ color_dark.change,
101
+ ],
102
  generate_output,
103
+ inputs={
104
+ text,
105
+ background,
106
+ scale,
107
+ color_light,
108
+ color_dark,
109
+ },
110
  outputs=output,
111
  )
112