mcuo commited on
Commit
d510d31
·
verified ·
1 Parent(s): beb0ea1

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -19
app.py CHANGED
@@ -86,16 +86,22 @@ with gr.Blocks(css=css) as demo:
86
  gr.Markdown("<br>" * 10)
87
 
88
  with gr.Row():
89
- prompt = gr.Text(label="Prompt", show_label=False, max_lines=1, placeholder="Enter your prompt", value="", container=False)
 
 
 
 
90
  run_button = gr.Button("Generate", scale=0, interactive=False)
91
- # <<< 変更: UIの状態管理を行わないため、ボタンは常に表示・有効
92
  consecutive_button = gr.Button("Consecutive", scale=0, variant="primary", interactive=False)
93
- stop_button = gr.Button("Stop", scale=0, variant="stop", interactive=False)
94
  lock_button = gr.Button("Lock", scale=0, variant="secondary")
95
  clear_button = gr.Button("Trash", scale=0, variant="secondary")
96
 
97
  with gr.Accordion("Advanced Settings", open=False):
98
- negative_prompt = gr.Text(label="Negative prompt", max_lines=1, placeholder="Enter a negative prompt", value="bad quality, low quality, worst quality, worst detail")
 
 
 
99
  seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
100
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
101
  with gr.Row():
@@ -115,24 +121,50 @@ with gr.Blocks(css=css) as demo:
115
  inputs=[prompt],
116
  label="Examples (Click to copy to prompt)"
117
  )
118
-
119
- # UIの状態管理を行う関数はすべて削除
120
 
 
121
  def generation_loop(prompt, negative_prompt, current_seed, randomize_seed, width, height, guidance_scale, num_inference_steps, interval_sec):
122
- # 最初の1枚を即時生成
123
- image, new_seed = infer(prompt, negative_prompt, current_seed, True, width, height, guidance_scale, num_inference_steps)
124
- yield {result: image, seed: new_seed}
125
-
126
- # 2枚目以降をループで生成
127
- try:
128
- while True:
129
- time.sleep(interval_sec)
130
- image, new_seed = infer(prompt, negative_prompt, new_seed, True, width, height, guidance_scale, num_inference_steps)
131
  yield {result: image, seed: new_seed}
132
- except gr.exceptions.CancelledError:
133
- print("Generation loop cancelled by user.")
134
- return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
 
 
136
  def toggle_lock(is_locked, current_prompt):
137
  new_lock_state = not is_locked
138
  if new_lock_state:
@@ -141,4 +173,70 @@ with gr.Blocks(css=css) as demo:
141
  is_prompt_valid = bool(current_prompt.strip())
142
  return False, gr.update(value="Lock"), gr.update(interactive=is_prompt_valid)
143
 
144
- # プロンプト入力に応じて
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  gr.Markdown("<br>" * 10)
87
 
88
  with gr.Row():
89
+ prompt = gr.Text(
90
+ label="Prompt", show_label=False, max_lines=1,
91
+ placeholder="Enter your prompt",
92
+ value="", container=False,
93
+ )
94
  run_button = gr.Button("Generate", scale=0, interactive=False)
 
95
  consecutive_button = gr.Button("Consecutive", scale=0, variant="primary", interactive=False)
96
+ stop_button = gr.Button("Stop", scale=0, variant="stop", visible=False)
97
  lock_button = gr.Button("Lock", scale=0, variant="secondary")
98
  clear_button = gr.Button("Trash", scale=0, variant="secondary")
99
 
100
  with gr.Accordion("Advanced Settings", open=False):
101
+ negative_prompt = gr.Text(
102
+ label="Negative prompt", max_lines=1, placeholder="Enter a negative prompt",
103
+ value="bad quality, low quality, worst quality, worst detail"
104
+ )
105
  seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
106
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
107
  with gr.Row():
 
121
  inputs=[prompt],
122
  label="Examples (Click to copy to prompt)"
123
  )
 
 
124
 
125
+ # 連続生成のループ処理 (この関数は変更なし)
126
  def generation_loop(prompt, negative_prompt, current_seed, randomize_seed, width, height, guidance_scale, num_inference_steps, interval_sec):
127
+ while True:
128
+ try:
129
+ image, new_seed = infer(prompt, negative_prompt, current_seed, True, width, height, guidance_scale, num_inference_steps)
 
 
 
 
 
 
130
  yield {result: image, seed: new_seed}
131
+ time.sleep(interval_sec)
132
+ except gr.exceptions.CancelledError:
133
+ # ユーザーがStopボタンを押したときに発生する例外をキャッチ
134
+ print("Generation loop cancelled by user.")
135
+ # ループを正常に終了させる
136
+ break
137
+
138
+ # --- 削除 ---
139
+ # 多くのUIコンポーネントを管理していたリストは不要になったため削除
140
+ # ui_components = [ ... ]
141
+
142
+ # --- 変更 ---
143
+ # 連続生成開始時の処理を簡略化
144
+ def start_consecutive(prompt_text):
145
+ if not prompt_text.strip():
146
+ raise gr.Error("Prompt cannot be empty.")
147
+
148
+ # UI全体の無効化をやめ、最低限のボタン制御のみ行う
149
+ return {
150
+ consecutive_button: gr.update(visible=False),
151
+ stop_button: gr.update(visible=True),
152
+ run_button: gr.update(interactive=False), # 通常のGenerateボタンは無効化
153
+ }
154
+
155
+ # --- 変更 ---
156
+ # 連続生成停止時の処理を簡略化
157
+ def stop_consecutive(prompt_text, is_locked):
158
+ is_prompt_valid = bool(prompt_text.strip())
159
+
160
+ # UI全体の有効化をやめ、最低限のボタン制御のみ元に戻す
161
+ return {
162
+ consecutive_button: gr.update(visible=True, interactive=is_prompt_valid),
163
+ stop_button: gr.update(visible=False),
164
+ run_button: gr.update(interactive=is_prompt_valid and not is_locked),
165
+ }
166
 
167
+ # 以下のUI制御ロジックは元のまま変更なし
168
  def toggle_lock(is_locked, current_prompt):
169
  new_lock_state = not is_locked
170
  if new_lock_state:
 
173
  is_prompt_valid = bool(current_prompt.strip())
174
  return False, gr.update(value="Lock"), gr.update(interactive=is_prompt_valid)
175
 
176
+ def update_buttons_state(prompt_text, is_locked):
177
+ is_prompt_valid = bool(prompt_text.strip())
178
+ run_interactive = is_prompt_valid and not is_locked
179
+ consecutive_interactive = is_prompt_valid
180
+ return {
181
+ run_button: gr.update(interactive=run_interactive),
182
+ consecutive_button: gr.update(interactive=consecutive_interactive)
183
+ }
184
+
185
+ prompt.change(
186
+ fn=update_buttons_state,
187
+ inputs=[prompt, lock_state],
188
+ outputs=[run_button, consecutive_button],
189
+ )
190
+
191
+ lock_button.click(
192
+ fn=toggle_lock,
193
+ inputs=[lock_state, prompt],
194
+ outputs=[lock_state, lock_button, run_button],
195
+ )
196
+
197
+ clear_button.click(
198
+ fn=None,
199
+ inputs=None,
200
+ outputs=[prompt, run_button, consecutive_button, lock_button, lock_state],
201
+ js="""
202
+ function() {
203
+ return ["", { "interactive": false, "__type__": "update" }, { "interactive": false, "__type__": "update" }, { "value": "Lock", "__type__": "update" }, false];
204
+ }
205
+ """
206
+ )
207
+
208
+ run_button.click(
209
+ fn=infer,
210
+ inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
211
+ outputs=[result, seed]
212
+ )
213
+
214
+ gen_inputs = [
215
+ prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps,
216
+ interval_seconds
217
+ ]
218
+
219
+ # --- 変更 ---
220
+ # consecutive_button.click イベントの outputs を修正
221
+ consecutive_event = consecutive_button.click(
222
+ fn=start_consecutive,
223
+ inputs=[prompt],
224
+ # 更新対象を実際に変更するコンポーネントのみに限定
225
+ outputs=[consecutive_button, stop_button, run_button]
226
+ ).then(
227
+ fn=generation_loop,
228
+ inputs=gen_inputs,
229
+ outputs=[result, seed]
230
+ )
231
+
232
+ # --- 変更 ---
233
+ # stop_button.click イベントの outputs を修正
234
+ stop_button.click(
235
+ fn=stop_consecutive,
236
+ inputs=[prompt, lock_state],
237
+ # 更新対象を実際に変更するコンポーネントのみに限定
238
+ outputs=[consecutive_button, stop_button, run_button],
239
+ cancels=[consecutive_event]
240
+ )
241
+
242
+ demo.queue().launch()