Uthar commited on
Commit
dd5b582
·
verified ·
1 Parent(s): f994e4e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -16
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
  from all_models import models
3
- from externalmod import gr_Interface_load, save_image, randomize_seed
4
  import asyncio
5
  import os
6
  from threading import RLock
@@ -11,7 +11,54 @@ negPreSetPrompt = "[deformed | disfigured], poorly drawn, [bad : wrong] anatomy,
11
 
12
  lock = RLock()
13
 
14
- HF_TOKEN = os.environ.get("HF_TOKEN") if os.environ.get("HF_TOKEN") else None # If private or gated models aren't used, ENV setting is unnecessary.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  def get_current_time():
17
  now = datetime.now()
@@ -31,31 +78,18 @@ def load_fn(models):
31
  m = gr.Interface(lambda: None, ['text'], ['image'])
32
  models_load.update({model: m})
33
 
34
-
35
- load_fn(models)
36
-
37
- num_models = 12
38
- max_images = 12
39
- inference_timeout = 400
40
- default_models = models[:num_models]
41
- MAX_SEED = 2**32-1
42
-
43
-
44
  def extend_choices(choices):
45
  return choices[:num_models] + (num_models - len(choices[:num_models])) * ['NA']
46
 
47
-
48
  def update_imgbox(choices):
49
  choices_plus = extend_choices(choices[:num_models])
50
  return [gr.Image(None, label=m, visible=(m!='NA')) for m in choices_plus]
51
 
52
-
53
  def random_choices():
54
  import random
55
  random.seed()
56
  return random.choices(models, k=num_models)
57
 
58
-
59
  async def infer(model_str, prompt, nprompt="", height=0, width=0, steps=0, cfg=0, seed=-1, timeout=inference_timeout):
60
  kwargs = {}
61
  if height > 0: kwargs["height"] = height
@@ -105,7 +139,6 @@ def gen_fn(model_str, prompt, nprompt="", height=0, width=0, steps=0, cfg=0, see
105
  loop.close()
106
  return result
107
 
108
-
109
  def add_gallery(image, model_str, gallery):
110
  if gallery is None: gallery = []
111
  with lock:
 
1
  import gradio as gr
2
  from all_models import models
3
+ # from externalmod import gr_Interface_load, save_image, randomize_seed
4
  import asyncio
5
  import os
6
  from threading import RLock
 
11
 
12
  lock = RLock()
13
 
14
+ HF_TOKEN = os.environ.get("HF_TOKEN") if os.environ.get("HF_TOKEN") else None
15
+
16
+ num_models = 12
17
+ max_images = 12
18
+ inference_timeout = 400
19
+ default_models = models[:num_models]
20
+ MAX_SEED = 2**32-1
21
+
22
+ load_fn(models)
23
+
24
+ def gr_Interface_load(
25
+ name: str,
26
+ src: str | None = None,
27
+ hf_token: str | None = None,
28
+ alias: str | None = None,
29
+ **kwargs, # ignore
30
+ ) -> Blocks:
31
+ try:
32
+ return load_blocks_from_repo(name, src, hf_token, alias)
33
+ except Exception as e:
34
+ print(e)
35
+ return gradio.Interface(lambda: None, ['text'], ['image'])
36
+
37
+
38
+ def save_image(image, savefile, modelname, prompt, nprompt, height=0, width=0, steps=0, cfg=0, seed=-1):
39
+ from PIL import Image, PngImagePlugin
40
+ import json
41
+ try:
42
+ metadata = {"prompt": prompt, "negative_prompt": nprompt, "Model": {"Model": modelname.split("/")[-1]}}
43
+ if steps > 0: metadata["num_inference_steps"] = steps
44
+ if cfg > 0: metadata["guidance_scale"] = cfg
45
+ if seed != -1: metadata["seed"] = seed
46
+ if width > 0 and height > 0: metadata["resolution"] = f"{width} x {height}"
47
+ metadata_str = json.dumps(metadata)
48
+ info = PngImagePlugin.PngInfo()
49
+ info.add_text("metadata", metadata_str)
50
+ image.save(savefile, "PNG", pnginfo=info)
51
+ return str(Path(savefile).resolve())
52
+ except Exception as e:
53
+ print(f"Failed to save image file: {e}")
54
+ raise Exception(f"Failed to save image file:") from e
55
+
56
+ def randomize_seed():
57
+ from random import seed, randint
58
+ MAX_SEED = 2**32-1
59
+ seed()
60
+ rseed = randint(0, MAX_SEED)
61
+ return rseed
62
 
63
  def get_current_time():
64
  now = datetime.now()
 
78
  m = gr.Interface(lambda: None, ['text'], ['image'])
79
  models_load.update({model: m})
80
 
 
 
 
 
 
 
 
 
 
 
81
  def extend_choices(choices):
82
  return choices[:num_models] + (num_models - len(choices[:num_models])) * ['NA']
83
 
 
84
  def update_imgbox(choices):
85
  choices_plus = extend_choices(choices[:num_models])
86
  return [gr.Image(None, label=m, visible=(m!='NA')) for m in choices_plus]
87
 
 
88
  def random_choices():
89
  import random
90
  random.seed()
91
  return random.choices(models, k=num_models)
92
 
 
93
  async def infer(model_str, prompt, nprompt="", height=0, width=0, steps=0, cfg=0, seed=-1, timeout=inference_timeout):
94
  kwargs = {}
95
  if height > 0: kwargs["height"] = height
 
139
  loop.close()
140
  return result
141
 
 
142
  def add_gallery(image, model_str, gallery):
143
  if gallery is None: gallery = []
144
  with lock: