Pamudu13 commited on
Commit
fe9f7af
·
verified ·
1 Parent(s): f8a1403

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -60
app.py CHANGED
@@ -21,7 +21,7 @@ for model_path in models:
21
  except Exception as error:
22
  def the_fn(txt):
23
  return None
24
- model_functions[model_idx] = gr.Interface(fn=the_fn, inputs=gr.Textbox(), outputs=gr.Image())
25
  model_idx += 1
26
 
27
  def send_it_idx(idx):
@@ -51,65 +51,42 @@ def all_task_start():
51
  t_stamp = time.time()
52
  return gr.update(value=t_stamp), gr.update(value=t_stamp), gr.update(value=0)
53
 
54
- def clear_fn():
55
  nn = len(models)
56
  return (None, *[None for _ in range(nn)])
57
 
58
- with gr.Interface(title="SD Models") as my_interface:
59
- with gr.Column():
60
- with gr.Row():
61
- with gr.Column():
62
- primary_prompt = gr.Textbox(label="Prompt", default="")
63
- with gr.Column():
64
- run = gr.Button("Run")
65
- clear_btn = gr.Button("Clear")
66
- with gr.Row():
67
- sd_outputs = {}
68
- model_idx = 1
69
- for model_path in models:
70
- with gr.Column():
71
- sd_outputs[model_idx] = gr.Image(label=model_path)
72
- model_idx += 1
73
-
74
- start_box = gr.Number(visible=False)
75
- end_box = gr.Number(visible=False)
76
- tog_box = gr.Textbox(default=0, visible=False)
77
-
78
- def on_change_start_box(value):
79
- return all_task_end(value, end_box.value)
80
-
81
- start_box.change(
82
- on_change_start_box,
83
- inputs=[start_box],
84
- outputs=[start_box, tog_box],
85
- every=1
86
- )
87
-
88
- primary_prompt.submit(all_task_start, None, [start_box, end_box, tog_box])
89
- run.click(all_task_start, None, [start_box, end_box, tog_box])
90
-
91
- runs_dict = {}
92
- model_idx = 1
93
- for model_path in models:
94
- runs_dict[model_idx] = run.click(
95
- send_it_idx(model_idx),
96
- inputs=[primary_prompt],
97
- outputs=[sd_outputs[model_idx]]
98
- )
99
- model_idx += 1
100
-
101
- clear_btn.click(
102
- clear_fn,
103
- None,
104
- [primary_prompt, *list(sd_outputs.values())],
105
- cancels=[*list(runs_dict.values())]
106
- )
107
- tog_box.change(
108
- clear_it,
109
- tog_box,
110
- tog_box,
111
- cancels=[*list(runs_dict.values())]
112
- )
113
-
114
- my_interface.queue(concurrency_count=1)
115
- my_interface.launch()
 
21
  except Exception as error:
22
  def the_fn(txt):
23
  return None
24
+ model_functions[model_idx] = gr.Interface(fn=the_fn, inputs=gr.inputs.Textbox(), outputs=gr.outputs.Image())
25
  model_idx += 1
26
 
27
  def send_it_idx(idx):
 
51
  t_stamp = time.time()
52
  return gr.update(value=t_stamp), gr.update(value=t_stamp), gr.update(value=0)
53
 
54
+ def clear_fn(*args):
55
  nn = len(models)
56
  return (None, *[None for _ in range(nn)])
57
 
58
+ def run_model(prompt, model_idx):
59
+ return send_it_idx(model_idx)(prompt)
60
+
61
+ # Create UI components
62
+ def build_ui():
63
+ primary_prompt = gr.inputs.Textbox(label="Prompt", default="")
64
+ run = gr.Button("Run")
65
+ clear_btn = gr.Button("Clear")
66
+
67
+ sd_outputs = {}
68
+ for idx, model_path in enumerate(models, start=1):
69
+ sd_outputs[idx] = gr.outputs.Image(label=model_path)
70
+
71
+ start_box = gr.inputs.Number(visible=False)
72
+ end_box = gr.inputs.Number(visible=False)
73
+ tog_box = gr.inputs.Textbox(default=0, visible=False)
74
+
75
+ return primary_prompt, run, clear_btn, sd_outputs, start_box, end_box, tog_box
76
+
77
+ # Define the function that combines all components
78
+ def main_function(prompt):
79
+ return {idx: run_model(prompt, idx) for idx in range(1, len(models) + 1)}
80
+
81
+ primary_prompt, run, clear_btn, sd_outputs, start_box, end_box, tog_box = build_ui()
82
+
83
+ # Setup the interface
84
+ interface = gr.Interface(
85
+ fn=main_function,
86
+ inputs=[primary_prompt],
87
+ outputs=list(sd_outputs.values())
88
+ )
89
+
90
+ # Additional settings (e.g., queue, launch)
91
+ interface.queue(concurrency_count=1)
92
+ interface.launch()