nsfwalex commited on
Commit
1708f53
1 Parent(s): a455f3b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -21
app.py CHANGED
@@ -273,12 +273,27 @@ def generate(p, progress=gr.Progress(track_tqdm=True)):
273
  image_paths = [i[1] for i in images]
274
  print(prompt_str, image_paths)
275
  return [i[0] for i in images]
 
276
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
277
  with gr.Blocks(css=css,head=js,fill_height=True) as demo:
278
  with gr.Row(equal_height=False):
279
  with gr.Group():
280
  gr.HTML(value=desc_html, elem_id='desc_html_code')
281
- result = gr.Gallery(
282
  label="Result", show_label=False, columns=1, rows=1, show_share_button=True,elem_id=cfg["model_id"].replace("/", "-"),
283
  show_download_button=True,allow_preview=False,interactive=False, min_width=cfg.get("window_min_width", 340),height=360
284
  )
@@ -311,22 +326,7 @@ with gr.Blocks(css=css,head=js,fill_height=True) as demo:
311
  iframe_parent_domain = "Unable to parse referer"
312
 
313
  params = dict(request.query_params)
314
- default_image = cfg.get("cover_path", None)
315
-
316
- if default_image:
317
- if isinstance(default_image, list):
318
- # Filter out non-existent paths
319
- existing_images = [img for img in default_image if os.path.exists(img)]
320
- #print(f"found cover files: {existing_images}")
321
- if existing_images:
322
- default_image = existing_images[int(time.time()*1000)%len(existing_images)]
323
- else:
324
- default_image = None
325
- elif not os.path.exists(default_image):
326
- print(f"cover file not existed, {default_image}")
327
- default_image = None
328
- else:
329
- default_image = None
330
  print(f"load_demo, urlparams={params},cover={default_image},domain={current_domain},iframe={iframe_parent_domain}")
331
  session_data = {
332
  "params": params,
@@ -337,15 +337,16 @@ with gr.Blocks(css=css,head=js,fill_height=True) as demo:
337
  if params.get("e", "0") == "1" or "nsfwais.io" in current_domain or "nsfwais.io" in iframe_parent_domain or "127.0.0.1" in current_domain or "127.0.0.1" in iframe_parent_domain:
338
  #update the image
339
  #bind events
340
- return [Image.open(default_image)], session_data
341
-
342
 
343
- return [], session_data
 
344
 
345
  session_state = gr.State()
346
  result.change(fn=lambda x,y:None , inputs=[prompt,result], outputs=[], js=f'''(p,img)=>window.uploadImage(p, img,"process_finished","demo_hf_{cfg.get("name")}_card", "{cfg["model_id"]}")''')
347
  run_button.click(generate, inputs=[prompt], outputs=[result],trigger_mode="once",js=f'''(p)=>window.postMessageToParent(p,"process_started","demo_hf_{cfg.get("name")}_card", "click_go")''')
348
  random_button.click(fn=lambda x:x, inputs=[prompt], outputs=[prompt], js='''(p)=>window.g(p)''')
349
- demo.load(fn=on_demo_load, inputs=[], outputs=[result, session_state], js='''()=>onDemoLoad()''')
350
  if __name__ == "__main__":
351
  demo.queue(max_size=100).launch(show_api=False,show_error=False)
 
273
  image_paths = [i[1] for i in images]
274
  print(prompt_str, image_paths)
275
  return [i[0] for i in images]
276
+ default_image = cfg.get("cover_path", None)
277
 
278
+ if default_image:
279
+ if isinstance(default_image, list):
280
+ # Filter out non-existent paths
281
+ existing_images = [img for img in default_image if os.path.exists(img)]
282
+ #print(f"found cover files: {existing_images}")
283
+ if existing_images:
284
+ default_image = existing_images[int(time.time()*1000)%len(existing_images)]
285
+ else:
286
+ default_image = None
287
+ elif not os.path.exists(default_image):
288
+ print(f"cover file not existed, {default_image}")
289
+ default_image = None
290
+ else:
291
+ default_image = None
292
  with gr.Blocks(css=css,head=js,fill_height=True) as demo:
293
  with gr.Row(equal_height=False):
294
  with gr.Group():
295
  gr.HTML(value=desc_html, elem_id='desc_html_code')
296
+ result = gr.Gallery(value=default_image,
297
  label="Result", show_label=False, columns=1, rows=1, show_share_button=True,elem_id=cfg["model_id"].replace("/", "-"),
298
  show_download_button=True,allow_preview=False,interactive=False, min_width=cfg.get("window_min_width", 340),height=360
299
  )
 
326
  iframe_parent_domain = "Unable to parse referer"
327
 
328
  params = dict(request.query_params)
329
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
330
  print(f"load_demo, urlparams={params},cover={default_image},domain={current_domain},iframe={iframe_parent_domain}")
331
  session_data = {
332
  "params": params,
 
337
  if params.get("e", "0") == "1" or "nsfwais.io" in current_domain or "nsfwais.io" in iframe_parent_domain or "127.0.0.1" in current_domain or "127.0.0.1" in iframe_parent_domain:
338
  #update the image
339
  #bind events
340
+ #return [Image.open(default_image)], session_data
341
+ return session_data
342
 
343
+ return session_data
344
+ #return [], session_data
345
 
346
  session_state = gr.State()
347
  result.change(fn=lambda x,y:None , inputs=[prompt,result], outputs=[], js=f'''(p,img)=>window.uploadImage(p, img,"process_finished","demo_hf_{cfg.get("name")}_card", "{cfg["model_id"]}")''')
348
  run_button.click(generate, inputs=[prompt], outputs=[result],trigger_mode="once",js=f'''(p)=>window.postMessageToParent(p,"process_started","demo_hf_{cfg.get("name")}_card", "click_go")''')
349
  random_button.click(fn=lambda x:x, inputs=[prompt], outputs=[prompt], js='''(p)=>window.g(p)''')
350
+ demo.load(fn=on_demo_load, inputs=[], outputs=[session_state], js='''()=>onDemoLoad()''')
351
  if __name__ == "__main__":
352
  demo.queue(max_size=100).launch(show_api=False,show_error=False)