Pamudu13 commited on
Commit
3a46525
·
verified ·
1 Parent(s): 6db5647

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -73
app.py CHANGED
@@ -1,17 +1,12 @@
1
  import gradio as gr
2
- # import os
3
- # import sys
4
- # from pathlib import Path
5
  import time
 
6
 
7
- models =[
8
-
9
  "Yntec/ChickFlick",
10
  "Yntec/CrystalReality",
11
  "Yntec/DegreesOfFreedom",
12
-
13
  ]
14
-
15
 
16
  model_functions = {}
17
  model_idx = 1
@@ -19,74 +14,52 @@ for model_path in models:
19
  try:
20
  model_functions[model_idx] = gr.Interface.load(f"models/{model_path}", live=False, preprocess=True, postprocess=False)
21
  except Exception as error:
22
- def the_fn(txt):
23
-
24
  return None
25
- model_functions[model_idx] = gr.Interface(fn=the_fn, inputs=["text"], outputs=["image"])
26
-
27
- model_idx+=1
28
-
29
 
30
  def send_it_idx(idx):
31
  def send_it_fn(prompt):
32
- output = (model_functions.get(str(idx)) or model_functions.get(str(1)))(prompt)
 
33
  return output
34
  return send_it_fn
35
 
36
  def get_prompts(prompt_text):
37
-
38
  return prompt_text
39
 
40
  def clear_it(val):
41
- if int(val) != 0:
42
- val = 0
43
- else:
44
- val = 0
45
- pass
46
- return val
47
 
48
- def all_task_end(cnt,t_stamp):
49
  to = t_stamp + 360
50
  et = time.time()
51
  if et > to and t_stamp != 0:
52
  d = gr.update(value=0)
53
  tog = gr.update(value=1)
54
- #print(f'to: {to} et: {et}')
55
  else:
56
- if cnt != 0:
57
- d = gr.update(value=et)
58
- else:
59
- d = gr.update(value=0)
60
  tog = gr.update(value=0)
61
- #print (f'passing: to: {to} et: {et}')
62
- pass
63
  return d, tog
64
 
65
  def all_task_start():
66
- print("\n\n\n\n\n\n\n")
67
- t = time.gmtime()
68
  t_stamp = time.time()
69
- current_time = time.strftime("%H:%M:%S", t)
70
  return gr.update(value=t_stamp), gr.update(value=t_stamp), gr.update(value=0)
71
 
72
  def clear_fn():
73
  nn = len(models)
74
  return tuple([None, *[None for _ in range(nn)]])
75
 
76
-
77
  with gr.Blocks(title="SD Models") as my_interface:
78
  with gr.Column(scale=12):
79
- # with gr.Row():
80
- # gr.Markdown("""- Primary prompt: 你想画的内容(英文单词,如 a cat, 加英文逗号效果更好;点 Improve 按钮进行完善)\n- Real prompt: 完善后的提示词,出现后再点右边的 Run 按钮开始运行""")
81
  with gr.Row():
82
  with gr.Row(scale=6):
83
- primary_prompt=gr.Textbox(label="Prompt", value="")
84
- # real_prompt=gr.Textbox(label="Real prompt")
85
  with gr.Row(scale=6):
86
- # improve_prompts_btn=gr.Button("Improve")
87
  with gr.Row():
88
- run=gr.Button("Run",variant="primary")
89
- clear_btn=gr.Button("Clear")
90
  with gr.Row():
91
  sd_outputs = {}
92
  model_idx = 1
@@ -94,49 +67,26 @@ with gr.Blocks(title="SD Models") as my_interface:
94
  with gr.Column(scale=3, min_width=320):
95
  with gr.Box():
96
  sd_outputs[model_idx] = gr.Image(label=model_path)
97
- pass
98
  model_idx += 1
99
- pass
100
- pass
101
 
102
  with gr.Row(visible=False):
103
- start_box=gr.Number(interactive=False)
104
- end_box=gr.Number(interactive=False)
105
- tog_box=gr.Textbox(value=0,interactive=False)
106
-
107
- start_box.change(
108
- all_task_end,
109
- [start_box, end_box],
110
- [start_box, tog_box],
111
- every=1,
112
- show_progress=True)
113
 
 
114
  primary_prompt.submit(all_task_start, None, [start_box, end_box, tog_box])
115
  run.click(all_task_start, None, [start_box, end_box, tog_box])
116
  runs_dict = {}
117
  model_idx = 1
118
  for model_path in models:
119
- runs_dict[model_idx] = run.click(model_functions[model_idx], inputs=[primary_prompt], outputs=[sd_outputs[model_idx]])
120
  model_idx += 1
121
- pass
122
- pass
123
 
124
- # improve_prompts_btn_clicked=improve_prompts_btn.click(
125
- # get_prompts,
126
- # inputs=[primary_prompt],
127
- # outputs=[primary_prompt],
128
- # cancels=list(runs_dict.values()))
129
- clear_btn.click(
130
- clear_fn,
131
- None,
132
- [primary_prompt, *list(sd_outputs.values())],
133
- cancels=[*list(runs_dict.values())])
134
- tog_box.change(
135
- clear_it,
136
- tog_box,
137
- tog_box,
138
- cancels=[*list(runs_dict.values())])
139
- text_gen1=gr.Interface.load("spaces/phenomenon1981/MagicPrompt-Stable-Diffusion")
140
 
141
  my_interface.queue(concurrency_count=600, status_update_rate=1)
142
- my_interface.launch(inline=True, show_api=True)
 
1
  import gradio as gr
 
 
 
2
  import time
3
+ import random
4
 
5
+ models = [
 
6
  "Yntec/ChickFlick",
7
  "Yntec/CrystalReality",
8
  "Yntec/DegreesOfFreedom",
 
9
  ]
 
10
 
11
  model_functions = {}
12
  model_idx = 1
 
14
  try:
15
  model_functions[model_idx] = gr.Interface.load(f"models/{model_path}", live=False, preprocess=True, postprocess=False)
16
  except Exception as error:
17
+ def the_fn(txt, seed=None):
 
18
  return None
19
+ model_functions[model_idx] = gr.Interface(fn=the_fn, inputs=["text", "number"], outputs=["image"])
20
+ model_idx += 1
 
 
21
 
22
  def send_it_idx(idx):
23
  def send_it_fn(prompt):
24
+ seed = random.randint(0, 2**32 - 1)
25
+ output = (model_functions.get(idx) or model_functions.get(1))(prompt, seed)
26
  return output
27
  return send_it_fn
28
 
29
  def get_prompts(prompt_text):
 
30
  return prompt_text
31
 
32
  def clear_it(val):
33
+ return 0
 
 
 
 
 
34
 
35
+ def all_task_end(cnt, t_stamp):
36
  to = t_stamp + 360
37
  et = time.time()
38
  if et > to and t_stamp != 0:
39
  d = gr.update(value=0)
40
  tog = gr.update(value=1)
 
41
  else:
42
+ d = gr.update(value=et if cnt != 0 else 0)
 
 
 
43
  tog = gr.update(value=0)
 
 
44
  return d, tog
45
 
46
  def all_task_start():
 
 
47
  t_stamp = time.time()
 
48
  return gr.update(value=t_stamp), gr.update(value=t_stamp), gr.update(value=0)
49
 
50
  def clear_fn():
51
  nn = len(models)
52
  return tuple([None, *[None for _ in range(nn)]])
53
 
 
54
  with gr.Blocks(title="SD Models") as my_interface:
55
  with gr.Column(scale=12):
 
 
56
  with gr.Row():
57
  with gr.Row(scale=6):
58
+ primary_prompt = gr.Textbox(label="Prompt", value="")
 
59
  with gr.Row(scale=6):
 
60
  with gr.Row():
61
+ run = gr.Button("Run", variant="primary")
62
+ clear_btn = gr.Button("Clear")
63
  with gr.Row():
64
  sd_outputs = {}
65
  model_idx = 1
 
67
  with gr.Column(scale=3, min_width=320):
68
  with gr.Box():
69
  sd_outputs[model_idx] = gr.Image(label=model_path)
 
70
  model_idx += 1
 
 
71
 
72
  with gr.Row(visible=False):
73
+ start_box = gr.Number(interactive=False)
74
+ end_box = gr.Number(interactive=False)
75
+ tog_box = gr.Textbox(value=0, interactive=False)
 
 
 
 
 
 
 
76
 
77
+ start_box.change(all_task_end, [start_box, end_box], [start_box, tog_box], every=1, show_progress=True)
78
  primary_prompt.submit(all_task_start, None, [start_box, end_box, tog_box])
79
  run.click(all_task_start, None, [start_box, end_box, tog_box])
80
  runs_dict = {}
81
  model_idx = 1
82
  for model_path in models:
83
+ runs_dict[model_idx] = run.click(send_it_idx(model_idx), inputs=[primary_prompt], outputs=[sd_outputs[model_idx]])
84
  model_idx += 1
 
 
85
 
86
+ clear_btn.click(clear_fn, None, [primary_prompt, *list(sd_outputs.values())], cancels=[*list(runs_dict.values())])
87
+ tog_box.change(clear_it, tog_box, tog_box, cancels=[*list(runs_dict.values())])
88
+
89
+ text_gen1 = gr.Interface.load("spaces/phenomenon1981/MagicPrompt-Stable-Diffusion")
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
  my_interface.queue(concurrency_count=600, status_update_rate=1)
92
+ my_interface.launch(inline=True, show_api=True)