Brian Watson commited on
Commit
e8c3fc8
1 Parent(s): 3af2576

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -55
app.py CHANGED
@@ -23,10 +23,10 @@ def text_it(inputs, text_gen=text_gen):
23
  return text_gen(inputs)
24
 
25
 
26
- def set_model(current_model_name):
27
  global current_model
28
- current_model = next((m for m in models if m["name"] == current_model_name), None)
29
- return gr.outputs.Label(current_model["name"])
30
 
31
 
32
  def send_it(inputs, model_choice):
@@ -34,6 +34,34 @@ def send_it(inputs, model_choice):
34
  return proc(inputs)
35
 
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  def make_dropdown_searchable(dropdown_id):
38
  script = f"""
39
  <script>
@@ -41,7 +69,7 @@ def make_dropdown_searchable(dropdown_id):
41
  const input = document.getElementById(dropdownId);
42
  input.addEventListener("input", function() {{
43
  const value = this.value.toLowerCase();
44
- const options = document.querySelectorAll(`#${dropdownId} option`);
45
  let found = false;
46
  for (let i = 0; i < options.length; i++) {{
47
  const option = options[i];
@@ -67,63 +95,27 @@ def make_dropdown_searchable(dropdown_id):
67
  return gr.outputs.HTML(script)
68
 
69
 
70
- css = """"""
71
-
72
- iface = gr.Interface(
73
  fn=text_it,
74
  inputs="text",
75
  outputs="text",
76
  css=css,
77
  capture_session=True
78
- )
79
-
80
- make_dropdown_searchable(iface.inputs[0].component_id) # Make the model dropdown searchable
81
-
82
- input_text = iface.inputs[0]
83
- model_name1 = gr.inputs.Dropdown(
84
- label="Choose Model",
85
- choices=[m["name"] for m in models],
86
- type="index",
87
- value=current_model["name"],
88
- interactive=True,
89
- )
90
- see_prompts = gr.outputs.Button(label="Generate Prompts")
91
- run = gr.outputs.Button(label="Generate Images", type="primary")
92
- outputs = [
93
- gr.outputs.Image(label=current_model["name"]),
94
- gr.outputs.Image(label=current_model["name"]),
95
- gr.outputs.Image(label=current_model["name"]),
96
- gr.outputs.Image(label=current_model["name"]),
97
- gr.outputs.Image(label=current_model["name"]),
98
- gr.outputs.Image(label=current_model["name"]),
99
- gr.outputs.Image(label=current_model["name"]),
100
- gr.outputs.Image(label=current_model["name"]),
101
- ]
102
- magic_inputs = [
103
- gr.inputs.Textbox(lines=4),
104
- gr.inputs.Textbox(lines=4),
105
- gr.inputs.Textbox(lines=4),
106
- gr.inputs.Textbox(lines=4),
107
- gr.inputs.Textbox(lines=4),
108
- gr.inputs.Textbox(lines=4),
109
- gr.inputs.Textbox(lines=4),
110
- ]
111
-
112
- iface.inputs = [input_text, model_name1, see_prompts, run] + magic_inputs
113
- iface.outputs = outputs
114
-
115
-
116
- def generate_prompts():
117
- prompts = text_it(input_text.value)
118
- for i, magic_input in enumerate(magic_inputs):
119
- magic_input.update(prompts[i])
120
-
121
 
122
- def generate_images():
123
- return [send_it(magic_inputs[i].value, model_name1.value) for i in range(len(magic_inputs))]
124
 
 
125
 
126
- see_prompts.set_action(generate_prompts)
127
- run.set_action(generate_images)
128
 
129
- iface.launch(inline=True, show=True)
 
23
  return text_gen(inputs)
24
 
25
 
26
+ def set_model(current_model_index):
27
  global current_model
28
+ current_model = models[current_model_index]
29
+ return gr.update(label=f"{current_model['name']}")
30
 
31
 
32
  def send_it(inputs, model_choice):
 
34
  return proc(inputs)
35
 
36
 
37
+ css = """
38
+ <style>
39
+ .searchable-dropdown {
40
+ position: relative;
41
+ }
42
+
43
+ .searchable-dropdown input {
44
+ width: 100%;
45
+ padding: 0.5rem;
46
+ border-radius: 5px;
47
+ border: 1px solid #ccc;
48
+ }
49
+
50
+ .searchable-dropdown select {
51
+ width: 100%;
52
+ padding: 0.5rem;
53
+ border-radius: 5px;
54
+ border: 1px solid #ccc;
55
+ position: absolute;
56
+ top: 0;
57
+ left: 0;
58
+ opacity: 0;
59
+ pointer-events: none;
60
+ }
61
+ </style>
62
+ """
63
+
64
+
65
  def make_dropdown_searchable(dropdown_id):
66
  script = f"""
67
  <script>
 
69
  const input = document.getElementById(dropdownId);
70
  input.addEventListener("input", function() {{
71
  const value = this.value.toLowerCase();
72
+ const options = Array.from(document.querySelectorAll(`#${dropdownId} option`));
73
  let found = false;
74
  for (let i = 0; i < options.length; i++) {{
75
  const option = options[i];
 
95
  return gr.outputs.HTML(script)
96
 
97
 
98
+ with gr.Interface(
 
 
99
  fn=text_it,
100
  inputs="text",
101
  outputs="text",
102
  css=css,
103
  capture_session=True
104
+ ) as iface:
105
+ model_dropdown = gr.outputs.Dropdown(
106
+ label="Choose Model",
107
+ choices=[m["name"] for m in models],
108
+ type="index",
109
+ value=current_model["name"],
110
+ interactive=True,
111
+ component_id="model_dropdown"
112
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
 
114
+ make_dropdown_searchable("model_dropdown")
 
115
 
116
+ iface.inputs.insert(0, model_dropdown)
117
 
118
+ iface.layout["top"] = ["inputs", "outputs", "model_dropdown"]
119
+ iface.layout["heights"] = ["200px", "auto", "40px"]
120
 
121
+ iface.launch()