Yaron Koresh commited on
Commit
f2d1065
·
verified ·
1 Parent(s): ff20266

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -55
app.py CHANGED
@@ -83,28 +83,28 @@ def generate_random_string(length):
83
  characters = string.ascii_letters + string.digits
84
  return ''.join(random.choice(characters) for _ in range(length))
85
 
86
- @spaces.GPU(duration=40)
87
- def Piper1(_do):
88
- print("starting piper1")
89
- retu = pp1(
90
- _do,
 
91
  height=512,
92
  width=512,
93
  num_inference_steps=4,
94
  max_sequence_length=256,
95
  guidance_scale=0
96
  )
97
- print("returning piper1")
98
- return retu
99
-
100
- @spaces.GPU(duration=40)
101
- def Piper2(img,posi,neg):
102
- retu = pp2(
103
  prompt=posi,
104
  negative_prompt=neg,
105
  image=img
106
  )
107
- return retu
108
 
109
  def tok(pipe,txt):
110
  toks = pipe.tokenizer(txt)['input_ids']
@@ -150,39 +150,28 @@ function custom(){
150
  }
151
  """
152
 
153
- def infer1(p):
154
- print("infer1: started")
 
155
  p1 = p["a"]
156
- print(f'prompt 1: {p1}')
157
  p2 = p["b"]
158
- print(f'prompt 2: {p2}')
159
  name = generate_random_string(12)+".png"
160
- print(f'name: {name}')
 
161
  _do = ['beautiful', 'playful', 'photographed', 'realistic', 'dynamic poze', 'deep field', 'reasonable coloring', 'rough texture', 'best quality', 'focused']
162
  if p1 != "":
163
  _do.append(f'{p1}')
164
  posi = " ".join(_do)
165
- print(posi)
166
- output = Piper1(posi)
167
- output.images[0].save(name)
168
- return name
169
 
170
- def infer2(p):
171
- print("infer2: started")
172
- p1 = p["a"]
173
- p2 = p["b"]
174
- name = p["c"]
175
  if p2 != "":
176
- _dont = f'{p2} where in {p1}'
177
- neg = _dont
178
- else:
179
- return name
180
- img = load_image(name).convert("RGB")
181
- output2 = Piper2(img,p1,neg)
182
- output2.images[0].save("_"+name)
183
- return "_"+name
184
 
185
- def run1(p1,p2,*result):
186
 
187
  p1_en = translate(p1,"english")
188
  p2_en = translate(p2,"english")
@@ -193,31 +182,13 @@ def run1(p1,p2,*result):
193
 
194
  arr = [p for _ in rng]
195
  pool = Pool(ln)
196
- out = list(pool.imap(infer1,arr))
197
  pool.close()
198
  pool.join()
199
  pool.clear()
200
 
201
  return out
202
 
203
- def run2(p1,p2,*result):
204
-
205
- p1_en = translate(p1,"english")
206
- p2_en = translate(p2,"english")
207
- p = {"a":p1_en,"b":p2_en}
208
- ln = len(result)
209
- print("images: "+str(ln))
210
- rng = list(range(ln))
211
-
212
- arr = [{"a":p1_en,"b":p2_en,"c":result[_]} for _ in rng]
213
- pool = Pool(ln)
214
- out = list(pool.imap(infer2,arr))
215
- pool.close()
216
- pool.join()
217
- pool.clear()
218
-
219
- return out
220
-
221
  def main():
222
 
223
  global result
@@ -255,7 +226,7 @@ def main():
255
  result.append(gr.Image(interactive=False,elem_classes="image-container", label="Result", show_label=False, type='filepath', show_share_button=False))
256
  result.append(gr.Image(interactive=False,elem_classes="image-container", label="Result", show_label=False, type='filepath', show_share_button=False))
257
 
258
- run_button.click(fn=run1,inputs=[prompt,prompt2,*result],outputs=result).then(fn=run2,inputs=[prompt,prompt2,*result],outputs=result)
259
  demo.queue().launch()
260
 
261
  if __name__ == "__main__":
 
83
  characters = string.ascii_letters + string.digits
84
  return ''.join(random.choice(characters) for _ in range(length))
85
 
86
+ @spaces.GPU(duration=75,enable_queue=True)
87
+ def Piper(name,name2,posi,neg):
88
+ print("starting piper")
89
+
90
+ ret1 = pp1(
91
+ posi,
92
  height=512,
93
  width=512,
94
  num_inference_steps=4,
95
  max_sequence_length=256,
96
  guidance_scale=0
97
  )
98
+ ret1.images[0].save(name)
99
+
100
+ img = load_image(name).convert("RGB")
101
+
102
+ ret2 = pp2(
 
103
  prompt=posi,
104
  negative_prompt=neg,
105
  image=img
106
  )
107
+ ret2.images[0].save(name2)
108
 
109
  def tok(pipe,txt):
110
  toks = pipe.tokenizer(txt)['input_ids']
 
150
  }
151
  """
152
 
153
+ def infer(p):
154
+ print("infer: started")
155
+
156
  p1 = p["a"]
 
157
  p2 = p["b"]
 
158
  name = generate_random_string(12)+".png"
159
+ name2 = "_"+name
160
+
161
  _do = ['beautiful', 'playful', 'photographed', 'realistic', 'dynamic poze', 'deep field', 'reasonable coloring', 'rough texture', 'best quality', 'focused']
162
  if p1 != "":
163
  _do.append(f'{p1}')
164
  posi = " ".join(_do)
 
 
 
 
165
 
166
+ neg = " "
 
 
 
 
167
  if p2 != "":
168
+ neg = f'{p2} where in {p1}'
169
+
170
+ Piper(name,name2,posi,neg)
171
+
172
+ return name2
 
 
 
173
 
174
+ def run(p1,p2,*result):
175
 
176
  p1_en = translate(p1,"english")
177
  p2_en = translate(p2,"english")
 
182
 
183
  arr = [p for _ in rng]
184
  pool = Pool(ln)
185
+ out = list(pool.imap(infer,arr))
186
  pool.close()
187
  pool.join()
188
  pool.clear()
189
 
190
  return out
191
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
  def main():
193
 
194
  global result
 
226
  result.append(gr.Image(interactive=False,elem_classes="image-container", label="Result", show_label=False, type='filepath', show_share_button=False))
227
  result.append(gr.Image(interactive=False,elem_classes="image-container", label="Result", show_label=False, type='filepath', show_share_button=False))
228
 
229
+ run_button.click(fn=run,inputs=[prompt,prompt2,*result],outputs=result)
230
  demo.queue().launch()
231
 
232
  if __name__ == "__main__":